wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 06:16:19 2017 +0000
Revision:
13:80fb167dafdf
Parent:
11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 11:cee25a834751 1 /* ge_low_mem.c
wolfSSL 11:cee25a834751 2 *
wolfSSL 11:cee25a834751 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 11:cee25a834751 4 *
wolfSSL 11:cee25a834751 5 * This file is part of wolfSSL.
wolfSSL 11:cee25a834751 6 *
wolfSSL 11:cee25a834751 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 11:cee25a834751 8 * it under the terms of the GNU General Public License as published by
wolfSSL 11:cee25a834751 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 11:cee25a834751 10 * (at your option) any later version.
wolfSSL 11:cee25a834751 11 *
wolfSSL 11:cee25a834751 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 11:cee25a834751 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 11:cee25a834751 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 11:cee25a834751 15 * GNU General Public License for more details.
wolfSSL 11:cee25a834751 16 *
wolfSSL 11:cee25a834751 17 * You should have received a copy of the GNU General Public License
wolfSSL 11:cee25a834751 18 * along with this program; if not, write to the Free Software
wolfSSL 11:cee25a834751 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 11:cee25a834751 20 */
wolfSSL 11:cee25a834751 21
wolfSSL 11:cee25a834751 22
wolfSSL 11:cee25a834751 23 /* Based from Daniel Beer's public domain work. */
wolfSSL 11:cee25a834751 24
wolfSSL 11:cee25a834751 25 #ifdef HAVE_CONFIG_H
wolfSSL 11:cee25a834751 26 #include <config.h>
wolfSSL 11:cee25a834751 27 #endif
wolfSSL 11:cee25a834751 28
wolfSSL 11:cee25a834751 29 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 11:cee25a834751 30
wolfSSL 11:cee25a834751 31 #if defined(CURVED25519_SMALL) /* use slower code that takes less memory */
wolfSSL 11:cee25a834751 32 #if defined(HAVE_ED25519)
wolfSSL 11:cee25a834751 33
wolfSSL 11:cee25a834751 34 #include <wolfssl/wolfcrypt/ge_operations.h>
wolfSSL 11:cee25a834751 35 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 11:cee25a834751 36 #ifdef NO_INLINE
wolfSSL 11:cee25a834751 37 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 11:cee25a834751 38 #else
wolfSSL 11:cee25a834751 39 #define WOLFSSL_MISC_INCLUDED
wolfSSL 11:cee25a834751 40 #include <wolfcrypt/src/misc.c>
wolfSSL 11:cee25a834751 41 #endif
wolfSSL 11:cee25a834751 42
wolfSSL 11:cee25a834751 43 void ed25519_smult(ge_p3 *r, const ge_p3 *a, const byte *e);
wolfSSL 11:cee25a834751 44 void ed25519_add(ge_p3 *r, const ge_p3 *a, const ge_p3 *b);
wolfSSL 11:cee25a834751 45 void ed25519_double(ge_p3 *r, const ge_p3 *a);
wolfSSL 11:cee25a834751 46
wolfSSL 11:cee25a834751 47
wolfSSL 11:cee25a834751 48 static const byte ed25519_order[F25519_SIZE] = {
wolfSSL 11:cee25a834751 49 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
wolfSSL 11:cee25a834751 50 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
wolfSSL 11:cee25a834751 51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
wolfSSL 11:cee25a834751 52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
wolfSSL 11:cee25a834751 53 };
wolfSSL 11:cee25a834751 54
wolfSSL 11:cee25a834751 55 /*Arithmetic modulo the group order m = 2^252 +
wolfSSL 11:cee25a834751 56 27742317777372353535851937790883648493 =
wolfSSL 11:cee25a834751 57 7237005577332262213973186563042994240857116359379907606001950938285454250989 */
wolfSSL 11:cee25a834751 58
wolfSSL 11:cee25a834751 59 static const word32 m[32] = {
wolfSSL 11:cee25a834751 60 0xED,0xD3,0xF5,0x5C,0x1A,0x63,0x12,0x58,0xD6,0x9C,0xF7,0xA2,0xDE,0xF9,
wolfSSL 11:cee25a834751 61 0xDE,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
wolfSSL 11:cee25a834751 62 0x00,0x00,0x00,0x10
wolfSSL 11:cee25a834751 63 };
wolfSSL 11:cee25a834751 64
wolfSSL 11:cee25a834751 65 static const word32 mu[33] = {
wolfSSL 11:cee25a834751 66 0x1B,0x13,0x2C,0x0A,0xA3,0xE5,0x9C,0xED,0xA7,0x29,0x63,0x08,0x5D,0x21,
wolfSSL 11:cee25a834751 67 0x06,0x21,0xEB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
wolfSSL 11:cee25a834751 68 0xFF,0xFF,0xFF,0xFF,0x0F
wolfSSL 11:cee25a834751 69 };
wolfSSL 11:cee25a834751 70
wolfSSL 11:cee25a834751 71
wolfSSL 11:cee25a834751 72 int ge_compress_key(byte* out, const byte* xIn, const byte* yIn,
wolfSSL 11:cee25a834751 73 word32 keySz)
wolfSSL 11:cee25a834751 74 {
wolfSSL 11:cee25a834751 75 byte tmp[F25519_SIZE];
wolfSSL 11:cee25a834751 76 byte parity;
wolfSSL 11:cee25a834751 77 byte pt[32];
wolfSSL 11:cee25a834751 78 int i;
wolfSSL 11:cee25a834751 79
wolfSSL 11:cee25a834751 80 fe_copy(tmp, xIn);
wolfSSL 11:cee25a834751 81 parity = (tmp[0] & 1) << 7;
wolfSSL 11:cee25a834751 82
wolfSSL 11:cee25a834751 83 fe_copy(pt, yIn);
wolfSSL 11:cee25a834751 84 pt[31] |= parity;
wolfSSL 11:cee25a834751 85
wolfSSL 11:cee25a834751 86 for(i = 0; i < 32; i++) {
wolfSSL 11:cee25a834751 87 out[32-i-1] = pt[i];
wolfSSL 11:cee25a834751 88 }
wolfSSL 11:cee25a834751 89 (void)keySz;
wolfSSL 11:cee25a834751 90 return 0;
wolfSSL 11:cee25a834751 91 }
wolfSSL 11:cee25a834751 92
wolfSSL 11:cee25a834751 93
wolfSSL 11:cee25a834751 94 static word32 lt(word32 a,word32 b) /* 16-bit inputs */
wolfSSL 11:cee25a834751 95 {
wolfSSL 11:cee25a834751 96 unsigned int x = a;
wolfSSL 11:cee25a834751 97 x -= (unsigned int) b; /* 0..65535: no; 4294901761..4294967295: yes */
wolfSSL 11:cee25a834751 98 x >>= 31; /* 0: no; 1: yes */
wolfSSL 11:cee25a834751 99 return x;
wolfSSL 11:cee25a834751 100 }
wolfSSL 11:cee25a834751 101
wolfSSL 11:cee25a834751 102
wolfSSL 11:cee25a834751 103 /* Reduce coefficients of r before calling reduce_add_sub */
wolfSSL 11:cee25a834751 104 static void reduce_add_sub(word32 *r)
wolfSSL 11:cee25a834751 105 {
wolfSSL 11:cee25a834751 106 word32 pb = 0;
wolfSSL 11:cee25a834751 107 word32 b;
wolfSSL 11:cee25a834751 108 word32 mask;
wolfSSL 11:cee25a834751 109 int i;
wolfSSL 11:cee25a834751 110 unsigned char t[32];
wolfSSL 11:cee25a834751 111
wolfSSL 11:cee25a834751 112 for(i=0;i<32;i++)
wolfSSL 11:cee25a834751 113 {
wolfSSL 11:cee25a834751 114 pb += m[i];
wolfSSL 11:cee25a834751 115 b = lt(r[i],pb);
wolfSSL 11:cee25a834751 116 t[i] = r[i]-pb+(b<<8);
wolfSSL 11:cee25a834751 117 pb = b;
wolfSSL 11:cee25a834751 118 }
wolfSSL 11:cee25a834751 119 mask = b - 1;
wolfSSL 11:cee25a834751 120 for(i=0;i<32;i++)
wolfSSL 11:cee25a834751 121 r[i] ^= mask & (r[i] ^ t[i]);
wolfSSL 11:cee25a834751 122 }
wolfSSL 11:cee25a834751 123
wolfSSL 11:cee25a834751 124
wolfSSL 11:cee25a834751 125 /* Reduce coefficients of x before calling barrett_reduce */
wolfSSL 11:cee25a834751 126 static void barrett_reduce(word32* r, word32 x[64])
wolfSSL 11:cee25a834751 127 {
wolfSSL 11:cee25a834751 128 /* See HAC, Alg. 14.42 */
wolfSSL 11:cee25a834751 129 int i,j;
wolfSSL 11:cee25a834751 130 word32 q2[66];
wolfSSL 11:cee25a834751 131 word32 *q3 = q2 + 33;
wolfSSL 11:cee25a834751 132 word32 r1[33];
wolfSSL 11:cee25a834751 133 word32 r2[33];
wolfSSL 11:cee25a834751 134 word32 carry;
wolfSSL 11:cee25a834751 135 word32 pb = 0;
wolfSSL 11:cee25a834751 136 word32 b;
wolfSSL 11:cee25a834751 137
wolfSSL 11:cee25a834751 138 for (i = 0;i < 66;++i) q2[i] = 0;
wolfSSL 11:cee25a834751 139 for (i = 0;i < 33;++i) r2[i] = 0;
wolfSSL 11:cee25a834751 140
wolfSSL 11:cee25a834751 141 for(i=0;i<33;i++)
wolfSSL 11:cee25a834751 142 for(j=0;j<33;j++)
wolfSSL 11:cee25a834751 143 if(i+j >= 31) q2[i+j] += mu[i]*x[j+31];
wolfSSL 11:cee25a834751 144 carry = q2[31] >> 8;
wolfSSL 11:cee25a834751 145 q2[32] += carry;
wolfSSL 11:cee25a834751 146 carry = q2[32] >> 8;
wolfSSL 11:cee25a834751 147 q2[33] += carry;
wolfSSL 11:cee25a834751 148
wolfSSL 11:cee25a834751 149 for(i=0;i<33;i++)r1[i] = x[i];
wolfSSL 11:cee25a834751 150 for(i=0;i<32;i++)
wolfSSL 11:cee25a834751 151 for(j=0;j<33;j++)
wolfSSL 11:cee25a834751 152 if(i+j < 33) r2[i+j] += m[i]*q3[j];
wolfSSL 11:cee25a834751 153
wolfSSL 11:cee25a834751 154 for(i=0;i<32;i++)
wolfSSL 11:cee25a834751 155 {
wolfSSL 11:cee25a834751 156 carry = r2[i] >> 8;
wolfSSL 11:cee25a834751 157 r2[i+1] += carry;
wolfSSL 11:cee25a834751 158 r2[i] &= 0xff;
wolfSSL 11:cee25a834751 159 }
wolfSSL 11:cee25a834751 160
wolfSSL 11:cee25a834751 161 for(i=0;i<32;i++)
wolfSSL 11:cee25a834751 162 {
wolfSSL 11:cee25a834751 163 pb += r2[i];
wolfSSL 11:cee25a834751 164 b = lt(r1[i],pb);
wolfSSL 11:cee25a834751 165 r[i] = r1[i]-pb+(b<<8);
wolfSSL 11:cee25a834751 166 pb = b;
wolfSSL 11:cee25a834751 167 }
wolfSSL 11:cee25a834751 168
wolfSSL 11:cee25a834751 169 /* XXX: Can it really happen that r<0?, See HAC, Alg 14.42, Step 3
wolfSSL 11:cee25a834751 170 * r is an unsigned type.
wolfSSL 11:cee25a834751 171 * If so: Handle it here!
wolfSSL 11:cee25a834751 172 */
wolfSSL 11:cee25a834751 173
wolfSSL 11:cee25a834751 174 reduce_add_sub(r);
wolfSSL 11:cee25a834751 175 reduce_add_sub(r);
wolfSSL 11:cee25a834751 176 }
wolfSSL 11:cee25a834751 177
wolfSSL 11:cee25a834751 178
wolfSSL 11:cee25a834751 179 void sc_reduce(unsigned char x[64])
wolfSSL 11:cee25a834751 180 {
wolfSSL 11:cee25a834751 181 int i;
wolfSSL 11:cee25a834751 182 word32 t[64];
wolfSSL 11:cee25a834751 183 word32 r[32];
wolfSSL 11:cee25a834751 184 for(i=0;i<64;i++) t[i] = x[i];
wolfSSL 11:cee25a834751 185 barrett_reduce(r, t);
wolfSSL 11:cee25a834751 186 for(i=0;i<32;i++) x[i] = (r[i] & 0xFF);
wolfSSL 11:cee25a834751 187 }
wolfSSL 11:cee25a834751 188
wolfSSL 11:cee25a834751 189
wolfSSL 11:cee25a834751 190 void sc_muladd(byte* out, const byte* a, const byte* b, const byte* c)
wolfSSL 11:cee25a834751 191 {
wolfSSL 11:cee25a834751 192
wolfSSL 11:cee25a834751 193 byte s[32];
wolfSSL 11:cee25a834751 194 byte e[64];
wolfSSL 11:cee25a834751 195
wolfSSL 11:cee25a834751 196 XMEMSET(e, 0, sizeof(e));
wolfSSL 11:cee25a834751 197 XMEMCPY(e, b, 32);
wolfSSL 11:cee25a834751 198
wolfSSL 11:cee25a834751 199 /* Obtain e */
wolfSSL 11:cee25a834751 200 sc_reduce(e);
wolfSSL 11:cee25a834751 201
wolfSSL 11:cee25a834751 202 /* Compute s = ze + k */
wolfSSL 11:cee25a834751 203 fprime_mul(s, a, e, ed25519_order);
wolfSSL 11:cee25a834751 204 fprime_add(s, c, ed25519_order);
wolfSSL 11:cee25a834751 205
wolfSSL 11:cee25a834751 206 XMEMCPY(out, s, 32);
wolfSSL 11:cee25a834751 207 }
wolfSSL 11:cee25a834751 208
wolfSSL 11:cee25a834751 209
wolfSSL 11:cee25a834751 210 /* Base point is (numbers wrapped):
wolfSSL 11:cee25a834751 211 *
wolfSSL 11:cee25a834751 212 * x = 151122213495354007725011514095885315114
wolfSSL 11:cee25a834751 213 * 54012693041857206046113283949847762202
wolfSSL 11:cee25a834751 214 * y = 463168356949264781694283940034751631413
wolfSSL 11:cee25a834751 215 * 07993866256225615783033603165251855960
wolfSSL 11:cee25a834751 216 *
wolfSSL 11:cee25a834751 217 * y is derived by transforming the original Montgomery base (u=9). x
wolfSSL 11:cee25a834751 218 * is the corresponding positive coordinate for the new curve equation.
wolfSSL 11:cee25a834751 219 * t is x*y.
wolfSSL 11:cee25a834751 220 */
wolfSSL 11:cee25a834751 221 const ge_p3 ed25519_base = {
wolfSSL 11:cee25a834751 222 {
wolfSSL 11:cee25a834751 223 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9,
wolfSSL 11:cee25a834751 224 0xb2, 0xa7, 0x25, 0x95, 0x60, 0xc7, 0x2c, 0x69,
wolfSSL 11:cee25a834751 225 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
wolfSSL 11:cee25a834751 226 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
wolfSSL 11:cee25a834751 227 },
wolfSSL 11:cee25a834751 228 {
wolfSSL 11:cee25a834751 229 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
wolfSSL 11:cee25a834751 230 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
wolfSSL 11:cee25a834751 231 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
wolfSSL 11:cee25a834751 232 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66
wolfSSL 11:cee25a834751 233 },
wolfSSL 11:cee25a834751 234 {1, 0},
wolfSSL 11:cee25a834751 235 {
wolfSSL 11:cee25a834751 236 0xa3, 0xdd, 0xb7, 0xa5, 0xb3, 0x8a, 0xde, 0x6d,
wolfSSL 11:cee25a834751 237 0xf5, 0x52, 0x51, 0x77, 0x80, 0x9f, 0xf0, 0x20,
wolfSSL 11:cee25a834751 238 0x7d, 0xe3, 0xab, 0x64, 0x8e, 0x4e, 0xea, 0x66,
wolfSSL 11:cee25a834751 239 0x65, 0x76, 0x8b, 0xd7, 0x0f, 0x5f, 0x87, 0x67
wolfSSL 11:cee25a834751 240 },
wolfSSL 11:cee25a834751 241
wolfSSL 11:cee25a834751 242 };
wolfSSL 11:cee25a834751 243
wolfSSL 11:cee25a834751 244
wolfSSL 11:cee25a834751 245 const ge_p3 ed25519_neutral = {
wolfSSL 11:cee25a834751 246 {0},
wolfSSL 11:cee25a834751 247 {1, 0},
wolfSSL 11:cee25a834751 248 {1, 0},
wolfSSL 11:cee25a834751 249 {0},
wolfSSL 11:cee25a834751 250
wolfSSL 11:cee25a834751 251 };
wolfSSL 11:cee25a834751 252
wolfSSL 11:cee25a834751 253
wolfSSL 11:cee25a834751 254 static const byte ed25519_d[F25519_SIZE] = {
wolfSSL 11:cee25a834751 255 0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
wolfSSL 11:cee25a834751 256 0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
wolfSSL 11:cee25a834751 257 0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
wolfSSL 11:cee25a834751 258 0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52
wolfSSL 11:cee25a834751 259 };
wolfSSL 11:cee25a834751 260
wolfSSL 11:cee25a834751 261
wolfSSL 11:cee25a834751 262 /* k = 2d */
wolfSSL 11:cee25a834751 263 static const byte ed25519_k[F25519_SIZE] = {
wolfSSL 11:cee25a834751 264 0x59, 0xf1, 0xb2, 0x26, 0x94, 0x9b, 0xd6, 0xeb,
wolfSSL 11:cee25a834751 265 0x56, 0xb1, 0x83, 0x82, 0x9a, 0x14, 0xe0, 0x00,
wolfSSL 11:cee25a834751 266 0x30, 0xd1, 0xf3, 0xee, 0xf2, 0x80, 0x8e, 0x19,
wolfSSL 11:cee25a834751 267 0xe7, 0xfc, 0xdf, 0x56, 0xdc, 0xd9, 0x06, 0x24
wolfSSL 11:cee25a834751 268 };
wolfSSL 11:cee25a834751 269
wolfSSL 11:cee25a834751 270
wolfSSL 11:cee25a834751 271 void ed25519_add(ge_p3 *r,
wolfSSL 11:cee25a834751 272 const ge_p3 *p1, const ge_p3 *p2)
wolfSSL 11:cee25a834751 273 {
wolfSSL 11:cee25a834751 274 /* Explicit formulas database: add-2008-hwcd-3
wolfSSL 11:cee25a834751 275 *
wolfSSL 11:cee25a834751 276 * source 2008 Hisil--Wong--Carter--Dawson,
wolfSSL 11:cee25a834751 277 * http://eprint.iacr.org/2008/522, Section 3.1
wolfSSL 11:cee25a834751 278 * appliesto extended-1
wolfSSL 11:cee25a834751 279 * parameter k
wolfSSL 11:cee25a834751 280 * assume k = 2 d
wolfSSL 11:cee25a834751 281 * compute A = (Y1-X1)(Y2-X2)
wolfSSL 11:cee25a834751 282 * compute B = (Y1+X1)(Y2+X2)
wolfSSL 11:cee25a834751 283 * compute C = T1 k T2
wolfSSL 11:cee25a834751 284 * compute D = Z1 2 Z2
wolfSSL 11:cee25a834751 285 * compute E = B - A
wolfSSL 11:cee25a834751 286 * compute F = D - C
wolfSSL 11:cee25a834751 287 * compute G = D + C
wolfSSL 11:cee25a834751 288 * compute H = B + A
wolfSSL 11:cee25a834751 289 * compute X3 = E F
wolfSSL 11:cee25a834751 290 * compute Y3 = G H
wolfSSL 11:cee25a834751 291 * compute T3 = E H
wolfSSL 11:cee25a834751 292 * compute Z3 = F G
wolfSSL 11:cee25a834751 293 */
wolfSSL 11:cee25a834751 294 byte a[F25519_SIZE];
wolfSSL 11:cee25a834751 295 byte b[F25519_SIZE];
wolfSSL 11:cee25a834751 296 byte c[F25519_SIZE];
wolfSSL 11:cee25a834751 297 byte d[F25519_SIZE];
wolfSSL 11:cee25a834751 298 byte e[F25519_SIZE];
wolfSSL 11:cee25a834751 299 byte f[F25519_SIZE];
wolfSSL 11:cee25a834751 300 byte g[F25519_SIZE];
wolfSSL 11:cee25a834751 301 byte h[F25519_SIZE];
wolfSSL 11:cee25a834751 302
wolfSSL 11:cee25a834751 303 /* A = (Y1-X1)(Y2-X2) */
wolfSSL 11:cee25a834751 304 fe_sub(c, p1->Y, p1->X);
wolfSSL 11:cee25a834751 305 fe_sub(d, p2->Y, p2->X);
wolfSSL 11:cee25a834751 306 fe_mul__distinct(a, c, d);
wolfSSL 11:cee25a834751 307
wolfSSL 11:cee25a834751 308 /* B = (Y1+X1)(Y2+X2) */
wolfSSL 11:cee25a834751 309 fe_add(c, p1->Y, p1->X);
wolfSSL 11:cee25a834751 310 fe_add(d, p2->Y, p2->X);
wolfSSL 11:cee25a834751 311 fe_mul__distinct(b, c, d);
wolfSSL 11:cee25a834751 312
wolfSSL 11:cee25a834751 313 /* C = T1 k T2 */
wolfSSL 11:cee25a834751 314 fe_mul__distinct(d, p1->T, p2->T);
wolfSSL 11:cee25a834751 315 fe_mul__distinct(c, d, ed25519_k);
wolfSSL 11:cee25a834751 316
wolfSSL 11:cee25a834751 317 /* D = Z1 2 Z2 */
wolfSSL 11:cee25a834751 318 fe_mul__distinct(d, p1->Z, p2->Z);
wolfSSL 11:cee25a834751 319 fe_add(d, d, d);
wolfSSL 11:cee25a834751 320
wolfSSL 11:cee25a834751 321 /* E = B - A */
wolfSSL 11:cee25a834751 322 fe_sub(e, b, a);
wolfSSL 11:cee25a834751 323
wolfSSL 11:cee25a834751 324 /* F = D - C */
wolfSSL 11:cee25a834751 325 fe_sub(f, d, c);
wolfSSL 11:cee25a834751 326
wolfSSL 11:cee25a834751 327 /* G = D + C */
wolfSSL 11:cee25a834751 328 fe_add(g, d, c);
wolfSSL 11:cee25a834751 329
wolfSSL 11:cee25a834751 330 /* H = B + A */
wolfSSL 11:cee25a834751 331 fe_add(h, b, a);
wolfSSL 11:cee25a834751 332
wolfSSL 11:cee25a834751 333 /* X3 = E F */
wolfSSL 11:cee25a834751 334 fe_mul__distinct(r->X, e, f);
wolfSSL 11:cee25a834751 335
wolfSSL 11:cee25a834751 336 /* Y3 = G H */
wolfSSL 11:cee25a834751 337 fe_mul__distinct(r->Y, g, h);
wolfSSL 11:cee25a834751 338
wolfSSL 11:cee25a834751 339 /* T3 = E H */
wolfSSL 11:cee25a834751 340 fe_mul__distinct(r->T, e, h);
wolfSSL 11:cee25a834751 341
wolfSSL 11:cee25a834751 342 /* Z3 = F G */
wolfSSL 11:cee25a834751 343 fe_mul__distinct(r->Z, f, g);
wolfSSL 11:cee25a834751 344 }
wolfSSL 11:cee25a834751 345
wolfSSL 11:cee25a834751 346
wolfSSL 11:cee25a834751 347 void ed25519_double(ge_p3 *r, const ge_p3 *p)
wolfSSL 11:cee25a834751 348 {
wolfSSL 11:cee25a834751 349 /* Explicit formulas database: dbl-2008-hwcd
wolfSSL 11:cee25a834751 350 *
wolfSSL 11:cee25a834751 351 * source 2008 Hisil--Wong--Carter--Dawson,
wolfSSL 11:cee25a834751 352 * http://eprint.iacr.org/2008/522, Section 3.3
wolfSSL 11:cee25a834751 353 * compute A = X1^2
wolfSSL 11:cee25a834751 354 * compute B = Y1^2
wolfSSL 11:cee25a834751 355 * compute C = 2 Z1^2
wolfSSL 11:cee25a834751 356 * compute D = a A
wolfSSL 11:cee25a834751 357 * compute E = (X1+Y1)^2-A-B
wolfSSL 11:cee25a834751 358 * compute G = D + B
wolfSSL 11:cee25a834751 359 * compute F = G - C
wolfSSL 11:cee25a834751 360 * compute H = D - B
wolfSSL 11:cee25a834751 361 * compute X3 = E F
wolfSSL 11:cee25a834751 362 * compute Y3 = G H
wolfSSL 11:cee25a834751 363 * compute T3 = E H
wolfSSL 11:cee25a834751 364 * compute Z3 = F G
wolfSSL 11:cee25a834751 365 */
wolfSSL 11:cee25a834751 366 byte a[F25519_SIZE];
wolfSSL 11:cee25a834751 367 byte b[F25519_SIZE];
wolfSSL 11:cee25a834751 368 byte c[F25519_SIZE];
wolfSSL 11:cee25a834751 369 byte e[F25519_SIZE];
wolfSSL 11:cee25a834751 370 byte f[F25519_SIZE];
wolfSSL 11:cee25a834751 371 byte g[F25519_SIZE];
wolfSSL 11:cee25a834751 372 byte h[F25519_SIZE];
wolfSSL 11:cee25a834751 373
wolfSSL 11:cee25a834751 374 /* A = X1^2 */
wolfSSL 11:cee25a834751 375 fe_mul__distinct(a, p->X, p->X);
wolfSSL 11:cee25a834751 376
wolfSSL 11:cee25a834751 377 /* B = Y1^2 */
wolfSSL 11:cee25a834751 378 fe_mul__distinct(b, p->Y, p->Y);
wolfSSL 11:cee25a834751 379
wolfSSL 11:cee25a834751 380 /* C = 2 Z1^2 */
wolfSSL 11:cee25a834751 381 fe_mul__distinct(c, p->Z, p->Z);
wolfSSL 11:cee25a834751 382 fe_add(c, c, c);
wolfSSL 11:cee25a834751 383
wolfSSL 11:cee25a834751 384 /* D = a A (alter sign) */
wolfSSL 11:cee25a834751 385 /* E = (X1+Y1)^2-A-B */
wolfSSL 11:cee25a834751 386 fe_add(f, p->X, p->Y);
wolfSSL 11:cee25a834751 387 fe_mul__distinct(e, f, f);
wolfSSL 11:cee25a834751 388 fe_sub(e, e, a);
wolfSSL 11:cee25a834751 389 fe_sub(e, e, b);
wolfSSL 11:cee25a834751 390
wolfSSL 11:cee25a834751 391 /* G = D + B */
wolfSSL 11:cee25a834751 392 fe_sub(g, b, a);
wolfSSL 11:cee25a834751 393
wolfSSL 11:cee25a834751 394 /* F = G - C */
wolfSSL 11:cee25a834751 395 fe_sub(f, g, c);
wolfSSL 11:cee25a834751 396
wolfSSL 11:cee25a834751 397 /* H = D - B */
wolfSSL 11:cee25a834751 398 fe_neg(h, b);
wolfSSL 11:cee25a834751 399 fe_sub(h, h, a);
wolfSSL 11:cee25a834751 400
wolfSSL 11:cee25a834751 401 /* X3 = E F */
wolfSSL 11:cee25a834751 402 fe_mul__distinct(r->X, e, f);
wolfSSL 11:cee25a834751 403
wolfSSL 11:cee25a834751 404 /* Y3 = G H */
wolfSSL 11:cee25a834751 405 fe_mul__distinct(r->Y, g, h);
wolfSSL 11:cee25a834751 406
wolfSSL 11:cee25a834751 407 /* T3 = E H */
wolfSSL 11:cee25a834751 408 fe_mul__distinct(r->T, e, h);
wolfSSL 11:cee25a834751 409
wolfSSL 11:cee25a834751 410 /* Z3 = F G */
wolfSSL 11:cee25a834751 411 fe_mul__distinct(r->Z, f, g);
wolfSSL 11:cee25a834751 412 }
wolfSSL 11:cee25a834751 413
wolfSSL 11:cee25a834751 414
wolfSSL 11:cee25a834751 415 void ed25519_smult(ge_p3 *r_out, const ge_p3 *p, const byte *e)
wolfSSL 11:cee25a834751 416 {
wolfSSL 11:cee25a834751 417 ge_p3 r;
wolfSSL 11:cee25a834751 418 int i;
wolfSSL 11:cee25a834751 419
wolfSSL 11:cee25a834751 420 XMEMCPY(&r, &ed25519_neutral, sizeof(r));
wolfSSL 11:cee25a834751 421
wolfSSL 11:cee25a834751 422 for (i = 255; i >= 0; i--) {
wolfSSL 11:cee25a834751 423 const byte bit = (e[i >> 3] >> (i & 7)) & 1;
wolfSSL 11:cee25a834751 424 ge_p3 s;
wolfSSL 11:cee25a834751 425
wolfSSL 11:cee25a834751 426 ed25519_double(&r, &r);
wolfSSL 11:cee25a834751 427 ed25519_add(&s, &r, p);
wolfSSL 11:cee25a834751 428
wolfSSL 11:cee25a834751 429 fe_select(r.X, r.X, s.X, bit);
wolfSSL 11:cee25a834751 430 fe_select(r.Y, r.Y, s.Y, bit);
wolfSSL 11:cee25a834751 431 fe_select(r.Z, r.Z, s.Z, bit);
wolfSSL 11:cee25a834751 432 fe_select(r.T, r.T, s.T, bit);
wolfSSL 11:cee25a834751 433 }
wolfSSL 11:cee25a834751 434 XMEMCPY(r_out, &r, sizeof(r));
wolfSSL 11:cee25a834751 435 }
wolfSSL 11:cee25a834751 436
wolfSSL 11:cee25a834751 437
wolfSSL 11:cee25a834751 438 void ge_scalarmult_base(ge_p3 *R,const unsigned char *nonce)
wolfSSL 11:cee25a834751 439 {
wolfSSL 11:cee25a834751 440 ed25519_smult(R, &ed25519_base, nonce);
wolfSSL 11:cee25a834751 441 }
wolfSSL 11:cee25a834751 442
wolfSSL 11:cee25a834751 443
wolfSSL 11:cee25a834751 444 /* pack the point h into array s */
wolfSSL 11:cee25a834751 445 void ge_p3_tobytes(unsigned char *s,const ge_p3 *h)
wolfSSL 11:cee25a834751 446 {
wolfSSL 11:cee25a834751 447 byte x[F25519_SIZE];
wolfSSL 11:cee25a834751 448 byte y[F25519_SIZE];
wolfSSL 11:cee25a834751 449 byte z1[F25519_SIZE];
wolfSSL 11:cee25a834751 450 byte parity;
wolfSSL 11:cee25a834751 451
wolfSSL 11:cee25a834751 452 fe_inv__distinct(z1, h->Z);
wolfSSL 11:cee25a834751 453 fe_mul__distinct(x, h->X, z1);
wolfSSL 11:cee25a834751 454 fe_mul__distinct(y, h->Y, z1);
wolfSSL 11:cee25a834751 455
wolfSSL 11:cee25a834751 456 fe_normalize(x);
wolfSSL 11:cee25a834751 457 fe_normalize(y);
wolfSSL 11:cee25a834751 458
wolfSSL 11:cee25a834751 459 parity = (x[0] & 1) << 7;
wolfSSL 11:cee25a834751 460 fe_copy(s, y);
wolfSSL 11:cee25a834751 461 fe_normalize(s);
wolfSSL 11:cee25a834751 462 s[31] |= parity;
wolfSSL 11:cee25a834751 463 }
wolfSSL 11:cee25a834751 464
wolfSSL 11:cee25a834751 465
wolfSSL 11:cee25a834751 466 /* pack the point h into array s */
wolfSSL 11:cee25a834751 467 void ge_tobytes(unsigned char *s,const ge_p2 *h)
wolfSSL 11:cee25a834751 468 {
wolfSSL 11:cee25a834751 469 byte x[F25519_SIZE];
wolfSSL 11:cee25a834751 470 byte y[F25519_SIZE];
wolfSSL 11:cee25a834751 471 byte z1[F25519_SIZE];
wolfSSL 11:cee25a834751 472 byte parity;
wolfSSL 11:cee25a834751 473
wolfSSL 11:cee25a834751 474 fe_inv__distinct(z1, h->Z);
wolfSSL 11:cee25a834751 475 fe_mul__distinct(x, h->X, z1);
wolfSSL 11:cee25a834751 476 fe_mul__distinct(y, h->Y, z1);
wolfSSL 11:cee25a834751 477
wolfSSL 11:cee25a834751 478 fe_normalize(x);
wolfSSL 11:cee25a834751 479 fe_normalize(y);
wolfSSL 11:cee25a834751 480
wolfSSL 11:cee25a834751 481 parity = (x[0] & 1) << 7;
wolfSSL 11:cee25a834751 482 fe_copy(s, y);
wolfSSL 11:cee25a834751 483 fe_normalize(s);
wolfSSL 11:cee25a834751 484 s[31] |= parity;
wolfSSL 11:cee25a834751 485 }
wolfSSL 11:cee25a834751 486
wolfSSL 11:cee25a834751 487
wolfSSL 11:cee25a834751 488 /*
wolfSSL 11:cee25a834751 489 Test if the public key can be uncompressed and negate it (-X,Y,Z,-T)
wolfSSL 11:cee25a834751 490 return 0 on success
wolfSSL 11:cee25a834751 491 */
wolfSSL 11:cee25a834751 492 int ge_frombytes_negate_vartime(ge_p3 *p,const unsigned char *s)
wolfSSL 11:cee25a834751 493 {
wolfSSL 11:cee25a834751 494
wolfSSL 11:cee25a834751 495 byte parity;
wolfSSL 11:cee25a834751 496 byte x[F25519_SIZE];
wolfSSL 11:cee25a834751 497 byte y[F25519_SIZE];
wolfSSL 11:cee25a834751 498 byte a[F25519_SIZE];
wolfSSL 11:cee25a834751 499 byte b[F25519_SIZE];
wolfSSL 11:cee25a834751 500 byte c[F25519_SIZE];
wolfSSL 11:cee25a834751 501 int ret = 0;
wolfSSL 11:cee25a834751 502
wolfSSL 11:cee25a834751 503 /* unpack the key s */
wolfSSL 11:cee25a834751 504 parity = s[31] >> 7;
wolfSSL 11:cee25a834751 505 fe_copy(y, s);
wolfSSL 11:cee25a834751 506 y[31] &= 127;
wolfSSL 11:cee25a834751 507
wolfSSL 11:cee25a834751 508 fe_mul__distinct(c, y, y);
wolfSSL 11:cee25a834751 509 fe_mul__distinct(b, c, ed25519_d);
wolfSSL 11:cee25a834751 510 fe_add(a, b, f25519_one);
wolfSSL 11:cee25a834751 511 fe_inv__distinct(b, a);
wolfSSL 11:cee25a834751 512 fe_sub(a, c, f25519_one);
wolfSSL 11:cee25a834751 513 fe_mul__distinct(c, a, b);
wolfSSL 11:cee25a834751 514 fe_sqrt(a, c);
wolfSSL 11:cee25a834751 515 fe_neg(b, a);
wolfSSL 11:cee25a834751 516 fe_select(x, a, b, (a[0] ^ parity) & 1);
wolfSSL 11:cee25a834751 517
wolfSSL 11:cee25a834751 518 /* test that x^2 is equal to c */
wolfSSL 11:cee25a834751 519 fe_mul__distinct(a, x, x);
wolfSSL 11:cee25a834751 520 fe_normalize(a);
wolfSSL 11:cee25a834751 521 fe_normalize(c);
wolfSSL 11:cee25a834751 522 ret |= ConstantCompare(a, c, F25519_SIZE);
wolfSSL 11:cee25a834751 523
wolfSSL 11:cee25a834751 524 /* project the key s onto p */
wolfSSL 11:cee25a834751 525 fe_copy(p->X, x);
wolfSSL 11:cee25a834751 526 fe_copy(p->Y, y);
wolfSSL 11:cee25a834751 527 fe_load(p->Z, 1);
wolfSSL 11:cee25a834751 528 fe_mul__distinct(p->T, x, y);
wolfSSL 11:cee25a834751 529
wolfSSL 11:cee25a834751 530 /* negate, the point becomes (-X,Y,Z,-T) */
wolfSSL 11:cee25a834751 531 fe_neg(p->X,p->X);
wolfSSL 11:cee25a834751 532 fe_neg(p->T,p->T);
wolfSSL 11:cee25a834751 533
wolfSSL 11:cee25a834751 534 return ret;
wolfSSL 11:cee25a834751 535 }
wolfSSL 11:cee25a834751 536
wolfSSL 11:cee25a834751 537
wolfSSL 11:cee25a834751 538 int ge_double_scalarmult_vartime(ge_p2* R, const unsigned char *h,
wolfSSL 11:cee25a834751 539 const ge_p3 *inA,const unsigned char *sig)
wolfSSL 11:cee25a834751 540 {
wolfSSL 11:cee25a834751 541 ge_p3 p, A;
wolfSSL 11:cee25a834751 542 int ret = 0;
wolfSSL 11:cee25a834751 543
wolfSSL 11:cee25a834751 544 XMEMCPY(&A, inA, sizeof(ge_p3));
wolfSSL 11:cee25a834751 545
wolfSSL 11:cee25a834751 546 /* find SB */
wolfSSL 11:cee25a834751 547 ed25519_smult(&p, &ed25519_base, sig);
wolfSSL 11:cee25a834751 548
wolfSSL 11:cee25a834751 549 /* find H(R,A,M) * -A */
wolfSSL 11:cee25a834751 550 ed25519_smult(&A, &A, h);
wolfSSL 11:cee25a834751 551
wolfSSL 11:cee25a834751 552 /* SB + -H(R,A,M)A */
wolfSSL 11:cee25a834751 553 ed25519_add(&A, &p, &A);
wolfSSL 11:cee25a834751 554
wolfSSL 11:cee25a834751 555 fe_copy(R->X, A.X);
wolfSSL 11:cee25a834751 556 fe_copy(R->Y, A.Y);
wolfSSL 11:cee25a834751 557 fe_copy(R->Z, A.Z);
wolfSSL 11:cee25a834751 558
wolfSSL 11:cee25a834751 559 return ret;
wolfSSL 11:cee25a834751 560 }
wolfSSL 11:cee25a834751 561
wolfSSL 11:cee25a834751 562 #endif /* HAVE_ED25519 */
wolfSSL 11:cee25a834751 563 #endif /* CURVED25519_SMALL */
wolfSSL 11:cee25a834751 564
wolfSSL 11:cee25a834751 565