Skip to content

Commit

Permalink
Adding NonceSize function to AEAD.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Apr 12, 2023
1 parent eaec71f commit 2475a3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hpke/aead_test.go
Expand Up @@ -34,7 +34,7 @@ func setupAeadTest() (*sealContext, *openContext, error) {
return nil, nil, err
}

Nn := aead.NonceSize()
Nn := suite.aeadID.NonceSize()
baseNonce := make([]byte, Nn)
if n, err := rand.Read(baseNonce); err != nil {
return nil, nil, err
Expand Down
14 changes: 13 additions & 1 deletion hpke/algs.go
Expand Up @@ -196,7 +196,7 @@ func (a AEAD) IsValid() bool {
}
}

// KeySize returns the size in bytes of the keys used by AEAD cipher.
// KeySize returns the size in bytes of the keys used by the AEAD cipher.
func (a AEAD) KeySize() uint {
switch a {
case AEAD_AES128GCM:
Expand All @@ -210,6 +210,18 @@ func (a AEAD) KeySize() uint {
}
}

// NonceSize returns the size in bytes of the nonce used by the AEAD cipher.
func (a AEAD) NonceSize() uint {
switch a {
case AEAD_AES128GCM,
AEAD_AES256GCM,
AEAD_ChaCha20Poly1305:
return 12
default:
panic(ErrInvalidAEAD)
}
}

// CipherLen returns the length of a ciphertext corresponding to a message of
// length mLen.
func (a AEAD) CipherLen(mLen uint) uint {
Expand Down
2 changes: 1 addition & 1 deletion hpke/marshal.go
Expand Up @@ -69,7 +69,7 @@ func unmarshalContext(raw []byte) (*encdecContext, error) {
return nil, err
}

Nn := c.AEAD.NonceSize()
Nn := int(c.suite.aeadID.NonceSize())
if len(c.baseNonce) != Nn {
return nil, errors.New("invalid base nonce length")
}
Expand Down

0 comments on commit 2475a3f

Please sign in to comment.