| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/aes" |
| 5 |
"crypto/cipher" |
| 6 |
"crypto/hmac" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"github.com/dvsekhvalnov/jose2go/arrays" |
| 10 |
"github.com/dvsekhvalnov/jose2go/padding" |
| 11 |
) |
| 12 |
|
| 13 |
// AES CBC with HMAC authenticated encryption algorithm implementation |
| 14 |
type AesCbcHmac struct { |
| 15 |
keySizeBits int |
| 16 |
} |
| 17 |
|
| 18 |
func init() { |
| 19 |
RegisterJwe(&AesCbcHmac{keySizeBits: 256}) |
| 20 |
RegisterJwe(&AesCbcHmac{keySizeBits: 384}) |
| 21 |
RegisterJwe(&AesCbcHmac{keySizeBits: 512}) |
| 22 |
} |
| 23 |
|
| 24 |
func (alg *AesCbcHmac) Name() string { |
| 25 |
switch alg.keySizeBits { |
| 26 |
case 256: |
| 27 |
return A128CBC_HS256 |
| 28 |
case 384: |
| 29 |
return A192CBC_HS384 |
| 30 |
default: |
| 31 |
return A256CBC_HS512 |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func (alg *AesCbcHmac) KeySizeBits() int { |
| 36 |
return alg.keySizeBits |
| 37 |
} |
| 38 |
|
| 39 |
func (alg *AesCbcHmac) SetKeySizeBits(bits int) { |
| 40 |
alg.keySizeBits = bits |
| 41 |
} |
| 42 |
|
| 43 |
func (alg *AesCbcHmac) Encrypt(aad, plainText, cek []byte) (iv, cipherText, authTag []byte, err error) { |
| 44 |
|
| 45 |
cekSizeBits := len(cek) << 3 |
| 46 |
if cekSizeBits != alg.keySizeBits { |
| 47 |
return nil, nil, nil, errors.New(fmt.Sprintf("AesCbcHmac.Encrypt(): expected key of size %v bits, but was given %v bits.", alg.keySizeBits, cekSizeBits)) |
| 48 |
} |
| 49 |
|
| 50 |
hmacKey := cek[0 : len(cek)/2] |
| 51 |
aesKey := cek[len(cek)/2:] |
| 52 |
|
| 53 |
if iv, err = arrays.Random(16); err != nil { |
| 54 |
return nil, nil, nil, err |
| 55 |
} |
| 56 |
|
| 57 |
var block cipher.Block |
| 58 |
|
| 59 |
if block, err = aes.NewCipher(aesKey); err != nil { |
| 60 |
return nil, nil, nil, err |
| 61 |
} |
| 62 |
|
| 63 |
padded := padding.AddPkcs7(plainText, 16) |
| 64 |
|
| 65 |
cipherText = make([]byte, len(padded), cap(padded)) |
| 66 |
mode := cipher.NewCBCEncrypter(block, iv) |
| 67 |
mode.CryptBlocks(cipherText, padded) |
| 68 |
|
| 69 |
authTag = alg.computeAuthTag(aad, iv, cipherText, hmacKey) |
| 70 |
|
| 71 |
return iv, cipherText, authTag, nil |
| 72 |
} |
| 73 |
|
| 74 |
func (alg *AesCbcHmac) Decrypt(aad, cek, iv, cipherText, authTag []byte) (plainText []byte, err error) { |
| 75 |
|
| 76 |
cekSizeBits := len(cek) << 3 |
| 77 |
|
| 78 |
if cekSizeBits != alg.keySizeBits { |
| 79 |
return nil, errors.New(fmt.Sprintf("AesCbcHmac.Decrypt(): expected key of size %v bits, but was given %v bits.", alg.keySizeBits, cekSizeBits)) |
| 80 |
} |
| 81 |
|
| 82 |
hmacKey := cek[0 : len(cek)/2] |
| 83 |
aesKey := cek[len(cek)/2:] |
| 84 |
|
| 85 |
// Check MAC |
| 86 |
expectedAuthTag := alg.computeAuthTag(aad, iv, cipherText, hmacKey) |
| 87 |
|
| 88 |
if !hmac.Equal(expectedAuthTag, authTag) { |
| 89 |
return nil, errors.New("AesCbcHmac.Decrypt(): Authentication tag do not match.") |
| 90 |
} |
| 91 |
|
| 92 |
var block cipher.Block |
| 93 |
|
| 94 |
if block, err = aes.NewCipher(aesKey); err == nil { |
| 95 |
mode := cipher.NewCBCDecrypter(block, iv) |
| 96 |
|
| 97 |
var padded []byte = make([]byte, len(cipherText), cap(cipherText)) |
| 98 |
mode.CryptBlocks(padded, cipherText) |
| 99 |
|
| 100 |
return padding.RemovePkcs7(padded, 16), nil |
| 101 |
} |
| 102 |
|
| 103 |
return nil, err |
| 104 |
} |
| 105 |
|
| 106 |
func (alg *AesCbcHmac) computeAuthTag(aad []byte, iv []byte, cipherText []byte, hmacKey []byte) (signature []byte) { |
| 107 |
al := arrays.UInt64ToBytes(uint64(len(aad) << 3)) |
| 108 |
hmacInput := arrays.Concat(aad, iv, cipherText, al) |
| 109 |
hmac := calculateHmac(alg.keySizeBits, hmacInput, hmacKey) |
| 110 |
|
| 111 |
return hmac[0 : len(hmac)/2] |
| 112 |
} |