Debian Codegraph @

Modules/_hacl/refresh.sh

raw
1 #!/usr/bin/env bash
2 #
3 # Use this script to update the HACL generated hash algorithm implementation
4 # code from a local checkout of the upstream hacl-star repository.
5 #
6
7 set -e
8 set -o pipefail
9
10 if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
11 echo "A bash version >= 4 required. Got: $BASH_VERSION" >&2
12 exit 1
13 fi
14
15 if [[ $1 == "" ]]; then
16 echo "Usage: $0 path-to-hacl-directory"
17 echo ""
18 echo " path-to-hacl-directory should be a local git checkout of a"
19 echo " https://github.com/hacl-star/hacl-star/ repo."
20 exit 1
21 fi
22
23 # Update this when updating to a new version after verifying that the changes
24 # the update brings in are good.
25 expected_hacl_star_rev=8ba599b2f6c9701b3dc961db895b0856a2210f76
26
27 hacl_dir="$(realpath "$1")"
28 cd "$(dirname "$0")"
29 actual_rev=$(cd "$hacl_dir" && git rev-parse HEAD)
30
31 if [[ "$actual_rev" != "$expected_hacl_star_rev" ]]; then
32 echo "WARNING: HACL* in '$hacl_dir' is at revision:" >&2
33 echo " $actual_rev" >&2
34 echo "but expected revision:" >&2
35 echo " $expected_hacl_star_rev" >&2
36 echo "Edit the expected rev if the changes pulled in are what you want."
37 fi
38
39 # Step 1: copy files
40
41 declare -a dist_files
42 dist_files=(
43 Hacl_Streaming_Types.h
44 internal/Hacl_Streaming_Types.h
45 # Cryptographic Hash Functions (headers)
46 Hacl_Hash_MD5.h
47 Hacl_Hash_SHA1.h
48 Hacl_Hash_SHA2.h
49 Hacl_Hash_SHA3.h
50 Hacl_Hash_Blake2b.h
51 Hacl_Hash_Blake2s.h
52 Hacl_Hash_Blake2b_Simd256.h
53 Hacl_Hash_Blake2s_Simd128.h
54 # Cryptographic Primitives (headers)
55 Hacl_HMAC.h
56 Hacl_Streaming_HMAC.h
57 # Cryptographic Hash Functions (internal headers)
58 internal/Hacl_Hash_MD5.h
59 internal/Hacl_Hash_SHA1.h
60 internal/Hacl_Hash_SHA2.h
61 internal/Hacl_Hash_SHA3.h
62 internal/Hacl_Hash_Blake2b.h
63 internal/Hacl_Hash_Blake2s.h
64 internal/Hacl_Hash_Blake2b_Simd256.h
65 internal/Hacl_Hash_Blake2s_Simd128.h
66 internal/Hacl_Impl_Blake2_Constants.h
67 # Cryptographic Primitives (internal headers)
68 internal/Hacl_HMAC.h
69 internal/Hacl_Streaming_HMAC.h
70 # Cryptographic Hash Functions (sources)
71 Hacl_Hash_MD5.c
72 Hacl_Hash_SHA1.c
73 Hacl_Hash_SHA2.c
74 Hacl_Hash_SHA3.c
75 Hacl_Hash_Blake2b.c
76 Hacl_Hash_Blake2s.c
77 Hacl_Hash_Blake2b_Simd256.c
78 Hacl_Hash_Blake2s_Simd128.c
79 # Cryptographic Primitives (sources)
80 Hacl_HMAC.c
81 Hacl_Streaming_HMAC.c
82 # Miscellaneous
83 libintvector.h
84 libintvector-shim.h
85 lib_memzero0.h
86 Lib_Memzero0.c
87 )
88
89 declare -a include_files
90 include_files=(
91 include/krml/lowstar_endianness.h
92 include/krml/internal/compat.h
93 include/krml/internal/target.h
94 include/krml/internal/types.h
95 )
96
97 declare -a lib_files
98 lib_files=(
99 krmllib/dist/minimal/FStar_UInt_8_16_32_64.h
100 krmllib/dist/minimal/fstar_uint128_struct_endianness.h
101 krmllib/dist/minimal/FStar_UInt128_Verified.h
102 )
103
104 # C files for the algorithms themselves: current directory
105 (cd "$hacl_dir/dist/gcc-compatible" && tar cf - "${dist_files[@]}") | tar xf -
106
107 # Support header files (e.g. endianness macros): stays in include/
108 (cd "$hacl_dir/dist/karamel" && tar cf - "${include_files[@]}") | tar xf -
109
110 # Special treatment: we don't bother with an extra directory and move krmllib
111 # files to the same include directory
112 for f in "${lib_files[@]}"; do
113 cp "$hacl_dir/dist/karamel/$f" include/krml/
114 done
115
116 # Step 2: some in-place modifications to keep things simple and minimal
117
118 # This is basic, but refreshes of the vendored HACL code are infrequent, so
119 # let's not over-engineer this.
120 if [[ $(uname) == "Darwin" ]]; then
121 # You're already running with homebrew or macports to satisfy the
122 # bash>=4 requirement, so requiring GNU sed is entirely reasonable.
123 sed=gsed
124 else
125 sed=sed
126 fi
127
128 readarray -t all_files < <(find . -name '*.h' -or -name '*.c')
129
130 # Adjust the include path to reflect the local directory structure
131 $sed -i 's!#include "FStar_UInt128_Verified.h"!#include "krml/FStar_UInt128_Verified.h"!g' include/krml/internal/types.h
132 $sed -i 's!#include "fstar_uint128_struct_endianness.h"!#include "krml/fstar_uint128_struct_endianness.h"!g' include/krml/internal/types.h
133
134 # use KRML_VERIFIED_UINT128
135 $sed -i -z 's!#define KRML_TYPES_H!#define KRML_TYPES_H\n#define KRML_VERIFIED_UINT128!g' include/krml/internal/types.h
136
137 # This contains static inline prototypes that are defined in
138 # FStar_UInt_8_16_32_64; they are by default repeated for safety of separate
139 # compilation, but this is not necessary.
140 $sed -i 's!#include.*Hacl_Krmllib.h"!!g' "${all_files[@]}"
141
142 # Use globally unique names for the Hacl_ C APIs to avoid linkage conflicts.
143 $sed -i -z 's!#include <string.h>!#include <string.h>\n#include "python_hacl_namespaces.h"!' \
144 Hacl_Hash_*.h \
145 Hacl_HMAC.h \
146 Hacl_Streaming_HMAC.h
147
148 $sed -i -z 's!#include <inttypes.h>!#include <inttypes.h>\n#include "python_hacl_namespaces.h"!' \
149 lib_memzero0.h
150
151 # Step 3: trim whitespace (for the linter)
152
153 find . -name '*.c' -or -name '*.h' | xargs $sed -i 's![[:space:]]\+$!!'
154
155 echo "Updated; verify all is okay using git diff and git status."