comparison libtommath/bn_mp_import.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_IMPORT_C 2 #ifdef BN_MP_IMPORT_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* based on gmp's mpz_import. 15 /* based on gmp's mpz_import.
19 * see http://gmplib.org/manual/Integer-Import-and-Export.html 16 * see http://gmplib.org/manual/Integer-Import-and-Export.html
20 */ 17 */
21 int mp_import(mp_int* rop, size_t count, int order, size_t size, 18 int mp_import(mp_int *rop, size_t count, int order, size_t size,
22 int endian, size_t nails, const void* op) { 19 int endian, size_t nails, const void *op)
23 int result; 20 {
24 size_t odd_nails, nail_bytes, i, j; 21 int result;
25 unsigned char odd_nail_mask; 22 size_t odd_nails, nail_bytes, i, j;
23 unsigned char odd_nail_mask;
26 24
27 mp_zero(rop); 25 mp_zero(rop);
28 26
29 if (endian == 0) { 27 if (endian == 0) {
30 union { 28 union {
31 unsigned int i; 29 unsigned int i;
32 char c[4]; 30 char c[4];
33 } lint; 31 } lint;
34 lint.i = 0x01020304; 32 lint.i = 0x01020304;
35 33
36 endian = (lint.c[0] == 4) ? -1 : 1; 34 endian = (lint.c[0] == '\x04') ? -1 : 1;
37 } 35 }
38 36
39 odd_nails = (nails % 8); 37 odd_nails = (nails % 8u);
40 odd_nail_mask = 0xff; 38 odd_nail_mask = 0xff;
41 for (i = 0; i < odd_nails; ++i) { 39 for (i = 0; i < odd_nails; ++i) {
42 odd_nail_mask ^= (1 << (7 - i)); 40 odd_nail_mask ^= (unsigned char)(1u << (7u - i));
43 } 41 }
44 nail_bytes = nails / 8; 42 nail_bytes = nails / 8u;
45 43
46 for (i = 0; i < count; ++i) { 44 for (i = 0; i < count; ++i) {
47 for (j = 0; j < (size - nail_bytes); ++j) { 45 for (j = 0; j < (size - nail_bytes); ++j) {
48 unsigned char byte = *( 46 unsigned char byte = *((unsigned char *)op +
49 (unsigned char*)op + 47 (((order == 1) ? i : ((count - 1u) - i)) * size) +
50 (((order == 1) ? i : ((count - 1) - i)) * size) + 48 ((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
51 ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
52 );
53 49
54 if ( 50 if ((result = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
55 (result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) { 51 return result;
56 return result; 52 }
57 }
58 53
59 rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte; 54 rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte;
60 rop->used += 1; 55 rop->used += 1;
61 } 56 }
62 } 57 }
63 58
64 mp_clamp(rop); 59 mp_clamp(rop);
65 60
66 return MP_OKAY; 61 return MP_OKAY;
67 } 62 }
68 63
69 #endif 64 #endif
70 65
71 /* ref: $Format:%D$ */ 66 /* ref: HEAD -> master, tag: v1.1.0 */
72 /* git commit: $Format:%H$ */ 67 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
73 /* commit time: $Format:%ai$ */ 68 /* commit time: 2019-01-28 20:32:32 +0100 */