Debian Codegraph @

src/modexp.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <stddef.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include "common.h"
38 #include "endianess.h"
39
40 FAKE_INIT(modexp)
41
42 #include "mont.h"
43 #include "modexp_utils.h"
44
45 /** Multiplication will be replaced by a look-up **/
46 /** Do not change this value! **/
47 #define WINDOW_SIZE 4
48
49 /*
50 * Modular exponentiation. All numbers are
51 * encoded in big endian form, possibly with
52 * zero padding on the left.
53 *
54 * @param out The memory area where to store the result
55 * @param base Base number, strictly smaller than the modulus
56 * @param exp Exponent
57 * @param modulus Modulus, it must be odd
58 * @param len Size in bytes of out, base, exp, and modulus
59 * @param seed A random seed, used for avoiding side-channel
60 * attacks
61 * @return 0 in case of success, the appropriate error code otherwise
62 */
63 EXPORT_SYM int monty_pow(
64 uint8_t *out,
65 const uint8_t *base,
66 const uint8_t *exp,
67 const uint8_t *modulus,
68 size_t len,
69 uint64_t seed)
70 {
71 unsigned i, j;
72 size_t exp_len;
73 int res;
74
75 MontContext *ctx = NULL;
76 uint64_t *powers[1 << WINDOW_SIZE] = { NULL };
77 uint64_t *power_idx = NULL;
78 ProtMemory *prot = NULL;
79 uint64_t *mont_base = NULL;
80 uint64_t *x = NULL;
81 uint64_t *scratchpad = NULL;
82 uint8_t *buf_out = NULL;
83
84 struct BitWindow_LR bit_window;
85
86 if (!base || !exp || !modulus || !out)
87 return ERR_NULL;
88
89 if (len == 0)
90 return ERR_NOT_ENOUGH_DATA;
91
92 /* Allocations **/
93 res = mont_context_init(&ctx, modulus, len);
94 if (res)
95 return res;
96
97 for (i=0; i<(1 << WINDOW_SIZE); i++) {
98 res = mont_number(powers+i, 1, ctx);
99 if (res) goto cleanup;
100 }
101
102 res = mont_number(&power_idx, 1, ctx);
103 if (res) goto cleanup;
104
105 res = mont_from_bytes(&mont_base, base, len, ctx);
106 if (res) goto cleanup;
107
108 res = mont_number(&x, 1, ctx);
109 if (res) goto cleanup;
110
111 res = mont_number(&scratchpad, SCRATCHPAD_NR, ctx);
112 if (res) goto cleanup;
113
114 buf_out = (uint8_t*)calloc(1, mont_bytes(ctx));
115 if (NULL == buf_out) {
116 res = ERR_MEMORY;
117 goto cleanup;
118 }
119
120 /** Result is initially 1 in Montgomery form **/
121 mont_set(x, 1, ctx);
122
123 /** Pre-compute powers a^0 mod n, a^1 mod n, a^2 mod n, ... a^(2^WINDOW_SIZE-1) mod n **/
124 mont_copy(powers[0], x, ctx);
125 mont_copy(powers[1], mont_base, ctx);
126 for (i=1; i<(1 << (WINDOW_SIZE-1)); i++) {
127 mont_mult(powers[i*2], powers[i], powers[i], scratchpad, ctx);
128 mont_mult(powers[i*2+1], powers[i*2], mont_base, scratchpad, ctx);
129 }
130
131 res = scatter(&prot, (const void**)powers, 1<<WINDOW_SIZE, mont_bytes(ctx), seed);
132 if (res) goto cleanup;
133
134 /** Ignore leading zero bytes in the exponent **/
135 exp_len = len;
136 for (i=0; i<len && *exp==0; i++) {
137 exp_len--;
138 exp++;
139 }
140
141 /* If exponent is 0, the result is always 1 */
142 if (exp_len == 0) {
143 memset(out, 0, len);
144 out[len-1] = 1;
145 res = 0;
146 goto cleanup;
147 }
148
149 bit_window = init_bit_window_lr(WINDOW_SIZE, exp, exp_len);
150
151 /** Left-to-right exponentiation with fixed window **/
152 for (i=0; i < bit_window.nr_windows; i++) {
153 unsigned index;
154
155 for (j=0; j<WINDOW_SIZE; j++) {
156 mont_mult(x, x, x, scratchpad, ctx);
157 }
158
159 index = get_next_digit_lr(&bit_window);
160 gather(power_idx, prot, index);
161 mont_mult(x, x, power_idx, scratchpad, ctx);
162 }
163
164 /** Transform result back into big-endian, byte form **/
165 res = mont_to_bytes(out, len, x, ctx);
166
167 cleanup:
168 mont_context_free(ctx);
169 for (i=0; i<(1 << WINDOW_SIZE); i++) {
170 free(powers[i]);
171 }
172 free(power_idx);
173 free_scattered(prot);
174 free(mont_base);
175 free(x);
176 free(scratchpad);
177 free(buf_out);
178
179 return res;
180 }
181
182 /*
183 * Modular multiplication. All numbers are
184 * encoded in big endian form, possibly with
185 * zero padding on the left.
186 *
187 * @param out The memory area where to store the result
188 * @param term1 First term of the multiplication, strictly smaller than the modulus
189 * @param term2 Second term of the multiplication, strictly smaller than the modulus
190 * @param modulus Modulus, it must be odd
191 * @param len Size in bytes of out, term1, term2, and modulus
192 * @return 0 in case of success, the appropriate error code otherwise
193 */
194 EXPORT_SYM int monty_multiply(
195 uint8_t *out,
196 const uint8_t *term1,
197 const uint8_t *term2,
198 const uint8_t *modulus,
199 size_t len)
200 {
201 MontContext *ctx = NULL;
202 uint64_t *mont_term1 = NULL;
203 uint64_t *mont_term2 = NULL;
204 uint64_t *mont_output = NULL;
205 uint64_t *scratchpad = NULL;
206 int res;
207
208 if (!term1 || !term2 || !modulus || !out)
209 return ERR_NULL;
210
211 if (len == 0)
212 return ERR_NOT_ENOUGH_DATA;
213
214 /* Allocations **/
215 res = mont_context_init(&ctx, modulus, len);
216 if (res)
217 return res;
218
219 res = mont_from_bytes(&mont_term1, term1, len, ctx);
220 if (res) goto cleanup;
221
222 res = mont_from_bytes(&mont_term2, term2, len, ctx);
223 if (res) goto cleanup;
224
225 res = mont_number(&mont_output, 1, ctx);
226 if (res) goto cleanup;
227
228 res = mont_number(&scratchpad, SCRATCHPAD_NR, ctx);
229 if (res) goto cleanup;
230
231 /* Multiply, then transform result back into big-endian, byte form **/
232 res = mont_mult(mont_output, mont_term1, mont_term2, scratchpad, ctx);
233 if (res) goto cleanup;
234
235 res = mont_to_bytes(out, len, mont_output, ctx);
236
237 cleanup:
238 mont_context_free(ctx);
239 free(mont_term1);
240 free(mont_term2);
241 free(mont_output);
242 free(scratchpad);
243
244 return res;
245 }
246
247 #ifdef MAIN
248 int main(void)
249 {
250 uint16_t length;
251 uint8_t *base, *modulus, *exponent, *out;
252 int result;
253 size_t res;
254
255 res = fread(&length, 2, 1, stdin);
256 assert(res == 2);
257
258 base = malloc(length);
259 modulus = malloc(length);
260 exponent = malloc(length);
261 out = malloc(length);
262
263 res = fread(base, 1, length, stdin);
264 assert(res == length);
265 res = fread(modulus, 1, length, stdin);
266 assert(res == length);
267 res = fread(exponent, 1, length, stdin);
268 assert(res == length);
269 assert(res == length);
270 res = fread(out, 1, length, stdin);
271
272 result = monty_pow(out, base, exponent, modulus, length, 12);
273
274 free(base);
275 free(modulus);
276 free(exponent);
277 free(out);
278
279 return result;
280 }
281
282 #endif
283
284 #ifdef PROFILE
285 int main(void)
286 {
287 uint8_t base[256], exponent[256], modulus[256], out[256];
288 unsigned length = 256, i, j;
289
290 for (i=0; i<256; i++) {
291 base[i] = (uint8_t)i | 0x80 | 1;
292 exponent[i] = base[i] = modulus[i] = base[i];
293 }
294 base[0] = 0x7F;
295
296 for (j=0; j<50; j++) {
297 monty_pow(out, base, exponent, modulus, length, 12);
298 }
299
300 monty_multiply(out, base, out, modulus, length);
301 }
302 #endif