update FIPS status
[openssl-web.git] / docs / fips / incore2
1 #!/bin/sh
2 #
3 # calculate in-core fingerprint via looking at the object file rather than
4 # running a program on the target
5 #
6
7 DEBUG=
8
9 OBJCOPY=${CROSS_COMPILE}objcopy
10 OBJDUMP=${CROSS_COMPILE}objdump
11
12 #OBJCOPY=objcopy
13 #OBJDUMP=objdump
14
15 HMAC_KEY="etaonrishdlcupfm"
16 FINGERTYPE="openssl sha1 -hmac ${HMAC_KEY}"
17
18 # FINGERTYPE can be made via openssl or fips_standalone_sha1 (output word 2)
19
20 # allow for a simple -d option
21 if [ "$1" = "-d" ]; then
22   DEBUG=1
23   shift
24 fi
25
26 if [ "$1" != "-exe" -a "$1" != "-dso" ]; then
27   echo "usage: incore [-exe|-dso] executable" >&2
28   exit 1
29 fi
30
31 APP="$2"
32
33 if [ -z "$APP" ]; then
34   echo "usage: incore [-exe|-dso] executable" >&2
35   exit 1
36 fi
37
38 if [ ! -f "$APP" ]; then 
39   echo "incore: $APP not found" >&2
40   exit 1
41 fi
42
43 #TARGET=elf64-x86-64
44 TARGET=`$OBJDUMP -f $APP | grep 'file format' | awk '{print $4}'`
45
46 if [ ! -z "$DEBUG" ]; then
47   echo "TARGET: $TARGET" >&2
48 fi
49
50 # INCORE_ADJUST is the fixup allowance for FIPS_ref_point() handling in 
51 # fips/fips_canister.c which is used rather than the actual
52 # function address
53
54 if [ -z "$INCORE_ADJUST" ]; then
55
56   INCORE_ADJUST=4
57   case $TARGET in
58     elf64-x86-64) INCORE_ADJUST=4;;
59     #elf32-littlearm|elf32-little|elf32-bigarm) INCORE_ADJUST="-36";;
60     elf32-littlearm|elf32-little|elf32-bigarm) INCORE_ADJUST="0";;
61   esac
62
63 fi
64
65 #$OBJCOPY  -j .rodata -v -O binary $APP $APP-rodata  | grep -v '^copy from'
66 #$OBJCOPY  -j .text -v -O binary $APP $APP-text | grep -v '^copy from'
67 #$OBJCOPY  -j .rodata -v -F $TARGET $APP $APP-rodata  | grep -v '^copy from'
68 #$OBJCOPY  -j .text -v -F $TARGET $APP $APP-text | grep -v '^copy from'
69
70 #
71 # locate all the required symbols
72 #
73 eval `$OBJDUMP -t $APP | egrep 'FIPS_text_start|FIPS_text_end|FIPS_rodata_end|FIPS_rodata_start|FIPS_signature|FINGERPRINT_ascii_value' | awk '{printf("%s=%s\n",$6,toupper($1))}' | sed -e 's/\./_/g'`
74
75 #
76 # locate the offsets and length of the interesting sections
77 #
78 eval `$OBJDUMP -h $APP | egrep '.text|.rodata|.bss' | awk '{printf("%s=%s\n%s_OFF=%s\n",$2,toupper($4),$2,toupper($6))}' | sed -e 's/^\./DOT/' -e 's/\./_/g'`
79
80 #
81 # should now have the following variables set which can be used to 
82 # extract the right parts from the -rodata and -text files
83 #
84 # e.g.
85 # FIPS_rodata_end=0000000000436160
86 # FIPS_rodata_start=0000000000430B00
87 # FIPS_signature=000000000063EBE0
88 # FIPS_text_end=00000000004304E0
89 # FIPS_text_start=0000000000401780
90 # DOTrodata=0000000000430AE0
91 # DOTrodata_OFF=00030AE0
92 # DOTtext=0000000000401690
93 # DOTtext_OFF=00001690
94
95 #
96 # show the values - debug
97 #
98 if [ ! -z "$DEBUG" ]; then
99   (
100   echo "FIPS_rodata_end=$FIPS_rodata_end"
101   echo "FIPS_rodata_start=$FIPS_rodata_start"
102   echo "FIPS_signature=$FIPS_signature"
103   echo "FIPS_text_end=$FIPS_text_end"
104   echo "FIPS_text_start=$FIPS_text_start"
105   echo "FINGERPRINT_ascii_value=$FINGERPRINT_ascii_value"
106   echo "DOTrodata=$DOTrodata"
107   echo "DOTrodata_OFF=$DOTrodata_OFF"
108   echo "DOTtext=$DOTtext"
109   echo "DOTtext_OFF=$DOTtext_OFF"
110   ) >&2 
111 fi
112
113 if [ -z "$FIPS_rodata_start" ]; then
114   echo "$APP: Not a FIPS executable" >&2 
115   exit 1
116 fi
117 if [ -z "$FIPS_rodata_end" ]; then
118   echo "$APP: Not a FIPS executable" >&2 
119   exit 1
120 fi
121 if [ -z "$FIPS_text_start" ]; then
122   echo "$APP: Not a FIPS executable" >&2 
123   exit 1
124 fi
125 if [ -z "$FIPS_text_end" ]; then
126   echo "$APP: Not a FIPS executable" >&2 
127   exit 1
128 fi
129
130
131 # use 'bc' to calculate offsets and lengths for RODATA
132
133 RSTART=`cat <<EOF | bc
134 obase=10
135 ibase=16 
136 $FIPS_rodata_start-$DOTrodata
137 EOF`
138 RLEN=`cat <<EOF | bc
139 obase=10
140 ibase=16 
141 $FIPS_rodata_end-$FIPS_rodata_start
142 EOF`
143 ROFF=`cat <<EOF | bc
144 obase=10
145 ibase=16 
146 $DOTrodata_OFF
147 EOF`
148 ROFF=`expr $ROFF + $RSTART`
149
150
151 # use 'bc' to calculate offsets and lengths for TEXT
152
153 TSTART=`cat <<EOF | bc
154 obase=10
155 ibase=16 
156 $FIPS_text_start-$DOTtext
157 EOF`
158 TLEN=`cat <<EOF | bc
159 obase=10
160 ibase=16 
161 $FIPS_text_end-$FIPS_text_start
162 EOF`
163 TOFF=`cat <<EOF | bc
164 obase=10
165 ibase=16 
166 $DOTtext_OFF
167 EOF`
168 TOFF=`expr $TOFF + $TSTART + $INCORE_ADJUST`
169
170
171 # use 'bc' to calculate where to locate FINGERPRINT_ascii_value
172
173 FSTART=`cat <<EOF | bc
174 obase=10
175 ibase=16 
176 $FINGERPRINT_ascii_value-$DOTrodata
177 EOF`
178 # 20 bytes as ASCII HEX
179 FLEN=40
180 FOFF=`cat <<EOF | bc
181 obase=10
182 ibase=16 
183 $DOTrodata_OFF
184 EOF`
185 FOFF=`expr $FOFF + $FSTART`
186
187 #
188 # NOTE: this code does not check for FIPS_signature being inside the 
189 #       rodata segment and exclude it from the calculation which is what
190 #       the actual runtime code does as we do not update it; the 
191 #       FIPS_signature should be in BSS - but in either case our calculation 
192 #       is correct as the signature comes from FINGERPRINT_ascii_value
193 #       when FIPS_signature is actually blank (zero)
194 #
195
196 #
197 # dump useful values
198
199 if [ ! -z "$DEBUG" ]; then
200   (
201   echo "TSTART $TSTART"
202   echo "TLEN $TLEN"
203   echo "TOFF $TOFF"
204   echo "INCORE_ADJUST $INCORE_ADJUST"
205
206   echo "RSTART $RSTART"
207   echo "RLEN $RLEN"
208   echo "ROFF $ROFF"
209
210   echo "FSTART $FSTART"
211   echo "FLEN $FLEN"
212   echo "FOFF $FOFF"
213   ) >&2
214 fi
215
216 # some debug code when looking at the values
217 if [ ! -z "$INCORE_DEBUG" ]; then
218   dd if=$APP of=mac1 bs=1 skip=$TOFF count=$TLEN
219   dd if=$APP of=mac2 bs=1 skip=$ROFF count=$RLEN
220   cat mac1 mac2 > mac
221   $FINGERTYPE mac
222 fi
223
224 #
225 # show the actual value of FINGERPRINT_ascii_value as placed in 
226 # the program by fipsld
227 #
228 if [ ! -z "$DEBUG" ]; then
229   (
230   echo "embedded: "
231   dd if=$APP bs=1 skip=$FOFF count=$FLEN  2>/dev/null
232   echo 
233   ) >&2
234 fi
235
236 #
237 # now calculate what that value should be from the appropriate sections
238 # of the object file
239 #
240 if [ ! -z "$DEBUG" ]; then
241   echo "calculated: " >&2
242 fi
243 ( dd if=$APP bs=1 skip=$TOFF count=$TLEN && \
244   dd if=$APP bs=1 skip=$ROFF count=$RLEN ) 2>/dev/null | $FINGERTYPE
245
246 exit $?
247
248