2017-12-01 18:12:25 +00:00
|
|
|
/*
|
2018-01-18 13:12:46 +00:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-12-01 18:12:25 +00:00
|
|
|
* Copyright 2014 Cryptography Research, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Originally written by Mike Hamburg
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-12 14:27:02 +00:00
|
|
|
#ifndef HEADER_FIELD_H
|
|
|
|
# define HEADER_FIELD_H
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2018-02-01 13:53:56 +00:00
|
|
|
# include "internal/constant_time_locl.h"
|
2017-12-04 11:38:58 +00:00
|
|
|
# include <string.h>
|
2017-12-05 17:09:39 +00:00
|
|
|
# include <assert.h>
|
|
|
|
# include "word.h"
|
|
|
|
|
|
|
|
# define NLIMBS (64/sizeof(word_t))
|
|
|
|
# define X_SER_BYTES 56
|
|
|
|
# define SER_BYTES 56
|
2017-12-12 14:17:40 +00:00
|
|
|
|
|
|
|
# if defined(__GNUC__) || defined(__clang__)
|
2018-02-12 14:27:02 +00:00
|
|
|
# define INLINE_UNUSED __inline__ __attribute__((__unused__,__always_inline__))
|
2017-12-12 14:17:40 +00:00
|
|
|
# define RESTRICT __restrict__
|
2018-02-07 11:47:41 +00:00
|
|
|
# define ALIGNED __attribute__((__aligned__(32)))
|
2017-12-12 14:17:40 +00:00
|
|
|
# else
|
|
|
|
# define INLINE_UNUSED ossl_inline
|
|
|
|
# define RESTRICT
|
|
|
|
# define ALIGNED
|
|
|
|
# endif
|
|
|
|
|
2017-12-05 17:09:39 +00:00
|
|
|
typedef struct gf_s {
|
|
|
|
word_t limb[NLIMBS];
|
2017-12-12 14:17:40 +00:00
|
|
|
} ALIGNED gf_s, gf[1];
|
2017-12-05 17:09:39 +00:00
|
|
|
|
|
|
|
/* RFC 7748 support */
|
|
|
|
# define X_PUBLIC_BYTES X_SER_BYTES
|
|
|
|
# define X_PRIVATE_BYTES X_PUBLIC_BYTES
|
|
|
|
# define X_PRIVATE_BITS 448
|
|
|
|
|
|
|
|
static INLINE_UNUSED void gf_copy(gf out, const gf a)
|
|
|
|
{
|
|
|
|
*out = *a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
|
|
|
|
static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
|
|
|
|
static INLINE_UNUSED void gf_bias(gf inout, int amount);
|
|
|
|
static INLINE_UNUSED void gf_weak_reduce(gf inout);
|
|
|
|
|
|
|
|
void gf_strong_reduce(gf inout);
|
|
|
|
void gf_add(gf out, const gf a, const gf b);
|
|
|
|
void gf_sub(gf out, const gf a, const gf b);
|
2017-12-12 14:17:40 +00:00
|
|
|
void gf_mul(gf_s * RESTRICT out, const gf a, const gf b);
|
|
|
|
void gf_mulw_unsigned(gf_s * RESTRICT out, const gf a, uint32_t b);
|
|
|
|
void gf_sqr(gf_s * RESTRICT out, const gf a);
|
2017-12-05 17:09:39 +00:00
|
|
|
mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
|
|
|
|
mask_t gf_eq(const gf x, const gf y);
|
|
|
|
mask_t gf_lobit(const gf x);
|
|
|
|
mask_t gf_hibit(const gf x);
|
|
|
|
|
|
|
|
void gf_serialize(uint8_t *serial, const gf x, int with_highbit);
|
|
|
|
mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
|
|
|
|
uint8_t hi_nmask);
|
|
|
|
|
|
|
|
# include "f_impl.h" /* Bring in the inline implementations */
|
|
|
|
|
2018-02-07 11:47:41 +00:00
|
|
|
# define LIMBPERM(i) (i)
|
2017-12-05 17:09:39 +00:00
|
|
|
# define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
|
|
|
|
|
|
|
|
static const gf ZERO = {{{0}}}, ONE = {{{1}}};
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Square x, n times. */
|
2017-12-12 14:17:40 +00:00
|
|
|
static ossl_inline void gf_sqrn(gf_s * RESTRICT y, const gf x, int n)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
gf tmp;
|
2018-02-07 11:47:41 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
assert(n > 0);
|
|
|
|
if (n & 1) {
|
|
|
|
gf_sqr(y, x);
|
2017-11-15 15:27:21 +00:00
|
|
|
n--;
|
|
|
|
} else {
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_sqr(tmp, x);
|
|
|
|
gf_sqr(y, tmp);
|
|
|
|
n -= 2;
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
2017-12-04 11:38:58 +00:00
|
|
|
for (; n; n -= 2) {
|
|
|
|
gf_sqr(tmp, y);
|
|
|
|
gf_sqr(y, tmp);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
# define gf_add_nr gf_add_RAW
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Subtract mod p. Bias by 2 and don't reduce */
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
|
|
|
|
{
|
|
|
|
gf_sub_RAW(c, a, b);
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_bias(c, 2);
|
2017-12-04 11:38:58 +00:00
|
|
|
if (GF_HEADROOM < 3)
|
|
|
|
gf_weak_reduce(c);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Subtract mod p. Bias by amt but don't reduce. */
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
|
|
|
|
{
|
|
|
|
gf_sub_RAW(c, a, b);
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_bias(c, amt);
|
2017-12-04 11:38:58 +00:00
|
|
|
if (GF_HEADROOM < amt + 1)
|
|
|
|
gf_weak_reduce(c);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Mul by signed int. Not constant-time WRT the sign of that int. */
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
|
|
|
|
{
|
|
|
|
if (w > 0) {
|
2017-11-15 15:27:21 +00:00
|
|
|
gf_mulw_unsigned(c, a, w);
|
|
|
|
} else {
|
|
|
|
gf_mulw_unsigned(c, a, -w);
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_sub(c, ZERO, c);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Constant time, x = is_z ? z : y */
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
|
|
|
|
{
|
2018-02-01 13:53:56 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < NLIMBS; i++) {
|
|
|
|
#if ARCH_WORD_BITS == 32
|
2018-02-07 11:47:41 +00:00
|
|
|
x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
|
|
|
|
y[0].limb[i]);
|
2018-02-01 13:53:56 +00:00
|
|
|
#else
|
|
|
|
/* Must be 64 bit */
|
2018-02-07 11:47:41 +00:00
|
|
|
x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
|
|
|
|
y[0].limb[i]);
|
2018-02-01 13:53:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Constant time, if (neg) x=-x; */
|
2017-12-04 11:38:58 +00:00
|
|
|
static ossl_inline void gf_cond_neg(gf x, mask_t neg)
|
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
gf y;
|
2018-02-07 11:47:41 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
gf_sub(y, ZERO, x);
|
|
|
|
gf_cond_sel(x, x, y, neg);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Constant time, if (swap) (x,y) = (y,x); */
|
2017-12-12 14:17:40 +00:00
|
|
|
static ossl_inline void gf_cond_swap(gf x, gf_s * RESTRICT y, mask_t swap)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2018-02-01 13:53:56 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < NLIMBS; i++) {
|
|
|
|
#if ARCH_WORD_BITS == 32
|
2018-02-07 11:47:41 +00:00
|
|
|
constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
|
2018-02-01 13:53:56 +00:00
|
|
|
#else
|
|
|
|
/* Must be 64 bit */
|
2018-02-07 11:47:41 +00:00
|
|
|
constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
|
2018-02-01 13:53:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 14:27:02 +00:00
|
|
|
#endif /* HEADER_FIELD_H */
|