| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"errors" |
| 6 |
"crypto/aes" |
| 7 |
"crypto/cipher" |
| 8 |
"github.com/dvsekhvalnov/jose2go/arrays" |
| 9 |
) |
| 10 |
|
| 11 |
// AES GCM authenticated encryption algorithm implementation |
| 12 |
type AesGcm struct{ |
| 13 |
keySizeBits int |
| 14 |
} |
| 15 |
|
| 16 |
func init() { |
| 17 |
RegisterJwe(&AesGcm{keySizeBits:128}) |
| 18 |
RegisterJwe(&AesGcm{keySizeBits:192}) |
| 19 |
RegisterJwe(&AesGcm{keySizeBits:256}) |
| 20 |
} |
| 21 |
|
| 22 |
func (alg *AesGcm) Name() string { |
| 23 |
switch alg.keySizeBits { |
| 24 |
case 128: return A128GCM |
| 25 |
case 192: return A192GCM |
| 26 |
default: return A256GCM |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func (alg *AesGcm) KeySizeBits() int { |
| 31 |
return alg.keySizeBits |
| 32 |
} |
| 33 |
|
| 34 |
func (alg *AesGcm) Encrypt(aad, plainText, cek []byte) (iv, cipherText, authTag []byte, err error) { |
| 35 |
|
| 36 |
cekSizeBits := len(cek)<<3 |
| 37 |
|
| 38 |
if cekSizeBits != alg.keySizeBits { |
| 39 |
return nil,nil,nil, fmt.Errorf("AesGcm.Encrypt(): expected key of size %v bits, but was given %v bits.",alg.keySizeBits, cekSizeBits) |
| 40 |
} |
| 41 |
|
| 42 |
if iv,err = arrays.Random(12);err!=nil { |
| 43 |
return nil,nil,nil,err |
| 44 |
} |
| 45 |
|
| 46 |
var block cipher.Block |
| 47 |
|
| 48 |
if block, err = aes.NewCipher(cek);err!=nil { |
| 49 |
return nil,nil,nil,err |
| 50 |
} |
| 51 |
|
| 52 |
var aesgcm cipher.AEAD |
| 53 |
|
| 54 |
if aesgcm,err = cipher.NewGCM(block);err!=nil { |
| 55 |
return nil,nil,nil,err |
| 56 |
} |
| 57 |
|
| 58 |
cipherWithTag := aesgcm.Seal(nil, iv, plainText, aad) |
| 59 |
|
| 60 |
cipherText=cipherWithTag[:len(cipherWithTag)-aesgcm.Overhead()] |
| 61 |
authTag=cipherWithTag[len(cipherWithTag)-aesgcm.Overhead():] |
| 62 |
|
| 63 |
return iv, cipherText, authTag, nil |
| 64 |
} |
| 65 |
|
| 66 |
func (alg *AesGcm) Decrypt(aad, cek, iv, cipherText, authTag []byte) (plainText []byte, err error) { |
| 67 |
|
| 68 |
cekSizeBits := len(cek)<<3 |
| 69 |
|
| 70 |
if cekSizeBits != alg.keySizeBits { |
| 71 |
return nil, errors.New(fmt.Sprintf("AesGcm.Decrypt(): expected key of size %v bits, but was given %v bits.",alg.keySizeBits, cekSizeBits)) |
| 72 |
} |
| 73 |
|
| 74 |
var block cipher.Block |
| 75 |
|
| 76 |
if block, err = aes.NewCipher(cek);err!=nil { |
| 77 |
return nil,err |
| 78 |
} |
| 79 |
|
| 80 |
var aesgcm cipher.AEAD |
| 81 |
|
| 82 |
if aesgcm,err = cipher.NewGCM(block);err!=nil { |
| 83 |
return nil,err |
| 84 |
} |
| 85 |
|
| 86 |
cipherWithTag:=append(cipherText,authTag...) |
| 87 |
|
| 88 |
if nonceSize := len(iv); nonceSize != aesgcm.NonceSize() { |
| 89 |
return nil, errors.New(fmt.Sprintf("AesGcm.Decrypt(): expected nonce of size %v bits, but was given %v bits.", aesgcm.NonceSize()<<3, nonceSize<<3)) |
| 90 |
} |
| 91 |
|
| 92 |
if plainText,err = aesgcm.Open(nil, iv, cipherWithTag, aad);err!=nil { |
| 93 |
return nil,err |
| 94 |
} |
| 95 |
|
| 96 |
return plainText,nil |
| 97 |
} |
| 98 |
|