Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
14:167253f4e170
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 14:167253f4e170 1 /* sp_int.h
wolfSSL 14:167253f4e170 2 *
wolfSSL 14:167253f4e170 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 14:167253f4e170 4 *
wolfSSL 14:167253f4e170 5 * This file is part of wolfSSL.
wolfSSL 14:167253f4e170 6 *
wolfSSL 14:167253f4e170 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 14:167253f4e170 8 * it under the terms of the GNU General Public License as published by
wolfSSL 14:167253f4e170 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 14:167253f4e170 10 * (at your option) any later version.
wolfSSL 14:167253f4e170 11 *
wolfSSL 14:167253f4e170 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 14:167253f4e170 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 14:167253f4e170 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 14:167253f4e170 15 * GNU General Public License for more details.
wolfSSL 14:167253f4e170 16 *
wolfSSL 14:167253f4e170 17 * You should have received a copy of the GNU General Public License
wolfSSL 14:167253f4e170 18 * along with this program; if not, write to the Free Software
wolfSSL 14:167253f4e170 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 14:167253f4e170 20 */
wolfSSL 14:167253f4e170 21
wolfSSL 14:167253f4e170 22
wolfSSL 14:167253f4e170 23 #ifndef WOLF_CRYPT_SP_INT_H
wolfSSL 14:167253f4e170 24 #define WOLF_CRYPT_SP_INT_H
wolfSSL 14:167253f4e170 25
wolfSSL 14:167253f4e170 26 #include <stdint.h>
wolfSSL 14:167253f4e170 27 #include <limits.h>
wolfSSL 14:167253f4e170 28
wolfSSL 14:167253f4e170 29 #ifdef WOLFSSL_SP_X86_64_ASM
wolfSSL 14:167253f4e170 30 #define SP_WORD_SIZE 64
wolfSSL 14:167253f4e170 31
wolfSSL 14:167253f4e170 32 #define HAVE_INTEL_AVX1
wolfSSL 14:167253f4e170 33 #define HAVE_INTEL_AVX2
wolfSSL 14:167253f4e170 34 #elif defined(WOLFSSL_SP_ARM64_ASM)
wolfSSL 14:167253f4e170 35 #define SP_WORD_SIZE 64
wolfSSL 14:167253f4e170 36 #elif defined(WOLFSSL_SP_ARM32_ASM)
wolfSSL 14:167253f4e170 37 #define SP_WORD_SIZE 32
wolfSSL 14:167253f4e170 38 #endif
wolfSSL 14:167253f4e170 39
wolfSSL 14:167253f4e170 40 #ifndef SP_WORD_SIZE
wolfSSL 14:167253f4e170 41 #if defined(NO_64BIT) || !defined(HAVE___UINT128_T)
wolfSSL 14:167253f4e170 42 #define SP_WORD_SIZE 32
wolfSSL 14:167253f4e170 43 #else
wolfSSL 14:167253f4e170 44 #define SP_WORD_SIZE 64
wolfSSL 14:167253f4e170 45 #endif
wolfSSL 14:167253f4e170 46 #endif
wolfSSL 14:167253f4e170 47
wolfSSL 14:167253f4e170 48 #ifndef WOLFSSL_SP_ASM
wolfSSL 14:167253f4e170 49 #if SP_WORD_SIZE == 32
wolfSSL 14:167253f4e170 50 typedef int32_t sp_digit;
wolfSSL 14:167253f4e170 51 typedef uint32_t sp_int_digit;
wolfSSL 14:167253f4e170 52 #elif SP_WORD_SIZE == 64
wolfSSL 14:167253f4e170 53 typedef int64_t sp_digit;
wolfSSL 14:167253f4e170 54 typedef uint64_t sp_int_digit;
wolfSSL 14:167253f4e170 55 typedef unsigned long uint128_t __attribute__ ((mode(TI)));
wolfSSL 14:167253f4e170 56 typedef long int128_t __attribute__ ((mode(TI)));
wolfSSL 14:167253f4e170 57 #else
wolfSSL 14:167253f4e170 58 #error Word size not defined
wolfSSL 14:167253f4e170 59 #endif
wolfSSL 14:167253f4e170 60 #else
wolfSSL 14:167253f4e170 61 #if SP_WORD_SIZE == 32
wolfSSL 14:167253f4e170 62 typedef uint32_t sp_digit;
wolfSSL 14:167253f4e170 63 typedef uint32_t sp_int_digit;
wolfSSL 14:167253f4e170 64 #elif SP_WORD_SIZE == 64
wolfSSL 14:167253f4e170 65 typedef uint64_t sp_digit;
wolfSSL 14:167253f4e170 66 typedef uint64_t sp_int_digit;
wolfSSL 14:167253f4e170 67 typedef unsigned long uint128_t __attribute__ ((mode(TI)));
wolfSSL 14:167253f4e170 68 typedef long int128_t __attribute__ ((mode(TI)));
wolfSSL 14:167253f4e170 69 #else
wolfSSL 14:167253f4e170 70 #error Word size not defined
wolfSSL 14:167253f4e170 71 #endif
wolfSSL 14:167253f4e170 72 #endif
wolfSSL 14:167253f4e170 73
wolfSSL 14:167253f4e170 74 #ifdef WOLFSSL_SP_MATH
wolfSSL 14:167253f4e170 75 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 14:167253f4e170 76
wolfSSL 14:167253f4e170 77 #ifndef MIN
wolfSSL 14:167253f4e170 78 #define MIN(x,y) ((x)<(y)?(x):(y))
wolfSSL 14:167253f4e170 79 #endif
wolfSSL 14:167253f4e170 80
wolfSSL 14:167253f4e170 81 #ifndef MAX
wolfSSL 14:167253f4e170 82 #define MAX(x,y) ((x)>(y)?(x):(y))
wolfSSL 14:167253f4e170 83 #endif
wolfSSL 14:167253f4e170 84
wolfSSL 14:167253f4e170 85 #ifdef WOLFSSL_PUBLIC_MP
wolfSSL 14:167253f4e170 86 #define MP_API WOLFSSL_API
wolfSSL 14:167253f4e170 87 #else
wolfSSL 14:167253f4e170 88 #define MP_API WOLFSSL_LOCAL
wolfSSL 14:167253f4e170 89 #endif
wolfSSL 14:167253f4e170 90
wolfSSL 14:167253f4e170 91 #if !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH)
wolfSSL 14:167253f4e170 92 #if !defined(NO_PWDBASED) && defined(WOLFSSL_SHA512)
wolfSSL 14:167253f4e170 93 #define SP_INT_DIGITS ((512 + SP_WORD_SIZE) / SP_WORD_SIZE)
wolfSSL 14:167253f4e170 94 #else
wolfSSL 14:167253f4e170 95 #define SP_INT_DIGITS ((256 + SP_WORD_SIZE) / SP_WORD_SIZE)
wolfSSL 14:167253f4e170 96 #endif
wolfSSL 14:167253f4e170 97 #elif !defined(WOLFSSL_SP_NO_3072)
wolfSSL 14:167253f4e170 98 #define SP_INT_DIGITS ((2048 + SP_WORD_SIZE) / SP_WORD_SIZE)
wolfSSL 14:167253f4e170 99 #else
wolfSSL 14:167253f4e170 100 #define SP_INT_DIGITS ((3072 + SP_WORD_SIZE) / SP_WORD_SIZE)
wolfSSL 14:167253f4e170 101 #endif
wolfSSL 14:167253f4e170 102
wolfSSL 14:167253f4e170 103 #define sp_isodd(a) (a->used != 0 && (a->dp[0] & 1))
wolfSSL 14:167253f4e170 104
wolfSSL 14:167253f4e170 105 typedef struct sp_int {
wolfSSL 14:167253f4e170 106 sp_int_digit dp[SP_INT_DIGITS];
wolfSSL 14:167253f4e170 107 int size;
wolfSSL 14:167253f4e170 108 int used;
wolfSSL 14:167253f4e170 109 } sp_int;
wolfSSL 14:167253f4e170 110
wolfSSL 14:167253f4e170 111
wolfSSL 14:167253f4e170 112 MP_API int sp_init(sp_int* a);
wolfSSL 14:167253f4e170 113 MP_API int sp_init_multi(sp_int* a, sp_int* b, sp_int* c, sp_int* d,
wolfSSL 14:167253f4e170 114 sp_int* e, sp_int* f);
wolfSSL 14:167253f4e170 115 MP_API void sp_clear(sp_int* a);
wolfSSL 14:167253f4e170 116 MP_API int sp_unsigned_bin_size(sp_int* a);
wolfSSL 14:167253f4e170 117 MP_API int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz);
wolfSSL 14:167253f4e170 118 MP_API int sp_read_radix(sp_int* a, const char* in, int radix);
wolfSSL 14:167253f4e170 119 MP_API int sp_cmp(sp_int* a, sp_int* b);
wolfSSL 14:167253f4e170 120 MP_API int sp_count_bits(sp_int* a);
wolfSSL 14:167253f4e170 121 MP_API int sp_leading_bit(sp_int* a);
wolfSSL 14:167253f4e170 122 MP_API int sp_to_unsigned_bin(sp_int* a, byte* in);
wolfSSL 14:167253f4e170 123 MP_API void sp_forcezero(sp_int* a);
wolfSSL 14:167253f4e170 124 MP_API int sp_copy(sp_int* a, sp_int* b);
wolfSSL 14:167253f4e170 125 MP_API int sp_set(sp_int* a, sp_int_digit d);
wolfSSL 14:167253f4e170 126 MP_API int sp_iszero(sp_int* a);
wolfSSL 14:167253f4e170 127 MP_API void sp_clamp(sp_int* a);
wolfSSL 14:167253f4e170 128 MP_API int sp_grow(sp_int* a, int l);
wolfSSL 14:167253f4e170 129 MP_API int sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r);
wolfSSL 14:167253f4e170 130 MP_API int sp_cmp_d(sp_int* a, sp_int_digit d);
wolfSSL 14:167253f4e170 131 MP_API int sp_mod(sp_int* a, sp_int* m, sp_int* r);
wolfSSL 14:167253f4e170 132 MP_API void sp_zero(sp_int* a);
wolfSSL 14:167253f4e170 133 MP_API int sp_add_d(sp_int* a, sp_int_digit d, sp_int* r);
wolfSSL 14:167253f4e170 134 MP_API int sp_lshd(sp_int* a, int s);
wolfSSL 14:167253f4e170 135 MP_API int sp_add(sp_int* a, sp_int* b, sp_int* r);
wolfSSL 14:167253f4e170 136 MP_API int sp_set_int(sp_int* a, unsigned long b);
wolfSSL 14:167253f4e170 137
wolfSSL 14:167253f4e170 138 typedef sp_int mp_int;
wolfSSL 14:167253f4e170 139 typedef sp_digit mp_digit;
wolfSSL 14:167253f4e170 140
wolfSSL 14:167253f4e170 141 #define MP_OKAY 0
wolfSSL 14:167253f4e170 142 #define MP_NO 0
wolfSSL 14:167253f4e170 143 #define MP_YES 1
wolfSSL 14:167253f4e170 144
wolfSSL 14:167253f4e170 145 #define MP_RADIX_HEX 16
wolfSSL 14:167253f4e170 146
wolfSSL 14:167253f4e170 147 #define MP_GT 1
wolfSSL 14:167253f4e170 148 #define MP_EQ 0
wolfSSL 14:167253f4e170 149 #define MP_LT -1
wolfSSL 14:167253f4e170 150
wolfSSL 14:167253f4e170 151 #define MP_MEM -2
wolfSSL 14:167253f4e170 152 #define MP_VAL -3
wolfSSL 14:167253f4e170 153
wolfSSL 14:167253f4e170 154 #define DIGIT_BIT SP_WORD_SIZE
wolfSSL 14:167253f4e170 155
wolfSSL 14:167253f4e170 156 #define CheckFastMathSettings() 1
wolfSSL 14:167253f4e170 157
wolfSSL 14:167253f4e170 158 #define mp_free(a)
wolfSSL 14:167253f4e170 159
wolfSSL 14:167253f4e170 160 #define mp_init sp_init
wolfSSL 14:167253f4e170 161 #define mp_init_multi sp_init_multi
wolfSSL 14:167253f4e170 162 #define mp_clear sp_clear
wolfSSL 14:167253f4e170 163 #define mp_read_unsigned_bin sp_read_unsigned_bin
wolfSSL 14:167253f4e170 164 #define mp_unsigned_bin_size sp_unsigned_bin_size
wolfSSL 14:167253f4e170 165 #define mp_read_radix sp_read_radix
wolfSSL 14:167253f4e170 166 #define mp_cmp sp_cmp
wolfSSL 14:167253f4e170 167 #define mp_count_bits sp_count_bits
wolfSSL 14:167253f4e170 168 #define mp_leading_bit sp_leading_bit
wolfSSL 14:167253f4e170 169 #define mp_to_unsigned_bin sp_to_unsigned_bin
wolfSSL 14:167253f4e170 170 #define mp_forcezero sp_forcezero
wolfSSL 14:167253f4e170 171 #define mp_copy sp_copy
wolfSSL 14:167253f4e170 172 #define mp_set sp_set
wolfSSL 14:167253f4e170 173 #define mp_iszero sp_iszero
wolfSSL 14:167253f4e170 174 #define mp_clamp sp_clamp
wolfSSL 14:167253f4e170 175 #define mp_grow sp_grow
wolfSSL 14:167253f4e170 176 #define mp_sub_d sp_sub_d
wolfSSL 14:167253f4e170 177 #define mp_cmp_d sp_cmp_d
wolfSSL 14:167253f4e170 178 #define mp_mod sp_mod
wolfSSL 14:167253f4e170 179 #define mp_zero sp_zero
wolfSSL 14:167253f4e170 180 #define mp_add_d sp_add_d
wolfSSL 14:167253f4e170 181 #define mp_lshd sp_lshd
wolfSSL 14:167253f4e170 182 #define mp_add sp_add
wolfSSL 14:167253f4e170 183 #define mp_isodd sp_isodd
wolfSSL 14:167253f4e170 184 #define mp_set_int sp_set_int
wolfSSL 14:167253f4e170 185
wolfSSL 14:167253f4e170 186 #define MP_INT_DEFINED
wolfSSL 14:167253f4e170 187
wolfSSL 14:167253f4e170 188 #include <wolfssl/wolfcrypt/wolfmath.h>
wolfSSL 14:167253f4e170 189 #endif
wolfSSL 14:167253f4e170 190
wolfSSL 14:167253f4e170 191 #endif /* WOLF_CRYPT_SP_H */
wolfSSL 14:167253f4e170 192
wolfSSL 14:167253f4e170 193