wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfssl/wolfcrypt/fe_operations.h@13:80fb167dafdf, 2017-05-30 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 30 06:16:19 2017 +0000
- Revision:
- 13:80fb167dafdf
- Parent:
- 11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 11:cee25a834751 | 1 | /* fe_operations.h |
wolfSSL | 11:cee25a834751 | 2 | * |
wolfSSL | 11:cee25a834751 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 11:cee25a834751 | 4 | * |
wolfSSL | 11:cee25a834751 | 5 | * This file is part of wolfSSL. |
wolfSSL | 11:cee25a834751 | 6 | * |
wolfSSL | 11:cee25a834751 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 11:cee25a834751 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 11:cee25a834751 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 11:cee25a834751 | 10 | * (at your option) any later version. |
wolfSSL | 11:cee25a834751 | 11 | * |
wolfSSL | 11:cee25a834751 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 11:cee25a834751 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 11:cee25a834751 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 11:cee25a834751 | 15 | * GNU General Public License for more details. |
wolfSSL | 11:cee25a834751 | 16 | * |
wolfSSL | 11:cee25a834751 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 11:cee25a834751 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 11:cee25a834751 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 11:cee25a834751 | 20 | */ |
wolfSSL | 11:cee25a834751 | 21 | |
wolfSSL | 11:cee25a834751 | 22 | |
wolfSSL | 11:cee25a834751 | 23 | #ifndef WOLF_CRYPT_FE_OPERATIONS_H |
wolfSSL | 11:cee25a834751 | 24 | #define WOLF_CRYPT_FE_OPERATIONS_H |
wolfSSL | 11:cee25a834751 | 25 | |
wolfSSL | 11:cee25a834751 | 26 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 11:cee25a834751 | 27 | |
wolfSSL | 11:cee25a834751 | 28 | #if defined(HAVE_CURVE25519) || defined(HAVE_ED25519) |
wolfSSL | 11:cee25a834751 | 29 | |
wolfSSL | 11:cee25a834751 | 30 | #ifndef CURVED25519_SMALL |
wolfSSL | 11:cee25a834751 | 31 | #include <stdint.h> |
wolfSSL | 11:cee25a834751 | 32 | #endif |
wolfSSL | 11:cee25a834751 | 33 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 11:cee25a834751 | 34 | |
wolfSSL | 11:cee25a834751 | 35 | /* |
wolfSSL | 11:cee25a834751 | 36 | fe means field element. |
wolfSSL | 11:cee25a834751 | 37 | Here the field is \Z/(2^255-19). |
wolfSSL | 11:cee25a834751 | 38 | An element t, entries t[0]...t[9], represents the integer |
wolfSSL | 11:cee25a834751 | 39 | t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9]. |
wolfSSL | 11:cee25a834751 | 40 | Bounds on each t[i] vary depending on context. |
wolfSSL | 11:cee25a834751 | 41 | */ |
wolfSSL | 11:cee25a834751 | 42 | |
wolfSSL | 11:cee25a834751 | 43 | #ifdef CURVED25519_SMALL |
wolfSSL | 11:cee25a834751 | 44 | #define F25519_SIZE 32 |
wolfSSL | 11:cee25a834751 | 45 | typedef byte fe[32]; |
wolfSSL | 11:cee25a834751 | 46 | #elif defined(HAVE___UINT128_T) |
wolfSSL | 11:cee25a834751 | 47 | typedef int64_t fe[5]; |
wolfSSL | 11:cee25a834751 | 48 | #else |
wolfSSL | 11:cee25a834751 | 49 | typedef int32_t fe[10]; |
wolfSSL | 11:cee25a834751 | 50 | #endif |
wolfSSL | 11:cee25a834751 | 51 | |
wolfSSL | 11:cee25a834751 | 52 | #if! defined FREESCALE_LTC_ECC |
wolfSSL | 11:cee25a834751 | 53 | WOLFSSL_LOCAL int curve25519(byte * q, byte * n, byte * p); |
wolfSSL | 11:cee25a834751 | 54 | #endif |
wolfSSL | 11:cee25a834751 | 55 | WOLFSSL_LOCAL void fe_copy(fe, const fe); |
wolfSSL | 11:cee25a834751 | 56 | WOLFSSL_LOCAL void fe_add(fe, const fe, const fe); |
wolfSSL | 11:cee25a834751 | 57 | WOLFSSL_LOCAL void fe_neg(fe,const fe); |
wolfSSL | 11:cee25a834751 | 58 | WOLFSSL_LOCAL void fe_sub(fe, const fe, const fe); |
wolfSSL | 11:cee25a834751 | 59 | WOLFSSL_LOCAL void fe_invert(fe, const fe); |
wolfSSL | 11:cee25a834751 | 60 | WOLFSSL_LOCAL void fe_mul(fe,const fe,const fe); |
wolfSSL | 11:cee25a834751 | 61 | |
wolfSSL | 11:cee25a834751 | 62 | /* default to be faster but take more memory */ |
wolfSSL | 11:cee25a834751 | 63 | #ifndef CURVED25519_SMALL |
wolfSSL | 11:cee25a834751 | 64 | |
wolfSSL | 11:cee25a834751 | 65 | /* Based On Daniel J Bernstein's curve25519 and ed25519 Public Domain ref10 |
wolfSSL | 11:cee25a834751 | 66 | work. */ |
wolfSSL | 11:cee25a834751 | 67 | |
wolfSSL | 11:cee25a834751 | 68 | WOLFSSL_LOCAL void fe_0(fe); |
wolfSSL | 11:cee25a834751 | 69 | WOLFSSL_LOCAL void fe_1(fe); |
wolfSSL | 11:cee25a834751 | 70 | WOLFSSL_LOCAL int fe_isnonzero(const fe); |
wolfSSL | 11:cee25a834751 | 71 | WOLFSSL_LOCAL int fe_isnegative(const fe); |
wolfSSL | 11:cee25a834751 | 72 | WOLFSSL_LOCAL void fe_tobytes(unsigned char *, const fe); |
wolfSSL | 11:cee25a834751 | 73 | WOLFSSL_LOCAL void fe_sq(fe, const fe); |
wolfSSL | 11:cee25a834751 | 74 | WOLFSSL_LOCAL void fe_sq2(fe,const fe); |
wolfSSL | 11:cee25a834751 | 75 | WOLFSSL_LOCAL void fe_frombytes(fe,const unsigned char *); |
wolfSSL | 11:cee25a834751 | 76 | WOLFSSL_LOCAL void fe_cswap(fe, fe, int); |
wolfSSL | 11:cee25a834751 | 77 | WOLFSSL_LOCAL void fe_mul121666(fe,fe); |
wolfSSL | 11:cee25a834751 | 78 | WOLFSSL_LOCAL void fe_cmov(fe,const fe, int); |
wolfSSL | 11:cee25a834751 | 79 | WOLFSSL_LOCAL void fe_pow22523(fe,const fe); |
wolfSSL | 11:cee25a834751 | 80 | |
wolfSSL | 11:cee25a834751 | 81 | /* 64 type needed for SHA512 */ |
wolfSSL | 11:cee25a834751 | 82 | WOLFSSL_LOCAL uint64_t load_3(const unsigned char *in); |
wolfSSL | 11:cee25a834751 | 83 | WOLFSSL_LOCAL uint64_t load_4(const unsigned char *in); |
wolfSSL | 11:cee25a834751 | 84 | #endif /* not defined CURVED25519_SMALL */ |
wolfSSL | 11:cee25a834751 | 85 | |
wolfSSL | 11:cee25a834751 | 86 | /* Use less memory and only 32bit types or less, but is slower |
wolfSSL | 11:cee25a834751 | 87 | Based on Daniel Beer's public domain work. */ |
wolfSSL | 11:cee25a834751 | 88 | #ifdef CURVED25519_SMALL |
wolfSSL | 11:cee25a834751 | 89 | static const byte c25519_base_x[F25519_SIZE] = {9}; |
wolfSSL | 11:cee25a834751 | 90 | static const byte f25519_zero[F25519_SIZE] = {0}; |
wolfSSL | 11:cee25a834751 | 91 | static const byte f25519_one[F25519_SIZE] = {1}; |
wolfSSL | 11:cee25a834751 | 92 | static const byte fprime_zero[F25519_SIZE] = {0}; |
wolfSSL | 11:cee25a834751 | 93 | static const byte fprime_one[F25519_SIZE] = {1}; |
wolfSSL | 11:cee25a834751 | 94 | |
wolfSSL | 11:cee25a834751 | 95 | WOLFSSL_LOCAL void fe_load(byte *x, word32 c); |
wolfSSL | 11:cee25a834751 | 96 | WOLFSSL_LOCAL void fe_normalize(byte *x); |
wolfSSL | 11:cee25a834751 | 97 | WOLFSSL_LOCAL void fe_inv__distinct(byte *r, const byte *x); |
wolfSSL | 11:cee25a834751 | 98 | |
wolfSSL | 11:cee25a834751 | 99 | /* Conditional copy. If condition == 0, then zero is copied to dst. If |
wolfSSL | 11:cee25a834751 | 100 | * condition == 1, then one is copied to dst. Any other value results in |
wolfSSL | 11:cee25a834751 | 101 | * undefined behavior. |
wolfSSL | 11:cee25a834751 | 102 | */ |
wolfSSL | 11:cee25a834751 | 103 | WOLFSSL_LOCAL void fe_select(byte *dst, const byte *zero, const byte *one, |
wolfSSL | 11:cee25a834751 | 104 | byte condition); |
wolfSSL | 11:cee25a834751 | 105 | |
wolfSSL | 11:cee25a834751 | 106 | /* Multiply a point by a small constant. The two pointers are not |
wolfSSL | 11:cee25a834751 | 107 | * required to be distinct. |
wolfSSL | 11:cee25a834751 | 108 | * |
wolfSSL | 11:cee25a834751 | 109 | * The constant must be less than 2^24. |
wolfSSL | 11:cee25a834751 | 110 | */ |
wolfSSL | 11:cee25a834751 | 111 | WOLFSSL_LOCAL void fe_mul_c(byte *r, const byte *a, word32 b); |
wolfSSL | 11:cee25a834751 | 112 | WOLFSSL_LOCAL void fe_mul__distinct(byte *r, const byte *a, const byte *b); |
wolfSSL | 11:cee25a834751 | 113 | |
wolfSSL | 11:cee25a834751 | 114 | /* Compute one of the square roots of the field element, if the element |
wolfSSL | 11:cee25a834751 | 115 | * is square. The other square is -r. |
wolfSSL | 11:cee25a834751 | 116 | * |
wolfSSL | 11:cee25a834751 | 117 | * If the input is not square, the returned value is a valid field |
wolfSSL | 11:cee25a834751 | 118 | * element, but not the correct answer. If you don't already know that |
wolfSSL | 11:cee25a834751 | 119 | * your element is square, you should square the return value and test. |
wolfSSL | 11:cee25a834751 | 120 | */ |
wolfSSL | 11:cee25a834751 | 121 | WOLFSSL_LOCAL void fe_sqrt(byte *r, const byte *x); |
wolfSSL | 11:cee25a834751 | 122 | |
wolfSSL | 11:cee25a834751 | 123 | /* Conditional copy. If condition == 0, then zero is copied to dst. If |
wolfSSL | 11:cee25a834751 | 124 | * condition == 1, then one is copied to dst. Any other value results in |
wolfSSL | 11:cee25a834751 | 125 | * undefined behavior. |
wolfSSL | 11:cee25a834751 | 126 | */ |
wolfSSL | 11:cee25a834751 | 127 | WOLFSSL_LOCAL void fprime_select(byte *dst, const byte *zero, const byte *one, |
wolfSSL | 11:cee25a834751 | 128 | byte condition); |
wolfSSL | 11:cee25a834751 | 129 | WOLFSSL_LOCAL void fprime_add(byte *r, const byte *a, const byte *modulus); |
wolfSSL | 11:cee25a834751 | 130 | WOLFSSL_LOCAL void fprime_sub(byte *r, const byte *a, const byte *modulus); |
wolfSSL | 11:cee25a834751 | 131 | WOLFSSL_LOCAL void fprime_mul(byte *r, const byte *a, const byte *b, |
wolfSSL | 11:cee25a834751 | 132 | const byte *modulus); |
wolfSSL | 11:cee25a834751 | 133 | WOLFSSL_LOCAL void fprime_copy(byte *x, const byte *a); |
wolfSSL | 11:cee25a834751 | 134 | #endif /* CURVED25519_SMALL */ |
wolfSSL | 11:cee25a834751 | 135 | #endif /* HAVE_CURVE25519 or HAVE_ED25519 */ |
wolfSSL | 11:cee25a834751 | 136 | #endif /* WOLF_CRYPT_FE_OPERATIONS_H */ |
wolfSSL | 11:cee25a834751 | 137 | |
wolfSSL | 11:cee25a834751 | 138 |