Home | History | Annotate | Line # | Download | only in curve448
      1      1.1  christos /*
      2  1.1.1.3  christos  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright 2014 Cryptography Research, Inc.
      4      1.1  christos  *
      5  1.1.1.4  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7      1.1  christos  * in the file LICENSE in the source distribution or at
      8      1.1  christos  * https://www.openssl.org/source/license.html
      9      1.1  christos  *
     10      1.1  christos  * Originally written by Mike Hamburg
     11      1.1  christos  */
     12      1.1  christos 
     13  1.1.1.2  christos #ifndef OSSL_CRYPTO_EC_CURVE448_FIELD_H
     14  1.1.1.2  christos # define OSSL_CRYPTO_EC_CURVE448_FIELD_H
     15      1.1  christos 
     16  1.1.1.2  christos # include "internal/constant_time.h"
     17      1.1  christos # include <string.h>
     18      1.1  christos # include <assert.h>
     19      1.1  christos # include "word.h"
     20      1.1  christos 
     21      1.1  christos # define NLIMBS (64/sizeof(word_t))
     22      1.1  christos # define X_SER_BYTES 56
     23      1.1  christos # define SER_BYTES 56
     24      1.1  christos 
     25      1.1  christos # if defined(__GNUC__) || defined(__clang__)
     26      1.1  christos #  define INLINE_UNUSED __inline__ __attribute__((__unused__,__always_inline__))
     27      1.1  christos #  define RESTRICT __restrict__
     28      1.1  christos #  define ALIGNED __attribute__((__aligned__(16)))
     29      1.1  christos # else
     30      1.1  christos #  define INLINE_UNUSED ossl_inline
     31      1.1  christos #  define RESTRICT
     32      1.1  christos #  define ALIGNED
     33      1.1  christos # endif
     34      1.1  christos 
     35      1.1  christos typedef struct gf_s {
     36      1.1  christos     word_t limb[NLIMBS];
     37      1.1  christos } ALIGNED gf_s, gf[1];
     38      1.1  christos 
     39      1.1  christos /* RFC 7748 support */
     40      1.1  christos # define X_PUBLIC_BYTES  X_SER_BYTES
     41      1.1  christos # define X_PRIVATE_BYTES X_PUBLIC_BYTES
     42      1.1  christos # define X_PRIVATE_BITS  448
     43      1.1  christos 
     44      1.1  christos static INLINE_UNUSED void gf_copy(gf out, const gf a)
     45      1.1  christos {
     46      1.1  christos     *out = *a;
     47      1.1  christos }
     48      1.1  christos 
     49      1.1  christos static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
     50      1.1  christos static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
     51      1.1  christos static INLINE_UNUSED void gf_bias(gf inout, int amount);
     52      1.1  christos static INLINE_UNUSED void gf_weak_reduce(gf inout);
     53      1.1  christos 
     54      1.1  christos void gf_strong_reduce(gf inout);
     55      1.1  christos void gf_add(gf out, const gf a, const gf b);
     56      1.1  christos void gf_sub(gf out, const gf a, const gf b);
     57      1.1  christos void gf_mul(gf_s * RESTRICT out, const gf a, const gf b);
     58      1.1  christos void gf_mulw_unsigned(gf_s * RESTRICT out, const gf a, uint32_t b);
     59      1.1  christos void gf_sqr(gf_s * RESTRICT out, const gf a);
     60      1.1  christos mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0.  Return true if successful */
     61      1.1  christos mask_t gf_eq(const gf x, const gf y);
     62      1.1  christos mask_t gf_lobit(const gf x);
     63      1.1  christos mask_t gf_hibit(const gf x);
     64      1.1  christos 
     65  1.1.1.4  christos void gf_serialize(uint8_t *serial, const gf x, int with_highbit);
     66      1.1  christos mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
     67      1.1  christos                       uint8_t hi_nmask);
     68      1.1  christos 
     69      1.1  christos 
     70      1.1  christos # define LIMBPERM(i) (i)
     71  1.1.1.4  christos # if (ARCH_WORD_BITS == 32)
     72  1.1.1.4  christos #  include "arch_32/f_impl.h"    /* Bring in the inline implementations */
     73  1.1.1.4  christos #  define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
     74  1.1.1.4  christos # elif (ARCH_WORD_BITS == 64)
     75  1.1.1.4  christos #  include "arch_64/f_impl.h"    /* Bring in the inline implementations */
     76  1.1.1.4  christos #  define LIMB_MASK(i) (((1ULL)<<LIMB_PLACE_VALUE(i))-1)
     77  1.1.1.4  christos # endif
     78      1.1  christos 
     79      1.1  christos static const gf ZERO = {{{0}}}, ONE = {{{1}}};
     80      1.1  christos 
     81      1.1  christos /* Square x, n times. */
     82      1.1  christos static ossl_inline void gf_sqrn(gf_s * RESTRICT y, const gf x, int n)
     83      1.1  christos {
     84      1.1  christos     gf tmp;
     85      1.1  christos 
     86      1.1  christos     assert(n > 0);
     87      1.1  christos     if (n & 1) {
     88      1.1  christos         gf_sqr(y, x);
     89      1.1  christos         n--;
     90      1.1  christos     } else {
     91      1.1  christos         gf_sqr(tmp, x);
     92      1.1  christos         gf_sqr(y, tmp);
     93      1.1  christos         n -= 2;
     94      1.1  christos     }
     95      1.1  christos     for (; n; n -= 2) {
     96      1.1  christos         gf_sqr(tmp, y);
     97      1.1  christos         gf_sqr(y, tmp);
     98      1.1  christos     }
     99      1.1  christos }
    100      1.1  christos 
    101      1.1  christos # define gf_add_nr gf_add_RAW
    102      1.1  christos 
    103      1.1  christos /* Subtract mod p.  Bias by 2 and don't reduce  */
    104      1.1  christos static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
    105      1.1  christos {
    106      1.1  christos     gf_sub_RAW(c, a, b);
    107      1.1  christos     gf_bias(c, 2);
    108      1.1  christos     if (GF_HEADROOM < 3)
    109      1.1  christos         gf_weak_reduce(c);
    110      1.1  christos }
    111      1.1  christos 
    112      1.1  christos /* Subtract mod p. Bias by amt but don't reduce.  */
    113      1.1  christos static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
    114      1.1  christos {
    115      1.1  christos     gf_sub_RAW(c, a, b);
    116      1.1  christos     gf_bias(c, amt);
    117      1.1  christos     if (GF_HEADROOM < amt + 1)
    118      1.1  christos         gf_weak_reduce(c);
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos /* Mul by signed int.  Not constant-time WRT the sign of that int. */
    122      1.1  christos static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
    123      1.1  christos {
    124      1.1  christos     if (w > 0) {
    125      1.1  christos         gf_mulw_unsigned(c, a, w);
    126      1.1  christos     } else {
    127      1.1  christos         gf_mulw_unsigned(c, a, -w);
    128      1.1  christos         gf_sub(c, ZERO, c);
    129      1.1  christos     }
    130      1.1  christos }
    131      1.1  christos 
    132      1.1  christos /* Constant time, x = is_z ? z : y */
    133      1.1  christos static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
    134      1.1  christos {
    135      1.1  christos     size_t i;
    136      1.1  christos 
    137      1.1  christos     for (i = 0; i < NLIMBS; i++) {
    138      1.1  christos #if ARCH_WORD_BITS == 32
    139      1.1  christos         x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
    140      1.1  christos                                                y[0].limb[i]);
    141      1.1  christos #else
    142      1.1  christos         /* Must be 64 bit */
    143      1.1  christos         x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
    144      1.1  christos                                                y[0].limb[i]);
    145      1.1  christos #endif
    146      1.1  christos     }
    147      1.1  christos }
    148      1.1  christos 
    149      1.1  christos /* Constant time, if (neg) x=-x; */
    150      1.1  christos static ossl_inline void gf_cond_neg(gf x, mask_t neg)
    151      1.1  christos {
    152      1.1  christos     gf y;
    153      1.1  christos 
    154      1.1  christos     gf_sub(y, ZERO, x);
    155      1.1  christos     gf_cond_sel(x, x, y, neg);
    156      1.1  christos }
    157      1.1  christos 
    158      1.1  christos /* Constant time, if (swap) (x,y) = (y,x); */
    159      1.1  christos static ossl_inline void gf_cond_swap(gf x, gf_s * RESTRICT y, mask_t swap)
    160      1.1  christos {
    161      1.1  christos     size_t i;
    162      1.1  christos 
    163      1.1  christos     for (i = 0; i < NLIMBS; i++) {
    164      1.1  christos #if ARCH_WORD_BITS == 32
    165      1.1  christos         constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
    166      1.1  christos #else
    167      1.1  christos         /* Must be 64 bit */
    168      1.1  christos         constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
    169      1.1  christos #endif
    170      1.1  christos     }
    171      1.1  christos }
    172      1.1  christos 
    173  1.1.1.2  christos #endif                          /* OSSL_CRYPTO_EC_CURVE448_FIELD_H */
    174