2017-12-01 18:12:25 +00:00
|
|
|
/*
|
2019-05-28 12:49:38 +00:00
|
|
|
* Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
|
2017-12-01 18:12:25 +00:00
|
|
|
* Copyright 2015-2016 Cryptography Research, Inc.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Originally written by Mike Hamburg
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
2017-11-15 16:39:33 +00:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
#include "word.h"
|
2017-11-15 16:15:18 +00:00
|
|
|
#include "point_448.h"
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2019-03-06 02:57:09 +00:00
|
|
|
static const c448_word_t MONTGOMERY_FACTOR = (c448_word_t) 0x3bd440fae918bc5ULL;
|
2017-12-04 13:30:53 +00:00
|
|
|
static const curve448_scalar_t sc_p = {
|
|
|
|
{
|
|
|
|
{
|
2019-03-06 02:57:09 +00:00
|
|
|
SC_LIMB(0x2378c292ab5844f3ULL), SC_LIMB(0x216cc2728dc58f55ULL),
|
|
|
|
SC_LIMB(0xc44edb49aed63690ULL), SC_LIMB(0xffffffff7cca23e9ULL),
|
|
|
|
SC_LIMB(0xffffffffffffffffULL), SC_LIMB(0xffffffffffffffffULL),
|
|
|
|
SC_LIMB(0x3fffffffffffffffULL)
|
2017-12-04 13:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, sc_r2 = {
|
|
|
|
{
|
|
|
|
{
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2019-03-06 02:57:09 +00:00
|
|
|
SC_LIMB(0xe3539257049b9b60ULL), SC_LIMB(0x7af32c4bc1b195d9ULL),
|
|
|
|
SC_LIMB(0x0d66de2388ea1859ULL), SC_LIMB(0xae17cf725ee4d838ULL),
|
|
|
|
SC_LIMB(0x1a9cc14ba3c47c44ULL), SC_LIMB(0x2052bcb7e4d070afULL),
|
|
|
|
SC_LIMB(0x3402a939f823b729ULL)
|
2017-12-04 13:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
#define WBITS C448_WORD_BITS /* NB this may be different from ARCH_WORD_BITS */
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
const curve448_scalar_t curve448_scalar_one = {{{1}}};
|
2018-02-07 17:03:14 +00:00
|
|
|
const curve448_scalar_t curve448_scalar_zero = {{{0}}};
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/*
|
|
|
|
* {extra,accum} - sub +? p
|
2017-11-15 15:27:21 +00:00
|
|
|
* Must have extra <= 1
|
|
|
|
*/
|
2017-12-04 11:38:58 +00:00
|
|
|
static void sc_subx(curve448_scalar_t out,
|
2017-12-06 13:06:13 +00:00
|
|
|
const c448_word_t accum[C448_SCALAR_LIMBS],
|
2017-12-04 11:38:58 +00:00
|
|
|
const curve448_scalar_t sub,
|
2017-12-05 16:37:57 +00:00
|
|
|
const curve448_scalar_t p, c448_word_t extra)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_dsword_t chain = 0;
|
2017-11-15 15:27:21 +00:00
|
|
|
unsigned int i;
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_word_t borrow;
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
chain = (chain + accum[i]) - sub->limb[i];
|
2017-12-12 14:17:40 +00:00
|
|
|
out->limb[i] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
chain >>= WBITS;
|
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
borrow = (c448_word_t)chain + extra; /* = 0 or -1 */
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
chain = 0;
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
chain = (chain + out->limb[i]) + (p->limb[i] & borrow);
|
2017-12-12 14:17:40 +00:00
|
|
|
out->limb[i] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
chain >>= WBITS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
static void sc_montmul(curve448_scalar_t out, const curve448_scalar_t a,
|
|
|
|
const curve448_scalar_t b)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
|
|
|
unsigned int i, j;
|
2017-12-06 13:06:13 +00:00
|
|
|
c448_word_t accum[C448_SCALAR_LIMBS + 1] = { 0 };
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_word_t hi_carry = 0;
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_word_t mand = a->limb[i];
|
|
|
|
const c448_word_t *mier = b->limb;
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_dword_t chain = 0;
|
2017-12-06 13:06:13 +00:00
|
|
|
for (j = 0; j < C448_SCALAR_LIMBS; j++) {
|
2017-12-05 16:37:57 +00:00
|
|
|
chain += ((c448_dword_t) mand) * mier[j] + accum[j];
|
2017-12-12 14:17:40 +00:00
|
|
|
accum[j] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
chain >>= WBITS;
|
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
accum[j] = (c448_word_t)chain;
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
mand = accum[0] * MONTGOMERY_FACTOR;
|
|
|
|
chain = 0;
|
|
|
|
mier = sc_p->limb;
|
2017-12-06 13:06:13 +00:00
|
|
|
for (j = 0; j < C448_SCALAR_LIMBS; j++) {
|
2017-12-05 16:37:57 +00:00
|
|
|
chain += (c448_dword_t) mand *mier[j] + accum[j];
|
2017-12-04 11:38:58 +00:00
|
|
|
if (j)
|
2017-12-12 14:17:40 +00:00
|
|
|
accum[j - 1] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
chain >>= WBITS;
|
|
|
|
}
|
|
|
|
chain += accum[j];
|
|
|
|
chain += hi_carry;
|
2017-12-12 14:17:40 +00:00
|
|
|
accum[j - 1] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
hi_carry = chain >> WBITS;
|
|
|
|
}
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
sc_subx(out, accum, sc_p, sc_p, hi_carry);
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
void curve448_scalar_mul(curve448_scalar_t out, const curve448_scalar_t a,
|
|
|
|
const curve448_scalar_t b)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
|
|
|
sc_montmul(out, a, b);
|
|
|
|
sc_montmul(out, out, sc_r2);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
void curve448_scalar_sub(curve448_scalar_t out, const curve448_scalar_t a,
|
|
|
|
const curve448_scalar_t b)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
sc_subx(out, a->limb, b, sc_p, 0);
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
void curve448_scalar_add(curve448_scalar_t out, const curve448_scalar_t a,
|
|
|
|
const curve448_scalar_t b)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_dword_t chain = 0;
|
2017-11-15 15:27:21 +00:00
|
|
|
unsigned int i;
|
2017-12-04 13:30:53 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
chain = (chain + a->limb[i]) + b->limb[i];
|
2017-12-12 14:17:40 +00:00
|
|
|
out->limb[i] = (c448_word_t)chain;
|
2017-11-15 15:27:21 +00:00
|
|
|
chain >>= WBITS;
|
|
|
|
}
|
2017-12-12 14:17:40 +00:00
|
|
|
sc_subx(out, out->limb, sc_p, sc_p, (c448_word_t)chain);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void scalar_decode_short(curve448_scalar_t s,
|
|
|
|
const unsigned char *ser,
|
2018-03-02 21:16:29 +00:00
|
|
|
size_t nbytes)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2018-03-02 21:16:29 +00:00
|
|
|
size_t i, j, k = 0;
|
2017-12-04 13:30:53 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_word_t out = 0;
|
2017-12-04 13:30:53 +00:00
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
for (j = 0; j < sizeof(c448_word_t) && k < nbytes; j++, k++)
|
|
|
|
out |= ((c448_word_t) ser[k]) << (8 * j);
|
2017-11-15 15:27:21 +00:00
|
|
|
s->limb[i] = out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t curve448_scalar_decode(
|
2017-12-04 13:30:53 +00:00
|
|
|
curve448_scalar_t s,
|
2017-12-06 13:06:13 +00:00
|
|
|
const unsigned char ser[C448_SCALAR_BYTES])
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
unsigned int i;
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_dsword_t accum = 0;
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
scalar_decode_short(s, ser, C448_SCALAR_BYTES);
|
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++)
|
2017-11-15 15:27:21 +00:00
|
|
|
accum = (accum + s->limb[i] - sc_p->limb[i]) >> WBITS;
|
|
|
|
/* Here accum == 0 or -1 */
|
2017-12-04 11:38:58 +00:00
|
|
|
|
|
|
|
curve448_scalar_mul(s, s, curve448_scalar_one); /* ham-handed reduce */
|
|
|
|
|
2017-12-12 14:17:40 +00:00
|
|
|
return c448_succeed_if(~word_is_zero((uint32_t)accum));
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
void curve448_scalar_destroy(curve448_scalar_t scalar)
|
|
|
|
{
|
2017-11-16 16:58:20 +00:00
|
|
|
OPENSSL_cleanse(scalar, sizeof(curve448_scalar_t));
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
void curve448_scalar_decode_long(curve448_scalar_t s,
|
|
|
|
const unsigned char *ser, size_t ser_len)
|
|
|
|
{
|
2018-01-18 12:55:23 +00:00
|
|
|
size_t i;
|
|
|
|
curve448_scalar_t t1, t2;
|
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
if (ser_len == 0) {
|
2017-11-16 16:27:48 +00:00
|
|
|
curve448_scalar_copy(s, curve448_scalar_zero);
|
2017-11-15 15:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
i = ser_len - (ser_len % C448_SCALAR_BYTES);
|
2017-12-04 11:38:58 +00:00
|
|
|
if (i == ser_len)
|
2017-12-06 13:06:13 +00:00
|
|
|
i -= C448_SCALAR_BYTES;
|
2017-12-04 11:38:58 +00:00
|
|
|
|
|
|
|
scalar_decode_short(t1, &ser[i], ser_len - i);
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-11-16 16:58:20 +00:00
|
|
|
if (ser_len == sizeof(curve448_scalar_t)) {
|
2017-12-04 11:38:58 +00:00
|
|
|
assert(i == 0);
|
2017-11-15 15:27:21 +00:00
|
|
|
/* ham-handed reduce */
|
2017-12-04 11:38:58 +00:00
|
|
|
curve448_scalar_mul(s, t1, curve448_scalar_one);
|
2017-11-16 16:27:48 +00:00
|
|
|
curve448_scalar_destroy(t1);
|
2017-11-15 15:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i) {
|
2017-12-06 13:06:13 +00:00
|
|
|
i -= C448_SCALAR_BYTES;
|
2017-12-04 11:38:58 +00:00
|
|
|
sc_montmul(t1, t1, sc_r2);
|
2018-02-07 17:03:14 +00:00
|
|
|
(void)curve448_scalar_decode(t2, ser + i);
|
2017-11-16 16:27:48 +00:00
|
|
|
curve448_scalar_add(t1, t1, t2);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:27:48 +00:00
|
|
|
curve448_scalar_copy(s, t1);
|
|
|
|
curve448_scalar_destroy(t1);
|
|
|
|
curve448_scalar_destroy(t2);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
|
2017-12-04 11:38:58 +00:00
|
|
|
const curve448_scalar_t s)
|
|
|
|
{
|
|
|
|
unsigned int i, j, k = 0;
|
2017-12-04 13:30:53 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-12-05 16:37:57 +00:00
|
|
|
for (j = 0; j < sizeof(c448_word_t); j++, k++)
|
2017-12-04 11:38:58 +00:00
|
|
|
ser[k] = s->limb[i] >> (8 * j);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a)
|
|
|
|
{
|
2017-12-12 14:17:40 +00:00
|
|
|
c448_word_t mask = 0 - (a->limb[0] & 1);
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_dword_t chain = 0;
|
2017-11-15 15:27:21 +00:00
|
|
|
unsigned int i;
|
2018-02-07 11:47:41 +00:00
|
|
|
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS; i++) {
|
2017-11-15 15:27:21 +00:00
|
|
|
chain = (chain + a->limb[i]) + (sc_p->limb[i] & mask);
|
2017-12-12 14:17:40 +00:00
|
|
|
out->limb[i] = (c448_word_t)chain;
|
2017-12-05 16:37:57 +00:00
|
|
|
chain >>= C448_WORD_BITS;
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
2017-12-06 13:06:13 +00:00
|
|
|
for (i = 0; i < C448_SCALAR_LIMBS - 1; i++)
|
2017-12-04 11:38:58 +00:00
|
|
|
out->limb[i] = out->limb[i] >> 1 | out->limb[i + 1] << (WBITS - 1);
|
2017-12-12 14:17:40 +00:00
|
|
|
out->limb[i] = out->limb[i] >> 1 | (c448_word_t)(chain << (WBITS - 1));
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|