/
crystal-ecdsa.cr
288 lines (237 loc) · 11.1 KB
/
crystal-ecdsa.cr
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
require "openssl"
require "./lib_eccrypto"
module ECCrypto
# Creates a Key Pair
def self.create_key_pair
# Create a EC key structure, setting the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
myecc = LibECCrypto.EC_KEY_new_by_curve_name(eccgrp_id)
raise "Error could not create curve" if myecc.null?
# Create the public/private EC key pair
gen_res = LibECCrypto.EC_KEY_generate_key(myecc)
raise "Error could not generate an EC public/private key pair" unless gen_res == 1
# Get the private key
bn = LibECCrypto.EC_KEY_get0_private_key(myecc)
raise "Error could not get the private key" if bn.null?
private_key_pointer = LibECCrypto.BN_bn2hex(bn)
raise "Error could not get the private key pointer" if private_key_pointer.null?
private_key = String.new(private_key_pointer).downcase
# Get the public key
ec_point = LibECCrypto.EC_KEY_get0_public_key(myecc)
raise "Error could not get the public key" if ec_point.null?
form = LibECCrypto::PointConversionFormT::PointConversionUncompressed
eccgrp = LibECCrypto.EC_GROUP_new_by_curve_name(eccgrp_id)
raise "Error could not get the group curve" if eccgrp.null?
public_key_pointer = LibECCrypto.EC_POINT_point2hex(eccgrp, ec_point, form, nil)
raise "Error could not get the public key pointer" if public_key_pointer.null?
public_key = String.new(public_key_pointer).downcase
# Free up mem
LibECCrypto.EC_KEY_free(myecc)
LibECCrypto.EC_GROUP_free(eccgrp)
return create_key_pair if private_key.hexbytes? == nil || private_key.size != 64
return create_key_pair if public_key.hexbytes? == nil || public_key.size != 130
{
hex_private_key: private_key,
hex_public_key: public_key,
}
end
def self.sha256(base : Bytes | String) : String
hash = OpenSSL::Digest.new("SHA256")
hash.update(base)
hash.hexdigest
end
# Signs a message with a private key
def self.sign(hex_private_key : String, message : String)
# Create a EC key structure, setting the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
myecc = LibECCrypto.EC_KEY_new_by_curve_name(eccgrp_id)
raise "Error could not create curve" if myecc.null?
# set signing algo
LibECCrypto.EC_KEY_set_asn1_flag(myecc, 1)
# convert hex private key to binary
bn = LibECCrypto.BN_new
raise "Error could not create a new bn" if bn.null?
binary_private_key = LibECCrypto.BN_hex2bn(pointerof(bn), hex_private_key)
raise "Error private key binary is wrong size" if binary_private_key != 64
# add binary private key to EC structure
set_key = LibECCrypto.EC_KEY_set_private_key(myecc, bn)
raise "Error could not set private key to EC" unless set_key == 1
# # ------------
# # convert binary public key to point
# eccgrp = LibECCrypto.EC_GROUP_new_by_curve_name(eccgrp_id)
# raise "Error could not get the group curve" if eccgrp.null?
# ec_point = LibECCrypto.EC_POINT_new(eccgrp)
# raise "Error could not create point from group" if ec_point.null?
#
# point_res = LibECCrypto.EC_POINT_hex2point(eccgrp, hex_public_key.to_unsafe, ec_point, nil)
# raise "Error could not get point from public key" if point_res.null?
#
# # set the public key on the EC structure
# set_key = LibECCrypto.EC_KEY_set_public_key(myecc, ec_point)
# raise "Error could not set public key to EC" unless set_key == 1
#
# # ------------
# sign
sign_pointer = LibECCrypto.ECDSA_do_sign(message, message.bytesize, myecc)
raise "Error could not sign message with key" if sign_pointer.null?
# get the r,s from the signing
sign_value = sign_pointer.value
r_hex = LibECCrypto.BN_bn2hex(sign_value.r)
s_hex = LibECCrypto.BN_bn2hex(sign_value.s)
r = String.new(r_hex).downcase
s = String.new(s_hex).downcase
# Free up mem
LibECCrypto.EC_KEY_free(myecc)
# LibECCrypto.BN_clear_free(bn)
LibECCrypto.ECDSA_SIG_free(sign_pointer)
{
r: r,
s: s,
}
end
# Verifies a signed message with a public key and the signature
def self.verify(hex_public_key : String, message : String, r : String, s : String)
# Create a EC key structure, setting the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
myecc = LibECCrypto.EC_KEY_new_by_curve_name(eccgrp_id)
raise "Error could not create curve" if myecc.null?
# convert binary public key to point
eccgrp = LibECCrypto.EC_GROUP_new_by_curve_name(eccgrp_id)
raise "Error could not get the group curve" if eccgrp.null?
ec_point = LibECCrypto.EC_POINT_new(eccgrp)
raise "Error could not create point from group" if ec_point.null?
point_res = LibECCrypto.EC_POINT_hex2point(eccgrp, hex_public_key.to_unsafe, ec_point, nil)
raise "Error could not get point from public key" if point_res.null?
# set the public key on the EC structure
set_key = LibECCrypto.EC_KEY_set_public_key(myecc, ec_point)
raise "Error could not set public key to EC" unless set_key == 1
# convert r,s hex to bn
r_bn = LibECCrypto.BN_new
raise "Error could not create a new bn for r" if r_bn.null?
LibECCrypto.BN_hex2bn(pointerof(r_bn), r)
s_bn = LibECCrypto.BN_new
raise "Error could not create a new bn for s" if s_bn.null?
LibECCrypto.BN_hex2bn(pointerof(s_bn), s)
# set r,s onto signature
sig = LibECCrypto::ECDSA_SIG_Struct_setter.new
sig.r = r_bn
sig.s = s_bn
# verify
result = LibECCrypto.ECDSA_do_verify(message, message.bytesize, pointerof(sig), myecc)
# Free up mem
LibECCrypto.EC_KEY_free(myecc)
LibECCrypto.EC_POINT_free(ec_point)
LibECCrypto.BN_clear_free(r_bn)
LibECCrypto.BN_clear_free(s_bn)
result == 1
end
def self.get_public_key_from_private(hex_private_key : String)
# Create a EC key structure, setting the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
myecc = LibECCrypto.EC_KEY_new_by_curve_name(eccgrp_id)
raise "Error could not create curve" if myecc.null?
# Create a group
eccgrp = LibECCrypto.EC_GROUP_new_by_curve_name(eccgrp_id)
raise "Error could not get the group curve" if eccgrp.null?
# Create an EC_POINT to hold the public_key
ec_point = LibECCrypto.EC_POINT_new(eccgrp)
raise "Error could not create point from group" if ec_point.null?
# Convert hex private key to binary
bn = LibECCrypto.BN_new
raise "Error could not create a new bn" if bn.null?
binary_private_key = LibECCrypto.BN_hex2bn(pointerof(bn), hex_private_key)
raise "Error private key binary is wrong size" if binary_private_key != 64
# Do the mul that sets the public key onto ec_point
mul = LibECCrypto.EC_POINT_mul(eccgrp, ec_point, bn, nil, nil, nil)
raise "Error could not find public key" unless mul == 1
# Get the public key in hex
form = LibECCrypto::PointConversionFormT::PointConversionUncompressed
public_key_pointer = LibECCrypto.EC_POINT_point2hex(eccgrp, ec_point, form, nil)
raise "Error could not get the public key pointer" if public_key_pointer.null?
# Free up mem
LibECCrypto.EC_KEY_free(myecc)
LibECCrypto.EC_GROUP_free(eccgrp)
LibECCrypto.EC_POINT_free(ec_point)
LibECCrypto.BN_clear_free(bn)
String.new(public_key_pointer).downcase
end
def self.encrypt(hex_receiver_public_key : String, message : String) : String
dummy : UInt8 = 0
epubk : UInt8* = pointerof(dummy)
epubk_len : LibC::SizeT = 0
iv : UInt8* = pointerof(dummy)
iv_len : LibC::Int = 0
tag : UInt8* = pointerof(dummy)
tag_len : LibC::Int = 0
ciphertext : UInt8* = pointerof(dummy)
ciphertext_len : LibC::SizeT = 0
# set the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
raise "Message must not be empty" unless message.size > 0
# use the crypto library to encrypt the message using the receiver public key and get the ephemeral public key, the init vector and the tag
# (we add one to message size to encrypt the null at the end of the string)
status = LibECCrypto.encrypt_message(message.bytes, message.size + 1,
eccgrp_id, hex_receiver_public_key,
pointerof(epubk), pointerof(epubk_len),
pointerof(iv), pointerof(iv_len),
pointerof(tag), pointerof(tag_len),
pointerof(ciphertext), pointerof(ciphertext_len))
raise String.new(ciphertext) unless status == 0
# put the encrypted elements all together into one long hex string that can be transmitted (pieces are separated by 'fx')
msg = bytes2hex(ciphertext, ciphertext_len) + "fx" + bytes2hex(tag, tag_len) + "fx" + bytes2hex(iv, iv_len) + "fx" + bytes2hex(epubk, epubk_len)
return msg
end
def self.decrypt(hex_receiver_private_key : String, encrypted_message : String) : String
dummy : UInt8 = 0
decrypted_message : UInt8* = pointerof(dummy)
raise "Encrypted message must not be empty" unless encrypted_message.size > 0
# pull the componente of the encrypted message apart
chunks = encrypted_message.split("fx")
raise "Message not encrypted by ECCrypto.encrypt" if chunks.size != 4
ciphertext_len : LibC::SizeT = (chunks[0].size / 2).to_u64
ciphertext = hex2bytes(chunks[0])
tag_len : LibC::Int = (chunks[1].size / 2).to_i
tag = hex2bytes(chunks[1])
iv_len : LibC::Int = (chunks[2].size / 2).to_i
iv = hex2bytes(chunks[2])
epubk_len : LibC::SizeT = (chunks[3].size / 2).to_u64
epubk = hex2bytes(chunks[3])
# set the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
# use the crypto library decrypt the message using the private key, ephemeral public key, init vector and tag
status = LibECCrypto.decrypt_message(eccgrp_id, hex_receiver_private_key,
epubk, epubk_len, iv, iv_len, tag, tag_len, ciphertext, ciphertext_len,
pointerof(decrypted_message))
raise String.new(decrypted_message) unless status == 0
return String.new(decrypted_message)
end
def self.bytes2hex(the_bytes : UInt8*, the_length : Int) : String
str = ""
i = 0
while i < the_length
the_hex = the_bytes[i].to_s(16)
the_hex = "0" + the_hex if the_hex.size == 1
str += the_hex
i += 1
end
return str
end
def self.hex2bytes(hex_string : String) : UInt8*
str = hex_string
byte_ptr = Pointer(UInt8).malloc((hex_string.size / 2).to_i)
i = 0
while str.size > 0
digits = str[0..1]
str = str[2..-1]
byte_ptr[i] = digits.to_u8(16)
i += 1
end
return byte_ptr
end
end