/src/mbedtls/library/bignum.c
Line | Count | Source |
1 | | /* |
2 | | * Multi-precision integer library |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
6 | | */ |
7 | | |
8 | | /* |
9 | | * The following sources were referenced in the design of this Multi-precision |
10 | | * Integer library: |
11 | | * |
12 | | * [1] Handbook of Applied Cryptography - 1997 |
13 | | * Menezes, van Oorschot and Vanstone |
14 | | * |
15 | | * [2] Multi-Precision Math |
16 | | * Tom St Denis |
17 | | * https://github.com/libtom/libtommath/blob/develop/tommath.pdf |
18 | | * |
19 | | * [3] GNU Multi-Precision Arithmetic Library |
20 | | * https://gmplib.org/manual/index.html |
21 | | * |
22 | | */ |
23 | | |
24 | | #include "common.h" |
25 | | |
26 | | #if defined(MBEDTLS_BIGNUM_C) |
27 | | |
28 | | #include "mbedtls/bignum.h" |
29 | | #include "bignum_core.h" |
30 | | #include "bignum_internal.h" |
31 | | #include "bn_mul.h" |
32 | | #include "mbedtls/platform_util.h" |
33 | | #include "mbedtls/error.h" |
34 | | #include "constant_time_internal.h" |
35 | | |
36 | | #include <limits.h> |
37 | | #include <string.h> |
38 | | |
39 | | #include "mbedtls/platform.h" |
40 | | |
41 | | |
42 | | |
43 | | /* |
44 | | * Conditionally select an MPI sign in constant time. |
45 | | * (MPI sign is the field s in mbedtls_mpi. It is unsigned short and only 1 and -1 are valid |
46 | | * values.) |
47 | | */ |
48 | | static inline signed short mbedtls_ct_mpi_sign_if(mbedtls_ct_condition_t cond, |
49 | | signed short sign1, signed short sign2) |
50 | 3.84M | { |
51 | 3.84M | return (signed short) mbedtls_ct_uint_if(cond, sign1 + 1, sign2 + 1) - 1; |
52 | 3.84M | } |
53 | | |
54 | | /* |
55 | | * Compare signed values in constant time |
56 | | */ |
57 | | int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, |
58 | | const mbedtls_mpi *Y, |
59 | | unsigned *ret) |
60 | 0 | { |
61 | 0 | mbedtls_ct_condition_t different_sign, X_is_negative, Y_is_negative, result; |
62 | |
|
63 | 0 | if (X->n != Y->n) { |
64 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
65 | 0 | } |
66 | | |
67 | | /* |
68 | | * Set N_is_negative to MBEDTLS_CT_FALSE if N >= 0, MBEDTLS_CT_TRUE if N < 0. |
69 | | * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0. |
70 | | */ |
71 | 0 | X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1); |
72 | 0 | Y_is_negative = mbedtls_ct_bool((Y->s & 2) >> 1); |
73 | | |
74 | | /* |
75 | | * If the signs are different, then the positive operand is the bigger. |
76 | | * That is if X is negative (X_is_negative == 1), then X < Y is true and it |
77 | | * is false if X is positive (X_is_negative == 0). |
78 | | */ |
79 | 0 | different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign |
80 | 0 | result = mbedtls_ct_bool_and(different_sign, X_is_negative); |
81 | | |
82 | | /* |
83 | | * Assuming signs are the same, compare X and Y. We switch the comparison |
84 | | * order if they are negative so that we get the right result, regardles of |
85 | | * sign. |
86 | | */ |
87 | | |
88 | | /* This array is used to conditionally swap the pointers in const time */ |
89 | 0 | void * const p[2] = { X->p, Y->p }; |
90 | 0 | size_t i = mbedtls_ct_size_if_else_0(X_is_negative, 1); |
91 | 0 | mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(p[i], p[i ^ 1], X->n); |
92 | | |
93 | | /* |
94 | | * Store in result iff the signs are the same (i.e., iff different_sign == false). If |
95 | | * the signs differ, result has already been set, so we don't change it. |
96 | | */ |
97 | 0 | result = mbedtls_ct_bool_or(result, |
98 | 0 | mbedtls_ct_bool_and(mbedtls_ct_bool_not(different_sign), lt)); |
99 | |
|
100 | 0 | *ret = mbedtls_ct_uint_if_else_0(result, 1); |
101 | |
|
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | | /* |
106 | | * Conditionally assign X = Y, without leaking information |
107 | | * about whether the assignment was made or not. |
108 | | * (Leaking information about the respective sizes of X and Y is ok however.) |
109 | | */ |
110 | | #if defined(_MSC_VER) && defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64) && \ |
111 | | (_MSC_FULL_VER < 193131103) |
112 | | /* |
113 | | * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See: |
114 | | * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989 |
115 | | */ |
116 | | __declspec(noinline) |
117 | | #endif |
118 | | int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, |
119 | | const mbedtls_mpi *Y, |
120 | | unsigned char assign) |
121 | 3.75M | { |
122 | 3.75M | int ret = 0; |
123 | | |
124 | 3.75M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n)); |
125 | | |
126 | 3.75M | { |
127 | 3.75M | mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign); |
128 | | |
129 | 3.75M | X->s = mbedtls_ct_mpi_sign_if(do_assign, Y->s, X->s); |
130 | | |
131 | 3.75M | mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign); |
132 | | |
133 | 3.75M | mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign); |
134 | 3.76M | for (size_t i = Y->n; i < X->n; i++) { |
135 | 8.28k | X->p[i] = mbedtls_ct_mpi_uint_if_else_0(do_not_assign, X->p[i]); |
136 | 8.28k | } |
137 | 3.75M | } |
138 | | |
139 | 3.75M | cleanup: |
140 | 3.75M | return ret; |
141 | 3.75M | } |
142 | | |
143 | | /* |
144 | | * Conditionally swap X and Y, without leaking information |
145 | | * about whether the swap was made or not. |
146 | | * Here it is not ok to simply swap the pointers, which would lead to |
147 | | * different memory access patterns when X and Y are used afterwards. |
148 | | */ |
149 | | int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, |
150 | | mbedtls_mpi *Y, |
151 | | unsigned char swap) |
152 | 43.6k | { |
153 | 43.6k | int ret = 0; |
154 | 43.6k | int s; |
155 | | |
156 | 43.6k | if (X == Y) { |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | 43.6k | mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap); |
161 | | |
162 | 43.6k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n)); |
163 | 43.6k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n)); |
164 | | |
165 | 43.6k | s = X->s; |
166 | 43.6k | X->s = mbedtls_ct_mpi_sign_if(do_swap, Y->s, X->s); |
167 | 43.6k | Y->s = mbedtls_ct_mpi_sign_if(do_swap, s, Y->s); |
168 | | |
169 | 43.6k | mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap); |
170 | | |
171 | 43.6k | cleanup: |
172 | 43.6k | return ret; |
173 | 43.6k | } |
174 | | |
175 | | /* Implementation that should never be optimized out by the compiler */ |
176 | 2.58M | #define mbedtls_mpi_zeroize_and_free(v, n) mbedtls_zeroize_and_free(v, ciL * (n)) |
177 | | |
178 | | /* |
179 | | * Initialize one MPI |
180 | | */ |
181 | | void mbedtls_mpi_init(mbedtls_mpi *X) |
182 | 8.56M | { |
183 | 8.56M | X->s = 1; |
184 | 8.56M | X->n = 0; |
185 | 8.56M | X->p = NULL; |
186 | 8.56M | } |
187 | | |
188 | | /* |
189 | | * Unallocate one MPI |
190 | | */ |
191 | | void mbedtls_mpi_free(mbedtls_mpi *X) |
192 | 8.74M | { |
193 | 8.74M | if (X == NULL) { |
194 | 0 | return; |
195 | 0 | } |
196 | | |
197 | 8.74M | if (X->p != NULL) { |
198 | 2.04M | mbedtls_mpi_zeroize_and_free(X->p, X->n); |
199 | 2.04M | } |
200 | | |
201 | 8.74M | X->s = 1; |
202 | 8.74M | X->n = 0; |
203 | 8.74M | X->p = NULL; |
204 | 8.74M | } |
205 | | |
206 | | /* |
207 | | * Enlarge to the specified number of limbs |
208 | | */ |
209 | | int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs) |
210 | 26.6M | { |
211 | 26.6M | mbedtls_mpi_uint *p; |
212 | | |
213 | 26.6M | if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) { |
214 | 0 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
215 | 0 | } |
216 | | |
217 | 26.6M | if (X->n < nblimbs) { |
218 | 2.56M | if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) { |
219 | 0 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
220 | 0 | } |
221 | | |
222 | 2.56M | if (X->p != NULL) { |
223 | 521k | memcpy(p, X->p, X->n * ciL); |
224 | 521k | mbedtls_mpi_zeroize_and_free(X->p, X->n); |
225 | 521k | } |
226 | | |
227 | | /* nblimbs fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS |
228 | | * fits, and we've checked that nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */ |
229 | 2.56M | X->n = (unsigned short) nblimbs; |
230 | 2.56M | X->p = p; |
231 | 2.56M | } |
232 | | |
233 | 26.6M | return 0; |
234 | 26.6M | } |
235 | | |
236 | | /* |
237 | | * Resize down as much as possible, |
238 | | * while keeping at least the specified number of limbs |
239 | | */ |
240 | | int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs) |
241 | 14.9k | { |
242 | 14.9k | mbedtls_mpi_uint *p; |
243 | 14.9k | size_t i; |
244 | | |
245 | 14.9k | if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) { |
246 | 0 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
247 | 0 | } |
248 | | |
249 | | /* Actually resize up if there are currently fewer than nblimbs limbs. */ |
250 | 14.9k | if (X->n <= nblimbs) { |
251 | 0 | return mbedtls_mpi_grow(X, nblimbs); |
252 | 0 | } |
253 | | /* After this point, then X->n > nblimbs and in particular X->n > 0. */ |
254 | | |
255 | 102k | for (i = X->n - 1; i > 0; i--) { |
256 | 102k | if (X->p[i] != 0) { |
257 | 14.9k | break; |
258 | 14.9k | } |
259 | 102k | } |
260 | 14.9k | i++; |
261 | | |
262 | 14.9k | if (i < nblimbs) { |
263 | 1.02k | i = nblimbs; |
264 | 1.02k | } |
265 | | |
266 | 14.9k | if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) { |
267 | 0 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
268 | 0 | } |
269 | | |
270 | 14.9k | if (X->p != NULL) { |
271 | 14.9k | memcpy(p, X->p, i * ciL); |
272 | 14.9k | mbedtls_mpi_zeroize_and_free(X->p, X->n); |
273 | 14.9k | } |
274 | | |
275 | | /* i fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS |
276 | | * fits, and we've checked that i <= nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */ |
277 | 14.9k | X->n = (unsigned short) i; |
278 | 14.9k | X->p = p; |
279 | | |
280 | 14.9k | return 0; |
281 | 14.9k | } |
282 | | |
283 | | /* Resize X to have exactly n limbs and set it to 0. */ |
284 | | static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs) |
285 | 61.2k | { |
286 | 61.2k | if (limbs == 0) { |
287 | 956 | mbedtls_mpi_free(X); |
288 | 956 | return 0; |
289 | 60.2k | } else if (X->n == limbs) { |
290 | 580 | memset(X->p, 0, limbs * ciL); |
291 | 580 | X->s = 1; |
292 | 580 | return 0; |
293 | 59.7k | } else { |
294 | 59.7k | mbedtls_mpi_free(X); |
295 | 59.7k | return mbedtls_mpi_grow(X, limbs); |
296 | 59.7k | } |
297 | 61.2k | } |
298 | | |
299 | | /* |
300 | | * Copy the contents of Y into X. |
301 | | * |
302 | | * This function is not constant-time. Leading zeros in Y may be removed. |
303 | | * |
304 | | * Ensure that X does not shrink. This is not guaranteed by the public API, |
305 | | * but some code in the bignum module might still rely on this property. |
306 | | */ |
307 | | int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y) |
308 | 5.80M | { |
309 | 5.80M | int ret = 0; |
310 | 5.80M | size_t i; |
311 | | |
312 | 5.80M | if (X == Y) { |
313 | 1.76M | return 0; |
314 | 1.76M | } |
315 | | |
316 | 4.04M | if (Y->n == 0) { |
317 | 533 | if (X->n != 0) { |
318 | 0 | X->s = 1; |
319 | 0 | memset(X->p, 0, X->n * ciL); |
320 | 0 | } |
321 | 533 | return 0; |
322 | 533 | } |
323 | | |
324 | 26.3M | for (i = Y->n - 1; i > 0; i--) { |
325 | 26.3M | if (Y->p[i] != 0) { |
326 | 4.03M | break; |
327 | 4.03M | } |
328 | 26.3M | } |
329 | 4.04M | i++; |
330 | | |
331 | 4.04M | X->s = Y->s; |
332 | | |
333 | 4.04M | if (X->n < i) { |
334 | 1.44M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i)); |
335 | 2.59M | } else { |
336 | 2.59M | memset(X->p + i, 0, (X->n - i) * ciL); |
337 | 2.59M | } |
338 | | |
339 | 4.04M | memcpy(X->p, Y->p, i * ciL); |
340 | | |
341 | 4.04M | cleanup: |
342 | | |
343 | 4.04M | return ret; |
344 | 4.04M | } |
345 | | |
346 | | /* |
347 | | * Swap the contents of X and Y |
348 | | */ |
349 | | void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y) |
350 | 0 | { |
351 | 0 | mbedtls_mpi T; |
352 | |
|
353 | 0 | memcpy(&T, X, sizeof(mbedtls_mpi)); |
354 | 0 | memcpy(X, Y, sizeof(mbedtls_mpi)); |
355 | 0 | memcpy(Y, &T, sizeof(mbedtls_mpi)); |
356 | 0 | } |
357 | | |
358 | | static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z) |
359 | 8.89M | { |
360 | 8.89M | if (z >= 0) { |
361 | 8.89M | return z; |
362 | 8.89M | } |
363 | | /* Take care to handle the most negative value (-2^(biL-1)) correctly. |
364 | | * A naive -z would have undefined behavior. |
365 | | * Write this in a way that makes popular compilers happy (GCC, Clang, |
366 | | * MSVC). */ |
367 | 418 | return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z; |
368 | 8.89M | } |
369 | | |
370 | | /* Convert x to a sign, i.e. to 1, if x is positive, or -1, if x is negative. |
371 | | * This looks awkward but generates smaller code than (x < 0 ? -1 : 1) */ |
372 | 8.89M | #define TO_SIGN(x) ((mbedtls_mpi_sint) (((mbedtls_mpi_uint) x) >> (biL - 1)) * -2 + 1) |
373 | | |
374 | | /* |
375 | | * Set value from integer |
376 | | */ |
377 | | int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z) |
378 | 5.44M | { |
379 | 5.44M | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
380 | | |
381 | 5.44M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1)); |
382 | 5.44M | memset(X->p, 0, X->n * ciL); |
383 | | |
384 | 5.44M | X->p[0] = mpi_sint_abs(z); |
385 | 5.44M | X->s = TO_SIGN(z); |
386 | | |
387 | 5.44M | cleanup: |
388 | | |
389 | 5.44M | return ret; |
390 | 5.44M | } |
391 | | |
392 | | /* |
393 | | * Get a specific bit |
394 | | */ |
395 | | int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos) |
396 | 555k | { |
397 | 555k | if (X->n * biL <= pos) { |
398 | 2.50k | return 0; |
399 | 2.50k | } |
400 | | |
401 | 553k | return (X->p[pos / biL] >> (pos % biL)) & 0x01; |
402 | 555k | } |
403 | | |
404 | | /* |
405 | | * Set a bit to a specific value of 0 or 1 |
406 | | */ |
407 | | int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val) |
408 | 1.00k | { |
409 | 1.00k | int ret = 0; |
410 | 1.00k | size_t off = pos / biL; |
411 | 1.00k | size_t idx = pos % biL; |
412 | | |
413 | 1.00k | if (val != 0 && val != 1) { |
414 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
415 | 0 | } |
416 | | |
417 | 1.00k | if (X->n * biL <= pos) { |
418 | 0 | if (val == 0) { |
419 | 0 | return 0; |
420 | 0 | } |
421 | | |
422 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1)); |
423 | 0 | } |
424 | | |
425 | 1.00k | X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx); |
426 | 1.00k | X->p[off] |= (mbedtls_mpi_uint) val << idx; |
427 | | |
428 | 1.00k | cleanup: |
429 | | |
430 | 1.00k | return ret; |
431 | 1.00k | } |
432 | | |
433 | | #if defined(__has_builtin) |
434 | | #if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_ctz) |
435 | | #define mbedtls_mpi_uint_ctz __builtin_ctz |
436 | | #elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_ctzl) |
437 | 0 | #define mbedtls_mpi_uint_ctz __builtin_ctzl |
438 | | #elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_ctzll) |
439 | | #define mbedtls_mpi_uint_ctz __builtin_ctzll |
440 | | #endif |
441 | | #endif |
442 | | |
443 | | #if !defined(mbedtls_mpi_uint_ctz) |
444 | | static size_t mbedtls_mpi_uint_ctz(mbedtls_mpi_uint x) |
445 | | { |
446 | | size_t count = 0; |
447 | | mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE; |
448 | | |
449 | | for (size_t i = 0; i < biL; i++) { |
450 | | mbedtls_ct_condition_t non_zero = mbedtls_ct_bool((x >> i) & 1); |
451 | | done = mbedtls_ct_bool_or(done, non_zero); |
452 | | count = mbedtls_ct_size_if(done, count, i + 1); |
453 | | } |
454 | | |
455 | | return count; |
456 | | } |
457 | | #endif |
458 | | |
459 | | /* |
460 | | * Return the number of less significant zero-bits |
461 | | */ |
462 | | size_t mbedtls_mpi_lsb(const mbedtls_mpi *X) |
463 | 0 | { |
464 | 0 | size_t i; |
465 | |
|
466 | 0 | for (i = 0; i < X->n; i++) { |
467 | 0 | if (X->p[i] != 0) { |
468 | 0 | return i * biL + mbedtls_mpi_uint_ctz(X->p[i]); |
469 | 0 | } |
470 | 0 | } |
471 | | |
472 | 0 | return 0; |
473 | 0 | } |
474 | | |
475 | | /* |
476 | | * Return the number of bits |
477 | | */ |
478 | | size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X) |
479 | 6.14M | { |
480 | 6.14M | return mbedtls_mpi_core_bitlen(X->p, X->n); |
481 | 6.14M | } |
482 | | |
483 | | /* |
484 | | * Return the total size in bytes |
485 | | */ |
486 | | size_t mbedtls_mpi_size(const mbedtls_mpi *X) |
487 | 39.6k | { |
488 | 39.6k | return (mbedtls_mpi_bitlen(X) + 7) >> 3; |
489 | 39.6k | } |
490 | | |
491 | | /* |
492 | | * Convert an ASCII character to digit value |
493 | | */ |
494 | | static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c) |
495 | 0 | { |
496 | 0 | *d = 255; |
497 | |
|
498 | 0 | if (c >= 0x30 && c <= 0x39) { |
499 | 0 | *d = c - 0x30; |
500 | 0 | } |
501 | 0 | if (c >= 0x41 && c <= 0x46) { |
502 | 0 | *d = c - 0x37; |
503 | 0 | } |
504 | 0 | if (c >= 0x61 && c <= 0x66) { |
505 | 0 | *d = c - 0x57; |
506 | 0 | } |
507 | |
|
508 | 0 | if (*d >= (mbedtls_mpi_uint) radix) { |
509 | 0 | return MBEDTLS_ERR_MPI_INVALID_CHARACTER; |
510 | 0 | } |
511 | | |
512 | 0 | return 0; |
513 | 0 | } |
514 | | |
515 | | /* |
516 | | * Import from an ASCII string |
517 | | */ |
518 | | int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s) |
519 | 0 | { |
520 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
521 | 0 | size_t i, j, slen, n; |
522 | 0 | int sign = 1; |
523 | 0 | mbedtls_mpi_uint d; |
524 | 0 | mbedtls_mpi T; |
525 | |
|
526 | 0 | if (radix < 2 || radix > 16) { |
527 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
528 | 0 | } |
529 | | |
530 | 0 | mbedtls_mpi_init(&T); |
531 | |
|
532 | 0 | if (s[0] == 0) { |
533 | 0 | mbedtls_mpi_free(X); |
534 | 0 | return 0; |
535 | 0 | } |
536 | | |
537 | 0 | if (s[0] == '-') { |
538 | 0 | ++s; |
539 | 0 | sign = -1; |
540 | 0 | } |
541 | |
|
542 | 0 | slen = strlen(s); |
543 | |
|
544 | 0 | if (radix == 16) { |
545 | 0 | if (slen > SIZE_MAX >> 2) { |
546 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
547 | 0 | } |
548 | | |
549 | 0 | n = BITS_TO_LIMBS(slen << 2); |
550 | |
|
551 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n)); |
552 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0)); |
553 | | |
554 | 0 | for (i = slen, j = 0; i > 0; i--, j++) { |
555 | 0 | MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1])); |
556 | 0 | X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2); |
557 | 0 | } |
558 | 0 | } else { |
559 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0)); |
560 | | |
561 | 0 | for (i = 0; i < slen; i++) { |
562 | 0 | MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i])); |
563 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix)); |
564 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d)); |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | 0 | if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) { |
569 | 0 | X->s = -1; |
570 | 0 | } |
571 | |
|
572 | 0 | cleanup: |
573 | |
|
574 | 0 | mbedtls_mpi_free(&T); |
575 | |
|
576 | 0 | return ret; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * Helper to write the digits high-order first. |
581 | | */ |
582 | | static int mpi_write_hlp(mbedtls_mpi *X, int radix, |
583 | | char **p, const size_t buflen) |
584 | 0 | { |
585 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
586 | 0 | mbedtls_mpi_uint r; |
587 | 0 | size_t length = 0; |
588 | 0 | char *p_end = *p + buflen; |
589 | |
|
590 | 0 | do { |
591 | 0 | if (length >= buflen) { |
592 | 0 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
593 | 0 | } |
594 | | |
595 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix)); |
596 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix)); |
597 | | /* |
598 | | * Write the residue in the current position, as an ASCII character. |
599 | | */ |
600 | 0 | if (r < 0xA) { |
601 | 0 | *(--p_end) = (char) ('0' + r); |
602 | 0 | } else { |
603 | 0 | *(--p_end) = (char) ('A' + (r - 0xA)); |
604 | 0 | } |
605 | |
|
606 | 0 | length++; |
607 | 0 | } while (mbedtls_mpi_cmp_int(X, 0) != 0); |
608 | | |
609 | 0 | memmove(*p, p_end, length); |
610 | 0 | *p += length; |
611 | |
|
612 | 0 | cleanup: |
613 | |
|
614 | 0 | return ret; |
615 | 0 | } |
616 | | |
617 | | /* |
618 | | * Export into an ASCII string |
619 | | */ |
620 | | int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, |
621 | | char *buf, size_t buflen, size_t *olen) |
622 | 0 | { |
623 | 0 | int ret = 0; |
624 | 0 | size_t n; |
625 | 0 | char *p; |
626 | 0 | mbedtls_mpi T; |
627 | |
|
628 | 0 | if (radix < 2 || radix > 16) { |
629 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
630 | 0 | } |
631 | | |
632 | 0 | n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */ |
633 | 0 | if (radix >= 4) { |
634 | 0 | n >>= 1; /* Number of 4-adic digits necessary to present |
635 | | * `n`. If radix > 4, this might be a strict |
636 | | * overapproximation of the number of |
637 | | * radix-adic digits needed to present `n`. */ |
638 | 0 | } |
639 | 0 | if (radix >= 16) { |
640 | 0 | n >>= 1; /* Number of hexadecimal digits necessary to |
641 | | * present `n`. */ |
642 | |
|
643 | 0 | } |
644 | 0 | n += 1; /* Terminating null byte */ |
645 | 0 | n += 1; /* Compensate for the divisions above, which round down `n` |
646 | | * in case it's not even. */ |
647 | 0 | n += 1; /* Potential '-'-sign. */ |
648 | 0 | n += (n & 1); /* Make n even to have enough space for hexadecimal writing, |
649 | | * which always uses an even number of hex-digits. */ |
650 | |
|
651 | 0 | if (buflen < n) { |
652 | 0 | *olen = n; |
653 | 0 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
654 | 0 | } |
655 | | |
656 | 0 | p = buf; |
657 | 0 | mbedtls_mpi_init(&T); |
658 | |
|
659 | 0 | if (X->s == -1) { |
660 | 0 | *p++ = '-'; |
661 | 0 | buflen--; |
662 | 0 | } |
663 | |
|
664 | 0 | if (radix == 16) { |
665 | 0 | int c; |
666 | 0 | size_t i, j, k; |
667 | |
|
668 | 0 | for (i = X->n, k = 0; i > 0; i--) { |
669 | 0 | for (j = ciL; j > 0; j--) { |
670 | 0 | c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF; |
671 | |
|
672 | 0 | if (c == 0 && k == 0 && (i + j) != 2) { |
673 | 0 | continue; |
674 | 0 | } |
675 | | |
676 | 0 | *(p++) = "0123456789ABCDEF" [c / 16]; |
677 | 0 | *(p++) = "0123456789ABCDEF" [c % 16]; |
678 | 0 | k = 1; |
679 | 0 | } |
680 | 0 | } |
681 | 0 | } else { |
682 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X)); |
683 | | |
684 | 0 | if (T.s == -1) { |
685 | 0 | T.s = 1; |
686 | 0 | } |
687 | |
|
688 | 0 | MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen)); |
689 | 0 | } |
690 | | |
691 | 0 | *p++ = '\0'; |
692 | 0 | *olen = (size_t) (p - buf); |
693 | |
|
694 | 0 | cleanup: |
695 | |
|
696 | 0 | mbedtls_mpi_free(&T); |
697 | |
|
698 | 0 | return ret; |
699 | 0 | } |
700 | | |
701 | | #if defined(MBEDTLS_FS_IO) |
702 | | /* |
703 | | * Read X from an opened file |
704 | | */ |
705 | | int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin) |
706 | 0 | { |
707 | 0 | mbedtls_mpi_uint d; |
708 | 0 | size_t slen; |
709 | 0 | char *p; |
710 | | /* |
711 | | * Buffer should have space for (short) label and decimal formatted MPI, |
712 | | * newline characters and '\0' |
713 | | */ |
714 | 0 | char s[MBEDTLS_MPI_RW_BUFFER_SIZE]; |
715 | |
|
716 | 0 | if (radix < 2 || radix > 16) { |
717 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
718 | 0 | } |
719 | | |
720 | 0 | memset(s, 0, sizeof(s)); |
721 | 0 | if (fgets(s, sizeof(s) - 1, fin) == NULL) { |
722 | 0 | return MBEDTLS_ERR_MPI_FILE_IO_ERROR; |
723 | 0 | } |
724 | | |
725 | 0 | slen = strlen(s); |
726 | 0 | if (slen == sizeof(s) - 2) { |
727 | 0 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
728 | 0 | } |
729 | | |
730 | 0 | if (slen > 0 && s[slen - 1] == '\n') { |
731 | 0 | slen--; s[slen] = '\0'; |
732 | 0 | } |
733 | 0 | if (slen > 0 && s[slen - 1] == '\r') { |
734 | 0 | slen--; s[slen] = '\0'; |
735 | 0 | } |
736 | |
|
737 | 0 | p = s + slen; |
738 | 0 | while (p-- > s) { |
739 | 0 | if (mpi_get_digit(&d, radix, *p) != 0) { |
740 | 0 | break; |
741 | 0 | } |
742 | 0 | } |
743 | |
|
744 | 0 | return mbedtls_mpi_read_string(X, radix, p + 1); |
745 | 0 | } |
746 | | |
747 | | /* |
748 | | * Write X into an opened file (or stdout if fout == NULL) |
749 | | */ |
750 | | int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout) |
751 | 0 | { |
752 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
753 | 0 | size_t n, slen, plen; |
754 | | /* |
755 | | * Buffer should have space for (short) label and decimal formatted MPI, |
756 | | * newline characters and '\0' |
757 | | */ |
758 | 0 | char s[MBEDTLS_MPI_RW_BUFFER_SIZE]; |
759 | |
|
760 | 0 | if (radix < 2 || radix > 16) { |
761 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
762 | 0 | } |
763 | | |
764 | 0 | memset(s, 0, sizeof(s)); |
765 | |
|
766 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n)); |
767 | | |
768 | 0 | if (p == NULL) { |
769 | 0 | p = ""; |
770 | 0 | } |
771 | |
|
772 | 0 | plen = strlen(p); |
773 | 0 | slen = strlen(s); |
774 | 0 | s[slen++] = '\r'; |
775 | 0 | s[slen++] = '\n'; |
776 | |
|
777 | 0 | if (fout != NULL) { |
778 | 0 | if (fwrite(p, 1, plen, fout) != plen || |
779 | 0 | fwrite(s, 1, slen, fout) != slen) { |
780 | 0 | return MBEDTLS_ERR_MPI_FILE_IO_ERROR; |
781 | 0 | } |
782 | 0 | } else { |
783 | 0 | mbedtls_printf("%s%s", p, s); |
784 | 0 | } |
785 | | |
786 | 0 | cleanup: |
787 | |
|
788 | 0 | return ret; |
789 | 0 | } |
790 | | #endif /* MBEDTLS_FS_IO */ |
791 | | |
792 | | /* |
793 | | * Import X from unsigned binary data, little endian |
794 | | * |
795 | | * This function is guaranteed to return an MPI with exactly the necessary |
796 | | * number of limbs (in particular, it does not skip 0s in the input). |
797 | | */ |
798 | | int mbedtls_mpi_read_binary_le(mbedtls_mpi *X, |
799 | | const unsigned char *buf, size_t buflen) |
800 | 900 | { |
801 | 900 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
802 | 900 | const size_t limbs = CHARS_TO_LIMBS(buflen); |
803 | | |
804 | | /* Ensure that target MPI has exactly the necessary number of limbs */ |
805 | 900 | MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs)); |
806 | | |
807 | 900 | MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen)); |
808 | | |
809 | 900 | cleanup: |
810 | | |
811 | | /* |
812 | | * This function is also used to import keys. However, wiping the buffers |
813 | | * upon failure is not necessary because failure only can happen before any |
814 | | * input is copied. |
815 | | */ |
816 | 900 | return ret; |
817 | 900 | } |
818 | | |
819 | | /* |
820 | | * Import X from unsigned binary data, big endian |
821 | | * |
822 | | * This function is guaranteed to return an MPI with exactly the necessary |
823 | | * number of limbs (in particular, it does not skip 0s in the input). |
824 | | */ |
825 | | int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen) |
826 | 58.5k | { |
827 | 58.5k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
828 | 58.5k | const size_t limbs = CHARS_TO_LIMBS(buflen); |
829 | | |
830 | | /* Ensure that target MPI has exactly the necessary number of limbs */ |
831 | 58.5k | MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs)); |
832 | | |
833 | 58.5k | MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen)); |
834 | | |
835 | 58.5k | cleanup: |
836 | | |
837 | | /* |
838 | | * This function is also used to import keys. However, wiping the buffers |
839 | | * upon failure is not necessary because failure only can happen before any |
840 | | * input is copied. |
841 | | */ |
842 | 58.5k | return ret; |
843 | 58.5k | } |
844 | | |
845 | | /* |
846 | | * Export X into unsigned binary data, little endian |
847 | | */ |
848 | | int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X, |
849 | | unsigned char *buf, size_t buflen) |
850 | 11 | { |
851 | 11 | return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen); |
852 | 11 | } |
853 | | |
854 | | /* |
855 | | * Export X into unsigned binary data, big endian |
856 | | */ |
857 | | int mbedtls_mpi_write_binary(const mbedtls_mpi *X, |
858 | | unsigned char *buf, size_t buflen) |
859 | 2.20k | { |
860 | 2.20k | return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen); |
861 | 2.20k | } |
862 | | |
863 | | /* |
864 | | * Left-shift: X <<= count |
865 | | */ |
866 | | int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count) |
867 | 2.66M | { |
868 | 2.66M | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
869 | 2.66M | size_t i; |
870 | | |
871 | 2.66M | i = mbedtls_mpi_bitlen(X) + count; |
872 | | |
873 | 2.66M | if (X->n * biL < i) { |
874 | 484k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i))); |
875 | 484k | } |
876 | | |
877 | 2.66M | ret = 0; |
878 | | |
879 | 2.66M | mbedtls_mpi_core_shift_l(X->p, X->n, count); |
880 | 2.66M | cleanup: |
881 | | |
882 | 2.66M | return ret; |
883 | 2.66M | } |
884 | | |
885 | | /* |
886 | | * Right-shift: X >>= count |
887 | | */ |
888 | | int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count) |
889 | 326k | { |
890 | 326k | if (X->n != 0) { |
891 | 326k | mbedtls_mpi_core_shift_r(X->p, X->n, count); |
892 | 326k | } |
893 | 326k | return 0; |
894 | 326k | } |
895 | | |
896 | | /* |
897 | | * Compare unsigned values |
898 | | */ |
899 | | int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y) |
900 | 4.24M | { |
901 | 4.24M | size_t i, j; |
902 | | |
903 | 23.2M | for (i = X->n; i > 0; i--) { |
904 | 23.2M | if (X->p[i - 1] != 0) { |
905 | 4.24M | break; |
906 | 4.24M | } |
907 | 23.2M | } |
908 | | |
909 | 19.7M | for (j = Y->n; j > 0; j--) { |
910 | 19.7M | if (Y->p[j - 1] != 0) { |
911 | 4.24M | break; |
912 | 4.24M | } |
913 | 19.7M | } |
914 | | |
915 | | /* If i == j == 0, i.e. abs(X) == abs(Y), |
916 | | * we end up returning 0 at the end of the function. */ |
917 | | |
918 | 4.24M | if (i > j) { |
919 | 184k | return 1; |
920 | 184k | } |
921 | 4.06M | if (j > i) { |
922 | 4.43k | return -1; |
923 | 4.43k | } |
924 | | |
925 | 4.98M | for (; i > 0; i--) { |
926 | 4.98M | if (X->p[i - 1] > Y->p[i - 1]) { |
927 | 2.16M | return 1; |
928 | 2.16M | } |
929 | 2.81M | if (X->p[i - 1] < Y->p[i - 1]) { |
930 | 1.89M | return -1; |
931 | 1.89M | } |
932 | 2.81M | } |
933 | | |
934 | 7 | return 0; |
935 | 4.06M | } |
936 | | |
937 | | /* |
938 | | * Compare signed values |
939 | | */ |
940 | | int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y) |
941 | 8.12M | { |
942 | 8.12M | size_t i, j; |
943 | | |
944 | 60.5M | for (i = X->n; i > 0; i--) { |
945 | 60.4M | if (X->p[i - 1] != 0) { |
946 | 8.08M | break; |
947 | 8.08M | } |
948 | 60.4M | } |
949 | | |
950 | 11.5M | for (j = Y->n; j > 0; j--) { |
951 | 8.25M | if (Y->p[j - 1] != 0) { |
952 | 4.84M | break; |
953 | 4.84M | } |
954 | 8.25M | } |
955 | | |
956 | 8.12M | if (i == 0 && j == 0) { |
957 | 40.5k | return 0; |
958 | 40.5k | } |
959 | | |
960 | 8.08M | if (i > j) { |
961 | 3.86M | return X->s; |
962 | 3.86M | } |
963 | 4.21M | if (j > i) { |
964 | 19.4k | return -Y->s; |
965 | 19.4k | } |
966 | | |
967 | 4.20M | if (X->s > 0 && Y->s < 0) { |
968 | 18 | return 1; |
969 | 18 | } |
970 | 4.20M | if (Y->s > 0 && X->s < 0) { |
971 | 0 | return -1; |
972 | 0 | } |
973 | | |
974 | 5.85M | for (; i > 0; i--) { |
975 | 5.71M | if (X->p[i - 1] > Y->p[i - 1]) { |
976 | 964k | return X->s; |
977 | 964k | } |
978 | 4.74M | if (X->p[i - 1] < Y->p[i - 1]) { |
979 | 3.08M | return -X->s; |
980 | 3.08M | } |
981 | 4.74M | } |
982 | | |
983 | 147k | return 0; |
984 | 4.20M | } |
985 | | |
986 | | /* |
987 | | * Compare signed values |
988 | | */ |
989 | | int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z) |
990 | 3.42M | { |
991 | 3.42M | mbedtls_mpi Y; |
992 | 3.42M | mbedtls_mpi_uint p[1]; |
993 | | |
994 | 3.42M | *p = mpi_sint_abs(z); |
995 | 3.42M | Y.s = TO_SIGN(z); |
996 | 3.42M | Y.n = 1; |
997 | 3.42M | Y.p = p; |
998 | | |
999 | 3.42M | return mbedtls_mpi_cmp_mpi(X, &Y); |
1000 | 3.42M | } |
1001 | | |
1002 | | /* |
1003 | | * Unsigned addition: X = |A| + |B| (HAC 14.7) |
1004 | | */ |
1005 | | int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1006 | 308k | { |
1007 | 308k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1008 | 308k | size_t j; |
1009 | 308k | mbedtls_mpi_uint *p; |
1010 | 308k | mbedtls_mpi_uint c; |
1011 | | |
1012 | 308k | if (X == B) { |
1013 | 10.9k | const mbedtls_mpi *T = A; A = X; B = T; |
1014 | 10.9k | } |
1015 | | |
1016 | 308k | if (X != A) { |
1017 | 264k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A)); |
1018 | 264k | } |
1019 | | |
1020 | | /* |
1021 | | * X must always be positive as a result of unsigned additions. |
1022 | | */ |
1023 | 308k | X->s = 1; |
1024 | | |
1025 | 1.90M | for (j = B->n; j > 0; j--) { |
1026 | 1.90M | if (B->p[j - 1] != 0) { |
1027 | 306k | break; |
1028 | 306k | } |
1029 | 1.90M | } |
1030 | | |
1031 | | /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0 |
1032 | | * and B is 0 (of any size). */ |
1033 | 308k | if (j == 0) { |
1034 | 1.80k | return 0; |
1035 | 1.80k | } |
1036 | | |
1037 | 306k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j)); |
1038 | | |
1039 | | /* j is the number of non-zero limbs of B. Add those to X. */ |
1040 | | |
1041 | 306k | p = X->p; |
1042 | | |
1043 | 306k | c = mbedtls_mpi_core_add(p, p, B->p, j); |
1044 | | |
1045 | 306k | p += j; |
1046 | | |
1047 | | /* Now propagate any carry */ |
1048 | | |
1049 | 402k | while (c != 0) { |
1050 | 95.9k | if (j >= X->n) { |
1051 | 888 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1)); |
1052 | 888 | p = X->p + j; |
1053 | 888 | } |
1054 | | |
1055 | 95.9k | *p += c; c = (*p < c); j++; p++; |
1056 | 95.9k | } |
1057 | | |
1058 | 306k | cleanup: |
1059 | | |
1060 | 306k | return ret; |
1061 | 306k | } |
1062 | | |
1063 | | /* |
1064 | | * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10) |
1065 | | */ |
1066 | | int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1067 | 5.00M | { |
1068 | 5.00M | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1069 | 5.00M | size_t n; |
1070 | 5.00M | mbedtls_mpi_uint carry; |
1071 | | |
1072 | 25.4M | for (n = B->n; n > 0; n--) { |
1073 | 25.4M | if (B->p[n - 1] != 0) { |
1074 | 4.99M | break; |
1075 | 4.99M | } |
1076 | 25.4M | } |
1077 | 5.00M | if (n > A->n) { |
1078 | | /* B >= (2^ciL)^n > A */ |
1079 | 0 | ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE; |
1080 | 0 | goto cleanup; |
1081 | 0 | } |
1082 | | |
1083 | 5.00M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n)); |
1084 | | |
1085 | | /* Set the high limbs of X to match A. Don't touch the lower limbs |
1086 | | * because X might be aliased to B, and we must not overwrite the |
1087 | | * significant digits of B. */ |
1088 | 5.00M | if (A->n > n && A != X) { |
1089 | 1.08M | memcpy(X->p + n, A->p + n, (A->n - n) * ciL); |
1090 | 1.08M | } |
1091 | 5.00M | if (X->n > A->n) { |
1092 | 1.01M | memset(X->p + A->n, 0, (X->n - A->n) * ciL); |
1093 | 1.01M | } |
1094 | | |
1095 | 5.00M | carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n); |
1096 | 5.00M | if (carry != 0) { |
1097 | | /* Propagate the carry through the rest of X. */ |
1098 | 618k | carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n); |
1099 | | |
1100 | | /* If we have further carry/borrow, the result is negative. */ |
1101 | 618k | if (carry != 0) { |
1102 | 0 | ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE; |
1103 | 0 | goto cleanup; |
1104 | 0 | } |
1105 | 618k | } |
1106 | | |
1107 | | /* X should always be positive as a result of unsigned subtractions. */ |
1108 | 5.00M | X->s = 1; |
1109 | | |
1110 | 5.00M | cleanup: |
1111 | 5.00M | return ret; |
1112 | 5.00M | } |
1113 | | |
1114 | | /* Common function for signed addition and subtraction. |
1115 | | * Calculate A + B * flip_B where flip_B is 1 or -1. |
1116 | | */ |
1117 | | static int add_sub_mpi(mbedtls_mpi *X, |
1118 | | const mbedtls_mpi *A, const mbedtls_mpi *B, |
1119 | | int flip_B) |
1120 | 4.39M | { |
1121 | 4.39M | int ret, s; |
1122 | | |
1123 | 4.39M | s = A->s; |
1124 | 4.39M | if (A->s * B->s * flip_B < 0) { |
1125 | 4.08M | int cmp = mbedtls_mpi_cmp_abs(A, B); |
1126 | 4.08M | if (cmp >= 0) { |
1127 | 2.19M | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B)); |
1128 | | /* If |A| = |B|, the result is 0 and we must set the sign bit |
1129 | | * to +1 regardless of which of A or B was negative. Otherwise, |
1130 | | * since |A| > |B|, the sign is the sign of A. */ |
1131 | 2.19M | X->s = cmp == 0 ? 1 : s; |
1132 | 2.19M | } else { |
1133 | 1.89M | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A)); |
1134 | | /* Since |A| < |B|, the sign is the opposite of A. */ |
1135 | 1.89M | X->s = -s; |
1136 | 1.89M | } |
1137 | 4.08M | } else { |
1138 | 308k | MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B)); |
1139 | 308k | X->s = s; |
1140 | 308k | } |
1141 | | |
1142 | 4.39M | cleanup: |
1143 | | |
1144 | 4.39M | return ret; |
1145 | 4.39M | } |
1146 | | |
1147 | | /* |
1148 | | * Signed addition: X = A + B |
1149 | | */ |
1150 | | int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1151 | 1.25M | { |
1152 | 1.25M | return add_sub_mpi(X, A, B, 1); |
1153 | 1.25M | } |
1154 | | |
1155 | | /* |
1156 | | * Signed subtraction: X = A - B |
1157 | | */ |
1158 | | int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1159 | 3.13M | { |
1160 | 3.13M | return add_sub_mpi(X, A, B, -1); |
1161 | 3.13M | } |
1162 | | |
1163 | | /* |
1164 | | * Signed addition: X = A + b |
1165 | | */ |
1166 | | int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
1167 | 3.12k | { |
1168 | 3.12k | mbedtls_mpi B; |
1169 | 3.12k | mbedtls_mpi_uint p[1]; |
1170 | | |
1171 | 3.12k | p[0] = mpi_sint_abs(b); |
1172 | 3.12k | B.s = TO_SIGN(b); |
1173 | 3.12k | B.n = 1; |
1174 | 3.12k | B.p = p; |
1175 | | |
1176 | 3.12k | return mbedtls_mpi_add_mpi(X, A, &B); |
1177 | 3.12k | } |
1178 | | |
1179 | | /* |
1180 | | * Signed subtraction: X = A - b |
1181 | | */ |
1182 | | int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
1183 | 19.0k | { |
1184 | 19.0k | mbedtls_mpi B; |
1185 | 19.0k | mbedtls_mpi_uint p[1]; |
1186 | | |
1187 | 19.0k | p[0] = mpi_sint_abs(b); |
1188 | 19.0k | B.s = TO_SIGN(b); |
1189 | 19.0k | B.n = 1; |
1190 | 19.0k | B.p = p; |
1191 | | |
1192 | 19.0k | return mbedtls_mpi_sub_mpi(X, A, &B); |
1193 | 19.0k | } |
1194 | | |
1195 | | /* |
1196 | | * Baseline multiplication: X = A * B (HAC 14.12) |
1197 | | */ |
1198 | | int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1199 | 3.37M | { |
1200 | 3.37M | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1201 | 3.37M | size_t i, j; |
1202 | 3.37M | mbedtls_mpi TA, TB; |
1203 | 3.37M | int result_is_zero = 0; |
1204 | | |
1205 | 3.37M | mbedtls_mpi_init(&TA); |
1206 | 3.37M | mbedtls_mpi_init(&TB); |
1207 | | |
1208 | 3.37M | if (X == A) { |
1209 | 1.02M | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA; |
1210 | 1.02M | } |
1211 | 3.37M | if (X == B) { |
1212 | 74.3k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB; |
1213 | 74.3k | } |
1214 | | |
1215 | 12.6M | for (i = A->n; i > 0; i--) { |
1216 | 12.6M | if (A->p[i - 1] != 0) { |
1217 | 3.37M | break; |
1218 | 3.37M | } |
1219 | 12.6M | } |
1220 | 3.37M | if (i == 0) { |
1221 | 1.29k | result_is_zero = 1; |
1222 | 1.29k | } |
1223 | | |
1224 | 16.8M | for (j = B->n; j > 0; j--) { |
1225 | 16.8M | if (B->p[j - 1] != 0) { |
1226 | 3.37M | break; |
1227 | 3.37M | } |
1228 | 16.8M | } |
1229 | 3.37M | if (j == 0) { |
1230 | 1.93k | result_is_zero = 1; |
1231 | 1.93k | } |
1232 | | |
1233 | 3.37M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j)); |
1234 | 3.37M | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0)); |
1235 | | |
1236 | 3.37M | mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j); |
1237 | | |
1238 | | /* If the result is 0, we don't shortcut the operation, which reduces |
1239 | | * but does not eliminate side channels leaking the zero-ness. We do |
1240 | | * need to take care to set the sign bit properly since the library does |
1241 | | * not fully support an MPI object with a value of 0 and s == -1. */ |
1242 | 3.37M | if (result_is_zero) { |
1243 | 1.93k | X->s = 1; |
1244 | 3.37M | } else { |
1245 | 3.37M | X->s = A->s * B->s; |
1246 | 3.37M | } |
1247 | | |
1248 | 3.37M | cleanup: |
1249 | | |
1250 | 3.37M | mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA); |
1251 | | |
1252 | 3.37M | return ret; |
1253 | 3.37M | } |
1254 | | |
1255 | | /* |
1256 | | * Baseline multiplication: X = A * b |
1257 | | */ |
1258 | | int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b) |
1259 | 3.10M | { |
1260 | 3.10M | size_t n = A->n; |
1261 | 39.5M | while (n > 0 && A->p[n - 1] == 0) { |
1262 | 36.4M | --n; |
1263 | 36.4M | } |
1264 | | |
1265 | | /* The general method below doesn't work if b==0. */ |
1266 | 3.10M | if (b == 0 || n == 0) { |
1267 | 5.64k | return mbedtls_mpi_lset(X, 0); |
1268 | 5.64k | } |
1269 | | |
1270 | | /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */ |
1271 | 3.10M | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1272 | | /* In general, A * b requires 1 limb more than b. If |
1273 | | * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same |
1274 | | * number of limbs as A and the call to grow() is not required since |
1275 | | * copy() will take care of the growth if needed. However, experimentally, |
1276 | | * making the call to grow() unconditional causes slightly fewer |
1277 | | * calls to calloc() in ECP code, presumably because it reuses the |
1278 | | * same mpi for a while and this way the mpi is more likely to directly |
1279 | | * grow to its final size. |
1280 | | * |
1281 | | * Note that calculating A*b as 0 + A*b doesn't work as-is because |
1282 | | * A,X can be the same. */ |
1283 | 3.10M | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1)); |
1284 | 3.10M | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A)); |
1285 | 3.10M | mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1); |
1286 | | |
1287 | 3.10M | cleanup: |
1288 | 3.10M | return ret; |
1289 | 3.10M | } |
1290 | | |
1291 | | /* |
1292 | | * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and |
1293 | | * mbedtls_mpi_uint divisor, d |
1294 | | */ |
1295 | | static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1, |
1296 | | mbedtls_mpi_uint u0, |
1297 | | mbedtls_mpi_uint d, |
1298 | | mbedtls_mpi_uint *r) |
1299 | 1.10M | { |
1300 | 1.10M | #if defined(MBEDTLS_HAVE_UDBL) |
1301 | 1.10M | mbedtls_t_udbl dividend, quotient; |
1302 | | #else |
1303 | | const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH; |
1304 | | const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1; |
1305 | | mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient; |
1306 | | mbedtls_mpi_uint u0_msw, u0_lsw; |
1307 | | size_t s; |
1308 | | #endif |
1309 | | |
1310 | | /* |
1311 | | * Check for overflow |
1312 | | */ |
1313 | 1.10M | if (0 == d || u1 >= d) { |
1314 | 0 | if (r != NULL) { |
1315 | 0 | *r = ~(mbedtls_mpi_uint) 0u; |
1316 | 0 | } |
1317 | |
|
1318 | 0 | return ~(mbedtls_mpi_uint) 0u; |
1319 | 0 | } |
1320 | | |
1321 | 1.10M | #if defined(MBEDTLS_HAVE_UDBL) |
1322 | 1.10M | dividend = (mbedtls_t_udbl) u1 << biL; |
1323 | 1.10M | dividend |= (mbedtls_t_udbl) u0; |
1324 | 1.10M | quotient = dividend / d; |
1325 | 1.10M | if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) { |
1326 | 0 | quotient = ((mbedtls_t_udbl) 1 << biL) - 1; |
1327 | 0 | } |
1328 | | |
1329 | 1.10M | if (r != NULL) { |
1330 | 0 | *r = (mbedtls_mpi_uint) (dividend - (quotient * d)); |
1331 | 0 | } |
1332 | | |
1333 | 1.10M | return (mbedtls_mpi_uint) quotient; |
1334 | | #else |
1335 | | |
1336 | | /* |
1337 | | * Algorithm D, Section 4.3.1 - The Art of Computer Programming |
1338 | | * Vol. 2 - Seminumerical Algorithms, Knuth |
1339 | | */ |
1340 | | |
1341 | | /* |
1342 | | * Normalize the divisor, d, and dividend, u0, u1 |
1343 | | */ |
1344 | | s = mbedtls_mpi_core_clz(d); |
1345 | | d = d << s; |
1346 | | |
1347 | | u1 = u1 << s; |
1348 | | u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1)); |
1349 | | u0 = u0 << s; |
1350 | | |
1351 | | d1 = d >> biH; |
1352 | | d0 = d & uint_halfword_mask; |
1353 | | |
1354 | | u0_msw = u0 >> biH; |
1355 | | u0_lsw = u0 & uint_halfword_mask; |
1356 | | |
1357 | | /* |
1358 | | * Find the first quotient and remainder |
1359 | | */ |
1360 | | q1 = u1 / d1; |
1361 | | r0 = u1 - d1 * q1; |
1362 | | |
1363 | | while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) { |
1364 | | q1 -= 1; |
1365 | | r0 += d1; |
1366 | | |
1367 | | if (r0 >= radix) { |
1368 | | break; |
1369 | | } |
1370 | | } |
1371 | | |
1372 | | rAX = (u1 * radix) + (u0_msw - q1 * d); |
1373 | | q0 = rAX / d1; |
1374 | | r0 = rAX - q0 * d1; |
1375 | | |
1376 | | while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) { |
1377 | | q0 -= 1; |
1378 | | r0 += d1; |
1379 | | |
1380 | | if (r0 >= radix) { |
1381 | | break; |
1382 | | } |
1383 | | } |
1384 | | |
1385 | | if (r != NULL) { |
1386 | | *r = (rAX * radix + u0_lsw - q0 * d) >> s; |
1387 | | } |
1388 | | |
1389 | | quotient = q1 * radix + q0; |
1390 | | |
1391 | | return quotient; |
1392 | | #endif |
1393 | 1.10M | } |
1394 | | |
1395 | | /* |
1396 | | * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20) |
1397 | | */ |
1398 | | int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, |
1399 | | const mbedtls_mpi *B) |
1400 | 162k | { |
1401 | 162k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1402 | 162k | size_t i, n, t, k; |
1403 | 162k | mbedtls_mpi X, Y, Z, T1, T2; |
1404 | 162k | mbedtls_mpi_uint TP2[3]; |
1405 | | |
1406 | 162k | if (mbedtls_mpi_cmp_int(B, 0) == 0) { |
1407 | 0 | return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO; |
1408 | 0 | } |
1409 | | |
1410 | 162k | mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); |
1411 | 162k | mbedtls_mpi_init(&T1); |
1412 | | /* |
1413 | | * Avoid dynamic memory allocations for constant-size T2. |
1414 | | * |
1415 | | * T2 is used for comparison only and the 3 limbs are assigned explicitly, |
1416 | | * so nobody increase the size of the MPI and we're safe to use an on-stack |
1417 | | * buffer. |
1418 | | */ |
1419 | 162k | T2.s = 1; |
1420 | 162k | T2.n = sizeof(TP2) / sizeof(*TP2); |
1421 | 162k | T2.p = TP2; |
1422 | | |
1423 | 162k | if (mbedtls_mpi_cmp_abs(A, B) < 0) { |
1424 | 910 | if (Q != NULL) { |
1425 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0)); |
1426 | 0 | } |
1427 | 910 | if (R != NULL) { |
1428 | 910 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A)); |
1429 | 910 | } |
1430 | 910 | return 0; |
1431 | 910 | } |
1432 | | |
1433 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A)); |
1434 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B)); |
1435 | 162k | X.s = Y.s = 1; |
1436 | | |
1437 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2)); |
1438 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0)); |
1439 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2)); |
1440 | | |
1441 | 162k | k = mbedtls_mpi_bitlen(&Y) % biL; |
1442 | 162k | if (k < biL - 1) { |
1443 | 161k | k = biL - 1 - k; |
1444 | 161k | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k)); |
1445 | 161k | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k)); |
1446 | 161k | } else { |
1447 | 64 | k = 0; |
1448 | 64 | } |
1449 | | |
1450 | 162k | n = X.n - 1; |
1451 | 162k | t = Y.n - 1; |
1452 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t))); |
1453 | | |
1454 | 165k | while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) { |
1455 | 3.39k | Z.p[n - t]++; |
1456 | 3.39k | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y)); |
1457 | 3.39k | } |
1458 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t))); |
1459 | | |
1460 | 1.26M | for (i = n; i > t; i--) { |
1461 | 1.10M | if (X.p[i] >= Y.p[t]) { |
1462 | 256 | Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u; |
1463 | 1.10M | } else { |
1464 | 1.10M | Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1], |
1465 | 1.10M | Y.p[t], NULL); |
1466 | 1.10M | } |
1467 | | |
1468 | 1.10M | T2.p[0] = (i < 2) ? 0 : X.p[i - 2]; |
1469 | 1.10M | T2.p[1] = (i < 1) ? 0 : X.p[i - 1]; |
1470 | 1.10M | T2.p[2] = X.p[i]; |
1471 | | |
1472 | 1.10M | Z.p[i - t - 1]++; |
1473 | 1.76M | do { |
1474 | 1.76M | Z.p[i - t - 1]--; |
1475 | | |
1476 | 1.76M | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0)); |
1477 | 1.76M | T1.p[0] = (t < 1) ? 0 : Y.p[t - 1]; |
1478 | 1.76M | T1.p[1] = Y.p[t]; |
1479 | 1.76M | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1])); |
1480 | 1.76M | } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0); |
1481 | | |
1482 | 1.10M | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1])); |
1483 | 1.10M | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1))); |
1484 | 1.10M | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1)); |
1485 | | |
1486 | 1.10M | if (mbedtls_mpi_cmp_int(&X, 0) < 0) { |
1487 | 160 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y)); |
1488 | 160 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1))); |
1489 | 160 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1)); |
1490 | 160 | Z.p[i - t - 1]--; |
1491 | 160 | } |
1492 | 1.10M | } |
1493 | | |
1494 | 162k | if (Q != NULL) { |
1495 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z)); |
1496 | 0 | Q->s = A->s * B->s; |
1497 | 0 | } |
1498 | | |
1499 | 162k | if (R != NULL) { |
1500 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k)); |
1501 | 162k | X.s = A->s; |
1502 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X)); |
1503 | | |
1504 | 162k | if (mbedtls_mpi_cmp_int(R, 0) == 0) { |
1505 | 0 | R->s = 1; |
1506 | 0 | } |
1507 | 162k | } |
1508 | | |
1509 | 162k | cleanup: |
1510 | | |
1511 | 162k | mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); |
1512 | 162k | mbedtls_mpi_free(&T1); |
1513 | 162k | mbedtls_platform_zeroize(TP2, sizeof(TP2)); |
1514 | | |
1515 | 162k | return ret; |
1516 | 162k | } |
1517 | | |
1518 | | /* |
1519 | | * Division by int: A = Q * b + R |
1520 | | */ |
1521 | | int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, |
1522 | | const mbedtls_mpi *A, |
1523 | | mbedtls_mpi_sint b) |
1524 | 0 | { |
1525 | 0 | mbedtls_mpi B; |
1526 | 0 | mbedtls_mpi_uint p[1]; |
1527 | |
|
1528 | 0 | p[0] = mpi_sint_abs(b); |
1529 | 0 | B.s = TO_SIGN(b); |
1530 | 0 | B.n = 1; |
1531 | 0 | B.p = p; |
1532 | |
|
1533 | 0 | return mbedtls_mpi_div_mpi(Q, R, A, &B); |
1534 | 0 | } |
1535 | | |
1536 | | /* |
1537 | | * Modulo: R = A mod B |
1538 | | */ |
1539 | | int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1540 | 162k | { |
1541 | 162k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1542 | | |
1543 | 162k | if (mbedtls_mpi_cmp_int(B, 0) < 0) { |
1544 | 0 | return MBEDTLS_ERR_MPI_NEGATIVE_VALUE; |
1545 | 0 | } |
1546 | | |
1547 | 162k | MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B)); |
1548 | | |
1549 | 162k | while (mbedtls_mpi_cmp_int(R, 0) < 0) { |
1550 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B)); |
1551 | 0 | } |
1552 | | |
1553 | 162k | while (mbedtls_mpi_cmp_mpi(R, B) >= 0) { |
1554 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B)); |
1555 | 0 | } |
1556 | | |
1557 | 162k | cleanup: |
1558 | | |
1559 | 162k | return ret; |
1560 | 162k | } |
1561 | | |
1562 | | /* |
1563 | | * Modulo: r = A mod b |
1564 | | */ |
1565 | | int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
1566 | 0 | { |
1567 | 0 | size_t i; |
1568 | 0 | mbedtls_mpi_uint x, y, z; |
1569 | |
|
1570 | 0 | if (b == 0) { |
1571 | 0 | return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO; |
1572 | 0 | } |
1573 | | |
1574 | 0 | if (b < 0) { |
1575 | 0 | return MBEDTLS_ERR_MPI_NEGATIVE_VALUE; |
1576 | 0 | } |
1577 | | |
1578 | | /* |
1579 | | * handle trivial cases |
1580 | | */ |
1581 | 0 | if (b == 1 || A->n == 0) { |
1582 | 0 | *r = 0; |
1583 | 0 | return 0; |
1584 | 0 | } |
1585 | | |
1586 | 0 | if (b == 2) { |
1587 | 0 | *r = A->p[0] & 1; |
1588 | 0 | return 0; |
1589 | 0 | } |
1590 | | |
1591 | | /* |
1592 | | * general case |
1593 | | */ |
1594 | 0 | for (i = A->n, y = 0; i > 0; i--) { |
1595 | 0 | x = A->p[i - 1]; |
1596 | 0 | y = (y << biH) | (x >> biH); |
1597 | 0 | z = y / b; |
1598 | 0 | y -= z * b; |
1599 | |
|
1600 | 0 | x <<= biH; |
1601 | 0 | y = (y << biH) | (x >> biH); |
1602 | 0 | z = y / b; |
1603 | 0 | y -= z * b; |
1604 | 0 | } |
1605 | | |
1606 | | /* |
1607 | | * If A is negative, then the current y represents a negative value. |
1608 | | * Flipping it to the positive side. |
1609 | | */ |
1610 | 0 | if (A->s < 0 && y != 0) { |
1611 | 0 | y = b - y; |
1612 | 0 | } |
1613 | |
|
1614 | 0 | *r = y; |
1615 | |
|
1616 | 0 | return 0; |
1617 | 0 | } |
1618 | | |
1619 | | /* |
1620 | | * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value, |
1621 | | * this function is not constant time with respect to the exponent (parameter E). |
1622 | | */ |
1623 | | static int mbedtls_mpi_exp_mod_optionally_safe(mbedtls_mpi *X, const mbedtls_mpi *A, |
1624 | | const mbedtls_mpi *E, int E_public, |
1625 | | const mbedtls_mpi *N, mbedtls_mpi *prec_RR) |
1626 | 3.31k | { |
1627 | 3.31k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1628 | | |
1629 | 3.31k | if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) { |
1630 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1631 | 0 | } |
1632 | | |
1633 | 3.31k | if (mbedtls_mpi_cmp_int(E, 0) < 0) { |
1634 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1635 | 0 | } |
1636 | | |
1637 | 3.31k | if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS || |
1638 | 3.31k | mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) { |
1639 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1640 | 0 | } |
1641 | | |
1642 | | /* |
1643 | | * Ensure that the exponent that we are passing to the core is not NULL. |
1644 | | */ |
1645 | 3.31k | if (E->n == 0) { |
1646 | 0 | ret = mbedtls_mpi_lset(X, 1); |
1647 | 0 | return ret; |
1648 | 0 | } |
1649 | | |
1650 | | /* |
1651 | | * Allocate working memory for mbedtls_mpi_core_exp_mod() |
1652 | | */ |
1653 | 3.31k | size_t T_limbs = mbedtls_mpi_core_exp_mod_working_limbs(N->n, E->n); |
1654 | 3.31k | mbedtls_mpi_uint *T = (mbedtls_mpi_uint *) mbedtls_calloc(T_limbs, sizeof(mbedtls_mpi_uint)); |
1655 | 3.31k | if (T == NULL) { |
1656 | 0 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
1657 | 0 | } |
1658 | | |
1659 | 3.31k | mbedtls_mpi RR; |
1660 | 3.31k | mbedtls_mpi_init(&RR); |
1661 | | |
1662 | | /* |
1663 | | * If 1st call, pre-compute R^2 mod N |
1664 | | */ |
1665 | 3.31k | if (prec_RR == NULL || prec_RR->p == NULL) { |
1666 | 3.31k | MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N)); |
1667 | | |
1668 | 3.31k | if (prec_RR != NULL) { |
1669 | 772 | *prec_RR = RR; |
1670 | 772 | } |
1671 | 3.31k | } else { |
1672 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(prec_RR, N->n)); |
1673 | 0 | RR = *prec_RR; |
1674 | 0 | } |
1675 | | |
1676 | | /* |
1677 | | * To preserve constness we need to make a copy of A. Using X for this to |
1678 | | * save memory. |
1679 | | */ |
1680 | 3.31k | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A)); |
1681 | | |
1682 | | /* |
1683 | | * Compensate for negative A (and correct at the end). |
1684 | | */ |
1685 | 3.31k | X->s = 1; |
1686 | | |
1687 | | /* |
1688 | | * Make sure that X is in a form that is safe for consumption by |
1689 | | * the core functions. |
1690 | | * |
1691 | | * - The core functions will not touch the limbs of X above N->n. The |
1692 | | * result will be correct if those limbs are 0, which the mod call |
1693 | | * ensures. |
1694 | | * - Also, X must have at least as many limbs as N for the calls to the |
1695 | | * core functions. |
1696 | | */ |
1697 | 3.31k | if (mbedtls_mpi_cmp_mpi(X, N) >= 0) { |
1698 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N)); |
1699 | 0 | } |
1700 | 3.31k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, N->n)); |
1701 | | |
1702 | | /* |
1703 | | * Convert to and from Montgomery around mbedtls_mpi_core_exp_mod(). |
1704 | | */ |
1705 | 3.31k | { |
1706 | 3.31k | mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p); |
1707 | 3.31k | mbedtls_mpi_core_to_mont_rep(X->p, X->p, N->p, N->n, mm, RR.p, T); |
1708 | 3.31k | if (E_public == MBEDTLS_MPI_IS_PUBLIC) { |
1709 | 648 | mbedtls_mpi_core_exp_mod_unsafe(X->p, X->p, N->p, N->n, E->p, E->n, RR.p, T); |
1710 | 2.66k | } else { |
1711 | 2.66k | mbedtls_mpi_core_exp_mod(X->p, X->p, N->p, N->n, E->p, E->n, RR.p, T); |
1712 | 2.66k | } |
1713 | 3.31k | mbedtls_mpi_core_from_mont_rep(X->p, X->p, N->p, N->n, mm, T); |
1714 | 3.31k | } |
1715 | | |
1716 | | /* |
1717 | | * Correct for negative A. |
1718 | | */ |
1719 | 3.31k | if (A->s == -1 && (E->p[0] & 1) != 0) { |
1720 | 0 | mbedtls_ct_condition_t is_x_non_zero = mbedtls_mpi_core_check_zero_ct(X->p, X->n); |
1721 | 0 | X->s = mbedtls_ct_mpi_sign_if(is_x_non_zero, -1, 1); |
1722 | |
|
1723 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, N, X)); |
1724 | 0 | } |
1725 | | |
1726 | 3.31k | cleanup: |
1727 | | |
1728 | 3.31k | mbedtls_mpi_zeroize_and_free(T, T_limbs); |
1729 | | |
1730 | 3.31k | if (prec_RR == NULL || prec_RR->p == NULL) { |
1731 | 2.54k | mbedtls_mpi_free(&RR); |
1732 | 2.54k | } |
1733 | | |
1734 | 3.31k | return ret; |
1735 | 3.31k | } |
1736 | | |
1737 | | int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, |
1738 | | const mbedtls_mpi *E, const mbedtls_mpi *N, |
1739 | | mbedtls_mpi *prec_RR) |
1740 | 2.66k | { |
1741 | 2.66k | return mbedtls_mpi_exp_mod_optionally_safe(X, A, E, MBEDTLS_MPI_IS_SECRET, N, prec_RR); |
1742 | 2.66k | } |
1743 | | |
1744 | | int mbedtls_mpi_exp_mod_unsafe(mbedtls_mpi *X, const mbedtls_mpi *A, |
1745 | | const mbedtls_mpi *E, const mbedtls_mpi *N, |
1746 | | mbedtls_mpi *prec_RR) |
1747 | 648 | { |
1748 | 648 | return mbedtls_mpi_exp_mod_optionally_safe(X, A, E, MBEDTLS_MPI_IS_PUBLIC, N, prec_RR); |
1749 | 648 | } |
1750 | | |
1751 | | /* Constant-time GCD and/or modinv with odd modulus and A <= N */ |
1752 | | int mbedtls_mpi_gcd_modinv_odd(mbedtls_mpi *G, |
1753 | | mbedtls_mpi *I, |
1754 | | const mbedtls_mpi *A, |
1755 | | const mbedtls_mpi *N) |
1756 | 3.23k | { |
1757 | 3.23k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1758 | 3.23k | mbedtls_mpi local_g; |
1759 | 3.23k | mbedtls_mpi_uint *T = NULL; |
1760 | 3.23k | const size_t T_factor = I != NULL ? 5 : 4; |
1761 | 3.23k | const mbedtls_mpi_uint zero = 0; |
1762 | | |
1763 | | /* Check requirements on A and N */ |
1764 | 3.23k | if (mbedtls_mpi_cmp_int(A, 0) < 0 || |
1765 | 3.23k | mbedtls_mpi_cmp_mpi(A, N) > 0 || |
1766 | 3.23k | mbedtls_mpi_get_bit(N, 0) != 1 || |
1767 | 3.23k | (I != NULL && mbedtls_mpi_cmp_int(N, 1) == 0)) { |
1768 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1769 | 0 | } |
1770 | | |
1771 | | /* Check aliasing requirements */ |
1772 | 3.23k | if (A == N || (I != NULL && (I == N || G == N))) { |
1773 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1774 | 0 | } |
1775 | | |
1776 | 3.23k | mbedtls_mpi_init(&local_g); |
1777 | | |
1778 | 3.23k | if (G == NULL) { |
1779 | 3.23k | G = &local_g; |
1780 | 3.23k | } |
1781 | | |
1782 | | /* We can't modify the values of G or I before use in the main function, |
1783 | | * as they could be aliased to A or N. */ |
1784 | 3.23k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(G, N->n)); |
1785 | 3.23k | if (I != NULL) { |
1786 | 3.23k | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(I, N->n)); |
1787 | 3.23k | } |
1788 | | |
1789 | 3.23k | T = mbedtls_calloc(sizeof(mbedtls_mpi_uint) * N->n, T_factor); |
1790 | 3.23k | if (T == NULL) { |
1791 | 0 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
1792 | 0 | goto cleanup; |
1793 | 0 | } |
1794 | | |
1795 | 3.23k | mbedtls_mpi_uint *Ip = I != NULL ? I->p : NULL; |
1796 | | /* If A is 0 (null), then A->p would be null, and A->n would be 0, |
1797 | | * which would be an issue if A->p and A->n were passed to |
1798 | | * mbedtls_mpi_core_gcd_modinv_odd below. */ |
1799 | 3.23k | const mbedtls_mpi_uint *Ap = A->p != NULL ? A->p : &zero; |
1800 | 3.23k | size_t An = A->n >= N->n ? N->n : A->p != NULL ? A->n : 1; |
1801 | 3.23k | mbedtls_mpi_core_gcd_modinv_odd(G->p, Ip, Ap, An, N->p, N->n, T); |
1802 | | |
1803 | 3.23k | G->s = 1; |
1804 | 3.23k | if (I != NULL) { |
1805 | 3.23k | I->s = 1; |
1806 | 3.23k | } |
1807 | | |
1808 | 3.23k | if (G->n > N->n) { |
1809 | 0 | memset(G->p + N->n, 0, ciL * (G->n - N->n)); |
1810 | 0 | } |
1811 | 3.23k | if (I != NULL && I->n > N->n) { |
1812 | 1.19k | memset(I->p + N->n, 0, ciL * (I->n - N->n)); |
1813 | 1.19k | } |
1814 | | |
1815 | 3.23k | cleanup: |
1816 | 3.23k | mbedtls_mpi_free(&local_g); |
1817 | 3.23k | mbedtls_free(T); |
1818 | 3.23k | return ret; |
1819 | 3.23k | } |
1820 | | |
1821 | | /* |
1822 | | * Greatest common divisor: G = gcd(A, B) |
1823 | | * Wrapper around mbedtls_mpi_gcd_modinv() that removes its restrictions. |
1824 | | */ |
1825 | | int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B) |
1826 | 0 | { |
1827 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1828 | 0 | mbedtls_mpi TA, TB; |
1829 | |
|
1830 | 0 | mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB); |
1831 | | |
1832 | | /* Make copies and take absolute values */ |
1833 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); |
1834 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); |
1835 | 0 | TA.s = TB.s = 1; |
1836 | | |
1837 | | /* Make the two values the same (non-zero) number of limbs. |
1838 | | * This is needed to use mbedtls_mpi_core functions below. */ |
1839 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&TA, TB.n != 0 ? TB.n : 1)); |
1840 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&TB, TA.n)); // non-zero from above |
1841 | | |
1842 | | /* Handle special cases (that don't happen in crypto usage) */ |
1843 | 0 | if (mbedtls_mpi_core_check_zero_ct(TA.p, TA.n) == MBEDTLS_CT_FALSE) { |
1844 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB)); // GCD(0, B) = abs(B) |
1845 | 0 | goto cleanup; |
1846 | 0 | } |
1847 | 0 | if (mbedtls_mpi_core_check_zero_ct(TB.p, TB.n) == MBEDTLS_CT_FALSE) { |
1848 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TA)); // GCD(A, 0) = abs(A) |
1849 | 0 | goto cleanup; |
1850 | 0 | } |
1851 | | |
1852 | | /* Make boths inputs odd by putting powers of 2 on the side */ |
1853 | 0 | const size_t za = mbedtls_mpi_lsb(&TA); |
1854 | 0 | const size_t zb = mbedtls_mpi_lsb(&TB); |
1855 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, za)); |
1856 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, zb)); |
1857 | | |
1858 | | /* Ensure A <= B: if B < A, swap them */ |
1859 | 0 | mbedtls_ct_condition_t swap = mbedtls_mpi_core_lt_ct(TB.p, TA.p, TA.n); |
1860 | 0 | mbedtls_mpi_core_cond_swap(TA.p, TB.p, TA.n, swap); |
1861 | |
|
1862 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(G, NULL, &TA, &TB)); |
1863 | | |
1864 | | /* Re-inject the power of 2 we had previously put aside */ |
1865 | 0 | size_t zg = za > zb ? zb : za; // zg = min(za, zb) |
1866 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(G, zg)); |
1867 | | |
1868 | 0 | cleanup: |
1869 | |
|
1870 | 0 | mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB); |
1871 | |
|
1872 | 0 | return ret; |
1873 | 0 | } |
1874 | | |
1875 | | /* |
1876 | | * Fill X with size bytes of random. |
1877 | | * The bytes returned from the RNG are used in a specific order which |
1878 | | * is suitable for deterministic ECDSA (see the specification of |
1879 | | * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()). |
1880 | | */ |
1881 | | int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, |
1882 | | int (*f_rng)(void *, unsigned char *, size_t), |
1883 | | void *p_rng) |
1884 | 11 | { |
1885 | 11 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1886 | 11 | const size_t limbs = CHARS_TO_LIMBS(size); |
1887 | | |
1888 | | /* Ensure that target MPI has exactly the necessary number of limbs */ |
1889 | 11 | MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs)); |
1890 | 11 | if (size == 0) { |
1891 | 0 | return 0; |
1892 | 0 | } |
1893 | | |
1894 | 11 | ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng); |
1895 | | |
1896 | 11 | cleanup: |
1897 | 11 | return ret; |
1898 | 11 | } |
1899 | | |
1900 | | int mbedtls_mpi_random(mbedtls_mpi *X, |
1901 | | mbedtls_mpi_sint min, |
1902 | | const mbedtls_mpi *N, |
1903 | | int (*f_rng)(void *, unsigned char *, size_t), |
1904 | | void *p_rng) |
1905 | 1.78k | { |
1906 | 1.78k | if (min < 0) { |
1907 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1908 | 0 | } |
1909 | 1.78k | if (mbedtls_mpi_cmp_int(N, min) <= 0) { |
1910 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
1911 | 0 | } |
1912 | | |
1913 | | /* Ensure that target MPI has exactly the same number of limbs |
1914 | | * as the upper bound, even if the upper bound has leading zeros. |
1915 | | * This is necessary for mbedtls_mpi_core_random. */ |
1916 | 1.78k | int ret = mbedtls_mpi_resize_clear(X, N->n); |
1917 | 1.78k | if (ret != 0) { |
1918 | 0 | return ret; |
1919 | 0 | } |
1920 | | |
1921 | 1.78k | return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng); |
1922 | 1.78k | } |
1923 | | |
1924 | | /* |
1925 | | * Modular inverse: X = A^-1 mod N with N odd (and A any range) |
1926 | | */ |
1927 | | int mbedtls_mpi_inv_mod_odd(mbedtls_mpi *X, |
1928 | | const mbedtls_mpi *A, |
1929 | | const mbedtls_mpi *N) |
1930 | 0 | { |
1931 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1932 | 0 | mbedtls_mpi T, G; |
1933 | |
|
1934 | 0 | mbedtls_mpi_init(&T); |
1935 | 0 | mbedtls_mpi_init(&G); |
1936 | |
|
1937 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, A, N)); |
1938 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &T, &T, N)); |
1939 | 0 | if (mbedtls_mpi_cmp_int(&G, 1) != 0) { |
1940 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
1941 | 0 | goto cleanup; |
1942 | 0 | } |
1943 | | |
1944 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &T)); |
1945 | | |
1946 | 0 | cleanup: |
1947 | 0 | mbedtls_mpi_free(&T); |
1948 | 0 | mbedtls_mpi_free(&G); |
1949 | |
|
1950 | 0 | return ret; |
1951 | 0 | } |
1952 | | |
1953 | | /* |
1954 | | * Compute X = A^-1 mod N with N even, A odd and 1 < A < N. |
1955 | | * |
1956 | | * This is not obvious because our constant-time modinv function only works with |
1957 | | * an odd modulus, and here the modulus is even. The idea is that computing a |
1958 | | * a^-1 mod b is really just computing the u coefficient in the Bézout relation |
1959 | | * a*u + b*v = 1 (assuming gcd(a,b) = 1, i.e. the inverse exists). But if we know |
1960 | | * one of u, v in this relation then the other is easy to find. So we can |
1961 | | * actually start by computing N^-1 mod A with gives us "the wrong half" of the |
1962 | | * Bézout relation, from which we'll deduce the interesting half A^-1 mod N. |
1963 | | * |
1964 | | * Return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the inverse doesn't exist. |
1965 | | */ |
1966 | | int mbedtls_mpi_inv_mod_even_in_range(mbedtls_mpi *X, |
1967 | | mbedtls_mpi const *A, |
1968 | | mbedtls_mpi const *N) |
1969 | 0 | { |
1970 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1971 | 0 | mbedtls_mpi I, G; |
1972 | |
|
1973 | 0 | mbedtls_mpi_init(&I); |
1974 | 0 | mbedtls_mpi_init(&G); |
1975 | | |
1976 | | /* Set I = N^-1 mod A */ |
1977 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&I, N, A)); |
1978 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &I, &I, A)); |
1979 | 0 | if (mbedtls_mpi_cmp_int(&G, 1) != 0) { |
1980 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
1981 | 0 | goto cleanup; |
1982 | 0 | } |
1983 | | |
1984 | | /* We know N * I = 1 + k * A for some k, which we can easily compute |
1985 | | * as k = (N*I - 1) / A (we know there will be no remainder). */ |
1986 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&I, &I, N)); |
1987 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&I, &I, 1)); |
1988 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&G, NULL, &I, A)); |
1989 | | |
1990 | | /* Now we have a Bézout relation N * (previous value of I) - G * A = 1, |
1991 | | * so A^-1 mod N is -G mod N, which is N - G. |
1992 | | * Note that 0 < k < N since 0 < I < A, so G (k) is already in range. */ |
1993 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, N, &G)); |
1994 | | |
1995 | 0 | cleanup: |
1996 | 0 | mbedtls_mpi_free(&I); |
1997 | 0 | mbedtls_mpi_free(&G); |
1998 | 0 | return ret; |
1999 | 0 | } |
2000 | | |
2001 | | /* |
2002 | | * Compute X = A^-1 mod N with N even and A odd (but in any range). |
2003 | | * |
2004 | | * Return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the inverse doesn't exist. |
2005 | | */ |
2006 | | static int mbedtls_mpi_inv_mod_even(mbedtls_mpi *X, |
2007 | | mbedtls_mpi const *A, |
2008 | | mbedtls_mpi const *N) |
2009 | 0 | { |
2010 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2011 | 0 | mbedtls_mpi AA; |
2012 | |
|
2013 | 0 | mbedtls_mpi_init(&AA); |
2014 | | |
2015 | | /* Bring A in the range [0, N). */ |
2016 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&AA, A, N)); |
2017 | | |
2018 | | /* We know A >= 0 but the next function wants A > 1 */ |
2019 | 0 | int cmp = mbedtls_mpi_cmp_int(&AA, 1); |
2020 | 0 | if (cmp < 0) { // AA == 0 |
2021 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2022 | 0 | goto cleanup; |
2023 | 0 | } |
2024 | 0 | if (cmp == 0) { // AA = 1 |
2025 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1)); |
2026 | 0 | goto cleanup; |
2027 | 0 | } |
2028 | | |
2029 | | /* Now we know 1 < A < N, N is even and AA is still odd */ |
2030 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_even_in_range(X, &AA, N)); |
2031 | | |
2032 | 0 | cleanup: |
2033 | 0 | mbedtls_mpi_free(&AA); |
2034 | 0 | return ret; |
2035 | 0 | } |
2036 | | |
2037 | | /* |
2038 | | * Modular inverse: X = A^-1 mod N |
2039 | | * |
2040 | | * Wrapper around mbedtls_mpi_gcd_modinv_odd() that lifts its limitations. |
2041 | | */ |
2042 | | int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N) |
2043 | 0 | { |
2044 | 0 | if (mbedtls_mpi_cmp_int(N, 1) <= 0) { |
2045 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
2046 | 0 | } |
2047 | | |
2048 | 0 | if (mbedtls_mpi_get_bit(N, 0) == 1) { |
2049 | 0 | return mbedtls_mpi_inv_mod_odd(X, A, N); |
2050 | 0 | } |
2051 | | |
2052 | 0 | if (mbedtls_mpi_get_bit(A, 0) == 1) { |
2053 | 0 | return mbedtls_mpi_inv_mod_even(X, A, N); |
2054 | 0 | } |
2055 | | |
2056 | | /* If A and N are both even, 2 divides their GCD, so no inverse. */ |
2057 | 0 | return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2058 | 0 | } |
2059 | | |
2060 | | #if defined(MBEDTLS_GENPRIME) |
2061 | | |
2062 | | static const mbedtls_mpi_sint small_primes_limit = 997; |
2063 | | /* Product of small primes up to small_primes_limit included */ |
2064 | | static const mbedtls_mpi_uint small_primes_product_limbs[] = { |
2065 | | MBEDTLS_BYTES_TO_T_UINT_8(0x4b, 0x13, 0x6a, 0x97, 0xbb, 0xd0, 0xdf, 0x95), |
2066 | | MBEDTLS_BYTES_TO_T_UINT_8(0xa7, 0x2c, 0x10, 0xa4, 0x20, 0xa4, 0x9f, 0x7b), |
2067 | | MBEDTLS_BYTES_TO_T_UINT_8(0x9d, 0x18, 0xd6, 0xdf, 0xc0, 0xf5, 0x61, 0x65), |
2068 | | MBEDTLS_BYTES_TO_T_UINT_8(0xfc, 0x35, 0x79, 0xfb, 0x30, 0xa8, 0xd5, 0xbf), |
2069 | | MBEDTLS_BYTES_TO_T_UINT_8(0xdb, 0x37, 0xba, 0x2c, 0xfb, 0xbb, 0x89, 0xfb), |
2070 | | MBEDTLS_BYTES_TO_T_UINT_8(0xad, 0xc2, 0x8c, 0x1d, 0x99, 0x18, 0xe8, 0xe5), |
2071 | | MBEDTLS_BYTES_TO_T_UINT_8(0x8d, 0x77, 0xc9, 0x5d, 0x96, 0x8a, 0x61, 0x9d), |
2072 | | MBEDTLS_BYTES_TO_T_UINT_8(0x39, 0x41, 0x3e, 0xf4, 0x34, 0x07, 0x57, 0xe0), |
2073 | | MBEDTLS_BYTES_TO_T_UINT_8(0x4a, 0xf1, 0x54, 0x3a, 0x43, 0x67, 0x46, 0xa2), |
2074 | | MBEDTLS_BYTES_TO_T_UINT_8(0x83, 0x0c, 0xe5, 0x31, 0xa2, 0xfc, 0x05, 0x45), |
2075 | | MBEDTLS_BYTES_TO_T_UINT_8(0xf0, 0x1d, 0x66, 0xfc, 0x7a, 0x85, 0x37, 0xe1), |
2076 | | MBEDTLS_BYTES_TO_T_UINT_8(0x17, 0xe0, 0x80, 0x62, 0x0d, 0xa2, 0xbc, 0x32), |
2077 | | MBEDTLS_BYTES_TO_T_UINT_8(0x6d, 0xce, 0x84, 0x68, 0x00, 0xb5, 0xe3, 0x35), |
2078 | | MBEDTLS_BYTES_TO_T_UINT_8(0x14, 0x19, 0x0d, 0xe4, 0x92, 0xd5, 0xd8, 0xdf), |
2079 | | MBEDTLS_BYTES_TO_T_UINT_8(0x20, 0x1c, 0x7d, 0x38, 0x3b, 0xe8, 0xd9, 0xa8), |
2080 | | MBEDTLS_BYTES_TO_T_UINT_8(0xf6, 0xac, 0x11, 0xe6, 0xb4, 0x03, 0xf7, 0x6c), |
2081 | | MBEDTLS_BYTES_TO_T_UINT_8(0x78, 0x3d, 0xf2, 0x3a, 0x8f, 0xf9, 0x2f, 0x6a), |
2082 | | MBEDTLS_BYTES_TO_T_UINT_8(0x7b, 0x72, 0x66, 0xa9, 0x48, 0xe4, 0x6d, 0x02), |
2083 | | MBEDTLS_BYTES_TO_T_UINT_8(0x69, 0xc7, 0x32, 0xcb, 0xf2, 0xf7, 0xa9, 0x0b), |
2084 | | MBEDTLS_BYTES_TO_T_UINT_8(0xd3, 0x52, 0x72, 0x9d, 0xbf, 0x54, 0xea, 0xc7), |
2085 | | MBEDTLS_BYTES_TO_T_UINT_8(0xf5, 0xf3, 0x8a, 0xc5, 0xf0, 0xe1, 0x21, 0x81), |
2086 | | MBEDTLS_BYTES_TO_T_UINT_8(0x8e, 0x61, 0x78, 0xf0, 0x05, 0x00, 0x00, 0x00), |
2087 | | }; |
2088 | | /* Could make ECP_MPI_INIT_ARRAY() available outside ecp, but not doing it now |
2089 | | * as it would lead to conflicts with other in-flight PRs. */ |
2090 | | static const mbedtls_mpi small_primes_product = { |
2091 | | .p = (mbedtls_mpi_uint *) small_primes_product_limbs, |
2092 | | .s = 1, |
2093 | | .n = sizeof(small_primes_product_limbs) / sizeof(mbedtls_mpi_uint), |
2094 | | }; |
2095 | | |
2096 | | /* |
2097 | | * Small divisors test (X must be positive) |
2098 | | * |
2099 | | * Return values: |
2100 | | * 0: no small factor (possible prime, more tests needed) |
2101 | | * 1: certain prime |
2102 | | * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime |
2103 | | * other negative: error |
2104 | | */ |
2105 | | static int mpi_check_small_factors(const mbedtls_mpi *X) |
2106 | 0 | { |
2107 | 0 | int ret = 0; |
2108 | 0 | mbedtls_mpi g; |
2109 | |
|
2110 | 0 | mbedtls_mpi_init(&g); |
2111 | |
|
2112 | 0 | if ((X->p[0] & 1) == 0) { |
2113 | 0 | return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2114 | 0 | } |
2115 | | |
2116 | | /* The GCD test below only works if X > small_primes_limit. |
2117 | | * Below this limit, use trial division: numbers that small are of no |
2118 | | * interest for cryptography, so we don't care about performance or side |
2119 | | * channels. We're supporting them only for backwards compatibility, so |
2120 | | * let's not waste code size on those. */ |
2121 | 0 | if (mbedtls_mpi_cmp_int(X, small_primes_limit) <= 0) { |
2122 | 0 | mbedtls_mpi_uint x = X->p[0]; |
2123 | 0 | mbedtls_mpi_uint d = 2; |
2124 | 0 | while (x % d != 0) { |
2125 | 0 | ++d; |
2126 | 0 | } |
2127 | 0 | return x == d ? 1 : MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2128 | 0 | } |
2129 | | |
2130 | | /* We can't directly use mbedtls_mpi_gcd_modinv_odd() because we don't know |
2131 | | * if X is larger than prod or not (prod is 1380 bits). So, use this generic |
2132 | | * wrapper - it does a bit more than what we need (handles even inputs as |
2133 | | * well, while we know our inputs are both odd), but that's OK. */ |
2134 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&g, &small_primes_product, X)); |
2135 | | |
2136 | 0 | if (mbedtls_mpi_cmp_int(&g, 1) == 0) { |
2137 | | /* X is not divisible by a small prime */ |
2138 | 0 | ret = 0; |
2139 | 0 | } else { |
2140 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2141 | 0 | } |
2142 | |
|
2143 | 0 | cleanup: |
2144 | 0 | mbedtls_mpi_free(&g); |
2145 | 0 | return ret; |
2146 | 0 | } |
2147 | | |
2148 | | /* |
2149 | | * Miller-Rabin pseudo-primality test (HAC 4.24) |
2150 | | */ |
2151 | | static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds, |
2152 | | int (*f_rng)(void *, unsigned char *, size_t), |
2153 | | void *p_rng) |
2154 | 0 | { |
2155 | 0 | int ret, count; |
2156 | 0 | size_t i, j, k, s; |
2157 | 0 | mbedtls_mpi W, R, T, A, RR; |
2158 | |
|
2159 | 0 | mbedtls_mpi_init(&W); mbedtls_mpi_init(&R); |
2160 | 0 | mbedtls_mpi_init(&T); mbedtls_mpi_init(&A); |
2161 | 0 | mbedtls_mpi_init(&RR); |
2162 | | |
2163 | | /* |
2164 | | * W = |X| - 1 |
2165 | | * R = W >> lsb( W ) |
2166 | | */ |
2167 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1)); |
2168 | 0 | s = mbedtls_mpi_lsb(&W); |
2169 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W)); |
2170 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s)); |
2171 | | |
2172 | 0 | for (i = 0; i < rounds; i++) { |
2173 | | /* |
2174 | | * pick a random A, 1 < A < |X| - 1 |
2175 | | */ |
2176 | 0 | count = 0; |
2177 | 0 | do { |
2178 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng)); |
2179 | | |
2180 | 0 | j = mbedtls_mpi_bitlen(&A); |
2181 | 0 | k = mbedtls_mpi_bitlen(&W); |
2182 | 0 | if (j > k) { |
2183 | 0 | A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1; |
2184 | 0 | } |
2185 | |
|
2186 | 0 | if (count++ > 30) { |
2187 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2188 | 0 | goto cleanup; |
2189 | 0 | } |
2190 | |
|
2191 | 0 | } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 || |
2192 | 0 | mbedtls_mpi_cmp_int(&A, 1) <= 0); |
2193 | | |
2194 | | /* |
2195 | | * A = A^R mod |X| |
2196 | | */ |
2197 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR)); |
2198 | | |
2199 | 0 | if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 || |
2200 | 0 | mbedtls_mpi_cmp_int(&A, 1) == 0) { |
2201 | 0 | continue; |
2202 | 0 | } |
2203 | | |
2204 | 0 | j = 1; |
2205 | 0 | while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) { |
2206 | | /* |
2207 | | * A = A * A mod |X| |
2208 | | */ |
2209 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A)); |
2210 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X)); |
2211 | | |
2212 | 0 | if (mbedtls_mpi_cmp_int(&A, 1) == 0) { |
2213 | 0 | break; |
2214 | 0 | } |
2215 | | |
2216 | 0 | j++; |
2217 | 0 | } |
2218 | | |
2219 | | /* |
2220 | | * not prime if A != |X| - 1 or A == 1 |
2221 | | */ |
2222 | 0 | if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 || |
2223 | 0 | mbedtls_mpi_cmp_int(&A, 1) == 0) { |
2224 | 0 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2225 | 0 | break; |
2226 | 0 | } |
2227 | 0 | } |
2228 | | |
2229 | 0 | cleanup: |
2230 | 0 | mbedtls_mpi_free(&W); mbedtls_mpi_free(&R); |
2231 | 0 | mbedtls_mpi_free(&T); mbedtls_mpi_free(&A); |
2232 | 0 | mbedtls_mpi_free(&RR); |
2233 | |
|
2234 | 0 | return ret; |
2235 | 0 | } |
2236 | | |
2237 | | /* |
2238 | | * Pseudo-primality test: small factors, then Miller-Rabin |
2239 | | */ |
2240 | | int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds, |
2241 | | int (*f_rng)(void *, unsigned char *, size_t), |
2242 | | void *p_rng) |
2243 | 0 | { |
2244 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
2245 | 0 | mbedtls_mpi XX; |
2246 | |
|
2247 | 0 | XX.s = 1; |
2248 | 0 | XX.n = X->n; |
2249 | 0 | XX.p = X->p; |
2250 | |
|
2251 | 0 | if (mbedtls_mpi_cmp_int(&XX, 0) == 0 || |
2252 | 0 | mbedtls_mpi_cmp_int(&XX, 1) == 0) { |
2253 | 0 | return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2254 | 0 | } |
2255 | | |
2256 | 0 | if (mbedtls_mpi_cmp_int(&XX, 2) == 0) { |
2257 | 0 | return 0; |
2258 | 0 | } |
2259 | | |
2260 | 0 | if ((ret = mpi_check_small_factors(&XX)) != 0) { |
2261 | 0 | if (ret == 1) { |
2262 | 0 | return 0; |
2263 | 0 | } |
2264 | | |
2265 | 0 | return ret; |
2266 | 0 | } |
2267 | | |
2268 | 0 | return mpi_miller_rabin(&XX, rounds, f_rng, p_rng); |
2269 | 0 | } |
2270 | | |
2271 | | /* |
2272 | | * Prime number generation |
2273 | | * |
2274 | | * To generate an RSA key in a way recommended by FIPS 186-4, both primes must |
2275 | | * be either 1024 bits or 1536 bits long, and flags must contain |
2276 | | * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR. |
2277 | | */ |
2278 | | int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags, |
2279 | | int (*f_rng)(void *, unsigned char *, size_t), |
2280 | | void *p_rng) |
2281 | 0 | { |
2282 | 0 | #ifdef MBEDTLS_HAVE_INT64 |
2283 | | // ceil(2^63.5) |
2284 | 0 | #define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL |
2285 | | #else |
2286 | | // ceil(2^31.5) |
2287 | | #define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U |
2288 | | #endif |
2289 | 0 | int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
2290 | 0 | size_t k, n; |
2291 | 0 | int rounds; |
2292 | 0 | mbedtls_mpi_uint r; |
2293 | 0 | mbedtls_mpi Y; |
2294 | |
|
2295 | 0 | if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) { |
2296 | 0 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
2297 | 0 | } |
2298 | | |
2299 | 0 | mbedtls_mpi_init(&Y); |
2300 | |
|
2301 | 0 | n = BITS_TO_LIMBS(nbits); |
2302 | |
|
2303 | 0 | if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) { |
2304 | | /* |
2305 | | * 2^-80 error probability, number of rounds chosen per HAC, table 4.4 |
2306 | | */ |
2307 | 0 | rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 : |
2308 | 0 | (nbits >= 650) ? 4 : (nbits >= 350) ? 8 : |
2309 | 0 | (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27); |
2310 | 0 | } else { |
2311 | | /* |
2312 | | * 2^-100 error probability, number of rounds computed based on HAC, |
2313 | | * fact 4.48 |
2314 | | */ |
2315 | 0 | rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 : |
2316 | 0 | (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 : |
2317 | 0 | (nbits >= 750) ? 8 : (nbits >= 500) ? 13 : |
2318 | 0 | (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51); |
2319 | 0 | } |
2320 | |
|
2321 | 0 | while (1) { |
2322 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng)); |
2323 | | /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */ |
2324 | 0 | if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) { |
2325 | 0 | continue; |
2326 | 0 | } |
2327 | | |
2328 | 0 | k = n * biL; |
2329 | 0 | if (k > nbits) { |
2330 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits)); |
2331 | 0 | } |
2332 | 0 | X->p[0] |= 1; |
2333 | |
|
2334 | 0 | if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) { |
2335 | 0 | ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng); |
2336 | |
|
2337 | 0 | if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { |
2338 | 0 | goto cleanup; |
2339 | 0 | } |
2340 | 0 | } else { |
2341 | | /* |
2342 | | * A necessary condition for Y and X = 2Y + 1 to be prime |
2343 | | * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3). |
2344 | | * Make sure it is satisfied, while keeping X = 3 mod 4 |
2345 | | */ |
2346 | |
|
2347 | 0 | X->p[0] |= 2; |
2348 | |
|
2349 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3)); |
2350 | 0 | if (r == 0) { |
2351 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8)); |
2352 | 0 | } else if (r == 1) { |
2353 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4)); |
2354 | 0 | } |
2355 | | |
2356 | | /* Set Y = (X-1) / 2, which is X / 2 because X is odd */ |
2357 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X)); |
2358 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1)); |
2359 | | |
2360 | 0 | while (1) { |
2361 | | /* |
2362 | | * First, check small factors for X and Y |
2363 | | * before doing Miller-Rabin on any of them |
2364 | | */ |
2365 | 0 | if ((ret = mpi_check_small_factors(X)) == 0 && |
2366 | 0 | (ret = mpi_check_small_factors(&Y)) == 0 && |
2367 | 0 | (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng)) |
2368 | 0 | == 0 && |
2369 | 0 | (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng)) |
2370 | 0 | == 0) { |
2371 | 0 | goto cleanup; |
2372 | 0 | } |
2373 | | |
2374 | 0 | if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { |
2375 | 0 | goto cleanup; |
2376 | 0 | } |
2377 | | |
2378 | | /* |
2379 | | * Next candidates. We want to preserve Y = (X-1) / 2 and |
2380 | | * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3) |
2381 | | * so up Y by 6 and X by 12. |
2382 | | */ |
2383 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12)); |
2384 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6)); |
2385 | 0 | } |
2386 | 0 | } |
2387 | 0 | } |
2388 | | |
2389 | 0 | cleanup: |
2390 | |
|
2391 | 0 | mbedtls_mpi_free(&Y); |
2392 | |
|
2393 | 0 | return ret; |
2394 | 0 | } |
2395 | | |
2396 | | #endif /* MBEDTLS_GENPRIME */ |
2397 | | |
2398 | | #if defined(MBEDTLS_SELF_TEST) |
2399 | | |
2400 | 0 | #define GCD_PAIR_COUNT 3 |
2401 | | |
2402 | | static const int gcd_pairs[GCD_PAIR_COUNT][3] = |
2403 | | { |
2404 | | { 693, 609, 21 }, |
2405 | | { 1764, 868, 28 }, |
2406 | | { 768454923, 542167814, 1 } |
2407 | | }; |
2408 | | |
2409 | | /* |
2410 | | * Checkup routine |
2411 | | */ |
2412 | | int mbedtls_mpi_self_test(int verbose) |
2413 | 0 | { |
2414 | 0 | int ret, i; |
2415 | 0 | mbedtls_mpi A, E, N, X, Y, U, V; |
2416 | |
|
2417 | 0 | mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X); |
2418 | 0 | mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V); |
2419 | |
|
2420 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16, |
2421 | 0 | "EFE021C2645FD1DC586E69184AF4A31E" \ |
2422 | 0 | "D5F53E93B5F123FA41680867BA110131" \ |
2423 | 0 | "944FE7952E2517337780CB0DB80E61AA" \ |
2424 | 0 | "E7C8DDC6C5C6AADEB34EB38A2F40D5E6")); |
2425 | | |
2426 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16, |
2427 | 0 | "B2E7EFD37075B9F03FF989C7C5051C20" \ |
2428 | 0 | "34D2A323810251127E7BF8625A4F49A5" \ |
2429 | 0 | "F3E27F4DA8BD59C47D6DAABA4C8127BD" \ |
2430 | 0 | "5B5C25763222FEFCCFC38B832366C29E")); |
2431 | | |
2432 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16, |
2433 | 0 | "0066A198186C18C10B2F5ED9B522752A" \ |
2434 | 0 | "9830B69916E535C8F047518A889A43A5" \ |
2435 | 0 | "94B6BED27A168D31D4A52F88925AA8F5")); |
2436 | | |
2437 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N)); |
2438 | | |
2439 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16, |
2440 | 0 | "602AB7ECA597A3D6B56FF9829A5E8B85" \ |
2441 | 0 | "9E857EA95A03512E2BAE7391688D264A" \ |
2442 | 0 | "A5663B0341DB9CCFD2C4C5F421FEC814" \ |
2443 | 0 | "8001B72E848A38CAE1C65F78E56ABDEF" \ |
2444 | 0 | "E12D3C039B8A02D6BE593F0BBBDA56F1" \ |
2445 | 0 | "ECF677152EF804370C1A305CAF3B5BF1" \ |
2446 | 0 | "30879B56C61DE584A0F53A2447A51E")); |
2447 | | |
2448 | 0 | if (verbose != 0) { |
2449 | 0 | mbedtls_printf(" MPI test #1 (mul_mpi): "); |
2450 | 0 | } |
2451 | |
|
2452 | 0 | if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { |
2453 | 0 | if (verbose != 0) { |
2454 | 0 | mbedtls_printf("failed\n"); |
2455 | 0 | } |
2456 | |
|
2457 | 0 | ret = 1; |
2458 | 0 | goto cleanup; |
2459 | 0 | } |
2460 | | |
2461 | 0 | if (verbose != 0) { |
2462 | 0 | mbedtls_printf("passed\n"); |
2463 | 0 | } |
2464 | |
|
2465 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N)); |
2466 | | |
2467 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16, |
2468 | 0 | "256567336059E52CAE22925474705F39A94")); |
2469 | | |
2470 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16, |
2471 | 0 | "6613F26162223DF488E9CD48CC132C7A" \ |
2472 | 0 | "0AC93C701B001B092E4E5B9F73BCD27B" \ |
2473 | 0 | "9EE50D0657C77F374E903CDFA4C642")); |
2474 | | |
2475 | 0 | if (verbose != 0) { |
2476 | 0 | mbedtls_printf(" MPI test #2 (div_mpi): "); |
2477 | 0 | } |
2478 | |
|
2479 | 0 | if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 || |
2480 | 0 | mbedtls_mpi_cmp_mpi(&Y, &V) != 0) { |
2481 | 0 | if (verbose != 0) { |
2482 | 0 | mbedtls_printf("failed\n"); |
2483 | 0 | } |
2484 | |
|
2485 | 0 | ret = 1; |
2486 | 0 | goto cleanup; |
2487 | 0 | } |
2488 | | |
2489 | 0 | if (verbose != 0) { |
2490 | 0 | mbedtls_printf("passed\n"); |
2491 | 0 | } |
2492 | |
|
2493 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL)); |
2494 | | |
2495 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16, |
2496 | 0 | "36E139AEA55215609D2816998ED020BB" \ |
2497 | 0 | "BD96C37890F65171D948E9BC7CBAA4D9" \ |
2498 | 0 | "325D24D6A3C12710F10A09FA08AB87")); |
2499 | | |
2500 | 0 | if (verbose != 0) { |
2501 | 0 | mbedtls_printf(" MPI test #3 (exp_mod): "); |
2502 | 0 | } |
2503 | |
|
2504 | 0 | if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { |
2505 | 0 | if (verbose != 0) { |
2506 | 0 | mbedtls_printf("failed\n"); |
2507 | 0 | } |
2508 | |
|
2509 | 0 | ret = 1; |
2510 | 0 | goto cleanup; |
2511 | 0 | } |
2512 | | |
2513 | 0 | if (verbose != 0) { |
2514 | 0 | mbedtls_printf("passed\n"); |
2515 | 0 | } |
2516 | |
|
2517 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N)); |
2518 | | |
2519 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16, |
2520 | 0 | "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \ |
2521 | 0 | "C3DBA76456363A10869622EAC2DD84EC" \ |
2522 | 0 | "C5B8A74DAC4D09E03B5E0BE779F2DF61")); |
2523 | | |
2524 | 0 | if (verbose != 0) { |
2525 | 0 | mbedtls_printf(" MPI test #4 (inv_mod): "); |
2526 | 0 | } |
2527 | |
|
2528 | 0 | if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { |
2529 | 0 | if (verbose != 0) { |
2530 | 0 | mbedtls_printf("failed\n"); |
2531 | 0 | } |
2532 | |
|
2533 | 0 | ret = 1; |
2534 | 0 | goto cleanup; |
2535 | 0 | } |
2536 | | |
2537 | 0 | if (verbose != 0) { |
2538 | 0 | mbedtls_printf("passed\n"); |
2539 | 0 | } |
2540 | |
|
2541 | 0 | if (verbose != 0) { |
2542 | 0 | mbedtls_printf(" MPI test #5 (simple gcd): "); |
2543 | 0 | } |
2544 | |
|
2545 | 0 | for (i = 0; i < GCD_PAIR_COUNT; i++) { |
2546 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0])); |
2547 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1])); |
2548 | | |
2549 | 0 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y)); |
2550 | | |
2551 | 0 | if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) { |
2552 | 0 | if (verbose != 0) { |
2553 | 0 | mbedtls_printf("failed at %d\n", i); |
2554 | 0 | } |
2555 | |
|
2556 | 0 | ret = 1; |
2557 | 0 | goto cleanup; |
2558 | 0 | } |
2559 | 0 | } |
2560 | | |
2561 | 0 | if (verbose != 0) { |
2562 | 0 | mbedtls_printf("passed\n"); |
2563 | 0 | } |
2564 | |
|
2565 | 0 | cleanup: |
2566 | |
|
2567 | 0 | if (ret != 0 && verbose != 0) { |
2568 | 0 | mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret); |
2569 | 0 | } |
2570 | |
|
2571 | 0 | mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X); |
2572 | 0 | mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V); |
2573 | |
|
2574 | 0 | if (verbose != 0) { |
2575 | 0 | mbedtls_printf("\n"); |
2576 | 0 | } |
2577 | |
|
2578 | 0 | return ret; |
2579 | 0 | } |
2580 | | |
2581 | | #endif /* MBEDTLS_SELF_TEST */ |
2582 | | |
2583 | | #endif /* MBEDTLS_BIGNUM_C */ |