|
1 /* |
|
2 * This product includes cryptographic software developed by the OpenSSL |
|
3 * Project for use in the OpenSSL Toolkit (http://www.openssl.org/). |
|
4 */ |
|
5 |
|
6 /* |
|
7 * ==================================================================== |
|
8 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved. |
|
9 * |
|
10 * Redistribution and use in source and binary forms, with or without |
|
11 * modification, are permitted provided that the following conditions |
|
12 * are met: |
|
13 * |
|
14 * 1. Redistributions of source code must retain the above copyright |
|
15 * notice, this list of conditions and the following disclaimer. |
|
16 * |
|
17 * 2. Redistributions in binary form must reproduce the above copyright |
|
18 * notice, this list of conditions and the following disclaimer in |
|
19 * the documentation and/or other materials provided with the |
|
20 * distribution. |
|
21 * |
|
22 * 3. All advertising materials mentioning features or use of this |
|
23 * software must display the following acknowledgment: |
|
24 * "This product includes software developed by the OpenSSL Project |
|
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
|
26 * |
|
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
|
28 * endorse or promote products derived from this software without |
|
29 * prior written permission. For written permission, please contact |
|
30 * [email protected]. |
|
31 * |
|
32 * 5. Products derived from this software may not be called "OpenSSL" |
|
33 * nor may "OpenSSL" appear in their names without prior written |
|
34 * permission of the OpenSSL Project. |
|
35 * |
|
36 * 6. Redistributions of any form whatsoever must retain the following |
|
37 * acknowledgment: |
|
38 * "This product includes software developed by the OpenSSL Project |
|
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
|
40 * |
|
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
|
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
|
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
52 * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
53 * ==================================================================== |
|
54 */ |
|
55 |
|
56 /* |
|
57 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
|
58 */ |
|
59 |
|
60 /* |
|
61 * This file is a copy of the ON gate's usr/src/common/bignum/bignum.h file |
|
62 */ |
|
63 |
|
64 #ifndef _BIGNUM_H |
|
65 #define _BIGNUM_H |
|
66 |
|
67 #ifdef __cplusplus |
|
68 extern "C" { |
|
69 #endif |
|
70 |
|
71 #include <sys/types.h> |
|
72 |
|
73 #if defined(__sparcv9) || defined(__amd64) || defined(__sparc) |
|
74 /* 64-bit chunk size */ |
|
75 #ifndef UMUL64 |
|
76 #define UMUL64 /* 64-bit multiplication results are supported */ |
|
77 #endif |
|
78 #else |
|
79 #define BIGNUM_CHUNK_32 |
|
80 #endif |
|
81 |
|
82 |
|
83 #define BITSINBYTE 8 |
|
84 |
|
85 /* Bignum "digits" (aka "chunks" or "words") are either 32- or 64-bits */ |
|
86 #ifdef BIGNUM_CHUNK_32 |
|
87 #define BIG_CHUNK_SIZE 32 |
|
88 #define BIG_CHUNK_TYPE uint32_t |
|
89 #define BIG_CHUNK_TYPE_SIGNED int32_t |
|
90 #define BIG_CHUNK_HIGHBIT 0x80000000 |
|
91 #define BIG_CHUNK_ALLBITS 0xffffffff |
|
92 #define BIG_CHUNK_LOWHALFBITS 0xffff |
|
93 #define BIG_CHUNK_HALF_HIGHBIT 0x8000 |
|
94 |
|
95 #else |
|
96 #define BIG_CHUNK_SIZE 64 |
|
97 #define BIG_CHUNK_TYPE uint64_t |
|
98 #define BIG_CHUNK_TYPE_SIGNED int64_t |
|
99 #define BIG_CHUNK_HIGHBIT 0x8000000000000000ULL |
|
100 #define BIG_CHUNK_ALLBITS 0xffffffffffffffffULL |
|
101 #define BIG_CHUNK_LOWHALFBITS 0xffffffffULL |
|
102 #define BIG_CHUNK_HALF_HIGHBIT 0x80000000ULL |
|
103 #endif |
|
104 |
|
105 #define BITLEN2BIGNUMLEN(x) ((x) > 0 ? \ |
|
106 ((((x) - 1) / BIG_CHUNK_SIZE) + 1) : 0) |
|
107 #define CHARLEN2BIGNUMLEN(x) ((x) > 0 ? \ |
|
108 ((((x) - 1) / sizeof (BIG_CHUNK_TYPE)) + 1) : 0) |
|
109 |
|
110 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */ |
|
111 #define BIG_CHUNKS_FOR_160BITS BITLEN2BIGNUMLEN(160) |
|
112 |
|
113 |
|
114 /* |
|
115 * leading 0's are permitted |
|
116 * 0 should be represented by size>=1, size>=len>=1, sign=1, |
|
117 * value[i]=0 for 0<i<len |
|
118 */ |
|
119 typedef struct { |
|
120 /* size and len in units of BIG_CHUNK_TYPE words */ |
|
121 uint32_t size; /* size of memory allocated for value */ |
|
122 uint32_t len; /* number of valid data words in value */ |
|
123 int sign; /* 1 for nonnegative, -1 for negative */ |
|
124 int malloced; /* 1 if value was malloced, 0 if not */ |
|
125 BIG_CHUNK_TYPE *value; |
|
126 } BIGNUM; |
|
127 |
|
128 #define BIGTMPSIZE 65 |
|
129 |
|
130 #define BIG_TRUE 1 |
|
131 #define BIG_FALSE 0 |
|
132 |
|
133 typedef int BIG_ERR_CODE; |
|
134 |
|
135 /* error codes */ |
|
136 #define BIG_OK 0 |
|
137 #define BIG_NO_MEM -1 |
|
138 #define BIG_INVALID_ARGS -2 |
|
139 #define BIG_DIV_BY_0 -3 |
|
140 #define BIG_NO_RANDOM -4 |
|
141 #define BIG_GENERAL_ERR -5 |
|
142 #define BIG_TEST_FAILED -6 |
|
143 #define BIG_BUFFER_TOO_SMALL -7 |
|
144 |
|
145 /* |
|
146 * this is not an error code, but should be different from possible error codes |
|
147 */ |
|
148 #define RND_TEST_VALUE_SUPPLIED -8 |
|
149 |
|
150 |
|
151 #define arraysize(x) (sizeof (x) / sizeof (x[0])) |
|
152 |
|
153 typedef BIG_ERR_CODE (*big_modexp_ncp_func_ptr)(BIGNUM *result, |
|
154 BIGNUM *ma, BIGNUM *e, BIGNUM *n, |
|
155 BIGNUM *tmp, BIG_CHUNK_TYPE n0, void *ncp, void *req); |
|
156 |
|
157 typedef struct { |
|
158 big_modexp_ncp_func_ptr func; |
|
159 void *ncp; |
|
160 void *reqp; |
|
161 } big_modexp_ncp_info_t; |
|
162 |
|
163 #ifdef YF_MODEXP |
|
164 BIG_ERR_CODE big_modexp_ncp_yf(BIGNUM *result, BIGNUM *ma, BIGNUM *e, BIGNUM *n, |
|
165 BIGNUM *tmp, BIG_CHUNK_TYPE n0); |
|
166 #endif |
|
167 |
|
168 #ifdef YF_MONTMUL |
|
169 BIG_ERR_CODE big_mont_mul_yf(BIGNUM *ret, |
|
170 BIGNUM *a, BIGNUM *b, BIGNUM *n, BIG_CHUNK_TYPE n0); |
|
171 #endif |
|
172 |
|
173 #ifdef YF_MPMUL |
|
174 BIG_ERR_CODE big_mp_mul_yf(BIGNUM *ret, BIGNUM *a, BIGNUM *b); |
|
175 void mpmul_arr_yf(uint64_t *res, uint64_t *m1, uint64_t *m2, int len); |
|
176 #endif |
|
177 |
|
178 #ifdef USE_FLOATING_POINT |
|
179 void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen); |
|
180 void conv_i32_to_d32(double *d32, uint32_t *i32, int len); |
|
181 void conv_i32_to_d16(double *d16, uint32_t *i32, int len); |
|
182 void conv_i32_to_d32_and_d16(double *d32, double *d16, |
|
183 uint32_t *i32, int len); |
|
184 void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt, |
|
185 double *dn, uint32_t *nint, int nlen, double dn0); |
|
186 #endif /* USE_FLOATING_POINT */ |
|
187 |
|
188 extern BIGNUM big_One; |
|
189 extern BIGNUM big_Two; |
|
190 |
|
191 void printbignum(char *aname, BIGNUM *a); |
|
192 |
|
193 BIG_ERR_CODE big_init(BIGNUM *number, int size); |
|
194 BIG_ERR_CODE big_extend(BIGNUM *number, int size); |
|
195 void big_finish(BIGNUM *number); |
|
196 void bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len); |
|
197 void bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len); |
|
198 BIG_ERR_CODE big_mont_rr(BIGNUM *result, BIGNUM *n); |
|
199 BIG_ERR_CODE big_modexp(BIGNUM *result, BIGNUM *a, BIGNUM *e, |
|
200 BIGNUM *n, BIGNUM *n_rr); |
|
201 BIG_ERR_CODE big_modexp_ext(BIGNUM *result, BIGNUM *a, BIGNUM *e, |
|
202 BIGNUM *n, BIGNUM *n_rr, big_modexp_ncp_info_t *info); |
|
203 BIG_ERR_CODE big_modexp_crt(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1, |
|
204 BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq, |
|
205 BIGNUM *p_rr, BIGNUM *q_rr); |
|
206 BIG_ERR_CODE big_modexp_crt_ext(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1, |
|
207 BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq, |
|
208 BIGNUM *p_rr, BIGNUM *q_rr, big_modexp_ncp_info_t *info); |
|
209 int big_cmp_abs(BIGNUM *a, BIGNUM *b); |
|
210 BIG_ERR_CODE big_random(BIGNUM *r, size_t length, |
|
211 int (*rfunc)(void *, size_t), boolean_t precise); |
|
212 BIG_ERR_CODE big_div_pos(BIGNUM *result, BIGNUM *remainder, |
|
213 BIGNUM *aa, BIGNUM *bb); |
|
214 BIG_ERR_CODE big_ext_gcd_pos(BIGNUM *gcd, BIGNUM *cm, BIGNUM *ce, |
|
215 BIGNUM *m, BIGNUM *e); |
|
216 BIG_ERR_CODE big_add(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); |
|
217 BIG_ERR_CODE big_add_abs(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); |
|
218 void big_mul_arr_64(uint64_t *result, uint64_t *a, uint64_t *b, int alen); |
|
219 BIG_ERR_CODE big_mul(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); |
|
220 void big_shiftright(BIGNUM *result, BIGNUM *aa, int offs); |
|
221 BIG_ERR_CODE big_nextprime_pos(BIGNUM *result, BIGNUM *n); |
|
222 BIG_ERR_CODE big_nextprime_pos_ext(BIGNUM *result, BIGNUM *n, |
|
223 big_modexp_ncp_info_t *info); |
|
224 BIG_ERR_CODE big_sub_pos(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); |
|
225 BIG_ERR_CODE big_copy(BIGNUM *dest, BIGNUM *src); |
|
226 BIG_ERR_CODE big_sub(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); |
|
227 int big_bitlength(BIGNUM *n); |
|
228 BIG_ERR_CODE big_init1(BIGNUM *number, int size, |
|
229 BIG_CHUNK_TYPE *buf, int bufsize); |
|
230 BIG_ERR_CODE big_mont_mul(BIGNUM *ret, |
|
231 BIGNUM *a, BIGNUM *b, BIGNUM *n, BIG_CHUNK_TYPE n0); |
|
232 int big_is_zero(BIGNUM *n); |
|
233 BIG_CHUNK_TYPE big_n0(BIG_CHUNK_TYPE n); |
|
234 |
|
235 |
|
236 /* |
|
237 * Kernel bignum module: module integrity test |
|
238 */ |
|
239 extern int bignum_fips_check(void); |
|
240 |
|
241 #if defined(HWCAP) |
|
242 |
|
243 #if (BIG_CHUNK_SIZE != 32) |
|
244 #error HWCAP works only with 32-bit bignum chunks |
|
245 #endif |
|
246 |
|
247 #define BIG_MUL_SET_VEC(r, a, len, digit) \ |
|
248 (*big_mul_set_vec_impl)(r, a, len, digit) |
|
249 #define BIG_MUL_ADD_VEC(r, a, len, digit) \ |
|
250 (*big_mul_add_vec_impl)(r, a, len, digit) |
|
251 #define BIG_MUL_VEC(r, a, alen, b, blen) \ |
|
252 (*big_mul_vec_impl)(r, a, alen, b, blen) |
|
253 #define BIG_SQR_VEC(r, a, len) \ |
|
254 (*big_sqr_vec_impl)(r, a, len) |
|
255 |
|
256 extern BIG_CHUNK_TYPE (*big_mul_set_vec_impl) |
|
257 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); |
|
258 extern BIG_CHUNK_TYPE (*big_mul_add_vec_impl) |
|
259 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); |
|
260 extern void (*big_mul_vec_impl) |
|
261 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, BIG_CHUNK_TYPE *b, |
|
262 int blen); |
|
263 extern void (*big_sqr_vec_impl) |
|
264 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); |
|
265 |
|
266 #else /* ! HWCAP */ |
|
267 |
|
268 #define BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit) |
|
269 #define BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit) |
|
270 #define BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen) |
|
271 #define BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len) |
|
272 |
|
273 extern BIG_CHUNK_TYPE big_mul_set_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, |
|
274 int len, BIG_CHUNK_TYPE d); |
|
275 extern BIG_CHUNK_TYPE big_mul_add_vec(BIG_CHUNK_TYPE *r, |
|
276 BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE d); |
|
277 extern void big_mul_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, |
|
278 BIG_CHUNK_TYPE *b, int blen); |
|
279 extern void big_sqr_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); |
|
280 |
|
281 #endif /* HWCAP */ |
|
282 |
|
283 #ifdef __cplusplus |
|
284 } |
|
285 #endif |
|
286 |
|
287 #endif /* _BIGNUM_H */ |