Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* fe_operations.h
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifndef WOLF_CRYPT_FE_OPERATIONS_H
wolfSSL 15:117db924cf7c 24 #define WOLF_CRYPT_FE_OPERATIONS_H
wolfSSL 15:117db924cf7c 25
wolfSSL 15:117db924cf7c 26 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 27
wolfSSL 15:117db924cf7c 28 #if defined(HAVE_CURVE25519) || defined(HAVE_ED25519)
wolfSSL 15:117db924cf7c 29
wolfSSL 15:117db924cf7c 30 #if !defined(CURVE25519_SMALL) || !defined(ED25519_SMALL)
wolfSSL 15:117db924cf7c 31 #include <stdint.h>
wolfSSL 15:117db924cf7c 32 #endif
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 35
wolfSSL 15:117db924cf7c 36 #if defined(USE_INTEL_SPEEDUP) && !defined(NO_CURVED25519_X64)
wolfSSL 15:117db924cf7c 37 #define CURVED25519_X64
wolfSSL 15:117db924cf7c 38 #elif defined(HAVE___UINT128_T) && !defined(NO_CURVED25519_128BIT)
wolfSSL 15:117db924cf7c 39 #define CURVED25519_128BIT
wolfSSL 15:117db924cf7c 40 #endif
wolfSSL 15:117db924cf7c 41
wolfSSL 15:117db924cf7c 42 /*
wolfSSL 15:117db924cf7c 43 fe means field element.
wolfSSL 15:117db924cf7c 44 Here the field is \Z/(2^255-19).
wolfSSL 15:117db924cf7c 45 An element t, entries t[0]...t[9], represents the integer
wolfSSL 15:117db924cf7c 46 t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
wolfSSL 15:117db924cf7c 47 Bounds on each t[i] vary depending on context.
wolfSSL 15:117db924cf7c 48 */
wolfSSL 15:117db924cf7c 49
wolfSSL 15:117db924cf7c 50 #if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
wolfSSL 15:117db924cf7c 51 #define F25519_SIZE 32
wolfSSL 15:117db924cf7c 52
wolfSSL 15:117db924cf7c 53 WOLFSSL_LOCAL void lm_copy(byte*, const byte*);
wolfSSL 15:117db924cf7c 54 WOLFSSL_LOCAL void lm_add(byte*, const byte*, const byte*);
wolfSSL 15:117db924cf7c 55 WOLFSSL_LOCAL void lm_sub(byte*, const byte*, const byte*);
wolfSSL 15:117db924cf7c 56 WOLFSSL_LOCAL void lm_neg(byte*,const byte*);
wolfSSL 15:117db924cf7c 57 WOLFSSL_LOCAL void lm_invert(byte*, const byte*);
wolfSSL 15:117db924cf7c 58 WOLFSSL_LOCAL void lm_mul(byte*,const byte*,const byte*);
wolfSSL 15:117db924cf7c 59 #endif
wolfSSL 15:117db924cf7c 60
wolfSSL 15:117db924cf7c 61
wolfSSL 15:117db924cf7c 62 #if !defined(FREESCALE_LTC_ECC)
wolfSSL 15:117db924cf7c 63 WOLFSSL_LOCAL void fe_init(void);
wolfSSL 15:117db924cf7c 64
wolfSSL 15:117db924cf7c 65 WOLFSSL_LOCAL int curve25519(byte * q, byte * n, byte * p);
wolfSSL 15:117db924cf7c 66 #endif
wolfSSL 15:117db924cf7c 67
wolfSSL 15:117db924cf7c 68 /* default to be faster but take more memory */
wolfSSL 15:117db924cf7c 69 #if !defined(CURVE25519_SMALL) || !defined(ED25519_SMALL)
wolfSSL 15:117db924cf7c 70
wolfSSL 15:117db924cf7c 71 #ifdef CURVED25519_X64
wolfSSL 15:117db924cf7c 72 typedef int64_t fe[4];
wolfSSL 15:117db924cf7c 73 #elif defined(CURVED25519_128BIT)
wolfSSL 15:117db924cf7c 74 typedef int64_t fe[5];
wolfSSL 15:117db924cf7c 75 #else
wolfSSL 15:117db924cf7c 76 typedef int32_t fe[10];
wolfSSL 15:117db924cf7c 77 #endif
wolfSSL 15:117db924cf7c 78
wolfSSL 15:117db924cf7c 79 WOLFSSL_LOCAL void fe_copy(fe, const fe);
wolfSSL 15:117db924cf7c 80 WOLFSSL_LOCAL void fe_add(fe, const fe, const fe);
wolfSSL 15:117db924cf7c 81 WOLFSSL_LOCAL void fe_neg(fe,const fe);
wolfSSL 15:117db924cf7c 82 WOLFSSL_LOCAL void fe_sub(fe, const fe, const fe);
wolfSSL 15:117db924cf7c 83 WOLFSSL_LOCAL void fe_invert(fe, const fe);
wolfSSL 15:117db924cf7c 84 WOLFSSL_LOCAL void fe_mul(fe,const fe,const fe);
wolfSSL 15:117db924cf7c 85
wolfSSL 15:117db924cf7c 86
wolfSSL 15:117db924cf7c 87 /* Based On Daniel J Bernstein's curve25519 and ed25519 Public Domain ref10
wolfSSL 15:117db924cf7c 88 work. */
wolfSSL 15:117db924cf7c 89
wolfSSL 15:117db924cf7c 90 WOLFSSL_LOCAL void fe_0(fe);
wolfSSL 15:117db924cf7c 91 WOLFSSL_LOCAL void fe_1(fe);
wolfSSL 15:117db924cf7c 92 WOLFSSL_LOCAL int fe_isnonzero(const fe);
wolfSSL 15:117db924cf7c 93 WOLFSSL_LOCAL int fe_isnegative(const fe);
wolfSSL 15:117db924cf7c 94 WOLFSSL_LOCAL void fe_tobytes(unsigned char *, const fe);
wolfSSL 15:117db924cf7c 95 WOLFSSL_LOCAL void fe_sq(fe, const fe);
wolfSSL 15:117db924cf7c 96 WOLFSSL_LOCAL void fe_sq2(fe,const fe);
wolfSSL 15:117db924cf7c 97 WOLFSSL_LOCAL void fe_frombytes(fe,const unsigned char *);
wolfSSL 15:117db924cf7c 98 WOLFSSL_LOCAL void fe_cswap(fe, fe, int);
wolfSSL 15:117db924cf7c 99 WOLFSSL_LOCAL void fe_mul121666(fe,fe);
wolfSSL 15:117db924cf7c 100 WOLFSSL_LOCAL void fe_cmov(fe,const fe, int);
wolfSSL 15:117db924cf7c 101 WOLFSSL_LOCAL void fe_pow22523(fe,const fe);
wolfSSL 15:117db924cf7c 102
wolfSSL 15:117db924cf7c 103 /* 64 type needed for SHA512 */
wolfSSL 15:117db924cf7c 104 WOLFSSL_LOCAL uint64_t load_3(const unsigned char *in);
wolfSSL 15:117db924cf7c 105 WOLFSSL_LOCAL uint64_t load_4(const unsigned char *in);
wolfSSL 15:117db924cf7c 106
wolfSSL 15:117db924cf7c 107 #ifdef CURVED25519_X64
wolfSSL 15:117db924cf7c 108 WOLFSSL_LOCAL void fe_ge_to_p2(fe rx, fe ry, fe rz, const fe px, const fe py,
wolfSSL 15:117db924cf7c 109 const fe pz, const fe pt);
wolfSSL 15:117db924cf7c 110 WOLFSSL_LOCAL void fe_ge_to_p3(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 111 const fe py, const fe pz, const fe pt);
wolfSSL 15:117db924cf7c 112 WOLFSSL_LOCAL void fe_ge_dbl(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 113 const fe py, const fe pz);
wolfSSL 15:117db924cf7c 114 WOLFSSL_LOCAL void fe_ge_madd(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 115 const fe py, const fe pz, const fe pt,
wolfSSL 15:117db924cf7c 116 const fe qxy2d, const fe qyplusx,
wolfSSL 15:117db924cf7c 117 const fe qyminusx);
wolfSSL 15:117db924cf7c 118 WOLFSSL_LOCAL void fe_ge_msub(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 119 const fe py, const fe pz, const fe pt,
wolfSSL 15:117db924cf7c 120 const fe qxy2d, const fe qyplusx,
wolfSSL 15:117db924cf7c 121 const fe qyminusx);
wolfSSL 15:117db924cf7c 122 WOLFSSL_LOCAL void fe_ge_add(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 123 const fe py, const fe pz, const fe pt, const fe qz,
wolfSSL 15:117db924cf7c 124 const fe qt2d, const fe qyplusx,
wolfSSL 15:117db924cf7c 125 const fe qyminusx);
wolfSSL 15:117db924cf7c 126 WOLFSSL_LOCAL void fe_ge_sub(fe rx, fe ry, fe rz, fe rt, const fe px,
wolfSSL 15:117db924cf7c 127 const fe py, const fe pz, const fe pt, const fe qz,
wolfSSL 15:117db924cf7c 128 const fe qt2d, const fe qyplusx,
wolfSSL 15:117db924cf7c 129 const fe qyminusx);
wolfSSL 15:117db924cf7c 130 WOLFSSL_LOCAL void fe_cmov_table(fe* r, fe* base, signed char b);
wolfSSL 15:117db924cf7c 131 #endif /* CURVED25519_X64 */
wolfSSL 15:117db924cf7c 132 #endif /* !CURVE25519_SMALL || !ED25519_SMALL */
wolfSSL 15:117db924cf7c 133
wolfSSL 15:117db924cf7c 134 /* Use less memory and only 32bit types or less, but is slower
wolfSSL 15:117db924cf7c 135 Based on Daniel Beer's public domain work. */
wolfSSL 15:117db924cf7c 136 #if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
wolfSSL 15:117db924cf7c 137 static const byte c25519_base_x[F25519_SIZE] = {9};
wolfSSL 15:117db924cf7c 138 static const byte f25519_zero[F25519_SIZE] = {0};
wolfSSL 15:117db924cf7c 139 static const byte f25519_one[F25519_SIZE] = {1};
wolfSSL 15:117db924cf7c 140 static const byte fprime_zero[F25519_SIZE] = {0};
wolfSSL 15:117db924cf7c 141 static const byte fprime_one[F25519_SIZE] = {1};
wolfSSL 15:117db924cf7c 142
wolfSSL 15:117db924cf7c 143 WOLFSSL_LOCAL void fe_load(byte *x, word32 c);
wolfSSL 15:117db924cf7c 144 WOLFSSL_LOCAL void fe_normalize(byte *x);
wolfSSL 15:117db924cf7c 145 WOLFSSL_LOCAL void fe_inv__distinct(byte *r, const byte *x);
wolfSSL 15:117db924cf7c 146
wolfSSL 15:117db924cf7c 147 /* Conditional copy. If condition == 0, then zero is copied to dst. If
wolfSSL 15:117db924cf7c 148 * condition == 1, then one is copied to dst. Any other value results in
wolfSSL 15:117db924cf7c 149 * undefined behavior.
wolfSSL 15:117db924cf7c 150 */
wolfSSL 15:117db924cf7c 151 WOLFSSL_LOCAL void fe_select(byte *dst, const byte *zero, const byte *one,
wolfSSL 15:117db924cf7c 152 byte condition);
wolfSSL 15:117db924cf7c 153
wolfSSL 15:117db924cf7c 154 /* Multiply a point by a small constant. The two pointers are not
wolfSSL 15:117db924cf7c 155 * required to be distinct.
wolfSSL 15:117db924cf7c 156 *
wolfSSL 15:117db924cf7c 157 * The constant must be less than 2^24.
wolfSSL 15:117db924cf7c 158 */
wolfSSL 15:117db924cf7c 159 WOLFSSL_LOCAL void fe_mul_c(byte *r, const byte *a, word32 b);
wolfSSL 15:117db924cf7c 160 WOLFSSL_LOCAL void fe_mul__distinct(byte *r, const byte *a, const byte *b);
wolfSSL 15:117db924cf7c 161
wolfSSL 15:117db924cf7c 162 /* Compute one of the square roots of the field element, if the element
wolfSSL 15:117db924cf7c 163 * is square. The other square is -r.
wolfSSL 15:117db924cf7c 164 *
wolfSSL 15:117db924cf7c 165 * If the input is not square, the returned value is a valid field
wolfSSL 15:117db924cf7c 166 * element, but not the correct answer. If you don't already know that
wolfSSL 15:117db924cf7c 167 * your element is square, you should square the return value and test.
wolfSSL 15:117db924cf7c 168 */
wolfSSL 15:117db924cf7c 169 WOLFSSL_LOCAL void fe_sqrt(byte *r, const byte *x);
wolfSSL 15:117db924cf7c 170
wolfSSL 15:117db924cf7c 171 /* Conditional copy. If condition == 0, then zero is copied to dst. If
wolfSSL 15:117db924cf7c 172 * condition == 1, then one is copied to dst. Any other value results in
wolfSSL 15:117db924cf7c 173 * undefined behavior.
wolfSSL 15:117db924cf7c 174 */
wolfSSL 15:117db924cf7c 175 WOLFSSL_LOCAL void fprime_select(byte *dst, const byte *zero, const byte *one,
wolfSSL 15:117db924cf7c 176 byte condition);
wolfSSL 15:117db924cf7c 177 WOLFSSL_LOCAL void fprime_add(byte *r, const byte *a, const byte *modulus);
wolfSSL 15:117db924cf7c 178 WOLFSSL_LOCAL void fprime_sub(byte *r, const byte *a, const byte *modulus);
wolfSSL 15:117db924cf7c 179 WOLFSSL_LOCAL void fprime_mul(byte *r, const byte *a, const byte *b,
wolfSSL 15:117db924cf7c 180 const byte *modulus);
wolfSSL 15:117db924cf7c 181 WOLFSSL_LOCAL void fprime_copy(byte *x, const byte *a);
wolfSSL 15:117db924cf7c 182
wolfSSL 15:117db924cf7c 183 #endif /* CURVE25519_SMALL || ED25519_SMALL */
wolfSSL 15:117db924cf7c 184 #endif /* HAVE_CURVE25519 || HAVE_ED25519 */
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186 #endif /* WOLF_CRYPT_FE_OPERATIONS_H */
wolfSSL 15:117db924cf7c 187