Mercurial > dropbear
comparison libtommath/bn_mp_export.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_EXPORT_C | 2 #ifdef BN_MP_EXPORT_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_export. | 15 /* based on gmp's mpz_export. |
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_export(void* rop, size_t* countp, int order, size_t size, | 18 int mp_export(void *rop, size_t *countp, int order, size_t size, |
22 int endian, size_t nails, mp_int* op) { | 19 int endian, size_t nails, const mp_int *op) |
23 int result; | 20 { |
24 size_t odd_nails, nail_bytes, i, j, bits, count; | 21 int result; |
25 unsigned char odd_nail_mask; | 22 size_t odd_nails, nail_bytes, i, j, bits, count; |
23 unsigned char odd_nail_mask; | |
26 | 24 |
27 mp_int t; | 25 mp_int t; |
28 | 26 |
29 if ((result = mp_init_copy(&t, op)) != MP_OKAY) { | 27 if ((result = mp_init_copy(&t, op)) != MP_OKAY) { |
30 return result; | 28 return result; |
31 } | 29 } |
32 | 30 |
33 if (endian == 0) { | 31 if (endian == 0) { |
34 union { | 32 union { |
35 unsigned int i; | 33 unsigned int i; |
36 char c[4]; | 34 char c[4]; |
37 } lint; | 35 } lint; |
38 lint.i = 0x01020304; | 36 lint.i = 0x01020304; |
39 | 37 |
40 endian = (lint.c[0] == 4) ? -1 : 1; | 38 endian = (lint.c[0] == '\x04') ? -1 : 1; |
41 } | 39 } |
42 | 40 |
43 odd_nails = (nails % 8); | 41 odd_nails = (nails % 8u); |
44 odd_nail_mask = 0xff; | 42 odd_nail_mask = 0xff; |
45 for (i = 0; i < odd_nails; ++i) { | 43 for (i = 0; i < odd_nails; ++i) { |
46 odd_nail_mask ^= (1 << (7 - i)); | 44 odd_nail_mask ^= (unsigned char)(1u << (7u - i)); |
47 } | 45 } |
48 nail_bytes = nails / 8; | 46 nail_bytes = nails / 8u; |
49 | 47 |
50 bits = mp_count_bits(&t); | 48 bits = (size_t)mp_count_bits(&t); |
51 count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0); | 49 count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u); |
52 | 50 |
53 for (i = 0; i < count; ++i) { | 51 for (i = 0; i < count; ++i) { |
54 for (j = 0; j < size; ++j) { | 52 for (j = 0; j < size; ++j) { |
55 unsigned char* byte = ( | 53 unsigned char *byte = (unsigned char *)rop + |
56 (unsigned char*)rop + | 54 (((order == -1) ? i : ((count - 1u) - i)) * size) + |
57 (((order == -1) ? i : ((count - 1) - i)) * size) + | 55 ((endian == -1) ? j : ((size - 1u) - j)); |
58 ((endian == -1) ? j : ((size - 1) - j)) | |
59 ); | |
60 | 56 |
61 if (j >= (size - nail_bytes)) { | 57 if (j >= (size - nail_bytes)) { |
62 *byte = 0; | 58 *byte = 0; |
63 continue; | 59 continue; |
64 } | 60 } |
65 | 61 |
66 *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF)); | 62 *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); |
67 | 63 |
68 if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) { | 64 if ((result = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) { |
69 mp_clear(&t); | 65 mp_clear(&t); |
70 return result; | 66 return result; |
71 } | 67 } |
72 } | 68 } |
73 } | 69 } |
74 | 70 |
75 mp_clear(&t); | 71 mp_clear(&t); |
76 | 72 |
77 if (countp != NULL) { | 73 if (countp != NULL) { |
78 *countp = count; | 74 *countp = count; |
79 } | 75 } |
80 | 76 |
81 return MP_OKAY; | 77 return MP_OKAY; |
82 } | 78 } |
83 | 79 |
84 #endif | 80 #endif |
85 | 81 |
86 /* ref: $Format:%D$ */ | 82 /* ref: HEAD -> master, tag: v1.1.0 */ |
87 /* git commit: $Format:%H$ */ | 83 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
88 /* commit time: $Format:%ai$ */ | 84 /* commit time: 2019-01-28 20:32:32 +0100 */ |