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