libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / hcrypto / libtommath / mtest / mpi.h
blob9a9cc410180a6ac8313b7163b541fb10d253aaa8
1 /*
2 mpi.h
4 by Michael J. Fromberger <sting@linguist.dartmouth.edu>
5 Copyright (C) 1998 Michael J. Fromberger
7 Arbitrary precision integer arithmetic library
9 SPDX-License-Identifier: Unlicense
11 $Id$
14 #ifndef _H_MPI_
15 #define _H_MPI_
17 #include "mpi-config.h"
19 #define MP_LT -1
20 #define MP_EQ 0
21 #define MP_GT 1
23 #if MP_DEBUG
24 #undef MP_IOFUNC
25 #define MP_IOFUNC 1
26 #endif
28 #if MP_IOFUNC
29 #include <stdio.h>
30 #include <ctype.h>
31 #endif
33 #include <limits.h>
35 #define MP_NEG 1
36 #define MP_ZPOS 0
38 /* Included for compatibility... */
39 #define NEG MP_NEG
40 #define ZPOS MP_ZPOS
42 #define MP_OKAY 0 /* no error, all is well */
43 #define MP_YES 0 /* yes (boolean result) */
44 #define MP_NO -1 /* no (boolean result) */
45 #define MP_MEM -2 /* out of memory */
46 #define MP_RANGE -3 /* argument out of range */
47 #define MP_BADARG -4 /* invalid parameter */
48 #define MP_UNDEF -5 /* answer is undefined */
49 #define MP_LAST_CODE MP_UNDEF
51 #include "mpi-types.h"
53 /* Included for compatibility... */
54 #define DIGIT_BIT MP_DIGIT_BIT
55 #define DIGIT_MAX MP_DIGIT_MAX
57 /* Macros for accessing the mp_int internals */
58 #define SIGN(MP) ((MP)->sign)
59 #define USED(MP) ((MP)->used)
60 #define ALLOC(MP) ((MP)->alloc)
61 #define DIGITS(MP) ((MP)->dp)
62 #define DIGIT(MP,N) (MP)->dp[(N)]
64 #if MP_ARGCHK == 1
65 #define ARGCHK(X,Y) {if(!(X)){return (Y);}}
66 #elif MP_ARGCHK == 2
67 #include <assert.h>
68 #define ARGCHK(X,Y) assert(X)
69 #else
70 #define ARGCHK(X,Y) /* */
71 #endif
73 /* This defines the maximum I/O base (minimum is 2) */
74 #define MAX_RADIX 64
76 typedef struct {
77 mp_sign sign; /* sign of this quantity */
78 mp_size alloc; /* how many digits allocated */
79 mp_size used; /* how many digits used */
80 mp_digit *dp; /* the digits themselves */
81 } mp_int;
83 /*------------------------------------------------------------------------*/
84 /* Default precision */
86 unsigned int mp_get_prec(void);
87 void mp_set_prec(unsigned int prec);
89 /*------------------------------------------------------------------------*/
90 /* Memory management */
92 mp_err mp_init(mp_int *mp);
93 mp_err mp_init_array(mp_int mp[], int count);
94 mp_err mp_init_size(mp_int *mp, mp_size prec);
95 mp_err mp_init_copy(mp_int *mp, mp_int *from);
96 mp_err mp_copy(mp_int *from, mp_int *to);
97 void mp_exch(mp_int *mp1, mp_int *mp2);
98 void mp_clear(mp_int *mp);
99 void mp_clear_array(mp_int mp[], int count);
100 void mp_zero(mp_int *mp);
101 void mp_set(mp_int *mp, mp_digit d);
102 mp_err mp_set_int(mp_int *mp, long z);
103 mp_err mp_shrink(mp_int *a);
106 /*------------------------------------------------------------------------*/
107 /* Single digit arithmetic */
109 mp_err mp_add_d(mp_int *a, mp_digit d, mp_int *b);
110 mp_err mp_sub_d(mp_int *a, mp_digit d, mp_int *b);
111 mp_err mp_mul_d(mp_int *a, mp_digit d, mp_int *b);
112 mp_err mp_mul_2(mp_int *a, mp_int *c);
113 mp_err mp_div_d(mp_int *a, mp_digit d, mp_int *q, mp_digit *r);
114 mp_err mp_div_2(mp_int *a, mp_int *c);
115 mp_err mp_expt_d(mp_int *a, mp_digit d, mp_int *c);
117 /*------------------------------------------------------------------------*/
118 /* Sign manipulations */
120 mp_err mp_abs(mp_int *a, mp_int *b);
121 mp_err mp_neg(mp_int *a, mp_int *b);
123 /*------------------------------------------------------------------------*/
124 /* Full arithmetic */
126 mp_err mp_add(mp_int *a, mp_int *b, mp_int *c);
127 mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c);
128 mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c);
129 mp_err mp_mul_2d(mp_int *a, mp_digit d, mp_int *c);
130 #if MP_SQUARE
131 mp_err mp_sqr(mp_int *a, mp_int *b);
132 #else
133 #define mp_sqr(a, b) mp_mul(a, a, b)
134 #endif
135 mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r);
136 mp_err mp_div_2d(mp_int *a, mp_digit d, mp_int *q, mp_int *r);
137 mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);
138 mp_err mp_2expt(mp_int *a, mp_digit k);
139 mp_err mp_sqrt(mp_int *a, mp_int *b);
141 /*------------------------------------------------------------------------*/
142 /* Modular arithmetic */
144 #if MP_MODARITH
145 mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c);
146 mp_err mp_mod_d(mp_int *a, mp_digit d, mp_digit *c);
147 mp_err mp_addmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
148 mp_err mp_submod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
149 mp_err mp_mulmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
150 #if MP_SQUARE
151 mp_err mp_sqrmod(mp_int *a, mp_int *m, mp_int *c);
152 #else
153 #define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c)
154 #endif
155 mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
156 mp_err mp_exptmod_d(mp_int *a, mp_digit d, mp_int *m, mp_int *c);
157 #endif /* MP_MODARITH */
159 /*------------------------------------------------------------------------*/
160 /* Comparisons */
162 int mp_cmp_z(mp_int *a);
163 int mp_cmp_d(mp_int *a, mp_digit d);
164 int mp_cmp(mp_int *a, mp_int *b);
165 int mp_cmp_mag(mp_int *a, mp_int *b);
166 int mp_cmp_int(mp_int *a, long z);
167 int mp_isodd(mp_int *a);
168 int mp_iseven(mp_int *a);
170 /*------------------------------------------------------------------------*/
171 /* Number theoretic */
173 #if MP_NUMTH
174 mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c);
175 mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c);
176 mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y);
177 mp_err mp_invmod(mp_int *a, mp_int *m, mp_int *c);
178 #endif /* end MP_NUMTH */
180 /*------------------------------------------------------------------------*/
181 /* Input and output */
183 #if MP_IOFUNC
184 void mp_print(mp_int *mp, FILE *ofp);
185 #endif /* end MP_IOFUNC */
187 /*------------------------------------------------------------------------*/
188 /* Base conversion */
190 #define BITS 1
191 #define BYTES CHAR_BIT
193 mp_err mp_read_signed_bin(mp_int *mp, unsigned char *str, int len);
194 int mp_signed_bin_size(mp_int *mp);
195 mp_err mp_to_signed_bin(mp_int *mp, unsigned char *str);
197 mp_err mp_read_unsigned_bin(mp_int *mp, unsigned char *str, int len);
198 int mp_unsigned_bin_size(mp_int *mp);
199 mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str);
201 int mp_count_bits(mp_int *mp);
203 #if MP_COMPAT_MACROS
204 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
205 #define mp_raw_size(mp) mp_signed_bin_size(mp)
206 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
207 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
208 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
209 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
210 #endif
212 mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix);
213 int mp_radix_size(mp_int *mp, int radix);
214 int mp_value_radix_size(int num, int qty, int radix);
215 mp_err mp_toradix(mp_int *mp, char *str, int radix);
217 int mp_char2value(char ch, int r);
219 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
220 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
221 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
222 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
224 /*------------------------------------------------------------------------*/
225 /* Error strings */
227 const char *mp_strerror(mp_err ec);
229 #endif /* end _H_MPI_ */
231 /* $Source$ */
232 /* $Revision$ */
233 /* $Date$ */