1 /*- 2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #ifndef FAUXBN_H_ 26 #define FAUXBN_H_ 20100108 27 28 #include <sys/types.h> 29 30 #ifndef _KERNEL 31 # include <inttypes.h> 32 # include <stdio.h> 33 #endif 34 35 #ifndef __BEGIN_DECLS 36 # if defined(__cplusplus) 37 # define __BEGIN_DECLS extern "C" { 38 # define __END_DECLS } 39 # else 40 # define __BEGIN_DECLS 41 # define __END_DECLS 42 # endif 43 #endif 44 45 __BEGIN_DECLS 46 47 /* should be 32bit on ILP32, 64bit on LP64 */ 48 typedef unsigned long mp_digit; 49 typedef uint64_t mp_word; 50 51 /* multi-precision integer */ 52 typedef struct mp_int { 53 mp_digit *dp; /* array of digits */ 54 int used; /* # of digits used */ 55 int alloc; /* # of digits allocated */ 56 int sign; /* non-zero if negative */ 57 } mp_int; 58 59 #define BIGNUM mp_int 60 #define BN_ULONG mp_digit 61 62 /* a "context" of mp integers - never really used */ 63 typedef struct bn_ctx_t { 64 size_t count; 65 size_t arraysize; 66 BIGNUM **v; 67 } BN_CTX; 68 69 #define MP_LT -1 70 #define MP_EQ 0 71 #define MP_GT 1 72 73 #define MP_ZPOS 0 74 #define MP_NEG 1 75 76 #define MP_OKAY 0 77 #define MP_MEM -2 78 #define MP_VAL -3 79 #define MP_RANGE MP_VAL 80 81 /*********************************/ 82 83 #define BN_is_negative(x) ((x)->sign == MP_NEG) 84 #define BN_is_zero(a) (((a)->used == 0) ? 1 : 0) 85 #define BN_is_odd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0) 86 #define BN_is_even(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? 1 : 0) 87 88 BIGNUM *BN_new(void); 89 BIGNUM *BN_dup(const BIGNUM */*a*/); 90 int BN_copy(BIGNUM */*b*/, const BIGNUM */*a*/); 91 92 void BN_init(BIGNUM */*a*/); 93 void BN_free(BIGNUM */*a*/); 94 void BN_clear(BIGNUM */*a*/); 95 void BN_clear_free(BIGNUM */*a*/); 96 97 int BN_cmp(BIGNUM */*a*/, BIGNUM */*b*/); 98 99 BIGNUM *BN_bin2bn(const uint8_t */*buf*/, int /*size*/, BIGNUM */*bn*/); 100 int BN_bn2bin(const BIGNUM */*a*/, unsigned char */*b*/); 101 char *BN_bn2hex(const BIGNUM */*a*/); 102 char *BN_bn2dec(const BIGNUM */*a*/); 103 char *BN_bn2radix(const BIGNUM */*a*/, unsigned /*radix*/); 104 int BN_hex2bn(BIGNUM **/*a*/, const char */*str*/); 105 int BN_dec2bn(BIGNUM **/*a*/, const char */*str*/); 106 int BN_radix2bn(BIGNUM **/*a*/, const char */*str*/, unsigned /*radix*/); 107 #ifndef _KERNEL 108 int BN_print_fp(FILE */*fp*/, const BIGNUM */*a*/); 109 #endif 110 111 int BN_add(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/); 112 int BN_sub(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/); 113 int BN_mul(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/); 114 int BN_div(BIGNUM */*q*/, BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/); 115 void BN_swap(BIGNUM */*a*/, BIGNUM */*b*/); 116 int BN_bitop(BIGNUM */*r*/, const BIGNUM */*a*/, char /*op*/, const BIGNUM */*b*/); 117 int BN_lshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/); 118 int BN_lshift1(BIGNUM */*r*/, BIGNUM */*a*/); 119 int BN_rshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/); 120 int BN_rshift1(BIGNUM */*r*/, BIGNUM */*a*/); 121 int BN_set_word(BIGNUM */*a*/, BN_ULONG /*w*/); 122 void BN_set_negative(BIGNUM */*a*/, int /*n*/); 123 124 int BN_num_bytes(const BIGNUM */*a*/); 125 int BN_num_bits(const BIGNUM */*a*/); 126 127 int BN_mod_exp(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*p*/, BIGNUM */*m*/, BN_CTX */*ctx*/); 128 BIGNUM *BN_mod_inverse(BIGNUM */*ret*/, BIGNUM */*a*/, const BIGNUM */*n*/, BN_CTX */*ctx*/); 129 int BN_mod_mul(BIGNUM */*ret*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/); 130 int BN_mod_sub(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/); 131 132 int BN_raise(BIGNUM */*res*/, BIGNUM */*a*/, BIGNUM */*b*/); 133 int BN_factorial(BIGNUM */*fact*/, BIGNUM */*f*/); 134 135 BN_CTX *BN_CTX_new(void); 136 BIGNUM *BN_CTX_get(BN_CTX */*ctx*/); 137 void BN_CTX_start(BN_CTX */*ctx*/); 138 void BN_CTX_end(BN_CTX */*ctx*/); 139 void BN_CTX_init(BN_CTX */*c*/); 140 void BN_CTX_free(BN_CTX */*c*/); 141 142 int BN_rand(BIGNUM */*rnd*/, int /*bits*/, int /*top*/, int /*bottom*/); 143 int BN_rand_range(BIGNUM */*rnd*/, BIGNUM */*range*/); 144 145 int BN_is_prime(const BIGNUM */*a*/, int /*checks*/, void (*callback)(int, int, void *), BN_CTX */*ctx*/, void */*cb_arg*/); 146 147 const BIGNUM *BN_value_one(void); 148 int BN_is_bit_set(const BIGNUM */*a*/, int /*n*/); 149 150 __END_DECLS 151 152 #endif 153