1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/mpi.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,300 @@ 1.4 +/* 1.5 + * mpi.h 1.6 + * 1.7 + * Arbitrary precision integer arithmetic library 1.8 + * 1.9 + * This Source Code Form is subject to the terms of the Mozilla Public 1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.12 + 1.13 +#ifndef _H_MPI_ 1.14 +#define _H_MPI_ 1.15 + 1.16 +#include "mpi-config.h" 1.17 + 1.18 +#if MP_DEBUG 1.19 +#undef MP_IOFUNC 1.20 +#define MP_IOFUNC 1 1.21 +#endif 1.22 + 1.23 +#if MP_IOFUNC 1.24 +#include <stdio.h> 1.25 +#include <ctype.h> 1.26 +#endif 1.27 + 1.28 +#include <limits.h> 1.29 + 1.30 +#if defined(BSDI) 1.31 +#undef ULLONG_MAX 1.32 +#endif 1.33 + 1.34 +#include <sys/types.h> 1.35 + 1.36 +#define MP_NEG 1 1.37 +#define MP_ZPOS 0 1.38 + 1.39 +#define MP_OKAY 0 /* no error, all is well */ 1.40 +#define MP_YES 0 /* yes (boolean result) */ 1.41 +#define MP_NO -1 /* no (boolean result) */ 1.42 +#define MP_MEM -2 /* out of memory */ 1.43 +#define MP_RANGE -3 /* argument out of range */ 1.44 +#define MP_BADARG -4 /* invalid parameter */ 1.45 +#define MP_UNDEF -5 /* answer is undefined */ 1.46 +#define MP_LAST_CODE MP_UNDEF 1.47 + 1.48 +typedef unsigned int mp_sign; 1.49 +typedef unsigned int mp_size; 1.50 +typedef int mp_err; 1.51 + 1.52 +#define MP_32BIT_MAX 4294967295U 1.53 + 1.54 +#if !defined(ULONG_MAX) 1.55 +#error "ULONG_MAX not defined" 1.56 +#elif !defined(UINT_MAX) 1.57 +#error "UINT_MAX not defined" 1.58 +#elif !defined(USHRT_MAX) 1.59 +#error "USHRT_MAX not defined" 1.60 +#endif 1.61 + 1.62 +#if defined(ULLONG_MAX) /* C99, Solaris */ 1.63 +#define MP_ULONG_LONG_MAX ULLONG_MAX 1.64 +/* MP_ULONG_LONG_MAX was defined to be ULLONG_MAX */ 1.65 +#elif defined(ULONG_LONG_MAX) /* HPUX */ 1.66 +#define MP_ULONG_LONG_MAX ULONG_LONG_MAX 1.67 +#elif defined(ULONGLONG_MAX) /* IRIX, AIX */ 1.68 +#define MP_ULONG_LONG_MAX ULONGLONG_MAX 1.69 +#endif 1.70 + 1.71 +/* We only use unsigned long for mp_digit iff long is more than 32 bits. */ 1.72 +#if !defined(MP_USE_UINT_DIGIT) && ULONG_MAX > MP_32BIT_MAX 1.73 +typedef unsigned long mp_digit; 1.74 +#define MP_DIGIT_MAX ULONG_MAX 1.75 +#define MP_DIGIT_FMT "%016lX" /* printf() format for 1 digit */ 1.76 +#define MP_HALF_DIGIT_MAX UINT_MAX 1.77 +#undef MP_NO_MP_WORD 1.78 +#define MP_NO_MP_WORD 1 1.79 +#undef MP_USE_LONG_DIGIT 1.80 +#define MP_USE_LONG_DIGIT 1 1.81 +#undef MP_USE_LONG_LONG_DIGIT 1.82 + 1.83 +#elif !defined(MP_USE_UINT_DIGIT) && defined(MP_ULONG_LONG_MAX) 1.84 +typedef unsigned long long mp_digit; 1.85 +#define MP_DIGIT_MAX MP_ULONG_LONG_MAX 1.86 +#define MP_DIGIT_FMT "%016llX" /* printf() format for 1 digit */ 1.87 +#define MP_HALF_DIGIT_MAX UINT_MAX 1.88 +#undef MP_NO_MP_WORD 1.89 +#define MP_NO_MP_WORD 1 1.90 +#undef MP_USE_LONG_LONG_DIGIT 1.91 +#define MP_USE_LONG_LONG_DIGIT 1 1.92 +#undef MP_USE_LONG_DIGIT 1.93 + 1.94 +#else 1.95 +typedef unsigned int mp_digit; 1.96 +#define MP_DIGIT_MAX UINT_MAX 1.97 +#define MP_DIGIT_FMT "%08X" /* printf() format for 1 digit */ 1.98 +#define MP_HALF_DIGIT_MAX USHRT_MAX 1.99 +#undef MP_USE_UINT_DIGIT 1.100 +#define MP_USE_UINT_DIGIT 1 1.101 +#undef MP_USE_LONG_LONG_DIGIT 1.102 +#undef MP_USE_LONG_DIGIT 1.103 +#endif 1.104 + 1.105 +#if !defined(MP_NO_MP_WORD) 1.106 +#if defined(MP_USE_UINT_DIGIT) && \ 1.107 + (defined(MP_ULONG_LONG_MAX) || (ULONG_MAX > UINT_MAX)) 1.108 + 1.109 +#if (ULONG_MAX > UINT_MAX) 1.110 +typedef unsigned long mp_word; 1.111 +typedef long mp_sword; 1.112 +#define MP_WORD_MAX ULONG_MAX 1.113 + 1.114 +#else 1.115 +typedef unsigned long long mp_word; 1.116 +typedef long long mp_sword; 1.117 +#define MP_WORD_MAX MP_ULONG_LONG_MAX 1.118 +#endif 1.119 + 1.120 +#else 1.121 +#define MP_NO_MP_WORD 1 1.122 +#endif 1.123 +#endif /* !defined(MP_NO_MP_WORD) */ 1.124 + 1.125 +#if !defined(MP_WORD_MAX) && defined(MP_DEFINE_SMALL_WORD) 1.126 +typedef unsigned int mp_word; 1.127 +typedef int mp_sword; 1.128 +#define MP_WORD_MAX UINT_MAX 1.129 +#endif 1.130 + 1.131 +#define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit)) 1.132 +#define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word)) 1.133 +#define MP_RADIX (1+(mp_word)MP_DIGIT_MAX) 1.134 + 1.135 +#define MP_HALF_DIGIT_BIT (MP_DIGIT_BIT/2) 1.136 +#define MP_HALF_RADIX (1+(mp_digit)MP_HALF_DIGIT_MAX) 1.137 +/* MP_HALF_RADIX really ought to be called MP_SQRT_RADIX, but it's named 1.138 +** MP_HALF_RADIX because it's the radix for MP_HALF_DIGITs, and it's 1.139 +** consistent with the other _HALF_ names. 1.140 +*/ 1.141 + 1.142 + 1.143 +/* Macros for accessing the mp_int internals */ 1.144 +#define MP_SIGN(MP) ((MP)->sign) 1.145 +#define MP_USED(MP) ((MP)->used) 1.146 +#define MP_ALLOC(MP) ((MP)->alloc) 1.147 +#define MP_DIGITS(MP) ((MP)->dp) 1.148 +#define MP_DIGIT(MP,N) (MP)->dp[(N)] 1.149 + 1.150 +/* This defines the maximum I/O base (minimum is 2) */ 1.151 +#define MP_MAX_RADIX 64 1.152 + 1.153 +typedef struct { 1.154 + mp_sign sign; /* sign of this quantity */ 1.155 + mp_size alloc; /* how many digits allocated */ 1.156 + mp_size used; /* how many digits used */ 1.157 + mp_digit *dp; /* the digits themselves */ 1.158 +} mp_int; 1.159 + 1.160 +/* Default precision */ 1.161 +mp_size mp_get_prec(void); 1.162 +void mp_set_prec(mp_size prec); 1.163 + 1.164 +/* Memory management */ 1.165 +mp_err mp_init(mp_int *mp); 1.166 +mp_err mp_init_size(mp_int *mp, mp_size prec); 1.167 +mp_err mp_init_copy(mp_int *mp, const mp_int *from); 1.168 +mp_err mp_copy(const mp_int *from, mp_int *to); 1.169 +void mp_exch(mp_int *mp1, mp_int *mp2); 1.170 +void mp_clear(mp_int *mp); 1.171 +void mp_zero(mp_int *mp); 1.172 +void mp_set(mp_int *mp, mp_digit d); 1.173 +mp_err mp_set_int(mp_int *mp, long z); 1.174 +#define mp_set_long(mp,z) mp_set_int(mp,z) 1.175 +mp_err mp_set_ulong(mp_int *mp, unsigned long z); 1.176 + 1.177 +/* Single digit arithmetic */ 1.178 +mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b); 1.179 +mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b); 1.180 +mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b); 1.181 +mp_err mp_mul_2(const mp_int *a, mp_int *c); 1.182 +mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r); 1.183 +mp_err mp_div_2(const mp_int *a, mp_int *c); 1.184 +mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c); 1.185 + 1.186 +/* Sign manipulations */ 1.187 +mp_err mp_abs(const mp_int *a, mp_int *b); 1.188 +mp_err mp_neg(const mp_int *a, mp_int *b); 1.189 + 1.190 +/* Full arithmetic */ 1.191 +mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c); 1.192 +mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c); 1.193 +mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c); 1.194 +#if MP_SQUARE 1.195 +mp_err mp_sqr(const mp_int *a, mp_int *b); 1.196 +#else 1.197 +#define mp_sqr(a, b) mp_mul(a, a, b) 1.198 +#endif 1.199 +mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r); 1.200 +mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r); 1.201 +mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c); 1.202 +mp_err mp_2expt(mp_int *a, mp_digit k); 1.203 +mp_err mp_sqrt(const mp_int *a, mp_int *b); 1.204 + 1.205 +/* Modular arithmetic */ 1.206 +#if MP_MODARITH 1.207 +mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c); 1.208 +mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c); 1.209 +mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); 1.210 +mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); 1.211 +mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); 1.212 +#if MP_SQUARE 1.213 +mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c); 1.214 +#else 1.215 +#define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c) 1.216 +#endif 1.217 +mp_err mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); 1.218 +mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c); 1.219 +#endif /* MP_MODARITH */ 1.220 + 1.221 +/* Comparisons */ 1.222 +int mp_cmp_z(const mp_int *a); 1.223 +int mp_cmp_d(const mp_int *a, mp_digit d); 1.224 +int mp_cmp(const mp_int *a, const mp_int *b); 1.225 +int mp_cmp_mag(mp_int *a, mp_int *b); 1.226 +int mp_cmp_int(const mp_int *a, long z); 1.227 +int mp_isodd(const mp_int *a); 1.228 +int mp_iseven(const mp_int *a); 1.229 + 1.230 +/* Number theoretic */ 1.231 +#if MP_NUMTH 1.232 +mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c); 1.233 +mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c); 1.234 +mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y); 1.235 +mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c); 1.236 +mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c); 1.237 +#endif /* end MP_NUMTH */ 1.238 + 1.239 +/* Input and output */ 1.240 +#if MP_IOFUNC 1.241 +void mp_print(mp_int *mp, FILE *ofp); 1.242 +#endif /* end MP_IOFUNC */ 1.243 + 1.244 +/* Base conversion */ 1.245 +mp_err mp_read_raw(mp_int *mp, char *str, int len); 1.246 +int mp_raw_size(mp_int *mp); 1.247 +mp_err mp_toraw(mp_int *mp, char *str); 1.248 +mp_err mp_read_radix(mp_int *mp, const char *str, int radix); 1.249 +mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix); 1.250 +int mp_radix_size(mp_int *mp, int radix); 1.251 +mp_err mp_toradix(mp_int *mp, char *str, int radix); 1.252 +int mp_tovalue(char ch, int r); 1.253 + 1.254 +#define mp_tobinary(M, S) mp_toradix((M), (S), 2) 1.255 +#define mp_tooctal(M, S) mp_toradix((M), (S), 8) 1.256 +#define mp_todecimal(M, S) mp_toradix((M), (S), 10) 1.257 +#define mp_tohex(M, S) mp_toradix((M), (S), 16) 1.258 + 1.259 +/* Error strings */ 1.260 +const char *mp_strerror(mp_err ec); 1.261 + 1.262 +/* Octet string conversion functions */ 1.263 +mp_err mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len); 1.264 +int mp_unsigned_octet_size(const mp_int *mp); 1.265 +mp_err mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen); 1.266 +mp_err mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen); 1.267 +mp_err mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size len); 1.268 + 1.269 +/* Miscellaneous */ 1.270 +mp_size mp_trailing_zeros(const mp_int *mp); 1.271 +void freebl_cpuid(unsigned long op, unsigned long *eax, 1.272 + unsigned long *ebx, unsigned long *ecx, 1.273 + unsigned long *edx); 1.274 + 1.275 + 1.276 +#define MP_CHECKOK(x) if (MP_OKAY > (res = (x))) goto CLEANUP 1.277 +#define MP_CHECKERR(x) if (MP_OKAY > (res = (x))) goto CLEANUP 1.278 + 1.279 +#if defined(MP_API_COMPATIBLE) 1.280 +#define NEG MP_NEG 1.281 +#define ZPOS MP_ZPOS 1.282 +#define DIGIT_MAX MP_DIGIT_MAX 1.283 +#define DIGIT_BIT MP_DIGIT_BIT 1.284 +#define DIGIT_FMT MP_DIGIT_FMT 1.285 +#define RADIX MP_RADIX 1.286 +#define MAX_RADIX MP_MAX_RADIX 1.287 +#define SIGN(MP) MP_SIGN(MP) 1.288 +#define USED(MP) MP_USED(MP) 1.289 +#define ALLOC(MP) MP_ALLOC(MP) 1.290 +#define DIGITS(MP) MP_DIGITS(MP) 1.291 +#define DIGIT(MP,N) MP_DIGIT(MP,N) 1.292 + 1.293 +#if MP_ARGCHK == 1 1.294 +#define ARGCHK(X,Y) {if(!(X)){return (Y);}} 1.295 +#elif MP_ARGCHK == 2 1.296 +#include <assert.h> 1.297 +#define ARGCHK(X,Y) assert(X) 1.298 +#else 1.299 +#define ARGCHK(X,Y) /* */ 1.300 +#endif 1.301 +#endif /* defined MP_API_COMPATIBLE */ 1.302 + 1.303 +#endif /* end _H_MPI_ */