1// Copyright 2018 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15//////////////////////////////////////////////////////////////////////////////// 16 17package aead_test 18 19import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "testing" 24 25 "github.com/google/go-cmp/cmp" 26 "golang.org/x/crypto/chacha20poly1305" 27 "google.golang.org/protobuf/proto" 28 "github.com/google/tink/go/core/registry" 29 "github.com/google/tink/go/internal/internalregistry" 30 "github.com/google/tink/go/subtle/random" 31 "github.com/google/tink/go/testutil" 32 33 "github.com/google/tink/go/aead/subtle" 34 tpb "github.com/google/tink/go/proto/tink_go_proto" 35 xpb "github.com/google/tink/go/proto/xchacha20_poly1305_go_proto" 36) 37 38func TestXChaCha20Poly1305GetPrimitive(t *testing.T) { 39 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 40 if err != nil { 41 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 42 } 43 m, _ := km.NewKey(nil) 44 key, _ := m.(*xpb.XChaCha20Poly1305Key) 45 serializedKey, _ := proto.Marshal(key) 46 p, err := km.Primitive(serializedKey) 47 if err != nil { 48 t.Errorf("km.Primitive(%v) = %v; want nil", serializedKey, err) 49 } 50 if err := validateXChaCha20Poly1305Primitive(p, key); err != nil { 51 t.Errorf("validateXChaCha20Poly1305Primitive(p, key) = %v; want nil", err) 52 } 53} 54 55func TestXChaCha20Poly1305GetPrimitiveWithInvalidKeys(t *testing.T) { 56 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 57 if err != nil { 58 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 59 } 60 invalidKeys := []*xpb.XChaCha20Poly1305Key{ 61 // Bad key size. 62 &xpb.XChaCha20Poly1305Key{ 63 Version: testutil.XChaCha20Poly1305KeyVersion, 64 KeyValue: random.GetRandomBytes(17), 65 }, 66 &xpb.XChaCha20Poly1305Key{ 67 Version: testutil.XChaCha20Poly1305KeyVersion, 68 KeyValue: random.GetRandomBytes(25), 69 }, 70 &xpb.XChaCha20Poly1305Key{ 71 Version: testutil.XChaCha20Poly1305KeyVersion, 72 KeyValue: random.GetRandomBytes(33), 73 }, 74 // Bad version. 75 &xpb.XChaCha20Poly1305Key{ 76 Version: testutil.XChaCha20Poly1305KeyVersion + 1, 77 KeyValue: random.GetRandomBytes(chacha20poly1305.KeySize), 78 }, 79 } 80 for _, key := range invalidKeys { 81 serializedKey, _ := proto.Marshal(key) 82 if _, err := km.Primitive(serializedKey); err == nil { 83 t.Errorf("km.Primitive(%v) = _, nil; want _, err", serializedKey) 84 } 85 } 86} 87 88func TestXChaCha20Poly1305NewKey(t *testing.T) { 89 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 90 if err != nil { 91 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 92 } 93 m, err := km.NewKey(nil) 94 if err != nil { 95 t.Errorf("km.NewKey(nil) = _, %v; want _, nil", err) 96 } 97 key, _ := m.(*xpb.XChaCha20Poly1305Key) 98 if err := validateXChaCha20Poly1305Key(key); err != nil { 99 t.Errorf("validateXChaCha20Poly1305Key(%v) = %v; want nil", key, err) 100 } 101} 102 103func TestXChaCha20Poly1305NewKeyData(t *testing.T) { 104 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 105 if err != nil { 106 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 107 } 108 kd, err := km.NewKeyData(nil) 109 if err != nil { 110 t.Errorf("km.NewKeyData(nil) = _, %v; want _, nil", err) 111 } 112 if kd.TypeUrl != testutil.XChaCha20Poly1305TypeURL { 113 t.Errorf("TypeUrl: %v != %v", kd.TypeUrl, testutil.XChaCha20Poly1305TypeURL) 114 } 115 if kd.KeyMaterialType != tpb.KeyData_SYMMETRIC { 116 t.Errorf("KeyMaterialType: %v != SYMMETRIC", kd.KeyMaterialType) 117 } 118 key := new(xpb.XChaCha20Poly1305Key) 119 if err := proto.Unmarshal(kd.Value, key); err != nil { 120 t.Errorf("proto.Unmarshal(%v, key) = %v; want nil", kd.Value, err) 121 } 122 if err := validateXChaCha20Poly1305Key(key); err != nil { 123 t.Errorf("validateXChaCha20Poly1305Key(%v) = %v; want nil", key, err) 124 } 125} 126 127func TestXChaCha20Poly1305DoesSupport(t *testing.T) { 128 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 129 if err != nil { 130 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 131 } 132 if !km.DoesSupport(testutil.XChaCha20Poly1305TypeURL) { 133 t.Errorf("XChaCha20Poly1305KeyManager must support %s", testutil.XChaCha20Poly1305TypeURL) 134 } 135 if km.DoesSupport("some bad type") { 136 t.Errorf("XChaCha20Poly1305KeyManager must only support %s", testutil.XChaCha20Poly1305TypeURL) 137 } 138} 139 140func TestXChaCha20Poly1305TypeURL(t *testing.T) { 141 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 142 if err != nil { 143 t.Errorf("cannot obtain XChaCha20Poly1305 key manager: %s", err) 144 } 145 if kt := km.TypeURL(); kt != testutil.XChaCha20Poly1305TypeURL { 146 t.Errorf("km.TypeURL() = %s; want %s", kt, testutil.XChaCha20Poly1305TypeURL) 147 } 148} 149 150func TestXChaCha20Poly1305KeyMaterialType(t *testing.T) { 151 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 152 if err != nil { 153 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testutil.XChaCha20Poly1305TypeURL, err) 154 } 155 keyManager, ok := km.(internalregistry.DerivableKeyManager) 156 if !ok { 157 t.Fatalf("key manager is not DerivableKeyManager") 158 } 159 if got, want := keyManager.KeyMaterialType(), tpb.KeyData_SYMMETRIC; got != want { 160 t.Errorf("KeyMaterialType() = %v, want %v", got, want) 161 } 162} 163 164func TestXChaCha20Poly1305DeriveKey(t *testing.T) { 165 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 166 if err != nil { 167 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testutil.XChaCha20Poly1305TypeURL, err) 168 } 169 keyManager, ok := km.(internalregistry.DerivableKeyManager) 170 if !ok { 171 t.Fatalf("key manager is not DerivableKeyManager") 172 } 173 keyFormat, err := proto.Marshal(&xpb.XChaCha20Poly1305KeyFormat{Version: 0}) 174 if err != nil { 175 t.Fatalf("proto.Marshal() err = %v, want nil", err) 176 } 177 for _, test := range []struct { 178 name string 179 keyFormat []byte 180 }{ 181 { 182 // nil unmarshals to an empty proto, which implies version = 0. 183 name: "nil", 184 keyFormat: nil, 185 }, 186 { 187 // An empty proto implies version = 0. 188 name: "empty", 189 keyFormat: []byte{}, 190 }, 191 { 192 name: "specified", 193 keyFormat: keyFormat, 194 }, 195 } { 196 t.Run(test.name, func(t *testing.T) { 197 rand := random.GetRandomBytes(chacha20poly1305.KeySize) 198 buf := &bytes.Buffer{} 199 buf.Write(rand) // never returns a non-nil error 200 k, err := keyManager.DeriveKey(test.keyFormat, buf) 201 if err != nil { 202 t.Fatalf("keyManager.DeriveKey() err = %v, want nil", err) 203 } 204 key := k.(*xpb.XChaCha20Poly1305Key) 205 if got, want := len(key.GetKeyValue()), chacha20poly1305.KeySize; got != want { 206 t.Errorf("key length = %d, want %d", got, want) 207 } 208 if diff := cmp.Diff(key.GetKeyValue(), rand); diff != "" { 209 t.Errorf("incorrect derived key: diff = %v", diff) 210 } 211 }) 212 } 213} 214 215func TestXChaCha20Poly1305DeriveKeyFailsWithInvalidKeyFormats(t *testing.T) { 216 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 217 if err != nil { 218 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testutil.XChaCha20Poly1305TypeURL, err) 219 } 220 keyManager, ok := km.(internalregistry.DerivableKeyManager) 221 if !ok { 222 t.Fatalf("key manager is not DerivableKeyManager") 223 } 224 invalidVersion, err := proto.Marshal(&xpb.XChaCha20Poly1305KeyFormat{Version: 10}) 225 if err != nil { 226 t.Fatalf("proto.Marshal() err = %v, want nil", err) 227 } 228 // Proto messages start with a VarInt, which always ends with a byte with the 229 // MSB unset, so 0x80 is invalid. 230 invalidSerialization, err := hex.DecodeString("80") 231 if err != nil { 232 t.Errorf("hex.DecodeString() err = %v, want nil", err) 233 } 234 for _, test := range []struct { 235 name string 236 keyFormat []byte 237 }{ 238 { 239 name: "invalid version", 240 keyFormat: invalidVersion, 241 }, 242 { 243 name: "invalid serialization", 244 keyFormat: invalidSerialization, 245 }, 246 } { 247 t.Run(test.name, func(t *testing.T) { 248 buf := bytes.NewBuffer(random.GetRandomBytes(chacha20poly1305.KeySize)) 249 if _, err := keyManager.DeriveKey(test.keyFormat, buf); err == nil { 250 t.Errorf("keyManager.DeriveKey() err = nil, want non-nil") 251 } 252 }) 253 } 254} 255 256func TestXChaCha20Poly1305DeriveKeyFailsWithInsufficientRandomness(t *testing.T) { 257 km, err := registry.GetKeyManager(testutil.XChaCha20Poly1305TypeURL) 258 if err != nil { 259 t.Fatalf("registry.GetKeyManager(%q) err = %v, want nil", testutil.XChaCha20Poly1305TypeURL, err) 260 } 261 keyManager, ok := km.(internalregistry.DerivableKeyManager) 262 if !ok { 263 t.Fatalf("key manager is not DerivableKeyManager") 264 } 265 keyFormat, err := proto.Marshal(&xpb.XChaCha20Poly1305KeyFormat{Version: 0}) 266 if err != nil { 267 t.Fatalf("proto.Marshal() err = %v, want nil", err) 268 } 269 { 270 buf := bytes.NewBuffer(random.GetRandomBytes(chacha20poly1305.KeySize)) 271 if _, err := keyManager.DeriveKey(keyFormat, buf); err != nil { 272 t.Errorf("keyManager.DeriveKey() err = %v, want nil", err) 273 } 274 } 275 { 276 insufficientBuf := bytes.NewBuffer(random.GetRandomBytes(chacha20poly1305.KeySize - 1)) 277 if _, err := keyManager.DeriveKey(keyFormat, insufficientBuf); err == nil { 278 t.Errorf("keyManager.DeriveKey() err = nil, want non-nil") 279 } 280 } 281} 282 283func validateXChaCha20Poly1305Primitive(p interface{}, key *xpb.XChaCha20Poly1305Key) error { 284 cipher := p.(*subtle.XChaCha20Poly1305) 285 if !bytes.Equal(cipher.Key, key.KeyValue) { 286 return fmt.Errorf("key and primitive don't match") 287 } 288 289 // Try to encrypt and decrypt. 290 pt := random.GetRandomBytes(32) 291 aad := random.GetRandomBytes(32) 292 ct, err := cipher.Encrypt(pt, aad) 293 if err != nil { 294 return fmt.Errorf("encryption failed") 295 } 296 decrypted, err := cipher.Decrypt(ct, aad) 297 if err != nil { 298 return fmt.Errorf("decryption failed") 299 } 300 if !bytes.Equal(decrypted, pt) { 301 return fmt.Errorf("decryption failed") 302 } 303 return nil 304} 305 306func validateXChaCha20Poly1305Key(key *xpb.XChaCha20Poly1305Key) error { 307 if key.Version != testutil.XChaCha20Poly1305KeyVersion { 308 return fmt.Errorf("incorrect key version: keyVersion != %d", testutil.XChaCha20Poly1305KeyVersion) 309 } 310 if uint32(len(key.KeyValue)) != chacha20poly1305.KeySize { 311 return fmt.Errorf("incorrect key size: keySize != %d", chacha20poly1305.KeySize) 312 } 313 314 // Try to encrypt and decrypt. 315 p, err := subtle.NewXChaCha20Poly1305(key.KeyValue) 316 if err != nil { 317 return fmt.Errorf("invalid key: %v", key.KeyValue) 318 } 319 return validateXChaCha20Poly1305Primitive(p, key) 320} 321