github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/sw/rsa.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sw 18 19 import ( 20 "crypto/rand" 21 "crypto/rsa" 22 "errors" 23 "fmt" 24 25 "github.com/hyperledger/fabric/bccsp" 26 ) 27 28 type rsaSigner struct{} 29 30 func (s *rsaSigner) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) { 31 if opts == nil { 32 return nil, errors.New("Invalid options. Must be different from nil.") 33 } 34 35 return k.(*rsaPrivateKey).privKey.Sign(rand.Reader, digest, opts) 36 } 37 38 type rsaPrivateKeyVerifier struct{} 39 40 func (v *rsaPrivateKeyVerifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) { 41 if opts == nil { 42 return false, errors.New("Invalid options. It must not be nil.") 43 } 44 switch opts.(type) { 45 case *rsa.PSSOptions: 46 err := rsa.VerifyPSS(&(k.(*rsaPrivateKey).privKey.PublicKey), 47 (opts.(*rsa.PSSOptions)).Hash, 48 digest, signature, opts.(*rsa.PSSOptions)) 49 50 return err == nil, err 51 default: 52 return false, fmt.Errorf("Opts type not recognized [%s]", opts) 53 } 54 } 55 56 type rsaPublicKeyKeyVerifier struct{} 57 58 func (v *rsaPublicKeyKeyVerifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) { 59 if opts == nil { 60 return false, errors.New("Invalid options. It must not be nil.") 61 } 62 switch opts.(type) { 63 case *rsa.PSSOptions: 64 err := rsa.VerifyPSS(k.(*rsaPublicKey).pubKey, 65 (opts.(*rsa.PSSOptions)).Hash, 66 digest, signature, opts.(*rsa.PSSOptions)) 67 68 return err == nil, err 69 default: 70 return false, fmt.Errorf("Opts type not recognized [%s]", opts) 71 } 72 }