ssh lib

Dependents:   OS

Committer:
sPymbed
Date:
Mon Nov 25 14:23:49 2019 +0000
Revision:
1:e4ea39eba2fb
Parent:
0:1387ff3eed4a
improved

Who changed what in which revision?

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