| 1 | package jose |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "crypto/rsa" |
| 6 | "crypto/rand" |
| 7 | "github.com/dvsekhvalnov/jose2go/arrays" |
| 8 | ) |
| 9 | |
| 10 | func init() { |
| 11 | RegisterJwa(new(RsaPkcs1v15)) |
| 12 | } |
| 13 | |
| 14 | // RS-AES using PKCS #1 v1.5 padding key management algorithm implementation |
| 15 | type RsaPkcs1v15 struct{ |
| 16 | } |
| 17 | |
| 18 | func (alg *RsaPkcs1v15) Name() string { |
| 19 | return RSA1_5 |
| 20 | } |
| 21 | |
| 22 | func (alg *RsaPkcs1v15) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) { |
| 23 | if pubKey,ok:=key.(*rsa.PublicKey);ok { |
| 24 | if cek,err = arrays.Random(cekSizeBits>>3);err==nil { |
| 25 | encryptedCek,err=rsa.EncryptPKCS1v15(rand.Reader,pubKey,cek) |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | return nil,nil,err |
| 30 | } |
| 31 | |
| 32 | return nil,nil,errors.New("RsaPkcs1v15.WrapNewKey(): expected key to be '*rsa.PublicKey'") |
| 33 | } |
| 34 | |
| 35 | func (alg *RsaPkcs1v15) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) { |
| 36 | if privKey,ok:=key.(*rsa.PrivateKey);ok { |
| 37 | return rsa.DecryptPKCS1v15(rand.Reader,privKey,encryptedCek) |
| 38 | } |
| 39 | |
| 40 | return nil,errors.New("RsaPkcs1v15.Unwrap(): expected key to be '*rsa.PrivateKey'") |
| 41 | } |