1#!/bin/bash -eu
2# Copyright 2018 Google Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16################################################################################
17
18# build projects
19#nettle
20(
21cd nettle
22tar -xvf ../gmp-6.1.2.tar.bz2
23cd gmp-6.1.2
24#do not use assembly instructions as we do not know if they will be available on the machine who will run the fuzzer
25#we could do instead --enable-fat
26./configure --disable-shared --disable-assembly
27make -j$(nproc)
28make install
29cd ..
30autoreconf
31./configure --disable-shared --disable-openssl
32make -j$(nproc)
33make install
34)
35
36#cryptopp
37(
38cd cryptopp
39make -j$(nproc)
40make install
41)
42
43#gcrypt
44(
45cd libgpg-error
46./autogen.sh
47if [ "$ARCHITECTURE" = 'i386' ]; then
48    ./configure -host=i386 --disable-doc --enable-static --disable-shared
49else
50    ./configure --disable-doc --enable-static --disable-shared
51fi
52make -j$(nproc)
53make install
54cd ../gcrypt
55./autogen.sh
56if [ "$ARCHITECTURE" = 'i386' ]; then
57    ./configure -host=i386 --enable-static --disable-shared --disable-doc --enable-maintainer-mode
58else
59    ./configure --enable-static --disable-shared --disable-doc --enable-maintainer-mode
60fi
61make -j$(nproc)
62make install
63)
64
65#mbedtls
66(
67cd mbedtls
68cmake . -DENABLE_PROGRAMS=0 -DENABLE_TESTING=0
69make -j$(nproc) all
70make install
71)
72
73#openssl
74(
75cd openssl
76#option to not have the same exported function poly1305_blocks as in gcrypt
77if [ "$ARCHITECTURE" = 'i386' ]; then
78    setarch i386 ./config no-poly1305 no-shared no-threads -m32
79else
80    ./config no-poly1305 no-shared no-threads
81fi
82make build_generated libcrypto.a
83make install
84)
85
86#libecc
87(
88cd libecc
89#required by libecc
90(export CFLAGS="$CFLAGS -fPIC"; make; cp build/*.a /usr/local/lib; cp -r src/* /usr/local/include/)
91)
92
93#botan
94(
95cd botan
96if [ "$ARCHITECTURE" = 'i386' ]; then
97    ./configure.py --cc-bin=$CXX --cc-abi-flags="$CXXFLAGS" \
98               --disable-shared --disable-modules=locking_allocator --disable-shared-library \
99               --without-os-features=getrandom,getentropy --cpu x86_32
100else
101    ./configure.py --cc-bin=$CXX --cc-abi-flags="$CXXFLAGS" \
102               --disable-shared --disable-modules=locking_allocator --disable-shared-library \
103               --without-os-features=getrandom,getentropy
104fi
105make -j$(nproc)
106make install
107)
108
109#quickjs
110(
111cd quickjs
112if [ "$ARCHITECTURE" = 'i386' ]; then
113    make qjsc
114    cp qjsc /usr/local/bin/
115    make clean
116    # Makefile should not override CFLAGS
117    sed -i -e 's/CFLAGS=/CFLAGS+=/' Makefile
118    CFLAGS="-m32" make libquickjs.a
119else
120    make && make install
121fi
122cp quickjs*.h /usr/local/include/
123cp libquickjs.a /usr/local/lib/
124)
125
126#build fuzz target
127cd ecfuzzer
128if [ "$ARCHITECTURE" = 'i386' ]; then
129    export GOARCH=386
130#needed explicitly because of cross compilation cf https://golang.org/cmd/cgo/
131    export CGO_ENABLED=1
132    export CARGO_BUILD_TARGET=i686-unknown-linux-gnu
133fi
134zip -r fuzz_ec_seed_corpus.zip corpus/
135cp fuzz_ec_seed_corpus.zip $OUT/
136cp fuzz_ec.dict $OUT/
137cp fuzz_ec.dict $OUT/fuzz_ec_noblocker.dict
138
139mkdir build
140cd build
141#no afl with long javascript initialization
142if [ "$FUZZING_ENGINE" != 'afl' ]; then
143    cmake ..
144    make -j$(nproc)
145    cp ecfuzzer $OUT/fuzz_ec
146    rm -Rf *
147fi
148
149#another target without javascript
150cmake -DDISABLE_JS=ON ..
151make -j$(nproc)
152cp ecfuzzer $OUT/fuzz_ec_noblocker
153