-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextract-kem-from-nist-submission
executable file
·111 lines (94 loc) · 2.55 KB
/
extract-kem-from-nist-submission
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
set -euo pipefail
shopt -s inherit_errexit
# Remove the old implementation.
rm -rf kem
archive=mceliece-20221023
archfile=$archive.tar.gz
echo -n "md5: "; md5sum $archfile | cut -c 1-32
echo -n "sha1: "; sha1sum $archfile | cut -c 1-40
echo -n "sha256: "; sha256sum $archfile | cut -c 1-64
echo
# Extract the reference implementation.
tar -xf $archfile $archive/Reference_Implementation/kem/ --strip-components=2
prepare_algorithm() {
echo "Preparing $id"
add_header $1
add_gyp $1
patch_files $1
}
add_header() {
id=$1
echo "#include \"kem/$id/crypto_kem_$id.h\"" >> mceliece.h
echo "- Added $id to mceliece.h"
}
add_gyp() {
id=$1
variables=("CRYPTO_NAMESPACE(x)=pqcrypto_kem_${id}_##x" \
"_CRYPTO_NAMESPACE(x)=_pqcrypto_kem_${id}_##x")
(
echo " {"
echo " 'target_name': 'only_$id',"
echo " 'type': 'static_library',"
echo " 'sources': ["
find kem/$id -maxdepth 1 -type f -name '*.c' | sort | while read source; do
echo " '$source',"
done
echo " ],"
echo " 'include_dirs': ["
echo " 'kem/$id/subroutines',"
echo " ],"
echo " 'defines': ["
for def in ${variables[@]}; do
echo " '$def',"
done
echo " ],"
echo " 'cflags': ['-fPIC', '-Wno-unused-function']"
echo " },"
) >> binding.gyp
echo "- Added $id to binding.gyp"
}
patch_files() {
id=$1
(
# Non-source files.
find kem/$id/ -type f ! -name '*.c' ! -name '*.h'
# Only required for the NIST submission.
echo kem/$id/nist
# Irrelevant header files.
echo kem/$id/api.h
) | while read f; do
rm -r "$f"
echo "- Removed $f"
done
find kem/$id -type f \( -name '*.c' -o -name '*.h' \) | while read source; do
oldsha=$(sha1sum "$source")
sed -i 's=<libkeccak\.a\.headers/SimpleFIPS202\.h>="../../mceliece_externals.h"=g; s="nist/rng.h"="../../mceliece_externals.h"=g' $source
newsha=$(sha1sum "$source")
if [ "$oldsha" != "$newsha" ]; then
echo "- Patched $source"
fi
done
}
rm -f mceliece.h
implementations=`ls -1 kem | while read id; do if [ -d "kem/$id" ]; then echo $id; fi; done | sort`
(
echo "{"
echo " 'targets': ["
echo " {"
echo " 'target_name': 'mceliece',"
echo " 'type': 'none',"
echo " 'dependencies': ["
for impl in $implementations; do
echo " 'only_$impl',"
done
echo " ]"
echo " },"
) > binding.gyp
for id in $implementations; do
prepare_algorithm $id
done
(
echo " ]"
echo "}"
) >> binding.gyp