Print this page
6799218 RSA using Solaris Kernel Crypto framework lagging behind OpenSSL
5016936 bignumimpl:big_mul: potential memory leak
6810280 panic from bignum module: vmem_xalloc(): size == 0
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/common/bignum/bignum.h
+++ new/usr/src/common/bignum/bignum.h
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 - * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
22 + * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #ifndef _BIGNUM_H
27 27 #define _BIGNUM_H
28 28
29 -#pragma ident "%Z%%M% %I% %E% SMI"
30 -
31 29 #ifdef __cplusplus
32 30 extern "C" {
33 31 #endif
34 32
35 33 #include <sys/types.h>
36 34
37 -#ifndef __sparcv9
38 -#define BIGNUM_CHUNK_32
39 -#else
35 +#if defined(__sparcv9) || defined(__amd64) /* 64-bit chunk size */
40 36 #ifndef UMUL64
41 -#define UMUL64
37 +#define UMUL64 /* 64-bit multiplication results are supported */
42 38 #endif
39 +#else
40 +#define BIGNUM_CHUNK_32
43 41 #endif
44 42
45 43
46 44 #define BITSINBYTE 8
47 45
46 +/* Bignum "digits" (aka "chunks" or "words") are either 32- or 64-bits */
48 47 #ifdef BIGNUM_CHUNK_32
49 48 #define BIG_CHUNK_SIZE 32
50 49 #define BIG_CHUNK_TYPE uint32_t
51 50 #define BIG_CHUNK_TYPE_SIGNED int32_t
52 51 #define BIG_CHUNK_HIGHBIT 0x80000000
53 52 #define BIG_CHUNK_ALLBITS 0xffffffff
54 53 #define BIG_CHUNK_LOWHALFBITS 0xffff
55 54 #define BIG_CHUNK_HALF_HIGHBIT 0x8000
55 +
56 56 #else
57 57 #define BIG_CHUNK_SIZE 64
58 58 #define BIG_CHUNK_TYPE uint64_t
59 59 #define BIG_CHUNK_TYPE_SIGNED int64_t
60 60 #define BIG_CHUNK_HIGHBIT 0x8000000000000000ULL
61 61 #define BIG_CHUNK_ALLBITS 0xffffffffffffffffULL
62 62 #define BIG_CHUNK_LOWHALFBITS 0xffffffffULL
63 63 #define BIG_CHUNK_HALF_HIGHBIT 0x80000000ULL
64 64 #endif
65 65
66 66 #define BITLEN2BIGNUMLEN(x) (((x) + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
67 67 #define CHARLEN2BIGNUMLEN(x) (((x) + sizeof (BIG_CHUNK_TYPE) - 1) / \
68 - sizeof (BIG_CHUNK_TYPE))
68 + sizeof (BIG_CHUNK_TYPE))
69 69
70 70 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */
71 71 #define BIG_CHUNKS_FOR_160BITS ((160 + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
72 72
73 73
74 74 /*
75 75 * leading 0's are permitted
76 76 * 0 should be represented by size>=1, size>=len>=1, sign=1,
77 77 * value[i]=0 for 0<i<len
78 78 */
79 79 typedef struct {
80 80 /* size and len in units of BIG_CHUNK_TYPE words */
81 - int size; /* size of memory allocated for value */
82 - int len; /* number of words that hold valid data in value */
83 - int sign; /* 1 for nonnegative, -1 for negative */
84 - int malloced; /* 1 if value was malloced 0 if not */
81 + uint32_t size; /* size of memory allocated for value */
82 + uint32_t len; /* number of valid data words in value */
83 + int sign; /* 1 for nonnegative, -1 for negative */
84 + int malloced; /* 1 if value was malloced, 0 if not */
85 85 BIG_CHUNK_TYPE *value;
86 86 } BIGNUM;
87 87
88 88 #define BIGTMPSIZE 65
89 89
90 90 #define BIG_TRUE 1
91 91 #define BIG_FALSE 0
92 92
93 93 typedef int BIG_ERR_CODE;
94 94
95 95 /* error codes */
96 96 #define BIG_OK 0
97 97 #define BIG_NO_MEM -1
98 98 #define BIG_INVALID_ARGS -2
99 99 #define BIG_DIV_BY_0 -3
100 100 #define BIG_NO_RANDOM -4
101 101 #define BIG_GENERAL_ERR -5
102 102 #define BIG_TEST_FAILED -6
103 103 #define BIG_BUFFER_TOO_SMALL -7
104 104
105 105
106 106 #define arraysize(x) (sizeof (x) / sizeof (x[0]))
107 107
108 108 typedef BIG_ERR_CODE (*big_modexp_ncp_func_ptr)(BIGNUM *result,
109 109 BIGNUM *ma, BIGNUM *e, BIGNUM *n,
110 110 BIGNUM *tmp, BIG_CHUNK_TYPE n0, void *ncp, void *req);
111 111
112 112 typedef struct {
113 113 big_modexp_ncp_func_ptr func;
114 114 void *ncp;
115 115 void *reqp;
116 116 } big_modexp_ncp_info_t;
117 117
118 118
119 119 #ifdef USE_FLOATING_POINT
120 120 void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen);
121 121 void conv_i32_to_d32(double *d32, uint32_t *i32, int len);
122 122 void conv_i32_to_d16(double *d16, uint32_t *i32, int len);
123 123 void conv_i32_to_d32_and_d16(double *d32, double *d16,
124 124 uint32_t *i32, int len);
125 125 void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt,
126 126 double *dn, uint32_t *nint, int nlen, double dn0);
127 127 #endif /* USE_FLOATING_POINT */
128 128
129 129 extern BIGNUM big_One;
130 130 extern BIGNUM big_Two;
131 131
132 132
133 133 void printbignum(char *aname, BIGNUM *a);
134 134
135 135 BIG_ERR_CODE big_init(BIGNUM *number, int size);
136 136 BIG_ERR_CODE big_extend(BIGNUM *number, int size);
137 137 void big_finish(BIGNUM *number);
138 138 void bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len);
139 139 void bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len);
140 140 BIG_ERR_CODE big_mont_rr(BIGNUM *result, BIGNUM *n);
141 141 BIG_ERR_CODE big_modexp(BIGNUM *result, BIGNUM *a, BIGNUM *e,
142 142 BIGNUM *n, BIGNUM *n_rr);
143 143 BIG_ERR_CODE big_modexp_ext(BIGNUM *result, BIGNUM *a, BIGNUM *e,
144 144 BIGNUM *n, BIGNUM *n_rr, big_modexp_ncp_info_t *info);
145 145 BIG_ERR_CODE big_modexp_crt(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1,
146 146 BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq,
147 147 BIGNUM *p_rr, BIGNUM *q_rr);
148 148 BIG_ERR_CODE big_modexp_crt_ext(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1,
149 149 BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq,
150 150 BIGNUM *p_rr, BIGNUM *q_rr, big_modexp_ncp_info_t *info);
151 151 int big_cmp_abs(BIGNUM *a, BIGNUM *b);
152 152 BIG_ERR_CODE randombignum(BIGNUM *r, int length);
153 153 BIG_ERR_CODE big_div_pos(BIGNUM *result, BIGNUM *remainder,
154 154 BIGNUM *aa, BIGNUM *bb);
155 155 BIG_ERR_CODE big_ext_gcd_pos(BIGNUM *gcd, BIGNUM *cm, BIGNUM *ce,
156 156 BIGNUM *m, BIGNUM *e);
157 157 BIG_ERR_CODE big_add(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
158 158 BIG_ERR_CODE big_add_abs(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
159 159 BIG_ERR_CODE big_mul(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
160 160 void big_shiftright(BIGNUM *result, BIGNUM *aa, int offs);
161 161 BIG_ERR_CODE big_nextprime_pos(BIGNUM *result, BIGNUM *n);
162 162 BIG_ERR_CODE big_nextprime_pos_ext(BIGNUM *result, BIGNUM *n,
163 163 big_modexp_ncp_info_t *info);
164 164 BIG_ERR_CODE big_sub_pos(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
165 165 BIG_ERR_CODE big_copy(BIGNUM *dest, BIGNUM *src);
166 166 BIG_ERR_CODE big_sub(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
167 167 int big_bitlength(BIGNUM *n);
168 168 BIG_ERR_CODE big_init1(BIGNUM *number, int size,
169 169 BIG_CHUNK_TYPE *buf, int bufsize);
170 170 BIG_ERR_CODE big_mont_mul(BIGNUM *ret,
171 171 BIGNUM *a, BIGNUM *b, BIGNUM *n, BIG_CHUNK_TYPE n0);
172 172 int big_is_zero(BIGNUM *n);
173 173 BIG_CHUNK_TYPE big_n0(BIG_CHUNK_TYPE n);
174 174
175 175
176 176 #if defined(HWCAP)
177 177
178 178 #if (BIG_CHUNK_SIZE != 32)
179 179 #error HWCAP works only with 32-bit bignum chunks
180 180 #endif
181 181
182 182 #define BIG_MUL_SET_VEC(r, a, len, digit) \
183 183 (*big_mul_set_vec_impl)(r, a, len, digit)
184 184 #define BIG_MUL_ADD_VEC(r, a, len, digit) \
185 185 (*big_mul_add_vec_impl)(r, a, len, digit)
186 186 #define BIG_MUL_VEC(r, a, alen, b, blen) \
187 187 (*big_mul_vec_impl)(r, a, alen, b, blen)
188 188 #define BIG_SQR_VEC(r, a, len) \
189 189 (*big_sqr_vec_impl)(r, a, len)
190 190
191 191 extern BIG_CHUNK_TYPE (*big_mul_set_vec_impl)
192 192 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit);
193 193 extern BIG_CHUNK_TYPE (*big_mul_add_vec_impl)
194 194 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit);
195 195 extern void (*big_mul_vec_impl)
196 196 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, BIG_CHUNK_TYPE *b,
197 197 int blen);
198 198 extern void (*big_sqr_vec_impl)
199 199 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len);
200 200
201 201 #else /* ! HWCAP */
202 202
203 203 #define BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit)
204 204 #define BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit)
205 205 #define BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen)
206 206 #define BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len)
207 207
208 208 extern BIG_CHUNK_TYPE big_mul_set_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a,
209 209 int len, BIG_CHUNK_TYPE d);
210 210 extern BIG_CHUNK_TYPE big_mul_add_vec(BIG_CHUNK_TYPE *r,
211 211 BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE d);
212 212 extern void big_mul_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen,
213 213 BIG_CHUNK_TYPE *b, int blen);
214 214 extern void big_sqr_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len);
215 215
216 216 #endif /* HWCAP */
217 217
218 218 #ifdef __cplusplus
219 219 }
220 220 #endif
221 221
222 222 #endif /* _BIGNUM_H */
↓ open down ↓ |
128 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX