-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ed448.rs
351 lines (283 loc) · 7.93 KB
/
Ed448.rs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(non_upper_case_globals)]
#![allow(unused_imports)]
// For example: cargo run --release --features ED448 --bin Ed448
// python curve_rust.py 64 ED448
// This completes edwards.rs for this curve
// EdDSA Implementation for curve ED448
// see https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf
// to include in library..
/*
use crate::edwards;
use crate::edwards::*;
use crate::edwards::ECP;
use crate::hash;
use crate::hash::*;
use crate::hash::SHA3;
*/
mod edwards;
use edwards::*;
use edwards::ECP;
mod hash;
use hash::*;
/*** Insert code automatically generated in group.rs here ***/
/* Note that much of this code is not needed and can be deleted */
/*** End of automatically generated code ***/
// number of limbs and bytes in representation
pub const BYTES:usize = NBYTES;
const LIMBS:usize= NLIMBS;
type GEL = [SPINT; LIMBS];
// reduce 114 byte array h to integer r modulo group order q, in constant time
// Consider h as 2^440.(2^440 + y) + z, where x,y and z < q (z is bottom 55 bytes, y is next 55 bytes, x is top 4 bytes)
// Important that x,y and z < q, 55 bytes = 440 bits, q is 446 bits
fn reduce(h:&[u8],r:&mut [SPINT]) {
let mut buff:[u8;BYTES]=[0;BYTES];
let mut x:GEL=[0;LIMBS];
let mut y:GEL=[0;LIMBS];
let mut z:GEL=[0;LIMBS];
let mut c:GEL=[0;LIMBS];
mod2r(8*(BYTES-1),&mut c);
for i in 0..55 { // bottom 55 bytes
buff[i]=h[i];
}
buff[55]=0;
buff.reverse();
modimp(&buff,&mut z);
for i in 0..55 { // middle 55 bytes
buff[i]=h[i+55];
}
buff[55]=0;
buff.reverse();
modimp(&buff,&mut y);
for i in 0..4 {
buff[i]=h[110+i];
}
for i in 4..56 {
buff[i]=0;
}
buff.reverse();
modimp(&buff,&mut x);
modmul(&c,&mut x);
modadd(&y,&mut x);
modmul(&c,&mut x);
modadd(&z,&mut x);
modcpy(&x,r);
}
// general purpose SHAKE256 hash function
// Input ilen bytes, output olen bytes
fn H(ilen:usize,olen:usize,s:&[u8],digest: &mut [u8])
{
let mut sha3=SHA3::new(SHAKE256);
for i in 0..ilen {
sha3.process(s[i]);
}
sha3.shake(digest,olen);
}
// Input private key - 57 random bytes
// Output public key - 57 bytes
pub fn KEY_PAIR(prv: &[u8],public: &mut [u8]) {
let mut P=ECP::new();
ecngen(&mut P);
let mut s:[u8;BYTES]=[0;BYTES];
H(BYTES+1,BYTES,&prv,&mut s);
// clamp s
s[0]&=0xFC;
s[55]|=0x80;
s.reverse(); // little endian to big endian
ecnmul(&s,&mut P);
let sign=ecnget(&mut P,None,Some(&mut s)); // get y coordinate and sign
s.reverse(); // big endian to little endian
for i in 0..56 {
public[i]=s[i];
}
public[56]=(sign<<7) as u8;
}
const dom4:[u8;10]=[b'S',b'i',b'g',b'E',b'd',b'4',b'4',b'8',0,0];
// input private key, public key, message to be signed. Output signature
pub fn SIGN(prv:&[u8],public: Option<&[u8]>,m:&[u8],sig:&mut [u8]) {
let mut h:[u8;2*BYTES+2]=[0;2*BYTES+2];
let mut ipub:[u8;BYTES+1]=[0;BYTES+1];
let mut sh:[u8;BYTES]=[0;BYTES];
let mut s:GEL=[0;LIMBS];
let mut r:GEL=[0;LIMBS];
let mut d:GEL=[0;LIMBS];
let mut sha3=SHA3::new(SHAKE256);
let mut R=ECP::new();
ecngen(&mut R);
if let Some(pb) = public {
for i in 0..BYTES+1 {
ipub[i]=pb[i];
}
} else {
KEY_PAIR(prv,&mut ipub);
}
H(BYTES+1,2*BYTES+2,&prv,&mut h);
for i in 0..BYTES {
sh[i]=h[i];
}
// derive and clamp s
sh[0]&=0xFC;
sh[55]|=0x80;
sh.reverse();
modimp(&sh,&mut s);
for i in 0..10 {
sha3.process(dom4[i]);
}
for i in BYTES+1..2*BYTES+2 {
sha3.process(h[i]);
}
for i in 0..m.len() {
sha3.process(m[i]);
}
sha3.shake(&mut h,2*BYTES+2);
reduce(&h,&mut r);
modexp(&r,&mut sh); // convert to big endian array
ecnmul(&sh,&mut R);
let sign=ecnget(&mut R,None,Some(&mut sh)); // get y coordinate and sign
sh.reverse(); // big endian to little endian
for i in 0..BYTES {
sig[i]=sh[i];
}
sig[BYTES]=(sign<<7) as u8; // first part of signature
sha3=SHA3::new(SHAKE256);
for i in 0..10 {
sha3.process(dom4[i]);
}
for i in 0..BYTES+1 {
sha3.process(sig[i]); // R
}
for i in 0..BYTES+1 {
sha3.process(ipub[i]); // Q
}
for i in 0..m.len() {
sha3.process(m[i]);
}
sha3.shake(&mut h,2*BYTES+2);
reduce(&h,&mut d);
modmul(&s,&mut d);
modadd(&r,&mut d);
modexp(&d,&mut sh);
sh.reverse();
for i in 0..BYTES {
sig[BYTES+i+1]=sh[i];
}
sig[2*BYTES+1]=0; // second part of signature
}
// input public key, message and signature
// NOTE signatures that are of the wrong length should be rejected prior to calling this function
pub fn VERIFY(public: &[u8],m:&[u8],sig:&[u8]) -> bool {
let mut buff:[u8;BYTES]=[0;BYTES];
let mut sh:[u8;BYTES]=[0;BYTES];
let mut G=ECP::new();
let mut R=ECP::new();
let mut Q=ECP::new();
let mut u:GEL=[0;LIMBS];
let mut h:[u8;2*BYTES+2]=[0;2*BYTES+2];
ecngen(&mut G);
// reconstruct point R
for i in 0..BYTES {
buff[i]=sig[i];
}
buff.reverse();
let mut sign=(sig[BYTES]>>7) as usize;
ecnset(sign,None,Some(&buff),&mut R);
if ecnisinf(&R) {
return false;
}
// reconstruct point Q
for i in 0..BYTES {
buff[i]=public[i];
}
buff.reverse();
sign=((public[BYTES]>>7)&1) as usize;
ecnset(sign,None,Some(&buff),&mut Q);
if ecnisinf(&Q) {
return false;
}
for i in 0..BYTES {
buff[i]=sig[i+BYTES+1];
}
buff.reverse();
let mut sha3=SHA3::new(SHAKE256);
for i in 0..10 {
sha3.process(dom4[i]);
}
for i in 0..BYTES+1 {
sha3.process(sig[i]); // R
}
for i in 0..BYTES+1 {
sha3.process(public[i]); // Q
}
for i in 0..m.len() {
sha3.process(m[i]);
}
sha3.shake(&mut h,2*BYTES+2);
reduce(&h,&mut u); modneg(&mut u); modexp(&u,&mut sh);
if !modimp(&buff,&mut u) {
return false; // out of range
}
ecncof(&mut G); ecncof(&mut R); ecncof(&mut Q);
Q=ecnmul2(&buff,&G,&sh,&Q);
if ecncmp(&R,&Q) {
return true;
}
return false;
}
// Some utility functions for I/O and debugging
fn char2int(inp: u8) -> u8 {
if inp>='0' as u8 && inp <='9' as u8 {
return inp-'0' as u8;
}
if inp>='A' as u8 && inp <='F' as u8 {
return inp-('A' as u8) +10;
}
if inp>='a' as u8 && inp <='f' as u8 {
return inp-('a' as u8) +10;
}
return 0;
}
// string s better have even number of characters!
fn from_hex(ilen:usize,s: &str,x: &mut[u8]) {
let mut pad:[u8;128]=[0;128];
let c=s.as_bytes();
let len=c.len();
let mut lz=2*ilen-len;
if 2*ilen<len {lz=0;}
for i in 0..lz {
pad[i]='0' as u8;
}
for i in lz..2*ilen {
pad[i]=c[i-lz];
}
for i in 0..ilen {
x[i]=char2int(pad[2*i])*16+char2int(pad[2*i+1]);
}
}
fn printhex(len:usize,array: &[u8]) {
for i in 0..len {
print!("{:02X}", array[i])
}
println!("")
}
fn main() {
const SK:&str="c4eab05d357007c632f3dbb48489924d552b08fe0c353a0d4a1f00acda2c463afbea67c5e8d2877c5e3bc397a659949ef8021e954e0a12274e";
let mut prv:[u8;BYTES+1]=[0;BYTES+1];
let mut public:[u8;BYTES+1]=[0;BYTES+1];
let mut sig:[u8;2*BYTES+2]=[0;2*BYTES+2];
let mut m:[u8;2]=[0;2];
println!("Run RFC8032 test vector");
from_hex(BYTES+1,&SK,&mut prv);
print!("private key= "); printhex(BYTES+1,&prv);
KEY_PAIR(&prv,&mut public);
print!("Public key= "); printhex(BYTES+1,&public);
m[0]=0x03; // message to be signed
SIGN(&prv,Some(&public),&m[0..1],&mut sig);
print!("signature= "); printhex(2*BYTES+2,&sig);
let res=VERIFY(&public,&m[0..1],&sig);
if res {
println!("Signature is valid");
} else {
println!("Signature is NOT valid");
}
}