Skip to content

Commit

Permalink
Register proto serialization for AES-GCM-SIV to the global registry
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 700308124
Change-Id: Ic50dd63e357116a1f5c8f68b34fc1916a6d0c04a
  • Loading branch information
morambro authored and copybara-github committed Nov 26, 2024
1 parent b6afeba commit 784e870
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
1 change: 1 addition & 0 deletions aead/aead.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/aead/aesctrhmac" // To register the AES-CTR-HMAC key manager.
_ "github.com/tink-crypto/tink-go/v2/aead/aesgcm" // To register the AES-GCM key manager, parser and serializer.
_ "github.com/tink-crypto/tink-go/v2/aead/aesgcmsiv" // To register the AES-GCM-SIV key parser and serializer.
_ "github.com/tink-crypto/tink-go/v2/aead/chacha20poly1305" // To register the ChaCha20Poly1305 key manager, parser and serializer.
_ "github.com/tink-crypto/tink-go/v2/aead/xchacha20poly1305" // To register the XChaCha20Poly1305 key manager.
"github.com/tink-crypto/tink-go/v2/core/registry"
Expand Down
36 changes: 36 additions & 0 deletions aead/aesgcmsiv/aesgcmsiv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package aesgcmsiv provides an implementation of AES-GCM-SIV.
//
// See https://www.rfc-editor.org/rfc/rfc8452.html for more information.
package aesgcmsiv

import (
"fmt"

"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
)

func init() {
if err := protoserialization.RegisterKeySerializer[*Key](&keySerializer{}); err != nil {
panic(fmt.Sprintf("aesgcmsiv.init() failed: %v", err))
}
if err := protoserialization.RegisterKeyParser(typeURL, &keyParser{}); err != nil {
panic(fmt.Sprintf("aesgcmsiv.init() failed: %v", err))
}
if err := protoserialization.RegisterParametersSerializer[*Parameters](&parametersSerializer{}); err != nil {
panic(fmt.Sprintf("aesgcmsiv.init() failed: %v", err))
}
}
141 changes: 141 additions & 0 deletions aead/aesgcmsiv/aesgcmsiv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aesgcmsiv_test

import (
"bytes"
"testing"

"github.com/tink-crypto/tink-go/v2/aead"
"github.com/tink-crypto/tink-go/v2/aead/aesgcmsiv"
"github.com/tink-crypto/tink-go/v2/keyset"
)

func TestGetKeyFromHandle(t *testing.T) {
keysetHandle, err := keyset.NewHandle(aead.AES128GCMSIVKeyTemplate())
if err != nil {
t.Fatalf("keyset.NewHandle(aead.AES128GCMSIVKeyTemplate()) err = %v, want nil", err)
}
entry, err := keysetHandle.Entry(0)
if err != nil {
t.Fatalf("keysetHandle.Entry(0) err = %v, want nil", err)
}
key, ok := entry.Key().(*aesgcmsiv.Key)
if !ok {
t.Fatalf("entry.Key() is not an *Key")
}
keySize := 16
expectedParameters, err := aesgcmsiv.NewParameters(keySize, aesgcmsiv.VariantTink)
if err != nil {
t.Fatalf("aesgcmsiv.NewParameters(%v, %v) err = %v, want nil", keySize, aesgcmsiv.VariantTink, err)
}
if !key.Parameters().Equals(expectedParameters) {
t.Errorf("key.Parameters().Equals(expectedParameters) = false, want true")
}
if _, hasIDRequirement := key.IDRequirement(); !hasIDRequirement {
t.Errorf("expected ID requirement, got none")
}
keyBytes := key.KeyBytes()
if keyBytes.Len() != keySize {
t.Errorf("keyBytes.Len() = %v, want %v", keyBytes.Len(), keySize)
}
}

