| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"github.com/dvsekhvalnov/jose2go/base64url" |
| 7 |
"github.com/dvsekhvalnov/jose2go/arrays" |
| 8 |
"crypto/aes" |
| 9 |
"crypto/cipher" |
| 10 |
) |
| 11 |
|
| 12 |
func init() { |
| 13 |
RegisterJwa(&AesGcmKW{ keySizeBits: 128}) |
| 14 |
RegisterJwa(&AesGcmKW{ keySizeBits: 192}) |
| 15 |
RegisterJwa(&AesGcmKW{ keySizeBits: 256}) |
| 16 |
} |
| 17 |
|
| 18 |
// AES GCM Key Wrap key management algorithm implementation |
| 19 |
type AesGcmKW struct { |
| 20 |
keySizeBits int |
| 21 |
} |
| 22 |
|
| 23 |
func (alg *AesGcmKW) Name() string { |
| 24 |
switch alg.keySizeBits { |
| 25 |
case 128: return A128GCMKW |
| 26 |
case 192: return A192GCMKW |
| 27 |
default: return A256GCMKW |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (alg *AesGcmKW) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) { |
| 32 |
if kek,ok:=key.([]byte); ok { |
| 33 |
|
| 34 |
kekSizeBits := len(kek) << 3 |
| 35 |
|
| 36 |
if kekSizeBits != alg.keySizeBits { |
| 37 |
return nil,nil, errors.New(fmt.Sprintf("AesGcmKW.WrapNewKey(): expected key of size %v bits, but was given %v bits.",alg.keySizeBits, kekSizeBits)) |
| 38 |
} |
| 39 |
|
| 40 |
if cek,err = arrays.Random(cekSizeBits>>3);err!=nil { |
| 41 |
return nil,nil,err |
| 42 |
} |
| 43 |
|
| 44 |
var iv []byte |
| 45 |
|
| 46 |
if iv,err = arrays.Random(12);err!=nil { |
| 47 |
return nil,nil,err |
| 48 |
} |
| 49 |
|
| 50 |
var block cipher.Block |
| 51 |
|
| 52 |
if block, err = aes.NewCipher(kek);err!=nil { |
| 53 |
return nil,nil,err |
| 54 |
} |
| 55 |
|
| 56 |
var aesgcm cipher.AEAD |
| 57 |
|
| 58 |
if aesgcm,err = cipher.NewGCM(block);err!=nil { |
| 59 |
return nil,nil,err |
| 60 |
} |
| 61 |
|
| 62 |
cipherWithTag := aesgcm.Seal(nil, iv, cek, nil) |
| 63 |
|
| 64 |
cipherText := cipherWithTag[:len(cipherWithTag)-aesgcm.Overhead()] |
| 65 |
authTag := cipherWithTag[len(cipherWithTag)-aesgcm.Overhead():] |
| 66 |
|
| 67 |
header["iv"]=base64url.Encode(iv) |
| 68 |
header["tag"]=base64url.Encode(authTag) |
| 69 |
|
| 70 |
return cek,cipherText,nil |
| 71 |
} |
| 72 |
|
| 73 |
return nil,nil,errors.New("AesGcmKW.WrapNewKey(): expected key to be '[]byte' array") |
| 74 |
} |
| 75 |
|
| 76 |
func (alg *AesGcmKW) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) { |
| 77 |
if kek,ok:=key.([]byte); ok { |
| 78 |
|
| 79 |
kekSizeBits := len(kek) << 3 |
| 80 |
|
| 81 |
if kekSizeBits != alg.keySizeBits { |
| 82 |
return nil,errors.New(fmt.Sprintf("AesGcmKW.Unwrap(): expected key of size %v bits, but was given %v bits.", alg.keySizeBits, kekSizeBits)) |
| 83 |
} |
| 84 |
|
| 85 |
var iv,tag string |
| 86 |
|
| 87 |
if iv,ok = header["iv"].(string);!ok { |
| 88 |
return nil,errors.New("AesGcmKW.Unwrap(): expected 'iv' param in JWT header, but was not found.") |
| 89 |
} |
| 90 |
|
| 91 |
if tag,ok = header["tag"].(string);!ok { |
| 92 |
return nil,errors.New("AesGcmKW.Unwrap(): expected 'tag' param in JWT header, but was not found.") |
| 93 |
} |
| 94 |
|
| 95 |
var ivBytes,tagBytes []byte |
| 96 |
|
| 97 |
if ivBytes,err = base64url.Decode(iv);err!=nil { |
| 98 |
return nil,err |
| 99 |
} |
| 100 |
|
| 101 |
if tagBytes,err = base64url.Decode(tag);err!=nil { |
| 102 |
return nil,err |
| 103 |
} |
| 104 |
|
| 105 |
var block cipher.Block |
| 106 |
|
| 107 |
if block, err = aes.NewCipher(kek);err!=nil { |
| 108 |
return nil,err |
| 109 |
} |
| 110 |
|
| 111 |
var aesgcm cipher.AEAD |
| 112 |
|
| 113 |
if aesgcm,err = cipher.NewGCM(block);err!=nil { |
| 114 |
return nil,err |
| 115 |
} |
| 116 |
|
| 117 |
if nonceSize := len(ivBytes); nonceSize != aesgcm.NonceSize() { |
| 118 |
return nil, fmt.Errorf("AesGcmKW.Unwrap(): expected nonce of size %v bits, but was given %v bits.", aesgcm.NonceSize()<<3, nonceSize<<3) |
| 119 |
} |
| 120 |
|
| 121 |
cipherAndTag:=append(encryptedCek,tagBytes...) |
| 122 |
|
| 123 |
if cek,err = aesgcm.Open(nil, ivBytes,cipherAndTag , nil);err!=nil { |
| 124 |
return nil,err |
| 125 |
} |
| 126 |
|
| 127 |
return cek,nil |
| 128 |
} |
| 129 |
|
| 130 |
return nil,errors.New("AesGcmKW.Unwrap(): expected key to be '[]byte' array") |
| 131 |
} |