Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sp_int.h Source File

sp_int.h

00001 /* sp_int.h
00002  *
00003  * Copyright (C) 2006-2017 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifndef WOLF_CRYPT_SP_INT_H
00024 #define WOLF_CRYPT_SP_INT_H
00025 
00026 #include <stdint.h>
00027 #include <limits.h>
00028 
00029 #ifdef WOLFSSL_SP_X86_64_ASM
00030     #define SP_WORD_SIZE 64
00031 
00032     #define HAVE_INTEL_AVX1
00033     #define HAVE_INTEL_AVX2
00034 #elif defined(WOLFSSL_SP_ARM64_ASM)
00035     #define SP_WORD_SIZE 64
00036 #elif defined(WOLFSSL_SP_ARM32_ASM)
00037     #define SP_WORD_SIZE 32
00038 #endif
00039 
00040 #ifndef SP_WORD_SIZE
00041     #if defined(NO_64BIT) || !defined(HAVE___UINT128_T)
00042         #define SP_WORD_SIZE 32
00043     #else
00044         #define SP_WORD_SIZE 64
00045     #endif
00046 #endif
00047 
00048 #ifndef WOLFSSL_SP_ASM
00049   #if SP_WORD_SIZE == 32
00050     typedef int32_t sp_digit;
00051     typedef uint32_t sp_int_digit;
00052   #elif SP_WORD_SIZE == 64
00053     typedef int64_t sp_digit;
00054     typedef uint64_t sp_int_digit;
00055     typedef unsigned long uint128_t __attribute__ ((mode(TI)));
00056     typedef long int128_t __attribute__ ((mode(TI)));
00057   #else
00058     #error Word size not defined
00059   #endif
00060 #else
00061   #if SP_WORD_SIZE == 32
00062     typedef uint32_t sp_digit;
00063     typedef uint32_t sp_int_digit;
00064   #elif SP_WORD_SIZE == 64
00065     typedef uint64_t sp_digit;
00066     typedef uint64_t sp_int_digit;
00067     typedef unsigned long uint128_t __attribute__ ((mode(TI)));
00068     typedef long int128_t __attribute__ ((mode(TI)));
00069   #else
00070     #error Word size not defined
00071   #endif
00072 #endif
00073 
00074 #ifdef WOLFSSL_SP_MATH
00075 #include <wolfssl/wolfcrypt/random.h>
00076 
00077 #ifndef MIN
00078    #define MIN(x,y) ((x)<(y)?(x):(y))
00079 #endif
00080 
00081 #ifndef MAX
00082    #define MAX(x,y) ((x)>(y)?(x):(y))
00083 #endif
00084 
00085 #ifdef WOLFSSL_PUBLIC_MP
00086     #define MP_API   WOLFSSL_API
00087 #else
00088     #define MP_API   WOLFSSL_LOCAL
00089 #endif
00090 
00091 #if !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH)
00092     #if !defined(NO_PWDBASED) && defined(WOLFSSL_SHA512)
00093         #define SP_INT_DIGITS        ((512 + SP_WORD_SIZE) / SP_WORD_SIZE)
00094     #else
00095         #define SP_INT_DIGITS        ((256 + SP_WORD_SIZE) / SP_WORD_SIZE)
00096     #endif
00097 #elif !defined(WOLFSSL_SP_NO_3072)
00098     #define SP_INT_DIGITS        ((2048 + SP_WORD_SIZE) / SP_WORD_SIZE)
00099 #else
00100     #define SP_INT_DIGITS        ((3072 + SP_WORD_SIZE) / SP_WORD_SIZE)
00101 #endif
00102 
00103 #define sp_isodd(a) (a->used != 0 && (a->dp[0] & 1))
00104 
00105 typedef struct sp_int {
00106     sp_int_digit dp[SP_INT_DIGITS];
00107     int size;
00108     int used;
00109 } sp_int;
00110 
00111 
00112 MP_API int sp_init(sp_int* a);
00113 MP_API int sp_init_multi(sp_int* a, sp_int* b, sp_int* c, sp_int* d,
00114                          sp_int* e, sp_int* f);
00115 MP_API void sp_clear(sp_int* a);
00116 MP_API int sp_unsigned_bin_size(sp_int* a);
00117 MP_API int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz);
00118 MP_API int sp_read_radix(sp_int* a, const char* in, int radix);
00119 MP_API int sp_cmp(sp_int* a, sp_int* b);
00120 MP_API int sp_count_bits(sp_int* a);
00121 MP_API int sp_leading_bit(sp_int* a);
00122 MP_API int sp_to_unsigned_bin(sp_int* a, byte* in);
00123 MP_API void sp_forcezero(sp_int* a);
00124 MP_API int sp_copy(sp_int* a, sp_int* b);
00125 MP_API int sp_set(sp_int* a, sp_int_digit d);
00126 MP_API int sp_iszero(sp_int* a);
00127 MP_API void sp_clamp(sp_int* a);
00128 MP_API int sp_grow(sp_int* a, int l);
00129 MP_API int sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r);
00130 MP_API int sp_cmp_d(sp_int* a, sp_int_digit d);
00131 MP_API int sp_mod(sp_int* a, sp_int* m, sp_int* r);
00132 MP_API void sp_zero(sp_int* a);
00133 MP_API int sp_add_d(sp_int* a, sp_int_digit d, sp_int* r);
00134 MP_API int sp_lshd(sp_int* a, int s);
00135 MP_API int sp_add(sp_int* a, sp_int* b, sp_int* r);
00136 MP_API int sp_set_int(sp_int* a, unsigned long b);
00137 
00138 typedef sp_int mp_int;
00139 typedef sp_digit mp_digit;
00140 
00141 #define MP_OKAY    0
00142 #define MP_NO      0
00143 #define MP_YES     1
00144 
00145 #define MP_RADIX_HEX     16
00146 
00147 #define MP_GT    1
00148 #define MP_EQ    0
00149 #define MP_LT    -1
00150 
00151 #define MP_MEM   -2
00152 #define MP_VAL   -3
00153 
00154 #define DIGIT_BIT  SP_WORD_SIZE
00155 
00156 #define CheckFastMathSettings() 1
00157 
00158 #define mp_free(a)
00159 
00160 #define mp_init                 sp_init
00161 #define mp_init_multi           sp_init_multi
00162 #define mp_clear                sp_clear
00163 #define mp_read_unsigned_bin    sp_read_unsigned_bin
00164 #define mp_unsigned_bin_size    sp_unsigned_bin_size
00165 #define mp_read_radix           sp_read_radix
00166 #define mp_cmp                  sp_cmp
00167 #define mp_count_bits           sp_count_bits
00168 #define mp_leading_bit          sp_leading_bit
00169 #define mp_to_unsigned_bin      sp_to_unsigned_bin
00170 #define mp_forcezero            sp_forcezero
00171 #define mp_copy                 sp_copy
00172 #define mp_set                  sp_set
00173 #define mp_iszero               sp_iszero
00174 #define mp_clamp                sp_clamp
00175 #define mp_grow                 sp_grow
00176 #define mp_sub_d                sp_sub_d
00177 #define mp_cmp_d                sp_cmp_d
00178 #define mp_mod                  sp_mod
00179 #define mp_zero                 sp_zero
00180 #define mp_add_d                sp_add_d
00181 #define mp_lshd                 sp_lshd
00182 #define mp_add                  sp_add
00183 #define mp_isodd                sp_isodd
00184 #define mp_set_int              sp_set_int
00185 
00186 #define MP_INT_DEFINED
00187 
00188 #include <wolfssl/wolfcrypt/wolfmath.h>
00189 #endif
00190 
00191 #endif /* WOLF_CRYPT_SP_H */
00192 
00193