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

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

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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