diff --git a/crypto_api.h b/crypto_api.h index 1827003e0..5c3d97eaa 100644 --- a/crypto_api.h +++ b/crypto_api.h @@ -1,4 +1,4 @@ -/* $OpenBSD: crypto_api.h,v 1.6 2020/12/29 00:59:15 djm Exp $ */ +/* $OpenBSD: crypto_api.h,v 1.7 2021/01/08 02:33:13 dtucker Exp $ */ /* * Assembled from generated headers and source files by Markus Friedl. @@ -21,6 +21,8 @@ typedef int16_t crypto_int16; typedef uint16_t crypto_uint16; typedef int32_t crypto_int32; typedef uint32_t crypto_uint32; +typedef int64_t crypto_int64; +typedef uint64_t crypto_uint64; #define randombytes(buf, buf_len) arc4random_buf((buf), (buf_len)) #define small_random32() arc4random() diff --git a/sntrup761.c b/sntrup761.c index de18ca953..01f1bc344 100644 --- a/sntrup761.c +++ b/sntrup761.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sntrup761.c,v 1.4 2021/01/04 21:58:58 dtucker Exp $ */ +/* $OpenBSD: sntrup761.c,v 1.5 2021/01/08 02:33:13 dtucker Exp $ */ /* * Public Domain, Authors: @@ -13,7 +13,14 @@ #include #include "crypto_api.h" -#define CRYPTO_NAMESPACE(s) s +#define int8 crypto_int8 +#define uint8 crypto_uint8 +#define int16 crypto_int16 +#define uint16 crypto_uint16 +#define int32 crypto_int32 +#define uint32 crypto_uint32 +#define int64 crypto_int64 +#define uint64 crypto_uint64 /* from supercop-20201130/crypto_sort/int32/portable4/int32_minmax.inc */ #define int32_MINMAX(a,b) \ @@ -28,7 +35,6 @@ do { \ } while(0) /* from supercop-20201130/crypto_sort/int32/portable4/sort.c */ -#define int32 crypto_int32 static void crypto_sort_int32(void *array,long long n) @@ -103,88 +109,6 @@ static void crypto_sort_uint32(void *array,long long n) for (j = 0;j < n;++j) x[j] ^= 0x80000000; } -#undef int32 - -/* from supercop-20201130/crypto_kem/sntrup761/ref/uint64.h */ -#ifndef UINT64_H -#define UINT64_H - - -typedef uint64_t uint64; - -#endif - -/* from supercop-20201130/crypto_kem/sntrup761/ref/uint16.h */ -#ifndef UINT16_H -#define UINT16_H - -typedef uint16_t uint16; - -#endif - -/* from supercop-20201130/crypto_kem/sntrup761/ref/uint32.h */ -#ifndef UINT32_H -#define UINT32_H - -#define uint32_div_uint14 CRYPTO_NAMESPACE(uint32_div_uint14) -#define uint32_mod_uint14 CRYPTO_NAMESPACE(uint32_mod_uint14) -#define uint32_divmod_uint14 CRYPTO_NAMESPACE(uint32_divmod_uint14) - - -typedef uint32_t uint32; - -/* -assuming 1 <= m < 16384: -q = uint32_div_uint14(x,m) means q = x/m -r = uint32_mod_uint14(x,m) means r = x/m -uint32_moddiv_uint14(&q,&r,x,m) means q = x/m, r = x%m -*/ - -extern uint32 uint32_div_uint14(uint32,uint16); -extern uint16 uint32_mod_uint14(uint32,uint16); -static void uint32_divmod_uint14(uint32 *,uint16 *,uint32,uint16); - -#endif - -/* from supercop-20201130/crypto_kem/sntrup761/ref/int8.h */ -#ifndef INT8_H -#define INT8_H - -typedef int8_t int8; - -#endif - -/* from supercop-20201130/crypto_kem/sntrup761/ref/int16.h */ -#ifndef INT16_H -#define INT16_H - -typedef int16_t int16; - -#endif - -/* from supercop-20201130/crypto_kem/sntrup761/ref/int32.h */ -#ifndef INT32_H -#define INT32_H - -#define int32_div_uint14 CRYPTO_NAMESPACE(int32_div_uint14) -#define int32_mod_uint14 CRYPTO_NAMESPACE(int32_mod_uint14) -#define int32_divmod_uint14 CRYPTO_NAMESPACE(int32_divmod_uint14) - - - -/* -assuming 1 <= m < 16384: -q = int32_div_uint14(x,m) means q = x/m -r = int32_mod_uint14(x,m) means r = x/m -int32_moddiv_uint14(&q,&r,x,m) means q = x/m, r = x%m -*/ - -extern int32 int32_div_uint14(int32,uint16); -extern uint16 int32_mod_uint14(int32,uint16); -static void int32_divmod_uint14(int32 *,uint16 *,int32,uint16); - -#endif - /* from supercop-20201130/crypto_kem/sntrup761/ref/uint32.c */ /* @@ -239,15 +163,8 @@ static void uint32_divmod_uint14(uint32 *q,uint16 *r,uint32 x,uint16 m) *r = x; } -uint32 uint32_div_uint14(uint32 x,uint16 m) -{ - uint32 q; - uint16 r; - uint32_divmod_uint14(&q,&r,x,m); - return q; -} -uint16 uint32_mod_uint14(uint32 x,uint16 m) +static uint16 uint32_mod_uint14(uint32 x,uint16 m) { uint32 q; uint16 r; @@ -271,15 +188,8 @@ static void int32_divmod_uint14(int32 *q,uint16 *r,int32 x,uint16 m) *r = ur; *q = uq; } -int32 int32_div_uint14(int32 x,uint16 m) -{ - int32 q; - uint16 r; - int32_divmod_uint14(&q,&r,x,m); - return q; -} -uint16 int32_mod_uint14(int32 x,uint16 m) +static uint16 int32_mod_uint14(int32 x,uint16 m) { int32 q; uint16 r; @@ -365,12 +275,10 @@ uint16 int32_mod_uint14(int32 x,uint16 m) #ifndef Decode_H #define Decode_H -#define Decode CRYPTO_NAMESPACE(Decode) /* Decode(R,s,M,len) */ /* assumes 0 < M[i] < 16384 */ /* produces 0 <= R[i] < M[i] */ -static void Decode(uint16 *,const unsigned char *,const uint16 *,long long); #endif @@ -432,11 +340,9 @@ static void Decode(uint16 *out,const unsigned char *S,const uint16 *M,long long #ifndef Encode_H #define Encode_H -#define Encode CRYPTO_NAMESPACE(Encode) /* Encode(s,R,M,len) */ /* assumes 0 <= R[i] < M[i] < 16384 */ -static void Encode(unsigned char *,const uint16 *,const uint16 *,long long); #endif diff --git a/sntrup761.sh b/sntrup761.sh index e7c5eed14..5cd5f92c3 100644 --- a/sntrup761.sh +++ b/sntrup761.sh @@ -1,5 +1,5 @@ #!/bin/sh -# $OpenBSD: sntrup761.sh,v 1.4 2021/01/04 21:58:58 dtucker Exp $ +# $OpenBSD: sntrup761.sh,v 1.5 2021/01/08 02:33:13 dtucker Exp $ # Placed in the Public Domain. # AUTHOR="supercop-20201130/crypto_kem/sntrup761/ref/implementors" @@ -7,12 +7,6 @@ FILES=" supercop-20201130/crypto_sort/int32/portable4/int32_minmax.inc supercop-20201130/crypto_sort/int32/portable4/sort.c supercop-20201130/crypto_sort/uint32/useint32/sort.c - supercop-20201130/crypto_kem/sntrup761/ref/uint64.h - supercop-20201130/crypto_kem/sntrup761/ref/uint16.h - supercop-20201130/crypto_kem/sntrup761/ref/uint32.h - supercop-20201130/crypto_kem/sntrup761/ref/int8.h - supercop-20201130/crypto_kem/sntrup761/ref/int16.h - supercop-20201130/crypto_kem/sntrup761/ref/int32.h supercop-20201130/crypto_kem/sntrup761/ref/uint32.c supercop-20201130/crypto_kem/sntrup761/ref/int32.c supercop-20201130/crypto_kem/sntrup761/ref/paramsmenu.h @@ -38,30 +32,54 @@ echo echo '#include ' echo '#include "crypto_api.h"' echo -echo '#define CRYPTO_NAMESPACE(s) s' +# Map the types used in this code to the ones in crypto_api.h. We use #define +# instead of typedef since some systems have existing intXX types and do not +# permit multiple typedefs even if they do not conflict. +for t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do + echo "#define $t crypto_${t}" +done echo for i in $FILES; do echo "/* from $i */" - grep \ - -v '#include' $i | \ + # Changes to all files: + # - remove all includes, we inline everything required. + # - make functions not required elsewhere static. + # - rename the functions we do use. + # - remove unneccesary defines and externs. + sed -e "/#include/d" \ + -e "s/crypto_kem_/crypto_kem_sntrup761_/g" \ + -e "s/^void /static void /g" \ + -e "s/^int16 /static int16 /g" \ + -e "s/^uint16 /static uint16 /g" \ + -e "/^extern /d" \ + -e '/CRYPTO_NAMESPACE/d' \ + -e "/^#define int32 crypto_int32/d" \ + $i | \ case "$i" in # Use int64_t for intermediate values in int32_MINMAX to prevent signed # 32-bit integer overflow when called by crypto_sort_uint32. */int32_minmax.inc) sed -e "s/int32 ab = b ^ a/int64_t ab = (int64_t)b ^ (int64_t)a/" \ - -e "s/int32 c = b - a/int64_t c = (int64_t)b - (int64_t)a/" + -e "s/int32 c = b - a/int64_t c = (int64_t)b - (int64_t)a/" ;; */int32/portable4/sort.c) - sed -e "s/void crypto_sort/static void crypto_sort_int32/g" + sed -e "s/void crypto_sort/void crypto_sort_int32/g" ;; */uint32/useint32/sort.c) - sed -e "s/void crypto_sort/static void crypto_sort_uint32/g" + sed -e "s/void crypto_sort/void crypto_sort_uint32/g" ;; + # Remove unused function to prevent warning. + */crypto_kem/sntrup761/ref/int32.c) + sed -e '/ int32_div_uint14/,/^}$/d' + ;; + # Remove unused function to prevent warning. + */crypto_kem/sntrup761/ref/uint32.c) + sed -e '/ uint32_div_uint14/,/^}$/d' + ;; + # Default: pass through. *) - sed -e "s/crypto_kem_/crypto_kem_sntrup761_/g" \ - -e "s/^extern void /static void /" \ - -e "s/^void /static void /" \ - -e "/^typedef int32_t int32;$/d" + cat + ;; esac echo done