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