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