github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/signer/signer.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 package signer 17 18 import ( 19 "crypto" 20 "errors" 21 "fmt" 22 "io" 23 24 "github.com/hyperledger/fabric/bccsp" 25 "github.com/hyperledger/fabric/bccsp/utils" 26 ) 27 28 // bccspCryptoSigner is the BCCSP-based implementation of a crypto.Signer 29 type bccspCryptoSigner struct { 30 csp bccsp.BCCSP 31 key bccsp.Key 32 pk interface{} 33 } 34 35 // New returns a new BCCSP-based crypto.Signer 36 // for the given BCCSP instance and key. 37 func New(csp bccsp.BCCSP, key bccsp.Key) (crypto.Signer, error) { 38 // Validate arguments 39 if csp == nil { 40 return nil, errors.New("bccsp instance must be different from nil.") 41 } 42 if key == nil { 43 return nil, errors.New("key must be different from nil.") 44 } 45 if key.Symmetric() { 46 return nil, errors.New("key must be asymmetric.") 47 } 48 49 // Marshall the bccsp public key as a crypto.PublicKey 50 pub, err := key.PublicKey() 51 if err != nil { 52 return nil, fmt.Errorf("failed getting public key [%s]", err) 53 } 54 55 raw, err := pub.Bytes() 56 if err != nil { 57 return nil, fmt.Errorf("failed marshalling public key [%s]", err) 58 } 59 60 pk, err := utils.DERToPublicKey(raw) 61 if err != nil { 62 return nil, fmt.Errorf("failed marshalling der to public key [%s]", err) 63 } 64 65 return &bccspCryptoSigner{csp, key, pk}, nil 66 } 67 68 // Public returns the public key corresponding to the opaque, 69 // private key. 70 func (s *bccspCryptoSigner) Public() crypto.PublicKey { 71 return s.pk 72 } 73 74 // Sign signs digest with the private key, possibly using entropy from 75 // rand. For an RSA key, the resulting signature should be either a 76 // PKCS#1 v1.5 or PSS signature (as indicated by opts). For an (EC)DSA 77 // key, it should be a DER-serialised, ASN.1 signature structure. 78 // 79 // Hash implements the SignerOpts interface and, in most cases, one can 80 // simply pass in the hash function used as opts. Sign may also attempt 81 // to type assert opts to other types in order to obtain algorithm 82 // specific values. See the documentation in each package for details. 83 // 84 // Note that when a signature of a hash of a larger message is needed, 85 // the caller is responsible for hashing the larger message and passing 86 // the hash (as digest) and the hash function (as opts) to Sign. 87 func (s *bccspCryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { 88 return s.csp.Sign(s.key, digest, opts) 89 }