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

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

Committer:
wolfSSL
Date:
Thu Apr 28 00:57:21 2016 +0000
Revision:
4:1b0d80432c79
wolfSSL 3.9.0

Who changed what in which revision?

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