10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56557Sfr41279 * Common Development and Distribution License (the "License"). 66557Sfr41279 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*8933Sopensolaris@drydog.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _BIGNUM_H 270Sstevel@tonic-gate #define _BIGNUM_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #ifdef __cplusplus 300Sstevel@tonic-gate extern "C" { 310Sstevel@tonic-gate #endif 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate 35*8933Sopensolaris@drydog.com #if defined(__sparcv9) || defined(__amd64) /* 64-bit chunk size */ 36*8933Sopensolaris@drydog.com #ifndef UMUL64 37*8933Sopensolaris@drydog.com #define UMUL64 /* 64-bit multiplication results are supported */ 38*8933Sopensolaris@drydog.com #endif 396557Sfr41279 #else 40*8933Sopensolaris@drydog.com #define BIGNUM_CHUNK_32 416557Sfr41279 #endif 426557Sfr41279 436557Sfr41279 446557Sfr41279 #define BITSINBYTE 8 456557Sfr41279 46*8933Sopensolaris@drydog.com /* Bignum "digits" (aka "chunks" or "words") are either 32- or 64-bits */ 476557Sfr41279 #ifdef BIGNUM_CHUNK_32 486557Sfr41279 #define BIG_CHUNK_SIZE 32 496557Sfr41279 #define BIG_CHUNK_TYPE uint32_t 506557Sfr41279 #define BIG_CHUNK_TYPE_SIGNED int32_t 516557Sfr41279 #define BIG_CHUNK_HIGHBIT 0x80000000 526557Sfr41279 #define BIG_CHUNK_ALLBITS 0xffffffff 536557Sfr41279 #define BIG_CHUNK_LOWHALFBITS 0xffff 546557Sfr41279 #define BIG_CHUNK_HALF_HIGHBIT 0x8000 55*8933Sopensolaris@drydog.com 566557Sfr41279 #else 576557Sfr41279 #define BIG_CHUNK_SIZE 64 586557Sfr41279 #define BIG_CHUNK_TYPE uint64_t 596557Sfr41279 #define BIG_CHUNK_TYPE_SIGNED int64_t 606557Sfr41279 #define BIG_CHUNK_HIGHBIT 0x8000000000000000ULL 616557Sfr41279 #define BIG_CHUNK_ALLBITS 0xffffffffffffffffULL 626557Sfr41279 #define BIG_CHUNK_LOWHALFBITS 0xffffffffULL 636557Sfr41279 #define BIG_CHUNK_HALF_HIGHBIT 0x80000000ULL 646557Sfr41279 #endif 656557Sfr41279 666557Sfr41279 #define BITLEN2BIGNUMLEN(x) (((x) + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE) 676557Sfr41279 #define CHARLEN2BIGNUMLEN(x) (((x) + sizeof (BIG_CHUNK_TYPE) - 1) / \ 68*8933Sopensolaris@drydog.com sizeof (BIG_CHUNK_TYPE)) 696557Sfr41279 706557Sfr41279 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */ 716557Sfr41279 #define BIG_CHUNKS_FOR_160BITS ((160 + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE) 720Sstevel@tonic-gate 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * leading 0's are permitted 760Sstevel@tonic-gate * 0 should be represented by size>=1, size>=len>=1, sign=1, 770Sstevel@tonic-gate * value[i]=0 for 0<i<len 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate typedef struct { 806557Sfr41279 /* size and len in units of BIG_CHUNK_TYPE words */ 81*8933Sopensolaris@drydog.com uint32_t size; /* size of memory allocated for value */ 82*8933Sopensolaris@drydog.com uint32_t len; /* number of valid data words in value */ 83*8933Sopensolaris@drydog.com int sign; /* 1 for nonnegative, -1 for negative */ 84*8933Sopensolaris@drydog.com int malloced; /* 1 if value was malloced, 0 if not */ 856557Sfr41279 BIG_CHUNK_TYPE *value; 860Sstevel@tonic-gate } BIGNUM; 870Sstevel@tonic-gate 880Sstevel@tonic-gate #define BIGTMPSIZE 65 890Sstevel@tonic-gate 900Sstevel@tonic-gate #define BIG_TRUE 1 910Sstevel@tonic-gate #define BIG_FALSE 0 920Sstevel@tonic-gate 936557Sfr41279 typedef int BIG_ERR_CODE; 946557Sfr41279 950Sstevel@tonic-gate /* error codes */ 960Sstevel@tonic-gate #define BIG_OK 0 970Sstevel@tonic-gate #define BIG_NO_MEM -1 980Sstevel@tonic-gate #define BIG_INVALID_ARGS -2 990Sstevel@tonic-gate #define BIG_DIV_BY_0 -3 1000Sstevel@tonic-gate #define BIG_NO_RANDOM -4 1010Sstevel@tonic-gate #define BIG_GENERAL_ERR -5 1026557Sfr41279 #define BIG_TEST_FAILED -6 1036557Sfr41279 #define BIG_BUFFER_TOO_SMALL -7 1046557Sfr41279 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #define arraysize(x) (sizeof (x) / sizeof (x[0])) 1070Sstevel@tonic-gate 1086557Sfr41279 typedef BIG_ERR_CODE (*big_modexp_ncp_func_ptr)(BIGNUM *result, 1096557Sfr41279 BIGNUM *ma, BIGNUM *e, BIGNUM *n, 1106557Sfr41279 BIGNUM *tmp, BIG_CHUNK_TYPE n0, void *ncp, void *req); 1116557Sfr41279 1126557Sfr41279 typedef struct { 1136557Sfr41279 big_modexp_ncp_func_ptr func; 1146557Sfr41279 void *ncp; 1156557Sfr41279 void *reqp; 1166557Sfr41279 } big_modexp_ncp_info_t; 1176557Sfr41279 1186557Sfr41279 1190Sstevel@tonic-gate #ifdef USE_FLOATING_POINT 1200Sstevel@tonic-gate void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen); 1210Sstevel@tonic-gate void conv_i32_to_d32(double *d32, uint32_t *i32, int len); 1220Sstevel@tonic-gate void conv_i32_to_d16(double *d16, uint32_t *i32, int len); 1230Sstevel@tonic-gate void conv_i32_to_d32_and_d16(double *d32, double *d16, 1240Sstevel@tonic-gate uint32_t *i32, int len); 1250Sstevel@tonic-gate void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt, 1260Sstevel@tonic-gate double *dn, uint32_t *nint, int nlen, double dn0); 1270Sstevel@tonic-gate #endif /* USE_FLOATING_POINT */ 1280Sstevel@tonic-gate 1296557Sfr41279 extern BIGNUM big_One; 1306557Sfr41279 extern BIGNUM big_Two; 1316557Sfr41279 1326557Sfr41279 1330Sstevel@tonic-gate void printbignum(char *aname, BIGNUM *a); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate BIG_ERR_CODE big_init(BIGNUM *number, int size); 1360Sstevel@tonic-gate BIG_ERR_CODE big_extend(BIGNUM *number, int size); 1370Sstevel@tonic-gate void big_finish(BIGNUM *number); 1380Sstevel@tonic-gate void bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len); 1390Sstevel@tonic-gate void bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len); 1400Sstevel@tonic-gate BIG_ERR_CODE big_mont_rr(BIGNUM *result, BIGNUM *n); 1410Sstevel@tonic-gate BIG_ERR_CODE big_modexp(BIGNUM *result, BIGNUM *a, BIGNUM *e, 1420Sstevel@tonic-gate BIGNUM *n, BIGNUM *n_rr); 1436557Sfr41279 BIG_ERR_CODE big_modexp_ext(BIGNUM *result, BIGNUM *a, BIGNUM *e, 1446557Sfr41279 BIGNUM *n, BIGNUM *n_rr, big_modexp_ncp_info_t *info); 1450Sstevel@tonic-gate BIG_ERR_CODE big_modexp_crt(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1, 1460Sstevel@tonic-gate BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq, 1470Sstevel@tonic-gate BIGNUM *p_rr, BIGNUM *q_rr); 1486557Sfr41279 BIG_ERR_CODE big_modexp_crt_ext(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1, 1496557Sfr41279 BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq, 1506557Sfr41279 BIGNUM *p_rr, BIGNUM *q_rr, big_modexp_ncp_info_t *info); 1510Sstevel@tonic-gate int big_cmp_abs(BIGNUM *a, BIGNUM *b); 1520Sstevel@tonic-gate BIG_ERR_CODE randombignum(BIGNUM *r, int length); 1530Sstevel@tonic-gate BIG_ERR_CODE big_div_pos(BIGNUM *result, BIGNUM *remainder, 1540Sstevel@tonic-gate BIGNUM *aa, BIGNUM *bb); 1550Sstevel@tonic-gate BIG_ERR_CODE big_ext_gcd_pos(BIGNUM *gcd, BIGNUM *cm, BIGNUM *ce, 1560Sstevel@tonic-gate BIGNUM *m, BIGNUM *e); 1570Sstevel@tonic-gate BIG_ERR_CODE big_add(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); 1586557Sfr41279 BIG_ERR_CODE big_add_abs(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); 1590Sstevel@tonic-gate BIG_ERR_CODE big_mul(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); 1606557Sfr41279 void big_shiftright(BIGNUM *result, BIGNUM *aa, int offs); 1610Sstevel@tonic-gate BIG_ERR_CODE big_nextprime_pos(BIGNUM *result, BIGNUM *n); 1626557Sfr41279 BIG_ERR_CODE big_nextprime_pos_ext(BIGNUM *result, BIGNUM *n, 1636557Sfr41279 big_modexp_ncp_info_t *info); 1640Sstevel@tonic-gate BIG_ERR_CODE big_sub_pos(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); 1650Sstevel@tonic-gate BIG_ERR_CODE big_copy(BIGNUM *dest, BIGNUM *src); 1660Sstevel@tonic-gate BIG_ERR_CODE big_sub(BIGNUM *result, BIGNUM *aa, BIGNUM *bb); 1670Sstevel@tonic-gate int big_bitlength(BIGNUM *n); 1686557Sfr41279 BIG_ERR_CODE big_init1(BIGNUM *number, int size, 1696557Sfr41279 BIG_CHUNK_TYPE *buf, int bufsize); 1706557Sfr41279 BIG_ERR_CODE big_mont_mul(BIGNUM *ret, 1716557Sfr41279 BIGNUM *a, BIGNUM *b, BIGNUM *n, BIG_CHUNK_TYPE n0); 1726557Sfr41279 int big_is_zero(BIGNUM *n); 1736557Sfr41279 BIG_CHUNK_TYPE big_n0(BIG_CHUNK_TYPE n); 1746557Sfr41279 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate #if defined(HWCAP) 1770Sstevel@tonic-gate 1786557Sfr41279 #if (BIG_CHUNK_SIZE != 32) 1796557Sfr41279 #error HWCAP works only with 32-bit bignum chunks 1806557Sfr41279 #endif 1816557Sfr41279 1820Sstevel@tonic-gate #define BIG_MUL_SET_VEC(r, a, len, digit) \ 1830Sstevel@tonic-gate (*big_mul_set_vec_impl)(r, a, len, digit) 1840Sstevel@tonic-gate #define BIG_MUL_ADD_VEC(r, a, len, digit) \ 1850Sstevel@tonic-gate (*big_mul_add_vec_impl)(r, a, len, digit) 1860Sstevel@tonic-gate #define BIG_MUL_VEC(r, a, alen, b, blen) \ 1870Sstevel@tonic-gate (*big_mul_vec_impl)(r, a, alen, b, blen) 1880Sstevel@tonic-gate #define BIG_SQR_VEC(r, a, len) \ 1890Sstevel@tonic-gate (*big_sqr_vec_impl)(r, a, len) 1900Sstevel@tonic-gate 1916557Sfr41279 extern BIG_CHUNK_TYPE (*big_mul_set_vec_impl) 1926557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); 1936557Sfr41279 extern BIG_CHUNK_TYPE (*big_mul_add_vec_impl) 1946557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); 1950Sstevel@tonic-gate extern void (*big_mul_vec_impl) 1966557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, BIG_CHUNK_TYPE *b, 1976557Sfr41279 int blen); 1980Sstevel@tonic-gate extern void (*big_sqr_vec_impl) 1996557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate #else /* ! HWCAP */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate #define BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit) 2040Sstevel@tonic-gate #define BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit) 2050Sstevel@tonic-gate #define BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen) 2060Sstevel@tonic-gate #define BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len) 2070Sstevel@tonic-gate 2086557Sfr41279 extern BIG_CHUNK_TYPE big_mul_set_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, 2096557Sfr41279 int len, BIG_CHUNK_TYPE d); 2106557Sfr41279 extern BIG_CHUNK_TYPE big_mul_add_vec(BIG_CHUNK_TYPE *r, 2116557Sfr41279 BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE d); 2126557Sfr41279 extern void big_mul_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, 2136557Sfr41279 BIG_CHUNK_TYPE *b, int blen); 2146557Sfr41279 extern void big_sqr_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate #endif /* HWCAP */ 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate #ifdef __cplusplus 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate #endif 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate #endif /* _BIGNUM_H */ 223