xref: /freebsd/crypto/openssh/sntrup761.sh (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1#!/bin/sh
2#       $OpenBSD: sntrup761.sh,v 1.7 2023/01/11 02:13:52 djm Exp $
3#       Placed in the Public Domain.
4#
5AUTHOR="supercop-20201130/crypto_kem/sntrup761/ref/implementors"
6FILES="
7	supercop-20201130/crypto_sort/int32/portable4/int32_minmax.inc
8	supercop-20201130/crypto_sort/int32/portable4/sort.c
9	supercop-20201130/crypto_sort/uint32/useint32/sort.c
10	supercop-20201130/crypto_kem/sntrup761/ref/uint32.c
11	supercop-20201130/crypto_kem/sntrup761/ref/int32.c
12	supercop-20201130/crypto_kem/sntrup761/ref/paramsmenu.h
13	supercop-20201130/crypto_kem/sntrup761/ref/params.h
14	supercop-20201130/crypto_kem/sntrup761/ref/Decode.h
15	supercop-20201130/crypto_kem/sntrup761/ref/Decode.c
16	supercop-20201130/crypto_kem/sntrup761/ref/Encode.h
17	supercop-20201130/crypto_kem/sntrup761/ref/Encode.c
18	supercop-20201130/crypto_kem/sntrup761/ref/kem.c
19"
20###
21
22set -e
23cd $1
24echo -n '/*  $'
25echo 'OpenBSD: $ */'
26echo
27echo '/*'
28echo ' * Public Domain, Authors:'
29sed -e '/Alphabetical order:/d' -e 's/^/ * - /' < $AUTHOR
30echo ' */'
31echo
32echo '#include <string.h>'
33echo '#include "crypto_api.h"'
34echo
35# Map the types used in this code to the ones in crypto_api.h.  We use #define
36# instead of typedef since some systems have existing intXX types and do not
37# permit multiple typedefs even if they do not conflict.
38for t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do
39	echo "#define $t crypto_${t}"
40done
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	# Use int64_t for intermediate values in int32_MINMAX to prevent signed
61	# 32-bit integer overflow when called by crypto_sort_uint32.
62	*/int32_minmax.inc)
63	    sed -e "s/int32 ab = b ^ a/int64_t ab = (int64_t)b ^ (int64_t)a/" \
64	        -e "s/int32 c = b - a/int64_t c = (int64_t)b - (int64_t)a/"
65	    ;;
66	*/int32/portable4/sort.c)
67	    sed -e "s/void crypto_sort/void crypto_sort_int32/g"
68	    ;;
69	*/uint32/useint32/sort.c)
70	    sed -e "s/void crypto_sort/void crypto_sort_uint32/g"
71	    ;;
72	# Remove unused function to prevent warning.
73	*/crypto_kem/sntrup761/ref/int32.c)
74	    sed -e '/ int32_div_uint14/,/^}$/d'
75	    ;;
76	# Remove unused function to prevent warning.
77	*/crypto_kem/sntrup761/ref/uint32.c)
78	    sed -e '/ uint32_div_uint14/,/^}$/d'
79	    ;;
80	# Default: pass through.
81	*)
82	    cat
83	    ;;
84	esac
85	echo
86done
87