github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/aead/subtle/subtle.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 // Package subtle provides subtle implementations of the AEAD primitive. 8 package subtle 9 10 import ( 11 "fmt" 12 ) 13 14 const ( 15 maxInt = int(^uint(0) >> 1) 16 // AES128Size value in number of bytes. 17 AES128Size = 16 18 // AES192Size value in number of bytes. 19 AES192Size = 24 20 // AES256Size value in number of bytes. 21 AES256Size = 32 22 ) 23 24 // ValidateAESKeySize checks if the given key size is a valid AES key size. 25 func ValidateAESKeySize(sizeInBytes uint32) error { 26 switch sizeInBytes { 27 case AES128Size, AES192Size, AES256Size: 28 return nil 29 default: 30 return fmt.Errorf("invalid AES key size; want 16, 24 or 32, got %d", sizeInBytes) 31 } 32 } 33 34 // ValidateAESKeySizeForGoJose checks if the given key size is a valid AES key size. 35 func ValidateAESKeySizeForGoJose(sizeInBytes uint32) error { 36 const doubleKeySize = 2 37 38 // double key size (hmac+cbc) 39 switch sizeInBytes { 40 case AES128Size * doubleKeySize, AES192Size * doubleKeySize, AES256Size * doubleKeySize: 41 return nil 42 default: 43 return fmt.Errorf("invalid AES CBC key size; want 32, 48 or 64, got %d", sizeInBytes) 44 } 45 }