comparison libtommath/tommath_private.h @ 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 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
5 * 5 *
6 * The library was designed directly after the MPI library by 6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with 7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place. 8 * additional optimizations in place.
9 * 9 *
10 * The library is free for all purposes without any express 10 * SPDX-License-Identifier: Unlicense
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.com
14 */ 11 */
15 #ifndef TOMMATH_PRIV_H_ 12 #ifndef TOMMATH_PRIV_H_
16 #define TOMMATH_PRIV_H_ 13 #define TOMMATH_PRIV_H_
17 14
18 #include <tommath.h> 15 #include "tommath.h"
19 #include <ctype.h> 16 #include <ctype.h>
20 17
21 #ifndef MIN 18 #ifndef MIN
22 #define MIN(x,y) (((x) < (y)) ? (x) : (y)) 19 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
23 #endif 20 #endif
24 21
25 #ifndef MAX 22 #ifndef MAX
26 #define MAX(x,y) (((x) > (y)) ? (x) : (y)) 23 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
27 #endif 24 #endif
28 25
29 #ifdef __cplusplus 26 #ifdef __cplusplus
30 extern "C" { 27 extern "C" {
31 28
32 /* C++ compilers don't like assigning void * to mp_digit * */ 29 /* C++ compilers don't like assigning void * to mp_digit * */
33 #define OPT_CAST(x) (x *) 30 #define OPT_CAST(x) (x *)
34 31
35 #else 32 #else
36 33
37 /* C on the other hand doesn't care */ 34 /* C on the other hand doesn't care */
38 #define OPT_CAST(x) 35 #define OPT_CAST(x)
39 36
40 #endif 37 #endif
41 38
42 /* define heap macros */ 39 /* define heap macros */
43 #ifndef XMALLOC 40 #ifndef XMALLOC
44 /* default to libc stuff */ 41 /* default to libc stuff */
45 #define XMALLOC malloc 42 # define XMALLOC malloc
46 #define XFREE free 43 # define XFREE free
47 #define XREALLOC realloc 44 # define XREALLOC realloc
48 #define XCALLOC calloc 45 # define XCALLOC calloc
49 #else 46 #else
50 /* prototypes for our heap functions */ 47 /* prototypes for our heap functions */
51 extern void *XMALLOC(size_t n); 48 extern void *XMALLOC(size_t n);
52 extern void *XREALLOC(void *p, size_t n); 49 extern void *XREALLOC(void *p, size_t n);
53 extern void *XCALLOC(size_t n, size_t s); 50 extern void *XCALLOC(size_t n, size_t s);
54 extern void XFREE(void *p); 51 extern void XFREE(void *p);
55 #endif 52 #endif
56 53
57 /* lowlevel functions, do not call! */ 54 /* lowlevel functions, do not call! */
58 int s_mp_add(mp_int *a, mp_int *b, mp_int *c); 55 int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
59 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c); 56 int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
60 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) 57 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
61 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 58 int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
62 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 59 int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
63 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 60 int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
64 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 61 int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
65 int fast_s_mp_sqr(mp_int *a, mp_int *b); 62 int fast_s_mp_sqr(const mp_int *a, mp_int *b);
66 int s_mp_sqr(mp_int *a, mp_int *b); 63 int s_mp_sqr(const mp_int *a, mp_int *b);
67 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c); 64 int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
68 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c); 65 int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
69 int mp_karatsuba_sqr(mp_int *a, mp_int *b); 66 int mp_karatsuba_sqr(const mp_int *a, mp_int *b);
70 int mp_toom_sqr(mp_int *a, mp_int *b); 67 int mp_toom_sqr(const mp_int *a, mp_int *b);
71 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c); 68 int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
72 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); 69 int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
73 int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho); 70 int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
74 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode); 71 int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
75 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode); 72 int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
76 void bn_reverse(unsigned char *s, int len); 73 void bn_reverse(unsigned char *s, int len);
77 74
78 extern const char *mp_s_rmap; 75 extern const char *const mp_s_rmap;
76 extern const uint8_t mp_s_rmap_reverse[];
77 extern const size_t mp_s_rmap_reverse_sz;
79 78
80 /* Fancy macro to set an MPI from another type. 79 /* Fancy macro to set an MPI from another type.
81 * There are several things assumed: 80 * There are several things assumed:
82 * x is the counter and unsigned 81 * x is the counter and unsigned
83 * a is the pointer to the MPI 82 * a is the pointer to the MPI
97 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ 96 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \
98 return res; \ 97 return res; \
99 } \ 98 } \
100 \ 99 \
101 /* OR in the top four bits of the source */ \ 100 /* OR in the top four bits of the source */ \
102 a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \ 101 a->dp[0] |= (mp_digit)(b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\
103 \ 102 \
104 /* shift the source up to the next four bits */ \ 103 /* shift the source up to the next four bits */ \
105 b <<= 4; \ 104 b <<= 4; \
106 \ 105 \
107 /* ensure that digits are not clamped off */ \ 106 /* ensure that digits are not clamped off */ \
110 mp_clamp (a); \ 109 mp_clamp (a); \
111 return MP_OKAY; \ 110 return MP_OKAY; \
112 } 111 }
113 112
114 #ifdef __cplusplus 113 #ifdef __cplusplus
115 } 114 }
116 #endif 115 #endif
117 116
118 #endif 117 #endif
119 118
120 119
121 /* ref: $Format:%D$ */ 120 /* ref: HEAD -> master, tag: v1.1.0 */
122 /* git commit: $Format:%H$ */ 121 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
123 /* commit time: $Format:%ai$ */ 122 /* commit time: 2019-01-28 20:32:32 +0100 */