2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <krb5-types.h>
44 #include <rfc2459_asn1.h> /* XXX */
55 hi
= calloc(1, sizeof(*hi
));
69 heim_integer
*hi
= (heim_integer
*)bn
;
71 memset(hi
->data
, 0, hi
->length
);
74 memset(hi
, 0, sizeof(*hi
));
78 BN_clear_free(BIGNUM
*bn
)
84 BN_dup(const BIGNUM
*bn
)
87 if (der_copy_heim_integer((const heim_integer
*)bn
, (heim_integer
*)b
)) {
95 * If the caller really want to know the number of bits used, subtract
96 * one from the length, multiply by 8, and then lookup in the table
97 * how many bits the hightest byte uses.
100 BN_num_bits(const BIGNUM
*bn
)
102 static unsigned char num2bits
[256] = {
103 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
104 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
105 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
106 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
107 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
108 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
109 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
110 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
112 const heim_integer
*i
= (const void *)bn
;
115 return (i
->length
- 1) * 8 + num2bits
[((unsigned char *)i
->data
)[0]];
119 BN_num_bytes(const BIGNUM
*bn
)
121 return ((const heim_integer
*)bn
)->length
;
125 * Ignore negative flag.
129 BN_bin2bn(const void *s
, int len
, BIGNUM
*bn
)
131 heim_integer
*hi
= (void *)bn
;
137 hi
= (heim_integer
*)BN_new();
142 BN_clear((BIGNUM
*)hi
);
144 hi
->data
= malloc(len
);
145 if (hi
->data
== NULL
&& len
!= 0) {
147 BN_free((BIGNUM
*)hi
);
151 memcpy(hi
->data
, s
, len
);
156 BN_bn2bin(const BIGNUM
*bn
, void *to
)
158 const heim_integer
*hi
= (const void *)bn
;
159 memcpy(to
, hi
->data
, hi
->length
);
164 BN_hex2bn(BIGNUM
**bnp
, const char *in
)
182 ret
= hex_decode(in
, data
, len
);
188 *bnp
= BN_bin2bn(data
, ret
, NULL
);
192 BN_set_negative(*bnp
, negative
);
197 BN_bn2hex(const BIGNUM
*bn
)
204 len
= BN_num_bytes(bn
);
209 len
= BN_bn2bin(bn
, data
);
211 ret
= hex_encode(data
, len
, &str
);
220 BN_cmp(const BIGNUM
*bn1
, const BIGNUM
*bn2
)
222 return der_heim_integer_cmp((const heim_integer
*)bn1
,
223 (const heim_integer
*)bn2
);
227 BN_set_negative(BIGNUM
*bn
, int flag
)
229 ((heim_integer
*)bn
)->negative
= (flag
? 1 : 0);
233 BN_is_negative(const BIGNUM
*bn
)
235 return ((const heim_integer
*)bn
)->negative
? 1 : 0;
238 static const unsigned char is_set
[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
241 BN_is_bit_set(const BIGNUM
*bn
, int bit
)
243 heim_integer
*hi
= (heim_integer
*)bn
;
244 unsigned char *p
= hi
->data
;
246 if ((bit
/ 8) > hi
->length
|| hi
->length
== 0)
249 return p
[hi
->length
- 1 - (bit
/ 8)] & is_set
[bit
% 8];
253 BN_set_bit(BIGNUM
*bn
, int bit
)
255 heim_integer
*hi
= (heim_integer
*)bn
;
258 if ((bit
/ 8) > hi
->length
|| hi
->length
== 0) {
259 size_t len
= (bit
+ 7) / 8;
260 void *d
= realloc(hi
->data
, len
);
265 memset(&p
[hi
->length
], 0, len
);
270 p
[hi
->length
- 1 - (bit
/ 8)] |= is_set
[bit
% 8];
275 BN_clear_bit(BIGNUM
*bn
, int bit
)
277 heim_integer
*hi
= (heim_integer
*)bn
;
278 unsigned char *p
= hi
->data
;
280 if ((bit
/ 8) > hi
->length
|| hi
->length
== 0)
283 p
[hi
->length
- 1 - (bit
/ 8)] &= (unsigned char)(~(is_set
[bit
% 8]));
289 BN_set_word(BIGNUM
*bn
, unsigned long num
)
291 unsigned char p
[sizeof(num
)];
295 for (num2
= num
, i
= 0; num2
> 0; i
++)
300 p
[i
- 1] = (num
& 0xff);
304 bn
= BN_bin2bn(p
, len
, bn
);
309 BN_get_word(const BIGNUM
*bn
)
311 heim_integer
*hi
= (heim_integer
*)bn
;
312 unsigned long num
= 0;
315 if (hi
->negative
|| hi
->length
> sizeof(num
))
318 for (i
= 0; i
< hi
->length
; i
++)
319 num
= ((unsigned char *)hi
->data
)[i
] | (num
<< 8);
324 BN_rand(BIGNUM
*bn
, int bits
, int top
, int bottom
)
326 size_t len
= (bits
+ 7) / 8;
327 heim_integer
*i
= (heim_integer
*)bn
;
332 i
->data
= malloc(len
);
333 if (i
->data
== NULL
&& len
!= 0)
337 if (RAND_bytes(i
->data
, i
->length
) != 1) {
346 BN_clear_bit(bn
, j
- 1);
353 } else if (top
== 0 && bits
> 0) {
354 BN_set_bit(bn
, bits
- 1);
355 } else if (top
== 1 && bits
> 1) {
356 BN_set_bit(bn
, bits
- 1);
357 BN_set_bit(bn
, bits
- 2);
363 if (bottom
&& bits
> 0)
374 BN_uadd(BIGNUM
*res
, const BIGNUM
*a
, const BIGNUM
*b
)
376 const heim_integer
*ai
= (const heim_integer
*)a
;
377 const heim_integer
*bi
= (const heim_integer
*)b
;
378 const unsigned char *ap
, *bp
;
384 if (ai
->negative
&& bi
->negative
)
386 if (ai
->length
< bi
->length
) {
387 const heim_integer
*si
= bi
;
392 ci
.length
= ai
->length
+ 1;
393 ci
.data
= malloc(ci
.length
);
397 ap
= &((const unsigned char *)ai
->data
)[ai
->length
- 1];
398 bp
= &((const unsigned char *)bi
->data
)[bi
->length
- 1];
399 cp
= &((unsigned char *)ci
.data
)[ci
.length
- 1];
401 for (len
= bi
->length
; len
> 0; len
--) {
402 carry
= *ap
+ *bp
+ carry
;
404 carry
= (carry
& ~0xff) ? 1 : 0;
407 for (len
= ai
->length
- bi
->length
; len
> 0; len
--) {
410 carry
= (carry
& ~0xff) ? 1 : 0;
414 memmove(cp
, cp
+ 1, --ci
.length
);
419 *((heim_integer
*)res
) = ci
;
426 * Callback when doing slow generation of numbers, like primes.
430 BN_GENCB_set(BN_GENCB
*gencb
, int (*cb_2
)(int, int, BN_GENCB
*), void *ctx
)
433 gencb
->cb
.cb_2
= cb_2
;
438 BN_GENCB_call(BN_GENCB
*cb
, int a
, int b
)
440 if (cb
== NULL
|| cb
->cb
.cb_2
== NULL
)
442 return cb
->cb
.cb_2(a
, b
, cb
);
466 c
= calloc(1, sizeof(*c
));
471 BN_CTX_free(BN_CTX
*c
)
474 for (i
= 0; i
< c
->bn
.len
; i
++)
475 BN_free(c
->bn
.val
[i
]);
481 BN_CTX_get(BN_CTX
*c
)
483 if (c
->bn
.used
== c
->bn
.len
) {
487 ptr
= realloc(c
->bn
.val
, c
->bn
.len
* sizeof(c
->bn
.val
[0]));
491 for (i
= c
->bn
.used
; i
< c
->bn
.len
; i
++) {
492 c
->bn
.val
[i
] = BN_new();
493 if (c
->bn
.val
[i
] == NULL
) {
499 return c
->bn
.val
[c
->bn
.used
++];
503 BN_CTX_start(BN_CTX
*c
)
505 if (c
->stack
.used
== c
->stack
.len
) {
508 ptr
= realloc(c
->stack
.val
, c
->stack
.len
* sizeof(c
->stack
.val
[0]));
513 c
->stack
.val
[c
->stack
.used
++] = c
->bn
.used
;
517 BN_CTX_end(BN_CTX
*c
)
519 const size_t prev
= c
->stack
.val
[c
->stack
.used
- 1];
522 if (c
->stack
.used
== 0)
525 for (i
= prev
; i
< c
->bn
.used
; i
++)
526 BN_clear(c
->bn
.val
[i
]);