-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
193 lines (158 loc) · 7.61 KB
/
crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# pip install ecdsa hashlib logging subprocess
from ecdsa import SigningKey, VerifyingKey, SECP256k1, BadSignatureError
from ecdsa.util import sigencode_string, sigdecode_string
from Crypto.PublicKey import RSA, ECC
from Crypto.Signature import pkcs1_15, DSS
from Crypto.Hash import SHA256
import hashlib
import logging
import subprocess
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
## BbsBlsSignature[Proof]2020 Status: "unstable" (deprecated)!
## https://github.com/mattrglobal/jsonld-signatures-bbs/issues/93
## https://identity.foundation/bbs-signature/draft-blind-bbs-signatures.html
## https://github.com/mattrglobal/jsonld-signatures-bbs/blob/master/sample/ts-node/src/data/bbs.json#L7C15-L7C60
class BoundBBSSignature:
def sign(self, message, key):
# Implement BoundBBS signing
# This is a placeholder implementation
return "BoundBBS_" + message.encode('utf-8').hex()
def verify(self, message, signature, public_key):
# Implement BoundBBS verification
# This is a placeholder implementation
return signature.startswith(
"BoundBBS_") and signature[9:] == message.encode('utf-8').hex()
## https://github.com/hyperledger-archives/indy-anoncreds/blob/master/anoncreds/test/test_anoncred_usage.py
## https://github.com/hyperledger-archives/indy-anoncreds/blob/master/anoncreds/protocol/wallet/wallet.py
class CLSignature:
def sign(self, message, key):
hash = SHA256.new(message.encode('utf-8'))
signature = pkcs1_15.new(key).sign(hash)
return signature.hex()
def verify(self, message, signature, public_key):
hash = SHA256.new(message.encode('utf-8'))
try:
pkcs1_15.new(RSA.import_key(public_key)).verify(
hash, bytes.fromhex(signature))
return True
except (ValueError, TypeError):
return False
class SchnorrSignature:
def sign(self, message, private_key_hex):
message_hash = hashlib.sha256(message.encode('utf-8')).digest()
# Extract the 32-byte private key from the DER format
private_key_bytes = bytes.fromhex(private_key_hex)
if len(private_key_bytes) != 32:
der = ECC.import_key(private_key_bytes)
private_key_bytes = der.d.to_bytes(32, 'big')
sk = SigningKey.from_string(private_key_bytes, curve=SECP256k1)
signature = sk.sign(message_hash)
logger.debug("Message to sign: %s", message)
logger.debug("Generated hash: %s", message_hash.hex())
logger.debug("Private key length: %d", len(private_key_bytes))
logger.debug("Schnorr Signature generated for message: %s", message)
logger.debug("Signature: %s", signature.hex())
return signature.hex()
def verify(self, message, signature_hex, public_key_hex):
message_hash = hashlib.sha256(message.encode('utf-8')).digest()
vk = VerifyingKey.from_string(bytes.fromhex(public_key_hex),
curve=SECP256k1)
logger.debug("Message to verify: %s", message)
logger.debug("Generated hash: %s", message_hash.hex())
logger.debug("Public key used for verification: %s", public_key_hex)
logger.debug("Signature to verify: %s", signature_hex)
try:
vk.verify(bytes.fromhex(signature_hex), message_hash)
logger.debug(
"Schnorr Signature verified successfully for message: %s",
message)
return True
except BadSignatureError as e:
logger.error(
"Schnorr Signature verification failed for message: %s",
message)
logger.error("Error: %s", str(e))
return False
# ECDSA (curve=SECP256k1) # !(FIPS-186-3)
class ECDSASignature:
# def sign(self, message, private_key_hex):
def sign(self, message, private_key):
# Create a hash of the message
message_hash = hashlib.sha256(message.encode('utf-8')).digest()
# Generate a signing key from the private key
# sk = SigningKey.from_string(bytes.fromhex(private_key_hex), curve=SECP256k1)
# Convert EccKey to ecdsa SigningKey
sk = SigningKey.from_string(private_key.d.to_bytes(32, 'big'),
curve=SECP256k1)
# Sign the hash
# signature = sk.sign(message_hash)
signature = sk.sign(message_hash, sigencode=sigencode_string)
logger.debug("Message to sign: %s", message)
logger.debug("Generated hash: %s", message_hash.hex())
logger.debug("ECDSA Signature generated for message: %s", message)
logger.debug("Signature: %s", signature.hex())
return signature.hex()
# def verify(self, message, signature_hex, public_key_hex):
def verify(self, message, signature_hex, public_key):
# Create a hash of the message
message_hash = hashlib.sha256(message.encode('utf-8')).digest()
# Generate a verifying key from the public key
# vk = VerifyingKey.from_string(bytes.fromhex(public_key_hex), curve=SECP256k1)
# Convert EccKey to ecdsa VerifyingKey
vk = VerifyingKey.from_string(
public_key.point.x().to_bytes(32, 'big') +
public_key.point.y().to_bytes(32, 'big'),
curve=SECP256k1)
logger.debug("Message to verify: %s", message)
logger.debug("Generated hash: %s", message_hash.hex())
# logger.debug("Public key used for verification: %s", public_key_hex)
logger.debug("Signature to verify: %s", signature_hex)
try:
# Verify the signature
# vk.verify(bytes.fromhex(signature_hex), message_hash)
vk.verify(bytes.fromhex(signature_hex),
message_hash,
sigdecode=sigdecode_string)
logger.debug(
"ECDSA Signature verified successfully for message: %s",
message)
return True
except BadSignatureError as e:
logger.error("ECDSA Signature verification failed for message: %s",
message)
logger.error("Error: %s", str(e))
return False
# Example usage:
# ecdsa_sig = ECDSASignature()
# private_key = ECC.generate(curve='P-256')
# public_key = private_key.public_key()
# signed_message = ecdsa_sig.sign('Hello World', private_key)
# is_verified = ecdsa_sig.verify('Hello World', signed_message, public_key)
# # ECDSA (FIPS-186-3) # !(curve=SECP256k1)
# class ECDSASignature:
# def sign(self, message, key):
# hash = SHA256.new(message.encode('utf-8'))
# signer = DSS.new(key, 'fips-186-3')
# signature = signer.sign(hash)
# logger.debug("Message to sign: %s", message)
# logger.debug("Generated hash: %s", hash.hexdigest())
# logger.debug("Public key used for signing: %s", key.public_key().export_key(format='PEM'))
# logger.debug("ECDSA Signature generated for message: %s", message)
# logger.debug("Signature: %s", signature.hex())
# return signature.hex()
# def verify(self, message, signature, public_key):
# hash = SHA256.new(message.encode('utf-8'))
# verifier = DSS.new(ECC.import_key(public_key), 'fips-186-3')
# logger.debug("Message to verify: %s", message)
# logger.debug("Generated hash: %s", hash.hexdigest())
# logger.debug("Public key used for verification: %s", public_key)
# logger.debug("Signature to verify: %s", signature)
# try:
# verifier.verify(hash, bytes.fromhex(signature))
# logger.debug("ECDSA Signature verified successfully for message: %s", message)
# return True
# except ValueError as e:
# logger.error("ECDSA Signature verification failed for message: %s", message)
# logger.error("Error: %s", str(e))
# return False