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