xref: /freebsd/crypto/openssh/sntrup761.sh (revision 3d9fd9fcb432750f3716b28f6ccb0104cd9d351a)
119261079SEd Maste#!/bin/sh
2*3d9fd9fcSEd Maste#       $OpenBSD: sntrup761.sh,v 1.9 2024/09/16 05:37:05 djm Exp $
319261079SEd Maste#       Placed in the Public Domain.
419261079SEd Maste#
5*3d9fd9fcSEd MasteAUTHOR="supercop-20240808/crypto_kem/sntrup761/ref/implementors"
6*3d9fd9fcSEd MasteFILES=" supercop-20240808/cryptoint/crypto_int16.h
7*3d9fd9fcSEd Maste	supercop-20240808/cryptoint/crypto_int32.h
8*3d9fd9fcSEd Maste	supercop-20240808/cryptoint/crypto_int64.h
9*3d9fd9fcSEd Maste	supercop-20240808/crypto_sort/int32/portable4/sort.c
10*3d9fd9fcSEd Maste	supercop-20240808/crypto_sort/uint32/useint32/sort.c
11*3d9fd9fcSEd Maste	supercop-20240808/crypto_kem/sntrup761/compact/kem.c
1219261079SEd Maste"
1319261079SEd Maste###
1419261079SEd Maste
15*3d9fd9fcSEd Masteset -euo pipefail
1619261079SEd Mastecd $1
1719261079SEd Masteecho -n '/*  $'
1819261079SEd Masteecho 'OpenBSD: $ */'
1919261079SEd Masteecho
2019261079SEd Masteecho '/*'
2119261079SEd Masteecho ' * Public Domain, Authors:'
2219261079SEd Mastesed -e '/Alphabetical order:/d' -e 's/^/ * - /' < $AUTHOR
2319261079SEd Masteecho ' */'
2419261079SEd Masteecho
2519261079SEd Masteecho '#include <string.h>'
2619261079SEd Masteecho '#include "crypto_api.h"'
2719261079SEd Masteecho
28*3d9fd9fcSEd Masteecho '#define crypto_declassify(x, y) do {} while (0)'
29*3d9fd9fcSEd Masteecho
3019261079SEd Maste# Map the types used in this code to the ones in crypto_api.h.  We use #define
3119261079SEd Maste# instead of typedef since some systems have existing intXX types and do not
3219261079SEd Maste# permit multiple typedefs even if they do not conflict.
3319261079SEd Mastefor t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do
3419261079SEd Maste	echo "#define $t crypto_${t}"
3519261079SEd Mastedone
36*3d9fd9fcSEd Maste
37*3d9fd9fcSEd Mastefor x in 16 32 64 ; do
38*3d9fd9fcSEd Maste	echo "extern volatile crypto_int$x crypto_int${x}_optblocker;"
39*3d9fd9fcSEd Mastedone
40*3d9fd9fcSEd Maste
4119261079SEd Masteecho
4219261079SEd Mastefor i in $FILES; do
4319261079SEd Maste	echo "/* from $i */"
4419261079SEd Maste	# Changes to all files:
4519261079SEd Maste	#  - remove all includes, we inline everything required.
4619261079SEd Maste	#  - make functions not required elsewhere static.
4719261079SEd Maste	#  - rename the functions we do use.
48f374ba41SEd Maste	#  - remove unnecessary defines and externs.
4919261079SEd Maste	sed -e "/#include/d" \
5019261079SEd Maste	    -e "s/crypto_kem_/crypto_kem_sntrup761_/g" \
5119261079SEd Maste	    -e "s/^void /static void /g" \
5219261079SEd Maste	    -e "s/^int16 /static int16 /g" \
5319261079SEd Maste	    -e "s/^uint16 /static uint16 /g" \
5419261079SEd Maste	    -e "/^extern /d" \
5519261079SEd Maste	    -e '/CRYPTO_NAMESPACE/d' \
5619261079SEd Maste	    -e "/^#define int32 crypto_int32/d" \
57f374ba41SEd Maste	    -e 's/[	 ]*$//' \
5819261079SEd Maste	    $i | \
5919261079SEd Maste	case "$i" in
60*3d9fd9fcSEd Maste	*/cryptoint/crypto_int16.h)
61*3d9fd9fcSEd Maste	    sed -e "s/static void crypto_int16_store/void crypto_int16_store/" \
62*3d9fd9fcSEd Maste		-e "s/^[#]define crypto_int16_optblocker.*//" \
63*3d9fd9fcSEd Maste	        -e "s/static void crypto_int16_minmax/void crypto_int16_minmax/"
64*3d9fd9fcSEd Maste	    ;;
65*3d9fd9fcSEd Maste	*/cryptoint/crypto_int32.h)
66*3d9fd9fcSEd Maste	# Use int64_t for intermediate values in crypto_int32_minmax to
67*3d9fd9fcSEd Maste	# prevent signed 32-bit integer overflow when called by
68*3d9fd9fcSEd Maste	# crypto_sort_int32. Original code depends on -fwrapv (we set -ftrapv)
69*3d9fd9fcSEd Maste	    sed -e "s/static void crypto_int32_store/void crypto_int32_store/" \
70*3d9fd9fcSEd Maste		-e "s/^[#]define crypto_int32_optblocker.*//" \
71*3d9fd9fcSEd Maste		-e "s/crypto_int32 crypto_int32_r = crypto_int32_y ^ crypto_int32_x;/crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;/" \
72*3d9fd9fcSEd Maste		-e "s/crypto_int32 crypto_int32_z = crypto_int32_y - crypto_int32_x;/crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;/" \
73*3d9fd9fcSEd Maste	        -e "s/static void crypto_int32_minmax/void crypto_int32_minmax/"
74*3d9fd9fcSEd Maste	    ;;
75*3d9fd9fcSEd Maste	*/cryptoint/crypto_int64.h)
76*3d9fd9fcSEd Maste	    sed -e "s/static void crypto_int64_store/void crypto_int64_store/" \
77*3d9fd9fcSEd Maste		-e "s/^[#]define crypto_int64_optblocker.*//" \
78*3d9fd9fcSEd Maste	        -e "s/static void crypto_int64_minmax/void crypto_int64_minmax/"
7919261079SEd Maste	    ;;
8019261079SEd Maste	*/int32/portable4/sort.c)
81*3d9fd9fcSEd Maste	    sed -e "s/void crypto_sort[(]/void crypto_sort_int32(/g"
82*3d9fd9fcSEd Maste	    ;;
83*3d9fd9fcSEd Maste	*/int32/portable5/sort.c)
84*3d9fd9fcSEd Maste	    sed -e "s/crypto_sort_smallindices/crypto_sort_int32_smallindices/"\
85*3d9fd9fcSEd Maste	        -e "s/void crypto_sort[(]/void crypto_sort_int32(/g"
8619261079SEd Maste	    ;;
8719261079SEd Maste	*/uint32/useint32/sort.c)
8819261079SEd Maste	    sed -e "s/void crypto_sort/void crypto_sort_uint32/g"
8919261079SEd Maste	    ;;
9019261079SEd Maste	# Remove unused function to prevent warning.
9119261079SEd Maste	*/crypto_kem/sntrup761/ref/int32.c)
9219261079SEd Maste	    sed -e '/ int32_div_uint14/,/^}$/d'
9319261079SEd Maste	    ;;
9419261079SEd Maste	# Remove unused function to prevent warning.
9519261079SEd Maste	*/crypto_kem/sntrup761/ref/uint32.c)
9619261079SEd Maste	    sed -e '/ uint32_div_uint14/,/^}$/d'
9719261079SEd Maste	    ;;
9819261079SEd Maste	# Default: pass through.
9919261079SEd Maste	*)
10019261079SEd Maste	    cat
10119261079SEd Maste	    ;;
10219261079SEd Maste	esac
10319261079SEd Maste	echo
10419261079SEd Mastedone
105