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 #ifdef USE_BN_INTERFACE 48 #define BIGNUM PGPV_BIGNUM 49 #define BN_ULONG PGPV_BN_ULONG 50 #define BN_CTX PGPV_BN_CTX 51 #define BN_is_negative PGPV_BN_is_negative 52 #define BN_is_zero PGPV_BN_is_zero 53 #define BN_is_odd PGPV_BN_is_odd 54 #define BN_is_even PGPV_BN_is_even 55 #define BN_new PGPV_BN_new 56 #define BN_dup PGPV_BN_dup 57 #define BN_copy PGPV_BN_copy 58 #define BN_init PGPV_BN_init 59 #define BN_free PGPV_BN_free 60 #define BN_clear PGPV_BN_clear 61 #define BN_clear_free PGPV_BN_clear_free 62 #define BN_cmp PGPV_BN_cmp 63 #define BN_bn2bin PGPV_BN_bn2bin 64 #define BN_bn2hex PGPV_BN_bn2hex 65 #define BN_bn2dec PGPV_BN_bn2dec 66 #define BN_bn2radix PGPV_BN_bn2radix 67 #define BN_hex2bn PGPV_BN_hex2bn 68 #define BN_dec2bn PGPV_BN_dec2bn 69 #define BN_radix2bn PGPV_BN_radix2bn 70 #ifndef _KERNEL 71 #define BN_print_fp PGPV_BN_print_fp 72 #endif 73 #define BN_add PGPV_BN_add 74 #define BN_sub PGPV_BN_sub 75 #define BN_mul PGPV_BN_mul 76 #define BN_div PGPV_BN_div 77 #define BN_swap PGPV_BN_swap 78 #define BN_bitop PGPV_BN_bitop 79 #define BN_lshift PGPV_BN_lshift 80 #define BN_lshift1 PGPV_BN_lshift1 81 #define BN_rshift PGPV_BN_rshift 82 #define BN_rshift1 PGPV_BN_rshift1 83 #define BN_set_word PGPV_BN_set_word 84 #define BN_set_negative PGPV_BN_set_negative 85 #define BN_num_bytes PGPV_BN_num_bytes 86 #define BN_num_bits PGPV_BN_num_bits 87 #define BN_mod_exp PGPV_BN_mod_exp 88 #define BN_mod_inverse PGPV_BN_mod_inverse 89 #define BN_mod_mul PGPV_BN_mod_mul 90 #define BN_mod_sub PGPV_BN_mod_sub 91 #define BN_raise PGPV_BN_raise 92 #define BN_factorial PGPV_BN_factorial 93 #define BN_CTX_new PGPV_BN_CTX_new 94 #define BN_CTX_get PGPV_BN_CTX_get 95 #define BN_CTX_start PGPV_BN_CTX_start 96 #define BN_CTX_end PGPV_BN_CTX_end 97 #define BN_CTX_init PGPV_BN_CTX_init 98 #define BN_CTX_free PGPV_BN_CTX_free 99 #define BN_rand PGPV_BN_rand 100 #define BN_rand_range PGPV_BN_rand_range 101 #define BN_is_prime PGPV_BN_is_prime 102 #define BN_value_one PGPV_BN_value_one 103 #define BN_is_bit_set PGPV_BN_is_bit_set 104 #define BN_gcd PGPV_BN_gcd 105 #endif /* USE_BN_INTERFACE */ 106 107 /* should be 32bit on ILP32, 64bit on LP64 */ 108 typedef unsigned long mp_digit; 109 typedef uint64_t mp_word; 110 111 /* multi-precision integer */ 112 typedef struct mp_int { 113 mp_digit *dp; /* array of digits */ 114 int used; /* # of digits used */ 115 int alloc; /* # of digits allocated */ 116 int sign; /* non-zero if negative */ 117 } mp_int; 118 119 #define PGPV_BIGNUM mp_int 120 #define PGPV_BN_ULONG mp_digit 121 122 /* a "context" of mp integers - never really used */ 123 typedef struct bn_ctx_t { 124 size_t count; 125 size_t arraysize; 126 PGPV_BIGNUM **v; 127 } PGPV_BN_CTX; 128 129 #define MP_LT -1 130 #define MP_EQ 0 131 #define MP_GT 1 132 133 #define MP_ZPOS 0 134 #define MP_NEG 1 135 136 #define MP_OKAY 0 137 #define MP_MEM -2 138 #define MP_VAL -3 139 #define MP_RANGE MP_VAL 140 141 /*********************************/ 142 143 #define PGPV_BN_is_negative(x) ((x)->sign == MP_NEG) 144 #define PGPV_BN_is_zero(a) (((a)->used == 0) ? 1 : 0) 145 #define PGPV_BN_is_odd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0) 146 #define PGPV_BN_is_even(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? 1 : 0) 147 148 PGPV_BIGNUM *PGPV_BN_new(void); 149 PGPV_BIGNUM *PGPV_BN_dup(const PGPV_BIGNUM */*a*/); 150 int PGPV_BN_copy(PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*a*/); 151 152 void PGPV_BN_init(PGPV_BIGNUM */*a*/); 153 void PGPV_BN_free(PGPV_BIGNUM */*a*/); 154 void PGPV_BN_clear(PGPV_BIGNUM */*a*/); 155 void PGPV_BN_clear_free(PGPV_BIGNUM */*a*/); 156 157 int PGPV_BN_cmp(PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); 158 159 PGPV_BIGNUM *PGPV_BN_bin2bn(const uint8_t */*buf*/, int /*size*/, PGPV_BIGNUM */*bn*/); 160 int PGPV_BN_bn2bin(const PGPV_BIGNUM */*a*/, unsigned char */*b*/); 161 char *PGPV_BN_bn2hex(const PGPV_BIGNUM */*a*/); 162 char *PGPV_BN_bn2dec(const PGPV_BIGNUM */*a*/); 163 char *PGPV_BN_bn2radix(const PGPV_BIGNUM */*a*/, unsigned /*radix*/); 164 int PGPV_BN_hex2bn(PGPV_BIGNUM **/*a*/, const char */*str*/); 165 int PGPV_BN_dec2bn(PGPV_BIGNUM **/*a*/, const char */*str*/); 166 int PGPV_BN_radix2bn(PGPV_BIGNUM **/*a*/, const char */*str*/, unsigned /*radix*/); 167 #ifndef _KERNEL 168 int PGPV_BN_print_fp(FILE */*fp*/, const PGPV_BIGNUM */*a*/); 169 #endif 170 171 int PGPV_BN_add(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/); 172 int PGPV_BN_sub(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/); 173 int PGPV_BN_mul(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); 174 int PGPV_BN_div(PGPV_BIGNUM */*q*/, PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); 175 void PGPV_BN_swap(PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); 176 int PGPV_BN_bitop(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, char /*op*/, const PGPV_BIGNUM */*b*/); 177 int PGPV_BN_lshift(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, int /*n*/); 178 int PGPV_BN_lshift1(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/); 179 int PGPV_BN_rshift(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, int /*n*/); 180 int PGPV_BN_rshift1(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/); 181 int PGPV_BN_set_word(PGPV_BIGNUM */*a*/, PGPV_BN_ULONG /*w*/); 182 void PGPV_BN_set_negative(PGPV_BIGNUM */*a*/, int /*n*/); 183 184 int PGPV_BN_num_bytes(const PGPV_BIGNUM */*a*/); 185 int PGPV_BN_num_bits(const PGPV_BIGNUM */*a*/); 186 187 int PGPV_BN_mod_exp(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*p*/, PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); 188 PGPV_BIGNUM *PGPV_BN_mod_inverse(PGPV_BIGNUM */*ret*/, PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*n*/, PGPV_BN_CTX */*ctx*/); 189 int PGPV_BN_mod_mul(PGPV_BIGNUM */*ret*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); 190 int PGPV_BN_mod_sub(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); 191 192 int PGPV_BN_raise(PGPV_BIGNUM */*res*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); 193 int PGPV_BN_factorial(PGPV_BIGNUM */*fact*/, PGPV_BIGNUM */*f*/); 194 195 PGPV_BN_CTX *PGPV_BN_CTX_new(void); 196 PGPV_BIGNUM *PGPV_BN_CTX_get(PGPV_BN_CTX */*ctx*/); 197 void PGPV_BN_CTX_start(PGPV_BN_CTX */*ctx*/); 198 void PGPV_BN_CTX_end(PGPV_BN_CTX */*ctx*/); 199 void PGPV_BN_CTX_init(PGPV_BN_CTX */*c*/); 200 void PGPV_BN_CTX_free(PGPV_BN_CTX */*c*/); 201 202 int PGPV_BN_rand(PGPV_BIGNUM */*rnd*/, int /*bits*/, int /*top*/, int /*bottom*/); 203 int PGPV_BN_rand_range(PGPV_BIGNUM */*rnd*/, PGPV_BIGNUM */*range*/); 204 205 int PGPV_BN_is_prime(const PGPV_BIGNUM */*a*/, int /*checks*/, void (*callback)(int, int, void *), PGPV_BN_CTX */*ctx*/, void */*cb_arg*/); 206 207 const PGPV_BIGNUM *PGPV_BN_value_one(void); 208 int PGPV_BN_is_bit_set(const PGPV_BIGNUM */*a*/, int /*n*/); 209 210 int PGPV_BN_gcd(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); 211 212 __END_DECLS 213 214 #endif 215