forked from cyph/ntru.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntru.c
executable file
·158 lines (134 loc) · 2.97 KB
/
ntru.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
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
#include "randombytes.h"
#include "ntru_crypto.h"
#include "ntru_crypto_ntru_encrypt_param_sets.h"
#include <memory.h>
DRBG_HANDLE drbg;
uint16_t public_key_len;
uint16_t private_key_len;
uint16_t cyphertext_len;
uint16_t plaintext_len;
static uint8_t *user_seed = NULL;
uint32_t dbrg_randombytes (uint8_t *out, uint32_t num_bytes) {
randombytes_buf(out, num_bytes);
DRBG_RET(DRBG_OK);
}
static uint8_t
get_entropy(
ENTROPY_CMD cmd,
uint8_t *out)
{
/* 2k/8 bytes of entropy are needed to instantiate a DRBG with a
* security strength of k bits. Here k = KLENNGTH.
*/
static size_t kMaxLength = KLENNGTH * 2 / 8;
static size_t index;
if (cmd == INIT) {
/* Any initialization for a real entropy source goes here. */
index = 0;
return 1;
}
if (out == NULL)
return 0;
if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
/* Here we return the number of bytes needed from the entropy
* source to obtain 8 bits of entropy. Maximum is 8.
*/
*out = 1; /* this is a perfectly random source */
return 1;
}
if (cmd == GET_BYTE_OF_ENTROPY) {
if (index == kMaxLength)
return 0; /* used up all our entropy */
*out = user_seed[index++]; /* deliver an entropy byte */
return 1;
}
return 0;
}
void dispose() {
user_seed = NULL;
}
void ntrujs_init (uint8_t *seed) {
user_seed = seed;
randombytes_stir();
ntru_crypto_drbg_instantiate(KLENNGTH, NULL,
0, (ENTROPY_FN) &get_entropy, &drbg);
// ntru_crypto_drbg_external_instantiate(
// (RANDOM_BYTES_FN) &dbrg_randombytes,
// &drbg
// );
NTRU_ENCRYPT_PARAM_SET* params_data =
ntru_encrypt_get_params_with_id(PARAMS)
;
ntru_crypto_ntru_encrypt_keygen(
drbg,
PARAMS,
&public_key_len,
NULL,
&private_key_len,
NULL
);
cyphertext_len = public_key_len - 5;
plaintext_len = params_data->m_len_max;
}
long ntrujs_public_key_bytes () {
return public_key_len;
}
long ntrujs_private_key_bytes () {
return private_key_len;
}
long ntrujs_encrypted_bytes () {
return cyphertext_len;
}
long ntrujs_decrypted_bytes () {
return plaintext_len;
}
long ntrujs_keypair (
uint8_t* public_key,
uint8_t* private_key
) {
return ntru_crypto_ntru_encrypt_keygen(
drbg,
PARAMS,
&public_key_len,
public_key,
&private_key_len,
private_key
);
}
long ntrujs_encrypt (
uint8_t* message,
long message_len,
uint8_t* public_key,
uint8_t* cyphertext
) {
return ntru_crypto_ntru_encrypt(
drbg,
public_key_len,
public_key,
message_len,
message,
&cyphertext_len,
cyphertext
);
}
long ntrujs_decrypt (
uint8_t* cyphertext,
uint8_t* private_key,
uint8_t* decrypted
) {
uint16_t decrypted_len = plaintext_len;
long rc = ntru_crypto_ntru_decrypt(
private_key_len,
private_key,
cyphertext_len,
cyphertext,
&decrypted_len,
decrypted
);
if (rc == NTRU_OK) {
return decrypted_len;
}
else {
return -rc;
}
}