github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bccsp/pkcs11/ecdsa.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 pkcs11 17 18 import ( 19 "crypto/ecdsa" 20 "crypto/elliptic" 21 "encoding/asn1" 22 "errors" 23 "fmt" 24 "math/big" 25 26 "github.com/hyperledger/fabric/bccsp" 27 ) 28 29 type ecdsaSignature struct { 30 R, S *big.Int 31 } 32 33 var ( 34 // curveHalfOrders contains the precomputed curve group orders halved. 35 // It is used to ensure that signature' S value is lower or equal to the 36 // curve group order halved. We accept only low-S signatures. 37 // They are precomputed for efficiency reasons. 38 curveHalfOrders map[elliptic.Curve]*big.Int = map[elliptic.Curve]*big.Int{ 39 elliptic.P224(): new(big.Int).Rsh(elliptic.P224().Params().N, 1), 40 elliptic.P256(): new(big.Int).Rsh(elliptic.P256().Params().N, 1), 41 elliptic.P384(): new(big.Int).Rsh(elliptic.P384().Params().N, 1), 42 elliptic.P521(): new(big.Int).Rsh(elliptic.P521().Params().N, 1), 43 } 44 ) 45 46 func marshalECDSASignature(r, s *big.Int) ([]byte, error) { 47 return asn1.Marshal(ecdsaSignature{r, s}) 48 } 49 50 func unmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) { 51 // Unmarshal 52 sig := new(ecdsaSignature) 53 _, err := asn1.Unmarshal(raw, sig) 54 if err != nil { 55 return nil, nil, fmt.Errorf("Failed unmashalling signature [%s]", err) 56 } 57 58 // Validate sig 59 if sig.R == nil { 60 return nil, nil, errors.New("Invalid signature. R must be different from nil.") 61 } 62 if sig.S == nil { 63 return nil, nil, errors.New("Invalid signature. S must be different from nil.") 64 } 65 66 if sig.R.Sign() != 1 { 67 return nil, nil, errors.New("Invalid signature. R must be larger than zero") 68 } 69 if sig.S.Sign() != 1 { 70 return nil, nil, errors.New("Invalid signature. S must be larger than zero") 71 } 72 73 return sig.R, sig.S, nil 74 } 75 76 func (csp *impl) signECDSA(k ecdsaPrivateKey, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) { 77 r, s, err := csp.signP11ECDSA(k.ski, digest) 78 if err != nil { 79 return nil, err 80 } 81 82 // check for low-S 83 halfOrder, ok := curveHalfOrders[k.pub.pub.Curve] 84 if !ok { 85 return nil, fmt.Errorf("Curve not recognized [%s]", k.pub.pub.Curve) 86 } 87 88 // is s > halfOrder Then 89 if s.Cmp(halfOrder) == 1 { 90 // Set s to N - s that will be then in the lower part of signature space 91 // less or equal to half order 92 s.Sub(k.pub.pub.Params().N, s) 93 } 94 95 return marshalECDSASignature(r, s) 96 } 97 98 func (csp *impl) verifyECDSA(k ecdsaPublicKey, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) { 99 r, s, err := unmarshalECDSASignature(signature) 100 if err != nil { 101 return false, fmt.Errorf("Failed unmashalling signature [%s]", err) 102 } 103 104 // check for low-S 105 halfOrder, ok := curveHalfOrders[k.pub.Curve] 106 if !ok { 107 return false, fmt.Errorf("Curve not recognized [%s]", k.pub.Curve) 108 } 109 110 // If s > halfOrder Then 111 if s.Cmp(halfOrder) == 1 { 112 return false, fmt.Errorf("Invalid S. Must be smaller than half the order [%s][%s].", s, halfOrder) 113 } 114 115 if csp.softVerify { 116 return ecdsa.Verify(k.pub, digest, r, s), nil 117 } else { 118 return csp.verifyP11ECDSA(k.ski, digest, r, s, k.pub.Curve.Params().BitSize/8) 119 } 120 }