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