wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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