Skip to content

Commit

Permalink
Add and register full primitive constructor for XChaCha20-Poly1305
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699932175
Change-Id: I8bf50098682293e9149e0b1c2ad7a424e24253c5
  • Loading branch information
morambro authored and copybara-github committed Nov 25, 2024
1 parent 8e03e1d commit d79bb15
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aead/xchacha20poly1305/aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/tink-crypto/tink-go/v2/aead/subtle"
"github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess"
"github.com/tink-crypto/tink-go/v2/key"
"github.com/tink-crypto/tink-go/v2/tink"
)

Expand Down Expand Up @@ -68,3 +69,11 @@ func (ca *aead) Decrypt(ciphertext []byte, associatedData []byte) ([]byte, error
}
return plaintext, nil
}

func primitiveConstructor(key key.Key) (any, error) {
that, ok := key.(*Key)
if !ok {
return nil, fmt.Errorf("key is not a *xchacha20poly1305.Key")
}
return newAEAD(that)
}
4 changes: 4 additions & 0 deletions aead/xchacha20poly1305/xchacha20poly1305.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/tink-crypto/tink-go/v2/core/registry"
"github.com/tink-crypto/tink-go/v2/internal/internalregistry"
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
)

func init() {
Expand All @@ -40,4 +41,7 @@ func init() {
if err := protoserialization.RegisterParametersSerializer[*Parameters](&parametersSerializer{}); err != nil {
panic(fmt.Sprintf("xchacha20poly1305.init() failed: %v", err))
}
if err := registryconfig.RegisterPrimitiveConstructor[*Key](primitiveConstructor); err != nil {
panic(fmt.Sprintf("xchacha20poly1305.init() failed: %v", err))
}
}

0 comments on commit d79bb15

Please sign in to comment.