]> git.ipfire.org Git - thirdparty/openssl.git/blobdiff - crypto/ec/curve448/field.h
Update copyright year
[thirdparty/openssl.git] / crypto / ec / curve448 / field.h
index b985d453e783b9e7274cfbf97bcf4fef375c7e63..e1c633378950db1ea5064c00b4cd34f917fcdd80 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  * Copyright 2014 Cryptography Research, Inc.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (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
  */
 
-#ifndef __GF_H__
-# define __GF_H__
+#ifndef OSSL_CRYPTO_EC_CURVE448_FIELD_H
+# define OSSL_CRYPTO_EC_CURVE448_FIELD_H
 
-# include "constant_time.h"
+# include "internal/constant_time.h"
 # include <string.h>
 # include <assert.h>
 # include "word.h"
@@ -23,9 +23,9 @@
 # define SER_BYTES 56
 
 # if defined(__GNUC__) || defined(__clang__)
-#  define INLINE_UNUSED __inline__ __attribute__((unused,always_inline))
+#  define INLINE_UNUSED __inline__ __attribute__((__unused__,__always_inline__))
 #  define RESTRICT __restrict__
-#  define ALIGNED __attribute__((aligned(32)))
+#  define ALIGNED __attribute__((__aligned__(16)))
 # else
 #  define INLINE_UNUSED ossl_inline
 #  define RESTRICT
@@ -66,12 +66,15 @@ 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 */
 
-# ifndef LIMBPERM
-#  define LIMBPERM(i) (i)
+# define LIMBPERM(i) (i)
+# if (ARCH_WORD_BITS == 32)
+#  include "arch_32/f_impl.h"    /* Bring in the inline implementations */
+#  define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
+# elif (ARCH_WORD_BITS == 64)
+#  include "arch_64/f_impl.h"    /* Bring in the inline implementations */
+#  define LIMB_MASK(i) (((1ULL)<<LIMB_PLACE_VALUE(i))-1)
 # endif
-# define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
 
 static const gf ZERO = {{{0}}}, ONE = {{{1}}};
 
@@ -79,6 +82,7 @@ static const gf ZERO = {{{0}}}, ONE = {{{1}}};
 static ossl_inline void gf_sqrn(gf_s * RESTRICT y, const gf x, int n)
 {
     gf tmp;
+
     assert(n > 0);
     if (n & 1) {
         gf_sqr(y, x);
@@ -128,13 +132,25 @@ static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
 /* Constant time, x = is_z ? z : y */
 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
 {
-    constant_time_select(x, y, z, sizeof(gf), is_z, 0);
+    size_t i;
+
+    for (i = 0; i < NLIMBS; i++) {
+#if ARCH_WORD_BITS == 32
+        x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
+                                               y[0].limb[i]);
+#else
+        /* Must be 64 bit */
+        x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
+                                               y[0].limb[i]);
+#endif
+    }
 }
 
 /* Constant time, if (neg) x=-x; */
 static ossl_inline void gf_cond_neg(gf x, mask_t neg)
 {
     gf y;
+
     gf_sub(y, ZERO, x);
     gf_cond_sel(x, x, y, neg);
 }
@@ -142,7 +158,16 @@ static ossl_inline void gf_cond_neg(gf x, mask_t neg)
 /* Constant time, if (swap) (x,y) = (y,x); */
 static ossl_inline void gf_cond_swap(gf x, gf_s * RESTRICT y, mask_t swap)
 {
-    constant_time_cond_swap(x, y, sizeof(gf_s), swap);
+    size_t i;
+
+    for (i = 0; i < NLIMBS; i++) {
+#if ARCH_WORD_BITS == 32
+        constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
+#else
+        /* Must be 64 bit */
+        constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
+#endif
+    }
 }
 
-#endif                          /* __GF_H__ */
+#endif                          /* OSSL_CRYPTO_EC_CURVE448_FIELD_H */