func TestCreateKeysetHandleFromKey(t *testing.T) {
keysetHandle, err := keyset.NewHandle(aead.AES256GCMSIVKeyTemplate())
if err != nil {
t.Fatalf("keyset.NewHandle(aead.AES256GCMSIVKeyTemplate()) err = %v, want nil", err)
}
aeadPrimitive, err := aead.New(keysetHandle)
if err != nil {
t.Fatalf("aead.New(keysetHandle) err = %v, want nil", err)
}
plaintext := []byte("plaintext")
additionalData := []byte("additionalData")
ciphertext, err := aeadPrimitive.Encrypt(plaintext, additionalData)
if err != nil {
t.Fatalf("aeadPrimitive.Encrypt(%v, %v) err = %v, want nil", plaintext, additionalData, err)
}

entry, err := keysetHandle.Entry(0)
if err != nil {
t.Fatalf("keysetHandle.Entry(0) err = %v, want nil", err)
}
key, ok := entry.Key().(*aesgcmsiv.Key)
if !ok {
t.Fatalf("entry.Key() is not *aesgcmsiv.Key")
}

// Create a new keyset handle with the same key.
manager := keyset.NewManager()
keyID, err := manager.AddKey(key)
if err != nil {
t.Fatalf("manager.AddKey(key) err = %v, want nil", err)
}
if err = manager.SetPrimary(keyID); err != nil {
t.Fatalf("manager.SetPrimary(%v) err = %v, want nil", keyID, err)
}
newHandle, err := manager.Handle()
if err != nil {
t.Fatalf("manager.Handle() err = %v, want nil", err)
}

// Get an AEAD primitive from the new handle and decrypt the ciphertext.
newAEAD, err := aead.New(newHandle)
if err != nil {
t.Fatalf("aead.New(newHandle) err = %v, want nil", err)
}
decrypt, err := newAEAD.Decrypt(ciphertext, additionalData)
if err != nil {
t.Fatalf("decrypt.New(otherAEADPrimitivce, %v, %v) err = %v, want nil", ciphertext, additionalData, err)
}
if !bytes.Equal(decrypt, plaintext) {
t.Errorf("decrypt = %v, want %v", decrypt, plaintext)
}
}

func TestCreateKeysetHandleFromParameters(t *testing.T) {
params, err := aesgcmsiv.NewParameters(32, aesgcmsiv.VariantTink)
if err != nil {
t.Fatalf("aesgcmsiv.NewParameters(%v, %v) err = %v, want nil", 32, aesgcmsiv.VariantTink, err)
}
manager := keyset.NewManager()
keyID, err := manager.AddNewKeyFromParameters(params)
if err != nil {
t.Fatalf("manager.AddNewKeyFromParameters(%v) err = %v, want nil", params, err)
}
manager.SetPrimary(keyID)
handle, err := manager.Handle()
if err != nil {
t.Fatalf("manager.Handle() err = %v, want nil", err)
}
aeadPrimitive, err := aead.New(handle)
if err != nil {
t.Fatalf("aead.New(handle) err = %v, want nil", err)
}
plaintext := []byte("plaintext")
additionalData := []byte("additionalData")
ciphertext, err := aeadPrimitive.Encrypt(plaintext, additionalData)
if err != nil {
t.Fatalf("aeadPrimitive.Encrypt(%v, %v) err = %v, want nil", plaintext, additionalData, err)
}
decrypted, err := aeadPrimitive.Decrypt(ciphertext, additionalData)
if err != nil {
t.Fatalf("aeadPrimitive.Decrypt(%v, %v) err = %v, want nil", ciphertext, additionalData, err)
}
if !bytes.Equal(decrypted, plaintext) {
t.Errorf("decrypted = %v, want %v", decrypted, plaintext)
}
}
3 changes: 0 additions & 3 deletions aead/aesgcmsiv/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package aesgcmsiv provides an implementation of AES-GCM-SIV.
//
// See https://www.rfc-editor.org/rfc/rfc8452.html for more information.
package aesgcmsiv

import (
Expand Down

0 comments on commit 784e870

Please sign in to comment.