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