github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bccsp/sw/ecdsakey.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 sw
    17  
    18  import (
    19  	"crypto/ecdsa"
    20  	"crypto/x509"
    21  	"fmt"
    22  
    23  	"crypto/sha256"
    24  
    25  	"errors"
    26  
    27  	"crypto/elliptic"
    28  
    29  	"github.com/hyperledger/fabric/bccsp"
    30  )
    31  
    32  type ecdsaPrivateKey struct {
    33  	privKey *ecdsa.PrivateKey
    34  }
    35  
    36  // Bytes converts this key to its byte representation,
    37  // if this operation is allowed.
    38  func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
    39  	return nil, errors.New("Not supported.")
    40  }
    41  
    42  // SKI returns the subject key identifier of this key.
    43  func (k *ecdsaPrivateKey) SKI() (ski []byte) {
    44  	if k.privKey == nil {
    45  		return nil
    46  	}
    47  
    48  	// Marshall the public key
    49  	raw := elliptic.Marshal(k.privKey.Curve, k.privKey.PublicKey.X, k.privKey.PublicKey.Y)
    50  
    51  	// Hash it
    52  	hash := sha256.New()
    53  	hash.Write(raw)
    54  	return hash.Sum(nil)
    55  }
    56  
    57  // Symmetric returns true if this key is a symmetric key,
    58  // false if this key is asymmetric
    59  func (k *ecdsaPrivateKey) Symmetric() bool {
    60  	return false
    61  }
    62  
    63  // Private returns true if this key is a private key,
    64  // false otherwise.
    65  func (k *ecdsaPrivateKey) Private() bool {
    66  	return true
    67  }
    68  
    69  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
    70  // This method returns an error in symmetric key schemes.
    71  func (k *ecdsaPrivateKey) PublicKey() (bccsp.Key, error) {
    72  	return &ecdsaPublicKey{&k.privKey.PublicKey}, nil
    73  }
    74  
    75  type ecdsaPublicKey struct {
    76  	pubKey *ecdsa.PublicKey
    77  }
    78  
    79  // Bytes converts this key to its byte representation,
    80  // if this operation is allowed.
    81  func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
    82  	raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
    83  	if err != nil {
    84  		return nil, fmt.Errorf("Failed marshalling key [%s]", err)
    85  	}
    86  	return
    87  }
    88  
    89  // SKI returns the subject key identifier of this key.
    90  func (k *ecdsaPublicKey) SKI() (ski []byte) {
    91  	if k.pubKey == nil {
    92  		return nil
    93  	}
    94  
    95  	// Marshall the public key
    96  	raw := elliptic.Marshal(k.pubKey.Curve, k.pubKey.X, k.pubKey.Y)
    97  
    98  	// Hash it
    99  	hash := sha256.New()
   100  	hash.Write(raw)
   101  	return hash.Sum(nil)
   102  }
   103  
   104  // Symmetric returns true if this key is a symmetric key,
   105  // false if this key is asymmetric
   106  func (k *ecdsaPublicKey) Symmetric() bool {
   107  	return false
   108  }
   109  
   110  // Private returns true if this key is a private key,
   111  // false otherwise.
   112  func (k *ecdsaPublicKey) Private() bool {
   113  	return false
   114  }
   115  
   116  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
   117  // This method returns an error in symmetric key schemes.
   118  func (k *ecdsaPublicKey) PublicKey() (bccsp.Key, error) {
   119  	return k, nil
   120  }