xref: /freebsd/crypto/openssh/sntrup761.sh (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
1#!/bin/sh
2#       $OpenBSD: sntrup761.sh,v 1.5 2021/01/08 02:33:13 dtucker 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 unneccesary 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	    $i | \
58	case "$i" in
59	# Use int64_t for intermediate values in int32_MINMAX to prevent signed
60	# 32-bit integer overflow when called by crypto_sort_uint32.
61	*/int32_minmax.inc)
62	    sed -e "s/int32 ab = b ^ a/int64_t ab = (int64_t)b ^ (int64_t)a/" \
63	        -e "s/int32 c = b - a/int64_t c = (int64_t)b - (int64_t)a/"
64	    ;;
65	*/int32/portable4/sort.c)
66	    sed -e "s/void crypto_sort/void crypto_sort_int32/g"
67	    ;;
68	*/uint32/useint32/sort.c)
69	    sed -e "s/void crypto_sort/void crypto_sort_uint32/g"
70	    ;;
71	# Remove unused function to prevent warning.
72	*/crypto_kem/sntrup761/ref/int32.c)
73	    sed -e '/ int32_div_uint14/,/^}$/d'
74	    ;;
75	# Remove unused function to prevent warning.
76	*/crypto_kem/sntrup761/ref/uint32.c)
77	    sed -e '/ uint32_div_uint14/,/^}$/d'
78	    ;;
79	# Default: pass through.
80	*)
81	    cat
82	    ;;
83	esac
84	echo
85done
86