github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/cryptkit/signature.go (about) 1 // Copyright 2020 Insolar Network Ltd. 2 // All rights reserved. 3 // This material is licensed under the Insolar License version 1.0, 4 // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md. 5 6 package cryptkit 7 8 import ( 9 "io" 10 "strings" 11 12 "github.com/insolar/vanilla/longbits" 13 "github.com/insolar/vanilla/throw" 14 ) 15 16 type SigningMethod string 17 18 func (s SigningMethod) String() string { 19 return string(s) 20 } 21 22 type SignatureMethod string /* Digest + Signing methods */ 23 24 func (s SignatureMethod) DigestMethod() DigestMethod { 25 parts := strings.Split(string(s), "/") 26 if len(parts) != 2 { 27 return "" 28 } 29 return DigestMethod(parts[0]) 30 } 31 32 func (s SignatureMethod) SigningMethod() SigningMethod { 33 parts := strings.Split(string(s), "/") 34 if len(parts) != 2 { 35 return "" 36 } 37 return SigningMethod(parts[1]) 38 } 39 40 func (s SignatureMethod) String() string { 41 return string(s) 42 } 43 44 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.SignatureHolder -o . -s _mock.go -g 45 46 type SignatureHolder interface { 47 longbits.FoldableReader 48 GetSignatureMethod() SignatureMethod 49 Equals(other SignatureHolder) bool 50 } 51 52 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.SigningKeyHolder -o . -s _mock.go -g 53 54 type SigningKeyHolder interface { 55 longbits.FoldableReader 56 GetSigningMethod() SigningMethod 57 GetSigningKeyType() SigningKeyType 58 Equals(other SigningKeyHolder) bool 59 } 60 61 type SignedDigestHolder interface { 62 Equals(o SignedDigestHolder) bool 63 GetDigestHolder() DigestHolder 64 GetSignatureHolder() SignatureHolder 65 GetSignatureMethod() SignatureMethod 66 IsVerifiableBy(v SignatureVerifier) bool 67 VerifyWith(v SignatureVerifier) bool 68 } 69 70 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.DigestSigner -o . -s _mock.go -g 71 72 type DigestSigner interface { 73 SignDigest(digest Digest) Signature 74 GetSigningMethod() SigningMethod 75 } 76 77 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.DataSigner -o . -s _mock.go -g 78 79 type DataSigner interface { 80 DigestSigner 81 DataDigester 82 GetSignatureMethod() SignatureMethod 83 } 84 85 type SequenceSigner interface { 86 DigestSigner 87 NewSequenceDigester() SequenceDigester 88 // GetSignatureMethod() SignatureMethod 89 } 90 91 type SignedEvidenceHolder interface { 92 GetEvidence() SignedData 93 } 94 95 type SigningKeyType uint8 96 97 const ( 98 SymmetricKey SigningKeyType = iota 99 SecretAsymmetricKey 100 PublicAsymmetricKey 101 ) 102 103 func (v SigningKeyType) IsSymmetric() bool { 104 return v == SymmetricKey 105 } 106 107 func (v SigningKeyType) IsSecret() bool { 108 return v != PublicAsymmetricKey 109 } 110 111 type DataSignatureVerifier interface { 112 DataDigester 113 GetDefaultSignatureMethod() SignatureMethod 114 SignatureVerifier 115 } 116 117 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.SignatureVerifier -o . -s _mock.go -g 118 119 type SignatureVerifier interface { 120 GetDefaultSigningMethod() SigningMethod 121 122 IsDigestMethodSupported(m DigestMethod) bool 123 IsSigningMethodSupported(m SigningMethod) bool 124 125 IsValidDigestSignature(digest DigestHolder, signature SignatureHolder) bool 126 IsValidDataSignature(data io.Reader, signature SignatureHolder) bool 127 } 128 129 //go:generate minimock -i github.com/insolar/vanilla/cryptkit.SignatureVerifierFactory -o . -s _mock.go -g 130 131 type SignatureVerifierFactory interface { 132 CreateSignatureVerifierWithPKS(PublicKeyStore) SignatureVerifier 133 // CreateSignatureVerifierWithKey(SigningKeyHolder) SignatureVerifier 134 // TODO Add CreateDataSignatureVerifier(k SigningKey, m SignatureMethod) DataSignatureVerifier 135 } 136 137 type DataSignatureVerifierFactory interface { 138 IsSignatureKeySupported(SigningKey) bool 139 CreateDataSignatureVerifier(SigningKey) DataSignatureVerifier 140 } 141 142 type DataSignerFactory interface { 143 IsSignatureKeySupported(SigningKey) bool 144 CreateDataSigner(SigningKey) DataSigner 145 } 146 147 148 149 type dataSigner struct { 150 DigestSigner 151 DataDigester 152 } 153 154 func (v dataSigner) GetSignatureMethod() SignatureMethod { 155 return v.DataDigester.GetDigestMethod().SignedBy(v.DigestSigner.GetSigningMethod()) 156 } 157 158 func AsDataSigner(dd DataDigester, ds DigestSigner) DataSigner { 159 switch { 160 case ds == nil: 161 panic(throw.IllegalValue()) 162 case dd == nil: 163 panic(throw.IllegalValue()) 164 } 165 166 return dataSigner{ds, dd} 167 } 168 169 func AsDataSignatureVerifier(dd DataDigester, sv SignatureVerifier, defSigning SigningMethod) DataSignatureVerifier { 170 switch { 171 case sv == nil: 172 panic(throw.IllegalValue()) 173 case dd == nil: 174 panic(throw.IllegalValue()) 175 } 176 177 return dataSignatureVerifier{dd, sv, dd.GetDigestMethod().SignedBy(defSigning) } 178 } 179 180 type dataSignatureVerifier struct { 181 DataDigester 182 SignatureVerifier 183 184 defSignature SignatureMethod 185 } 186 187 func (v dataSignatureVerifier) GetDefaultSignatureMethod() SignatureMethod { 188 return v.defSignature 189 } 190