1#!/bin/sh
2#       $OpenBSD: ed25519.sh,v 1.1 2023/01/15 23:05:32 djm Exp $
3#       Placed in the Public Domain.
4#
5AUTHOR="supercop-20221122/crypto_sign/ed25519/ref/implementors"
6FILES="
7	supercop-20221122/crypto_verify/32/ref/verify.c
8	supercop-20221122/crypto_sign/ed25519/ref/fe25519.h
9	supercop-20221122/crypto_sign/ed25519/ref/fe25519.c
10	supercop-20221122/crypto_sign/ed25519/ref/sc25519.h
11	supercop-20221122/crypto_sign/ed25519/ref/sc25519.c
12	supercop-20221122/crypto_sign/ed25519/ref/ge25519.h
13	supercop-20221122/crypto_sign/ed25519/ref/ge25519.c
14	supercop-20221122/crypto_sign/ed25519/ref/keypair.c
15	supercop-20221122/crypto_sign/ed25519/ref/sign.c
16	supercop-20221122/crypto_sign/ed25519/ref/open.c
17"
18###
19
20DATA="supercop-20221122/crypto_sign/ed25519/ref/ge25519_base.data"
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
34echo '#include "crypto_api.h"'
35echo
36# Map the types used in this code to the ones in crypto_api.h.  We use #define
37# instead of typedef since some systems have existing intXX types and do not
38# permit multiple typedefs even if they do not conflict.
39for t in int8 uint8 int16 uint16 int32 uint32 int64 uint64; do
40	echo "#define $t crypto_${t}"
41done
42echo
43for i in $FILES; do
44	echo "/* from $i */"
45	# Changes to all files:
46	#  - inline ge25519_base.data where it is included
47	#  - expand CRYPTO_NAMESPACE() namespacing define
48	#  - remove all includes, we inline everything required.
49	#  - make functions not required elsewhere static.
50	#  - rename the functions we do use.
51	sed \
52	    -e "/#include \"ge25519_base.data\"/r $DATA" \
53	    -e "/#include/d" \
54	    -e "s/^void /static void /g" \
55	    -e 's/CRYPTO_NAMESPACE[(]\([a-zA-Z0-9_]*\)[)]/crypto_sign_ed25519_ref_\1/g' \
56	    $i | \
57	case "$i" in
58	*/crypto_verify/32/ref/verify.c)
59	    # rename crypto_verify() to the name that the ed25519 code expects.
60	    sed -e "/^#include.*/d" \
61	        -e "s/crypto_verify/crypto_verify_32/g" \
62	        -e "s/^int /static int /g"
63	    ;;
64	*/crypto_sign/ed25519/ref/sign.c)
65	    # rename signing function to the name OpenSSH expects
66	    sed -e "s/crypto_sign/crypto_sign_ed25519/g"
67	    ;;
68	*/crypto_sign/ed25519/ref/keypair.c)
69	    # rename key generation function to the name OpenSSH expects
70	    sed -e "s/crypto_sign_keypair/crypto_sign_ed25519_keypair/g"
71	    ;;
72	*/crypto_sign/ed25519/ref/open.c)
73	    # rename verification function to the name OpenSSH expects
74	    sed -e "s/crypto_sign_open/crypto_sign_ed25519_open/g"
75	    ;;
76	*/crypto_sign/ed25519/ref/fe25519.*)
77	    # avoid a couple of name collions with other files
78	    sed -e "s/reduce_add_sub/fe25519_reduce_add_sub/g" \
79	        -e "s/ equal[(]/ fe25519_equal(/g" \
80	        -e "s/^int /static int /g"
81	    ;;
82	*/crypto_sign/ed25519/ref/sc25519.h)
83	    # Lots of unused prototypes to remove
84	    sed -e "s/^int /static int /g" \
85	        -e '/shortsc25519_from16bytes/d' \
86	        -e '/sc25519_iszero_vartime/d' \
87	        -e '/sc25519_isshort_vartime/d' \
88	        -e '/sc25519_lt_vartime/d' \
89	        -e '/sc25519_sub_nored/d' \
90	        -e '/sc25519_mul_shortsc/d' \
91	        -e '/sc25519_from_shortsc/d' \
92	        -e '/sc25519_window5/d'
93	    ;;
94	*/crypto_sign/ed25519/ref/sc25519.c)
95	    # Lots of unused code to remove, some name collisions to avoid
96	    sed -e "s/reduce_add_sub/sc25519_reduce_add_sub/g" \
97	        -e "s/ equal[(]/ sc25519_equal(/g" \
98	        -e "s/^int /static int /g" \
99	        -e "s/m[[]/sc25519_m[/g" \
100	        -e "s/mu[[]/sc25519_mu[/g" \
101	        -e '/shortsc25519_from16bytes/,/^}$/d' \
102	        -e '/sc25519_iszero_vartime/,/^}$/d' \
103	        -e '/sc25519_isshort_vartime/,/^}$/d' \
104	        -e '/sc25519_lt_vartime/,/^}$/d' \
105	        -e '/sc25519_sub_nored/,/^}$/d' \
106	        -e '/sc25519_mul_shortsc/,/^}$/d' \
107	        -e '/sc25519_from_shortsc/,/^}$/d' \
108	        -e '/sc25519_window5/,/^}$/d'
109	    ;;
110	*/crypto_sign/ed25519/ref//ge25519.*)
111	    sed -e "s/^int /static int /g"
112	    ;;
113	# Default: pass through.
114	*)
115	    cat
116	    ;;
117	esac | \
118	sed -e 's/[	 ]*$//'
119done
120