xref: /freebsd/crypto/openssh/sntrup761.sh (revision 3d9fd9fcb432750f3716b28f6ccb0104cd9d351a)
1#!/bin/sh
2#       $OpenBSD: sntrup761.sh,v 1.9 2024/09/16 05:37:05 djm Exp $
3#       Placed in the Public Domain.
4#
5AUTHOR="supercop-20240808/crypto_kem/sntrup761/ref/implementors"
6FILES=" supercop-20240808/cryptoint/crypto_int16.h
7	supercop-20240808/cryptoint/crypto_int32.h
8	supercop-20240808/cryptoint/crypto_int64.h
9	supercop-20240808/crypto_sort/int32/portable4/sort.c
10	supercop-20240808/crypto_sort/uint32/useint32/sort.c
11	supercop-20240808/crypto_kem/sntrup761/compact/kem.c
12"
13###
14
15set -euo pipefail
16cd $1
17echo -n '/*  $'
18echo 'OpenBSD: $ */'
19echo
20echo '/*'
21echo ' * Public Domain, Authors:'
22sed -e '/Alphabetical order:/d' -e 's/^/ * - /' < $AUTHOR
23echo ' */'
24echo
25echo '#include <string.h>'
26echo '#include "crypto_api.h"'
27echo
28echo '#define crypto_declassify(x, y) do {} while (0)'
29echo
30# Map the types used in this code to the ones in crypto_api.h.  We use #define
31# instead of typedef since some systems have existing intXX types and do not
32# permit multiple typedefs even if they do not conflict.
33for t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do
34	echo "#define $t crypto_${t}"
35done
36
37for x in 16 32 64 ; do
38	echo "extern volatile crypto_int$x crypto_int${x}_optblocker;"
39done
40
41echo
42for i in $FILES; do
43	echo "/* from $i */"
44	# Changes to all files:
45	#  - remove all includes, we inline everything required.
46	#  - make functions not required elsewhere static.
47	#  - rename the functions we do use.
48	#  - remove unnecessary defines and externs.
49	sed -e "/#include/d" \
50	    -e "s/crypto_kem_/crypto_kem_sntrup761_/g" \
51	    -e "s/^void /static void /g" \
52	    -e "s/^int16 /static int16 /g" \
53	    -e "s/^uint16 /static uint16 /g" \
54	    -e "/^extern /d" \
55	    -e '/CRYPTO_NAMESPACE/d' \
56	    -e "/^#define int32 crypto_int32/d" \
57	    -e 's/[	 ]*$//' \
58	    $i | \
59	case "$i" in
60	*/cryptoint/crypto_int16.h)
61	    sed -e "s/static void crypto_int16_store/void crypto_int16_store/" \
62		-e "s/^[#]define crypto_int16_optblocker.*//" \
63	        -e "s/static void crypto_int16_minmax/void crypto_int16_minmax/"
64	    ;;
65	*/cryptoint/crypto_int32.h)
66	# Use int64_t for intermediate values in crypto_int32_minmax to
67	# prevent signed 32-bit integer overflow when called by
68	# crypto_sort_int32. Original code depends on -fwrapv (we set -ftrapv)
69	    sed -e "s/static void crypto_int32_store/void crypto_int32_store/" \
70		-e "s/^[#]define crypto_int32_optblocker.*//" \
71		-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		-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	        -e "s/static void crypto_int32_minmax/void crypto_int32_minmax/"
74	    ;;
75	*/cryptoint/crypto_int64.h)
76	    sed -e "s/static void crypto_int64_store/void crypto_int64_store/" \
77		-e "s/^[#]define crypto_int64_optblocker.*//" \
78	        -e "s/static void crypto_int64_minmax/void crypto_int64_minmax/"
79	    ;;
80	*/int32/portable4/sort.c)
81	    sed -e "s/void crypto_sort[(]/void crypto_sort_int32(/g"
82	    ;;
83	*/int32/portable5/sort.c)
84	    sed -e "s/crypto_sort_smallindices/crypto_sort_int32_smallindices/"\
85	        -e "s/void crypto_sort[(]/void crypto_sort_int32(/g"
86	    ;;
87	*/uint32/useint32/sort.c)
88	    sed -e "s/void crypto_sort/void crypto_sort_uint32/g"
89	    ;;
90	# Remove unused function to prevent warning.
91	*/crypto_kem/sntrup761/ref/int32.c)
92	    sed -e '/ int32_div_uint14/,/^}$/d'
93	    ;;
94	# Remove unused function to prevent warning.
95	*/crypto_kem/sntrup761/ref/uint32.c)
96	    sed -e '/ uint32_div_uint14/,/^}$/d'
97	    ;;
98	# Default: pass through.
99	*)
100	    cat
101	    ;;
102	esac
103	echo
104done
105