| 1 | package jose |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/rsa" |
| 6 | "errors" |
| 7 | ) |
| 8 | |
| 9 | func init() { |
| 10 | RegisterJws(&RsaPssUsingSha{keySizeBits: 256, saltSizeBytes: 32}) |
| 11 | RegisterJws(&RsaPssUsingSha{keySizeBits: 384, saltSizeBytes: 48}) |
| 12 | RegisterJws(&RsaPssUsingSha{keySizeBits: 512, saltSizeBytes: 64}) |
| 13 | } |
| 14 | |
| 15 | // RSA with PSS using SHA signing algorithm implementation |
| 16 | type RsaPssUsingSha struct{ |
| 17 | keySizeBits int |
| 18 | saltSizeBytes int |
| 19 | } |
| 20 | |
| 21 | func (alg *RsaPssUsingSha) Name() string { |
| 22 | switch alg.keySizeBits { |
| 23 | case 256: return PS256 |
| 24 | case 384: return PS384 |
| 25 | default: return PS512 |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func (alg *RsaPssUsingSha) Verify(securedInput, signature []byte, key interface{}) error { |
| 30 | if pubKey,ok:=key.(*rsa.PublicKey);ok { |
| 31 | return rsa.VerifyPSS(pubKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput), signature, &rsa.PSSOptions{SaltLength:alg.saltSizeBytes}) |
| 32 | } |
| 33 | |
| 34 | return errors.New("RsaPssUsingSha.Verify(): expects key to be '*rsa.PublicKey'") |
| 35 | } |
| 36 | |
| 37 | func (alg *RsaPssUsingSha) Sign(securedInput []byte, key interface{}) (signature []byte, err error) { |
| 38 | if privKey,ok:=key.(*rsa.PrivateKey);ok { |
| 39 | return rsa.SignPSS(rand.Reader, privKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput), &rsa.PSSOptions{SaltLength:alg.saltSizeBytes}) |
| 40 | } |
| 41 | |
| 42 | return nil,errors.New("RsaPssUsingSha.Sign(): expects key to be '*rsa.PrivateKey'") |
| 43 | } |