Add FIPS selftests for ECDH algorithm.
[openssl.git] / fips / fipsld
1 #!/bin/sh -e
2 #
3 # Copyright (c) 2005-2007 The OpenSSL Project.
4 #
5 # Depending on output file name, the script either embeds fingerprint
6 # into libcrypto.so or static application. "Static" refers to static
7 # libcrypto.a, not [necessarily] application per se.
8 #
9 # Even though this script is called fipsld, it expects C compiler
10 # command line syntax and $FIPSLD_CC or $CC environment variable set
11 # and can even be used to compile source files.
12
13 #set -x
14
15 CC=${FIPSLD_CC:-${CC}}
16 [ -n "${CC}" ] || { echo '$CC is not defined'; exit 1; }
17
18 # Initially -c wasn't intended to be interpreted here, but it might
19 # make life easier for those who want to build FIPS-ified applications
20 # with minimal [if any] modifications to their Makefiles...
21 (   while [ "x$1" != "x" -a "x$1" != "x-c" -a "x$1" != "x-E" ]; do shift; done;
22     [ $# -ge 1 ]
23 ) && exec ${CC} "$@"
24
25 TARGET=`(while [ "x$1" != "x" -a "x$1" != "x-o" ]; do shift; done; echo $2)`
26
27 # If using an auto-tooled (autoconf/automake/libtool) project,
28 # configure will fail when testing the compiler or even performing
29 # simple checks. Pass-through to compiler directly if application is
30 # is not being linked with libcrypto, allowing auto-tooled applications
31 # to utilize fipsld (e.g. CC=/usr/local/ssl/bin/fipsld FIPSLD_CC=gcc
32 # ./configure && make). But keep in mind[!] that if certified code
33 # resides in a shared library, then fipsld *may not* be used and
34 # end-developer should not modify application configuration and build
35 # procedures. This is because in-core fingerprint and associated
36 # procedures are already embedded into and executed in shared library
37 # context.
38 case `basename "${TARGET}"` in
39 libcrypto*|libfips*|*.dll)              ;;
40 *)      case "$*" in
41         *libcrypto.a*|*-lcrypto*|*fipscanister.o*)      ;;
42         *)      exec ${CC} "$@"         ;;
43         esac
44 esac
45
46 [ -n "${TARGET}" ] || { echo 'no -o specified'; exit 1; }
47
48 # Turn on debugging output?
49 (   while [ "x$1" != "x" -a "x$1" != "x-DDEBUG_FINGERPRINT_PREMAIN" ]; do shift; done;
50     [ $# -ge 1 ]
51 ) && set -x
52
53 THERE="`echo $0 | sed -e 's|[^/]*$||'`"..
54
55 # fipscanister.o can appear in command line
56 CANISTER_O=`(while [ "x$1" != "x" ]; do case "$1" in *fipscanister.o) echo $1; exit;; esac; shift; done)`
57 if [ -z "${CANISTER_O}" ]; then
58         # If set, FIPSLIBDIR is location of installed validated FIPS module
59         if [ -n "${FIPSLIBDIR}" ]; then
60                 CANISTER_O="${FIPSLIBDIR}/fipscanister.o"
61         elif [ -f "${THERE}/fips/fipscanister.o" ]; then
62                 CANISTER_O="${THERE}/fips/fipscanister.o"
63         elif [ -f "${THERE}/lib/fipscanister.o" ]; then
64                 CANISTER_O="${THERE}/lib/fipscanister.o"
65         fi
66         CANISTER_O_CMD="${CANISTER_O}"
67 fi
68 [ -f ${CANISTER_O} ] || { echo "unable to find ${CANISTER_O}"; exit 1; }
69
70 PREMAIN_C=`dirname "${CANISTER_O}"`/fips_premain.c
71
72 HMAC_KEY="etaonrishdlcupfm"
73
74 case "`(uname -s) 2>/dev/null`" in
75 OSF1|IRIX*)     _WL_PREMAIN="-Wl,-init,FINGERPRINT_premain"     ;;
76 HP-UX)          _WL_PREMAIN="-Wl,+init,FINGERPRINT_premain"     ;;
77 AIX)            _WL_PREMAIN="-Wl,-binitfini:FINGERPRINT_premain,-bnoobjreorder";;
78 Darwin)         (   while [ "x$1" != "x" -a "x$1" != "x-dynamiclib" ]; do shift; done;
79                     [ $# -ge 1 ]
80                 ) && _WL_PREMAIN="-Wl,-init,_FINGERPRINT_premain" ;;
81 esac
82
83 case "${TARGET}" in
84 [!/]*)  TARGET=./${TARGET} ;;
85 esac
86
87 case `basename "${TARGET}"` in
88 lib*|*.dll)     # must be linking a shared lib...
89         # Shared lib creation can be taking place in the source
90         # directory only, but fipscanister.o can reside elsewhere...
91
92         if [ -x "${THERE}/fips/fips_standalone_sha1" ]; then
93                 FINGERTYPE="${THERE}/fips/fips_standalone_sha1"
94                 PREMAIN_DSO="${THERE}/fips/fips_premain_dso"
95         elif [ -x "${THERE}/bin/fips_standalone_sha1" ]; then
96                 FINGERTYPE="${THERE}/bin/fips_standalone_sha1"
97                 PREMAIN_DSO="./fips_premain_dso"
98         fi
99
100         # verify fipspremain.c against its detached signature...
101         ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
102                 diff -w "${PREMAIN_C}.sha1" - || \
103         { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
104         # verify fipscanister.o against its detached signature...
105         ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
106                 diff -w "${CANISTER_O}.sha1" - || \
107         { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
108
109         [ -z "${FIPSLD_LIBCRYPTO}" -a -f "${THERE}/libcrypto.a" ] && \
110                 FIPSLD_LIBCRYPTO="${THERE}/libcrypto.a"
111                 
112
113         # Temporarily remove fipscanister.o from libcrypto.a!
114         # We are required to use the standalone copy...
115         if [ -n  "${FIPSLD_LIBCRYPTO}" ]; then
116             if ar d "${FIPSLD_LIBCRYPTO}" fipscanister.o; then
117                 (ranlib "${FIPSLD_LIBCRYPTO}") 2>/dev/null || :
118                 trap    'ar r "${FIPSLD_LIBCRYPTO}" "${CANISTER_O}";
119                          (ranlib "${FIPSLD_LIBCRYPTO}") 2>/dev/null || :;
120                          sleep 1;
121                          touch -c "${TARGET}"' 0
122             fi
123         fi
124
125         /bin/rm -f "${TARGET}"
126         ${CC}   ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
127                 "${PREMAIN_C}" \
128                 ${_WL_PREMAIN} "$@"
129
130         # generate signature...
131         if [ -z "${FIPS_SIG}" ]; then
132                 SIG=`"${PREMAIN_DSO}" "${TARGET}"`
133         else
134                 SIG=`"${FIPS_SIG}" -dso "${TARGET}"`
135         fi
136         /bin/rm -f "${TARGET}"
137         if [ -z "${SIG}" ]; then
138            echo "unable to collect signature"; exit 1
139         fi
140
141         # recompile with signature...
142         ${CC}   ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
143                 -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \
144                 ${_WL_PREMAIN} "$@"
145         ;;
146
147 *)      # must be linking statically...
148         # Static linking can be taking place either in the source
149         # directory or off the installed binary target destination.
150         if [ -x "${THERE}/fips/fips_standalone_sha1" ]; then
151                 FINGERTYPE="${THERE}/fips/fips_standalone_sha1"
152         elif [ -x "${THERE}/bin/fips_standalone_sha1" ]; then
153                 FINGERTYPE="${THERE}/bin/fips_standalone_sha1"
154         else    # Installed tree is expected to contain
155                 # lib/fipscanister.o, lib/fipscanister.o.sha1 and
156                 # lib/fips_premain.c [not to mention bin/openssl].
157                 FINGERTYPE="${THERE}/bin/openssl sha1 -hmac ${HMAC_KEY}"
158         fi
159
160         # verify fipscanister.o against its detached signature...
161         ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
162                 diff -w "${CANISTER_O}.sha1" - || \
163         { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
164
165         # verify fips_premain.c against its detached signature...
166         ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
167                 diff -w "${PREMAIN_C}.sha1" - || \
168         { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
169
170         /bin/rm -f "${TARGET}"
171         ${CC}   ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
172                 "${PREMAIN_C}" \
173                 ${_WL_PREMAIN} "$@"
174
175         # generate signature...
176         if [ -z "${FIPS_SIG}" ]; then
177                 SIG=`"${TARGET}"`
178         else
179                 SIG=`"${FIPS_SIG}" -exe "${TARGET}"`
180         fi
181         /bin/rm -f "${TARGET}"
182         if [ -z "${SIG}" ]; then
183            echo "unable to collect signature"; exit 1
184         fi
185
186         # recompile with signature...
187         ${CC}   ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
188                 -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \
189                 ${_WL_PREMAIN} "$@"
190         ;;
191 esac