Home | History | Annotate | Line # | Download | only in nvmectl
      1 /*	$NetBSD: bn.h,v 1.1 2017/02/13 11:16:46 nonaka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 Alistair Crooks <agc (at) NetBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef FAUXBN_H_
     29 #define FAUXBN_H_	20100108
     30 
     31 #include <sys/types.h>
     32 #include <inttypes.h>
     33 #include <stdio.h>
     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 int BN_print_fp(FILE */*fp*/, const BIGNUM */*a*/);
    108 
    109 int BN_add(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/);
    110 int BN_sub(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/);
    111 int BN_mul(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/);
    112 int BN_div(BIGNUM */*q*/, BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/);
    113 void BN_swap(BIGNUM */*a*/, BIGNUM */*b*/);
    114 int BN_bitop(BIGNUM */*r*/, const BIGNUM */*a*/, char /*op*/, const BIGNUM */*b*/);
    115 int BN_lshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/);
    116 int BN_lshift1(BIGNUM */*r*/, BIGNUM */*a*/);
    117 int BN_rshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/);
    118 int BN_rshift1(BIGNUM */*r*/, BIGNUM */*a*/);
    119 int BN_set_word(BIGNUM */*a*/, BN_ULONG /*w*/);
    120 void BN_set_negative(BIGNUM */*a*/, int /*n*/);
    121 
    122 int BN_num_bytes(const BIGNUM */*a*/);
    123 int BN_num_bits(const BIGNUM */*a*/);
    124 
    125 int BN_mod_exp(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*p*/, BIGNUM */*m*/, BN_CTX */*ctx*/);
    126 BIGNUM *BN_mod_inverse(BIGNUM */*ret*/, BIGNUM */*a*/, const BIGNUM */*n*/, BN_CTX */*ctx*/);
    127 int BN_mod_mul(BIGNUM */*ret*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/);
    128 int BN_mod_sub(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/);
    129 
    130 int BN_raise(BIGNUM */*res*/, BIGNUM */*a*/, BIGNUM */*b*/);
    131 int BN_factorial(BIGNUM */*fact*/, BIGNUM */*f*/);
    132 
    133 BN_CTX *BN_CTX_new(void);
    134 BIGNUM *BN_CTX_get(BN_CTX */*ctx*/);
    135 void BN_CTX_start(BN_CTX */*ctx*/);
    136 void BN_CTX_end(BN_CTX */*ctx*/);
    137 void BN_CTX_init(BN_CTX */*c*/);
    138 void BN_CTX_free(BN_CTX */*c*/);
    139 
    140 int BN_rand(BIGNUM */*rnd*/, int /*bits*/, int /*top*/, int /*bottom*/);
    141 int BN_rand_range(BIGNUM */*rnd*/, BIGNUM */*range*/);
    142 
    143 int BN_is_prime(const BIGNUM */*a*/, int /*checks*/, void (*callback)(int, int, void *), BN_CTX */*ctx*/, void */*cb_arg*/);
    144 
    145 const BIGNUM *BN_value_one(void);
    146 int BN_is_bit_set(const BIGNUM */*a*/, int /*n*/);
    147 
    148 int BN_gcd(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*b*/, BN_CTX */*ctx*/);
    149 
    150 /* utilities */
    151 int humanize_bignum(char *, size_t, const BIGNUM *, const char *, int, int);
    152 
    153 __END_DECLS
    154 
    155 #endif
    156