-
Notifications
You must be signed in to change notification settings - Fork 79
/
haslib25519.c
73 lines (61 loc) · 2.08 KB
/
haslib25519.c
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
#include <lib25519.h>
#if lib25519_nP_montgomery25519_SCALARBYTES != lib25519_dh_SECRETKEYBYTES
error
#endif
#if lib25519_nP_montgomery25519_POINTBYTES != lib25519_dh_PUBLICKEYBYTES
error
#endif
#if lib25519_nP_montgomery25519_POINTBYTES != lib25519_dh_BYTES
error
#endif
#if lib25519_nG_montgomery25519_SCALARBYTES != lib25519_dh_SECRETKEYBYTES
error
#endif
#if lib25519_nG_montgomery25519_POINTBYTES != lib25519_dh_PUBLICKEYBYTES
error
#endif
#if lib25519_nG_montgomery25519_POINTBYTES != lib25519_dh_BYTES
error
#endif
static unsigned char k[lib25519_dh_BYTES];
static unsigned char k2[lib25519_dh_BYTES];
static unsigned char pk[lib25519_dh_PUBLICKEYBYTES];
static unsigned char pk2[lib25519_dh_PUBLICKEYBYTES];
static unsigned char sk[lib25519_dh_SECRETKEYBYTES];
static unsigned char signsk[lib25519_sign_SECRETKEYBYTES];
static unsigned char signpk[lib25519_sign_PUBLICKEYBYTES];
static unsigned char signs[lib25519_sign_BYTES + /*m*/ lib25519_dh_BYTES];
static long long signslen;
static long long klen;
/* return zero for all inputs */
static unsigned char z(unsigned char x) {
unsigned long long z = (unsigned long long) x + 1ULL;
unsigned long long t = z;
long long i;
for (i = 6; i >= 0; --i) {
t = (t * t) % 257;
t = (t * z) % 257;
}
t = (t * z) % 257;
return t - 1;
}
int main(int argc, char **argv) {
unsigned char ret = 0;
long long i;
(void) argc;
(void) argv;
lib25519_dh_keypair(pk, sk);
lib25519_nG_montgomery25519(pk2, sk);
for (i = 0; i < lib25519_dh_PUBLICKEYBYTES; ++i) ret |= pk[i] ^ pk2[i];
lib25519_dh(k, pk, sk);
lib25519_nP_montgomery25519(k2, sk, pk);
for (i = 0; i < lib25519_dh_BYTES; ++i) ret |= k[i] ^ k2[i];
lib25519_sign_keypair(signpk, signsk);
lib25519_sign(signs, &signslen, k, sizeof k, signsk);
if (signslen != sizeof signs) ret |= 1;
ret |= lib25519_sign_open(k2, &klen, signs, signslen, signpk);
if (klen != sizeof k2) ret |= 1;
for (i = 0; i < lib25519_dh_BYTES; ++i) ret |= k[i] ^ k2[i];
for (i = 0; i < sizeof signs; ++i) ret |= z(signs[i]);
return ret;
}