hpke: move published public keys to a new endpoint (#4044)

This commit is contained in:
Caleb Doxsey 2023-03-08 09:17:04 -07:00 committed by GitHub
parent 74463c5468
commit 0f295d4a63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 136 additions and 71 deletions

View file

@ -97,6 +97,16 @@ type PublicKey struct {
key kem.PublicKey
}
// PublicKeyFromBytes converts raw bytes into a public key.
func PublicKeyFromBytes(raw []byte) (*PublicKey, error) {
key, err := kemID.Scheme().UnmarshalBinaryPublicKey(raw)
if err != nil {
return nil, err
}
return &PublicKey{key: key}, nil
}
// PublicKeyFromString converts a string into a public key.
func PublicKeyFromString(raw string) (*PublicKey, error) {
bs, err := decode(raw)
@ -128,6 +138,20 @@ func (key *PublicKey) Equals(other *PublicKey) bool {
return key.key.Equal(other.key)
}
// Bytes returns the public key as raw bytes.
func (key *PublicKey) Bytes() []byte {
if key == nil || key.key == nil {
return nil
}
bs, err := key.key.MarshalBinary()
if err != nil {
// this should not happen
panic(fmt.Sprintf("failed to marshal public HPKE key: %v", err))
}
return bs
}
// MarshalJSON returns the JSON Web Key representation of the public key.
func (key *PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(JWK{