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