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 */ 2112573SDina.Nimeh@Sun.COM 220Sstevel@tonic-gate /* 2312573SDina.Nimeh@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 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 358933Sopensolaris@drydog.com #if defined(__sparcv9) || defined(__amd64) /* 64-bit chunk size */ 368933Sopensolaris@drydog.com #ifndef UMUL64 378933Sopensolaris@drydog.com #define UMUL64 /* 64-bit multiplication results are supported */ 388933Sopensolaris@drydog.com #endif 396557Sfr41279 #else 408933Sopensolaris@drydog.com #define BIGNUM_CHUNK_32 416557Sfr41279 #endif 426557Sfr41279 436557Sfr41279 446557Sfr41279 #define BITSINBYTE 8 456557Sfr41279 468933Sopensolaris@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 558933Sopensolaris@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 6612573SDina.Nimeh@Sun.COM #define BITLEN2BIGNUMLEN(x) ((x) > 0 ? \ 6712573SDina.Nimeh@Sun.COM ((((x) - 1) / BIG_CHUNK_SIZE) + 1) : 0) 6812573SDina.Nimeh@Sun.COM #define CHARLEN2BIGNUMLEN(x) ((x) > 0 ? \ 6912573SDina.Nimeh@Sun.COM ((((x) - 1) / sizeof (BIG_CHUNK_TYPE)) + 1) : 0) 706557Sfr41279 716557Sfr41279 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */ 7212573SDina.Nimeh@Sun.COM #define BIG_CHUNKS_FOR_160BITS BITLEN2BIGNUMLEN(160) 730Sstevel@tonic-gate 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * leading 0's are permitted 770Sstevel@tonic-gate * 0 should be represented by size>=1, size>=len>=1, sign=1, 780Sstevel@tonic-gate * value[i]=0 for 0<i<len 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate typedef struct { 816557Sfr41279 /* size and len in units of BIG_CHUNK_TYPE words */ 828933Sopensolaris@drydog.com uint32_t size; /* size of memory allocated for value */ 838933Sopensolaris@drydog.com uint32_t len; /* number of valid data words in value */ 848933Sopensolaris@drydog.com int sign; /* 1 for nonnegative, -1 for negative */ 858933Sopensolaris@drydog.com int malloced; /* 1 if value was malloced, 0 if not */ 866557Sfr41279 BIG_CHUNK_TYPE *value; 870Sstevel@tonic-gate } BIGNUM; 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define BIGTMPSIZE 65 900Sstevel@tonic-gate 910Sstevel@tonic-gate #define BIG_TRUE 1 920Sstevel@tonic-gate #define BIG_FALSE 0 930Sstevel@tonic-gate 946557Sfr41279 typedef int BIG_ERR_CODE; 956557Sfr41279 960Sstevel@tonic-gate /* error codes */ 970Sstevel@tonic-gate #define BIG_OK 0 980Sstevel@tonic-gate #define BIG_NO_MEM -1 990Sstevel@tonic-gate #define BIG_INVALID_ARGS -2 1000Sstevel@tonic-gate #define BIG_DIV_BY_0 -3 1010Sstevel@tonic-gate #define BIG_NO_RANDOM -4 1020Sstevel@tonic-gate #define BIG_GENERAL_ERR -5 1036557Sfr41279 #define BIG_TEST_FAILED -6 1046557Sfr41279 #define BIG_BUFFER_TOO_SMALL -7 1056557Sfr41279 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #define arraysize(x) (sizeof (x) / sizeof (x[0])) 1080Sstevel@tonic-gate 1096557Sfr41279 typedef BIG_ERR_CODE (*big_modexp_ncp_func_ptr)(BIGNUM *result, 1106557Sfr41279 BIGNUM *ma, BIGNUM *e, BIGNUM *n, 1116557Sfr41279 BIGNUM *tmp, BIG_CHUNK_TYPE n0, void *ncp, void *req); 1126557Sfr41279 1136557Sfr41279 typedef struct { 1146557Sfr41279 big_modexp_ncp_func_ptr func; 1156557Sfr41279 void *ncp; 1166557Sfr41279 void *reqp; 1176557Sfr41279 } big_modexp_ncp_info_t; 1186557Sfr41279 1196557Sfr41279 1200Sstevel@tonic-gate #ifdef USE_FLOATING_POINT 1210Sstevel@tonic-gate void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen); 1220Sstevel@tonic-gate void conv_i32_to_d32(double *d32, uint32_t *i32, int len); 1230Sstevel@tonic-gate void conv_i32_to_d16(double *d16, uint32_t *i32, int len); 1240Sstevel@tonic-gate void conv_i32_to_d32_and_d16(double *d32, double *d16, 1250Sstevel@tonic-gate uint32_t *i32, int len); 1260Sstevel@tonic-gate void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt, 1270Sstevel@tonic-gate double *dn, uint32_t *nint, int nlen, double dn0); 1280Sstevel@tonic-gate #endif /* USE_FLOATING_POINT */ 1290Sstevel@tonic-gate 1306557Sfr41279 extern BIGNUM big_One; 1316557Sfr41279 extern BIGNUM big_Two; 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); 15212573SDina.Nimeh@Sun.COM BIG_ERR_CODE big_random(BIGNUM *r, size_t length, int (*rfunc)(void *, size_t)); 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 176*12929SMisaki.Miyashita@Oracle.COM /* 177*12929SMisaki.Miyashita@Oracle.COM * Kernel bignum module: module integrity test 178*12929SMisaki.Miyashita@Oracle.COM */ 179*12929SMisaki.Miyashita@Oracle.COM extern int bignum_fips_check(void); 180*12929SMisaki.Miyashita@Oracle.COM 1810Sstevel@tonic-gate #if defined(HWCAP) 1820Sstevel@tonic-gate 1836557Sfr41279 #if (BIG_CHUNK_SIZE != 32) 1846557Sfr41279 #error HWCAP works only with 32-bit bignum chunks 1856557Sfr41279 #endif 1866557Sfr41279 1870Sstevel@tonic-gate #define BIG_MUL_SET_VEC(r, a, len, digit) \ 1880Sstevel@tonic-gate (*big_mul_set_vec_impl)(r, a, len, digit) 1890Sstevel@tonic-gate #define BIG_MUL_ADD_VEC(r, a, len, digit) \ 1900Sstevel@tonic-gate (*big_mul_add_vec_impl)(r, a, len, digit) 1910Sstevel@tonic-gate #define BIG_MUL_VEC(r, a, alen, b, blen) \ 1920Sstevel@tonic-gate (*big_mul_vec_impl)(r, a, alen, b, blen) 1930Sstevel@tonic-gate #define BIG_SQR_VEC(r, a, len) \ 1940Sstevel@tonic-gate (*big_sqr_vec_impl)(r, a, len) 1950Sstevel@tonic-gate 1966557Sfr41279 extern BIG_CHUNK_TYPE (*big_mul_set_vec_impl) 1976557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); 1986557Sfr41279 extern BIG_CHUNK_TYPE (*big_mul_add_vec_impl) 1996557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE digit); 2000Sstevel@tonic-gate extern void (*big_mul_vec_impl) 2016557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, BIG_CHUNK_TYPE *b, 2026557Sfr41279 int blen); 2030Sstevel@tonic-gate extern void (*big_sqr_vec_impl) 2046557Sfr41279 (BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate #else /* ! HWCAP */ 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate #define BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit) 2090Sstevel@tonic-gate #define BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit) 2100Sstevel@tonic-gate #define BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen) 2110Sstevel@tonic-gate #define BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len) 2120Sstevel@tonic-gate 2136557Sfr41279 extern BIG_CHUNK_TYPE big_mul_set_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, 2146557Sfr41279 int len, BIG_CHUNK_TYPE d); 2156557Sfr41279 extern BIG_CHUNK_TYPE big_mul_add_vec(BIG_CHUNK_TYPE *r, 2166557Sfr41279 BIG_CHUNK_TYPE *a, int len, BIG_CHUNK_TYPE d); 2176557Sfr41279 extern void big_mul_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen, 2186557Sfr41279 BIG_CHUNK_TYPE *b, int blen); 2196557Sfr41279 extern void big_sqr_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate #endif /* HWCAP */ 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate #ifdef __cplusplus 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate #endif 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate #endif /* _BIGNUM_H */ 228