xref: /onnv-gate/usr/src/common/bignum/bignum.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef _BIGNUM_H
28*0Sstevel@tonic-gate #define	_BIGNUM_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #ifdef	__cplusplus
33*0Sstevel@tonic-gate extern "C" {
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate typedef int BIG_ERR_CODE;
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * leading 0's are permitted
43*0Sstevel@tonic-gate  * 0 should be represented by size>=1, size>=len>=1, sign=1,
44*0Sstevel@tonic-gate  * value[i]=0 for 0<i<len
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate typedef struct {
47*0Sstevel@tonic-gate 	int size; /* the size of memory allocated for value (in words) */
48*0Sstevel@tonic-gate 	int len;  /* the number of words that hold valid data in value */
49*0Sstevel@tonic-gate 	int sign; /* 1 for nonnegative, -1 for negative   */
50*0Sstevel@tonic-gate 	int malloced; /* 1 if value was malloced 0 if not */
51*0Sstevel@tonic-gate 	uint32_t *value;
52*0Sstevel@tonic-gate } BIGNUM;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	BIGTMPSIZE 65
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	BIG_TRUE 1
57*0Sstevel@tonic-gate #define	BIG_FALSE 0
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /* error codes */
60*0Sstevel@tonic-gate #define	BIG_OK 0
61*0Sstevel@tonic-gate #define	BIG_NO_MEM -1
62*0Sstevel@tonic-gate #define	BIG_INVALID_ARGS -2
63*0Sstevel@tonic-gate #define	BIG_DIV_BY_0 -3
64*0Sstevel@tonic-gate #define	BIG_NO_RANDOM -4
65*0Sstevel@tonic-gate #define	BIG_GENERAL_ERR	-5
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #define	arraysize(x) (sizeof (x) / sizeof (x[0]))
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #ifdef USE_FLOATING_POINT
70*0Sstevel@tonic-gate void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen);
71*0Sstevel@tonic-gate void conv_i32_to_d32(double *d32, uint32_t *i32, int len);
72*0Sstevel@tonic-gate void conv_i32_to_d16(double *d16, uint32_t *i32, int len);
73*0Sstevel@tonic-gate void conv_i32_to_d32_and_d16(double *d32, double *d16,
74*0Sstevel@tonic-gate     uint32_t *i32, int len);
75*0Sstevel@tonic-gate void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt,
76*0Sstevel@tonic-gate     double *dn, uint32_t *nint, int nlen, double dn0);
77*0Sstevel@tonic-gate #endif /* USE_FLOATING_POINT */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate void printbignum(char *aname, BIGNUM *a);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate BIG_ERR_CODE big_init(BIGNUM *number, int size);
82*0Sstevel@tonic-gate BIG_ERR_CODE big_extend(BIGNUM *number, int size);
83*0Sstevel@tonic-gate void big_finish(BIGNUM *number);
84*0Sstevel@tonic-gate void bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len);
85*0Sstevel@tonic-gate void bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len);
86*0Sstevel@tonic-gate BIG_ERR_CODE big_mont_rr(BIGNUM *result, BIGNUM *n);
87*0Sstevel@tonic-gate BIG_ERR_CODE big_modexp(BIGNUM *result, BIGNUM *a, BIGNUM *e,
88*0Sstevel@tonic-gate     BIGNUM *n, BIGNUM *n_rr);
89*0Sstevel@tonic-gate BIG_ERR_CODE big_modexp_crt(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1,
90*0Sstevel@tonic-gate     BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq,
91*0Sstevel@tonic-gate     BIGNUM *p_rr, BIGNUM *q_rr);
92*0Sstevel@tonic-gate int big_cmp_abs(BIGNUM *a, BIGNUM *b);
93*0Sstevel@tonic-gate BIG_ERR_CODE randombignum(BIGNUM *r, int length);
94*0Sstevel@tonic-gate BIG_ERR_CODE big_div_pos(BIGNUM *result, BIGNUM *remainder,
95*0Sstevel@tonic-gate     BIGNUM *aa, BIGNUM *bb);
96*0Sstevel@tonic-gate BIG_ERR_CODE big_ext_gcd_pos(BIGNUM *gcd, BIGNUM *cm, BIGNUM *ce,
97*0Sstevel@tonic-gate     BIGNUM *m, BIGNUM *e);
98*0Sstevel@tonic-gate BIG_ERR_CODE big_add(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
99*0Sstevel@tonic-gate BIG_ERR_CODE big_mul(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
100*0Sstevel@tonic-gate BIG_ERR_CODE big_nextprime_pos(BIGNUM *result, BIGNUM *n);
101*0Sstevel@tonic-gate BIG_ERR_CODE big_sub_pos(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
102*0Sstevel@tonic-gate BIG_ERR_CODE big_copy(BIGNUM *dest, BIGNUM *src);
103*0Sstevel@tonic-gate BIG_ERR_CODE big_sub(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
104*0Sstevel@tonic-gate int big_bitlength(BIGNUM *n);
105*0Sstevel@tonic-gate BIG_ERR_CODE big_init1(BIGNUM *number, int size, uint32_t *buf, int bufsize);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #if defined(HWCAP)
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #define	BIG_MUL_SET_VEC(r, a, len, digit) \
110*0Sstevel@tonic-gate 	(*big_mul_set_vec_impl)(r, a, len, digit)
111*0Sstevel@tonic-gate #define	BIG_MUL_ADD_VEC(r, a, len, digit) \
112*0Sstevel@tonic-gate 	(*big_mul_add_vec_impl)(r, a, len, digit)
113*0Sstevel@tonic-gate #define	BIG_MUL_VEC(r, a, alen, b, blen) \
114*0Sstevel@tonic-gate 	(*big_mul_vec_impl)(r, a, alen, b, blen)
115*0Sstevel@tonic-gate #define	BIG_SQR_VEC(r, a, len) \
116*0Sstevel@tonic-gate 	(*big_sqr_vec_impl)(r, a, len)
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate extern uint32_t (*big_mul_set_vec_impl)
119*0Sstevel@tonic-gate 	(uint32_t *r, uint32_t *a, int len, uint32_t digit);
120*0Sstevel@tonic-gate extern uint32_t (*big_mul_add_vec_impl)
121*0Sstevel@tonic-gate 	(uint32_t *r, uint32_t *a, int len, uint32_t digit);
122*0Sstevel@tonic-gate extern void (*big_mul_vec_impl)
123*0Sstevel@tonic-gate 	(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen);
124*0Sstevel@tonic-gate extern void (*big_sqr_vec_impl)
125*0Sstevel@tonic-gate 	(uint32_t *r, uint32_t *a, int len);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate #else /* ! HWCAP */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate #define	BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit)
130*0Sstevel@tonic-gate #define	BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit)
131*0Sstevel@tonic-gate #define	BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen)
132*0Sstevel@tonic-gate #define	BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len)
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate extern uint32_t big_mul_set_vec(uint32_t *r, uint32_t *a, int len, uint32_t d);
135*0Sstevel@tonic-gate extern uint32_t big_mul_add_vec(uint32_t *r, uint32_t *a, int len, uint32_t d);
136*0Sstevel@tonic-gate extern void big_mul_vec(uint32_t *r, uint32_t *a, int alen,
137*0Sstevel@tonic-gate     uint32_t *b, int blen);
138*0Sstevel@tonic-gate extern void big_sqr_vec(uint32_t *r, uint32_t *a, int len);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate #endif /* HWCAP */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate #ifdef	__cplusplus
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate #endif
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate #endif	/* _BIGNUM_H */
147