1#####
2# Local unit test Makefile
3#
4# This makefile builds and runs the keymaster unit tests locally on the development
5# machine, not on an Android device.  Android.mk builds the same tests into the
6# "keymaster_tests" binary for execution on-device, but this Makefile runs them locally,
7# for a very fast edit/build/test development cycle.
8#
9# To build and run these tests, one pre-requisite must be manually installed: BoringSSL.
10# This Makefile expects to find BoringSSL in a directory adjacent to $ANDROID_BUILD_TOP.
11# To get and build it, first install the Ninja build tool (e.g. apt-get install
12# ninja-build), then do:
13#
14# cd $ANDROID_BUILD_TOP/..
15# git clone https://boringssl.googlesource.com/boringssl
16# cd boringssl
17# mdkir build
18# cd build
19# cmake -GNinja ..
20# ninja
21#
22# Then return to $ANDROID_BUILD_TOP/system/keymaster and run "make".
23#####
24
25BASE=../..
26SUBS=system/core \
27	hardware/libhardware \
28	external/gtest \
29	system/security/softkeymaster \
30	system/security/keystore
31GTEST=$(BASE)/external/googletest/googletest
32
33INCLUDES=$(foreach dir,$(SUBS),-I $(BASE)/$(dir)/include) \
34	-I $(BASE)/libnativehelper/include/nativehelper \
35	-I $(GTEST)/include -isystem $(GTEST) -Iinclude -I$(BASE)/../boringssl/include
36
37ifdef FORCE_32_BIT
38ARCH_FLAGS = -m32
39endif
40
41ifdef USE_GCC
42CXXFLAGS +=-std=c++14 -fprofile-arcs -ftest-coverage
43CFLAGS += -fprofile-arcs -ftest-coverage
44else
45CC=$(BASE)/prebuilts/clang/host/linux-x86/clang-r370808/bin/clang
46CXX=$(BASE)/prebuilts/clang/host/linux-x86/clang-r370808/bin/clang++
47CXXFLAGS +=-std=c++14 -DKEYMASTER_CLANG_TEST_BUILD
48CFLAGS += -DKEYMASTER_CLANG_TEST_BUILD
49endif
50
51LDFLAGS += $(ARCH_FLAGS)
52CPPFLAGS = $(INCLUDES) -g -O0 -MD -MP $(ARCH_FLAGS) -DKEYMASTER_UNIT_TEST_BUILD -DHOST_BUILD
53CXXFLAGS += -Wall -Werror -Wno-unused -Winit-self -Wpointer-arith -Wunused-parameter \
54	-Werror=sign-compare -Werror=return-type -fno-permissive \
55	-Wno-deprecated-declarations -fno-exceptions -DKEYMASTER_NAME_TAGS $(ARCH_FLAGS)
56CFLAGS += $(ARCH_FLAGS) -DKEYMASTER_UNIT_TEST_BUILD -DHOST_BUILD
57
58# Uncomment to enable debug logging.
59# CXXFLAGS += -DDEBUG
60
61LDLIBS=-L$(BASE)/../boringssl/build/crypto -lcrypto -lpthread -lstdc++ -lgcov
62
63CPPSRCS=\
64	km_openssl/aes_key.cpp \
65	km_openssl/aes_operation.cpp \
66	km_openssl/triple_des_key.cpp \
67	km_openssl/triple_des_operation.cpp \
68	android_keymaster/android_keymaster.cpp \
69	android_keymaster/android_keymaster_messages.cpp \
70	tests/android_keymaster_messages_test.cpp \
71	tests/android_keymaster_test.cpp \
72	tests/android_keymaster_test_utils.cpp \
73	android_keymaster/android_keymaster_utils.cpp \
74	km_openssl/asymmetric_key.cpp \
75	km_openssl/asymmetric_key_factory.cpp \
76	km_openssl/attestation_record.cpp \
77	km_openssl/block_cipher_operation.cpp \
78	tests/attestation_record_test.cpp \
79	key_blob_utils/auth_encrypted_key_blob.cpp \
80	android_keymaster/authorization_set.cpp \
81	tests/authorization_set_test.cpp \
82	km_openssl/ec_key.cpp \
83	km_openssl/ec_key_factory.cpp \
84	legacy_support/ec_keymaster0_key.cpp \
85	legacy_support/ec_keymaster1_key.cpp \
86	legacy_support/ecdsa_keymaster1_operation.cpp \
87	km_openssl/ecdsa_operation.cpp \
88	km_openssl/ecies_kem.cpp \
89	tests/ecies_kem_test.cpp \
90	tests/gtest_main.cpp \
91	km_openssl/ckdf.cpp \
92	tests/hkdf_test.cpp \
93	km_openssl/hkdf.cpp \
94	tests/hkdf_test.cpp \
95	km_openssl/hmac.cpp \
96	km_openssl/hmac_key.cpp \
97	km_openssl/hmac_operation.cpp \
98	tests/hmac_test.cpp \
99	key_blob_utils/integrity_assured_key_blob.cpp \
100	km_openssl/iso18033kdf.cpp \
101	km_openssl/kdf.cpp \
102	tests/kdf1_test.cpp \
103	tests/kdf2_test.cpp \
104	tests/kdf_test.cpp \
105	tests/key_blob_test.cpp \
106	legacy_support/keymaster0_engine.cpp \
107	legacy_support/keymaster1_engine.cpp \
108	android_keymaster/keymaster_configuration.cpp \
109	tests/keymaster_configuration_test.cpp \
110	android_keymaster/keymaster_enforcement.cpp \
111	km_openssl/soft_keymaster_enforcement.cpp \
112	tests/keymaster_enforcement_test.cpp \
113	android_keymaster/keymaster_tags.cpp \
114	android_keymaster/logger.cpp \
115	km_openssl/nist_curve_key_exchange.cpp \
116	tests/nist_curve_key_exchange_test.cpp \
117	key_blob_utils/ocb_utils.cpp \
118	km_openssl/openssl_err.cpp \
119	km_openssl/openssl_utils.cpp \
120	android_keymaster/operation.cpp \
121	android_keymaster/operation_table.cpp \
122	km_openssl/rsa_key.cpp \
123	km_openssl/rsa_key_factory.cpp \
124	legacy_support/rsa_keymaster0_key.cpp \
125	legacy_support/rsa_keymaster1_key.cpp \
126	legacy_support/rsa_keymaster1_operation.cpp \
127	km_openssl/rsa_operation.cpp \
128	android_keymaster/serializable.cpp \
129	contexts/soft_keymaster_context.cpp \
130	contexts/soft_keymaster_device.cpp \
131	contexts/pure_soft_keymaster_context.cpp \
132	km_openssl/symmetric_key.cpp \
133	km_openssl/software_random_source.cpp \
134	contexts/soft_attestation_cert.cpp \
135	km_openssl/attestation_utils.cpp \
136	key_blob_utils/software_keyblobs.cpp \
137	km_openssl/wrapped_key.cpp
138
139CCSRCS=$(GTEST)/src/gtest-all.cc
140CSRCS=key_blob_utils/ocb.c
141
142OBJS=$(CPPSRCS:.cpp=.o) $(CCSRCS:.cc=.o) $(CSRCS:.c=.o)
143DEPS=$(CPPSRCS:.cpp=.d) $(CCSRCS:.cc=.d) $(CSRCS:.c=.d)
144
145BINARIES = \
146	tests/android_keymaster_messages_test \
147	tests/android_keymaster_test \
148	tests/attestation_record_test \
149	tests/authorization_set_test \
150	tests/ecies_kem_test \
151	tests/ckdf_test \
152	tests/hkdf_test \
153	tests/hmac_test \
154	tests/kdf1_test \
155	tests/kdf2_test \
156	tests/kdf_test \
157	tests/key_blob_test \
158	tests/keymaster_configuration_test \
159	tests/keymaster_enforcement_test \
160	tests/nist_curve_key_exchange_test
161
162.PHONY: coverage memcheck massif clean run
163
164%.run: %
165	./$<
166	touch $@
167
168run: $(BINARIES:=.run)
169
170coverage: coverage.info
171	genhtml coverage.info --output-directory coverage
172
173coverage.info: run
174	lcov --capture --directory=. --output-file coverage.info
175
176%.coverage : %
177	$(MAKE) clean && $(MAKE) $<
178	./$<
179	lcov --capture --directory=. --output-file coverage.info
180	genhtml coverage.info --output-directory coverage
181
182#UNINIT_OPTS=--track-origins=yes
183UNINIT_OPTS=--undef-value-errors=no
184
185MEMCHECK_OPTS=--leak-check=full \
186	--show-reachable=yes \
187	--vgdb=full \
188	$(UNINIT_OPTS) \
189	--error-exitcode=1 \
190	--suppressions=valgrind.supp \
191	--gen-suppressions=all
192
193MASSIF_OPTS=--tool=massif \
194	--stacks=yes
195
196%.memcheck : %
197	valgrind $(MEMCHECK_OPTS) ./$< && \
198	touch $@
199
200%.massif : %
201	valgrind $(MASSIF_OPTS) --massif-out-file=$@ ./$<
202
203memcheck: $(BINARIES:=.memcheck)
204
205massif: $(BINARIES:=.massif)
206
207GTEST_OBJS = $(GTEST)/src/gtest-all.o tests/gtest_main.o
208
209tests/keymaster_configuration_test: tests/keymaster_configuration_test.o \
210	android_keymaster/authorization_set.o \
211	android_keymaster/serializable.o \
212	android_keymaster/logger.o \
213	android_keymaster/keymaster_configuration.o \
214	$(GTEST_OBJS)
215
216tests/hmac_test: tests/hmac_test.o \
217	tests/android_keymaster_test_utils.o \
218	android_keymaster/android_keymaster_utils.o \
219	android_keymaster/authorization_set.o \
220	km_openssl/hmac.o \
221	android_keymaster/keymaster_tags.o \
222	android_keymaster/logger.o \
223	android_keymaster/serializable.o \
224	$(GTEST_OBJS)
225
226tests/ckdf_test: tests/ckdf_test.o \
227	tests/android_keymaster_test_utils.o \
228	android_keymaster/android_keymaster_utils.o \
229	android_keymaster/authorization_set.o \
230	android_keymaster/keymaster_tags.o \
231	android_keymaster/logger.o \
232	android_keymaster/serializable.o \
233	km_openssl/ckdf.o \
234	km_openssl/openssl_err.o \
235	$(GTEST_OBJS)
236
237tests/hkdf_test: tests/hkdf_test.o \
238	tests/android_keymaster_test_utils.o \
239	android_keymaster/android_keymaster_utils.o \
240	android_keymaster/authorization_set.o \
241	km_openssl/hkdf.o \
242	km_openssl/hmac.o \
243	km_openssl/kdf.o \
244	android_keymaster/keymaster_tags.o \
245	android_keymaster/logger.o \
246	android_keymaster/serializable.o \
247	$(GTEST_OBJS)
248
249tests/kdf_test: tests/kdf_test.o \
250	android_keymaster/android_keymaster_utils.o \
251	km_openssl/kdf.o \
252	android_keymaster/logger.o \
253	android_keymaster/serializable.o \
254	$(GTEST_OBJS)
255
256tests/kdf1_test: tests/kdf1_test.o \
257	tests/android_keymaster_test_utils.o \
258	android_keymaster/android_keymaster_utils.o \
259	android_keymaster/authorization_set.o \
260	km_openssl/iso18033kdf.o \
261	km_openssl/kdf.o \
262	android_keymaster/keymaster_tags.o \
263	android_keymaster/logger.o \
264	android_keymaster/serializable.o \
265	$(GTEST_OBJS)
266
267tests/kdf2_test: tests/kdf2_test.o \
268	tests/android_keymaster_test_utils.o \
269	android_keymaster/android_keymaster_utils.o \
270	android_keymaster/authorization_set.o \
271	km_openssl/iso18033kdf.o \
272	km_openssl/kdf.o \
273	android_keymaster/keymaster_tags.o \
274	android_keymaster/logger.o \
275	android_keymaster/serializable.o \
276	$(GTEST_OBJS)
277
278tests/nist_curve_key_exchange_test: tests/nist_curve_key_exchange_test.o \
279	tests/android_keymaster_test_utils.o \
280	android_keymaster/authorization_set.o \
281	android_keymaster/keymaster_tags.o \
282	android_keymaster/logger.o \
283	km_openssl/nist_curve_key_exchange.o \
284	km_openssl/openssl_err.o \
285	km_openssl/openssl_utils.o \
286	android_keymaster/serializable.o \
287	$(GTEST_OBJS)
288
289tests/ecies_kem_test: tests/ecies_kem_test.o \
290	android_keymaster/android_keymaster_utils.o \
291	tests/android_keymaster_test_utils.o \
292	android_keymaster/authorization_set.o \
293	km_openssl/ecies_kem.o \
294	km_openssl/hkdf.o \
295	km_openssl/hmac.o \
296	km_openssl/kdf.o \
297	android_keymaster/keymaster_tags.o \
298	android_keymaster/logger.o \
299	km_openssl/nist_curve_key_exchange.o \
300	km_openssl/openssl_err.o \
301	km_openssl/openssl_utils.o \
302	android_keymaster/serializable.o \
303	$(GTEST_OBJS)
304
305tests/authorization_set_test: tests/authorization_set_test.o \
306	tests/android_keymaster_test_utils.o \
307	android_keymaster/authorization_set.o \
308	android_keymaster/keymaster_tags.o \
309	android_keymaster/logger.o \
310	android_keymaster/serializable.o \
311	$(GTEST_OBJS)
312
313tests/key_blob_test: tests/key_blob_test.o \
314	tests/android_keymaster_test_utils.o \
315	android_keymaster/android_keymaster_utils.o \
316	key_blob_utils/auth_encrypted_key_blob.o \
317	android_keymaster/authorization_set.o \
318	key_blob_utils/integrity_assured_key_blob.o \
319	android_keymaster/keymaster_tags.o \
320	android_keymaster/logger.o \
321	key_blob_utils/ocb.o \
322	key_blob_utils/ocb_utils.o \
323	km_openssl/openssl_err.o \
324	android_keymaster/serializable.o \
325	$(GTEST_OBJS)
326
327tests/android_keymaster_messages_test: tests/android_keymaster_messages_test.o \
328	android_keymaster/android_keymaster_messages.o \
329	tests/android_keymaster_test_utils.o \
330	android_keymaster/android_keymaster_utils.o \
331	android_keymaster/authorization_set.o \
332	android_keymaster/keymaster_tags.o \
333	android_keymaster/logger.o \
334	android_keymaster/serializable.o \
335	$(GTEST_OBJS)
336
337tests/android_keymaster_test: tests/android_keymaster_test.o \
338	android_keymaster/android_keymaster.o \
339	android_keymaster/android_keymaster_messages.o \
340	android_keymaster/android_keymaster_utils.o \
341	android_keymaster/authorization_set.o \
342	android_keymaster/keymaster_enforcement.o \
343	android_keymaster/keymaster_tags.o \
344	android_keymaster/logger.o \
345	android_keymaster/operation.o \
346	android_keymaster/operation_table.o \
347	android_keymaster/serializable.o \
348	contexts/pure_soft_keymaster_context.o \
349	contexts/soft_attestation_cert.o \
350	contexts/soft_keymaster_context.o \
351	contexts/soft_keymaster_device.o \
352	key_blob_utils/auth_encrypted_key_blob.o \
353	key_blob_utils/integrity_assured_key_blob.o \
354	key_blob_utils/ocb.o \
355	key_blob_utils/ocb_utils.o \
356	key_blob_utils/software_keyblobs.o \
357	km_openssl/aes_key.o \
358	km_openssl/aes_key.o \
359	km_openssl/aes_operation.o \
360	km_openssl/aes_operation.o \
361	km_openssl/asymmetric_key.o \
362	km_openssl/asymmetric_key_factory.o \
363	km_openssl/attestation_record.o \
364	km_openssl/attestation_utils.o \
365	km_openssl/block_cipher_operation.o \
366	km_openssl/ckdf.o \
367	km_openssl/ec_key.o \
368	km_openssl/ec_key_factory.o \
369	km_openssl/ecdsa_operation.o \
370	km_openssl/hmac_key.o \
371	km_openssl/hmac_operation.o \
372	km_openssl/openssl_err.o \
373	km_openssl/openssl_utils.o \
374	km_openssl/rsa_key.o \
375	km_openssl/rsa_key_factory.o \
376	km_openssl/rsa_operation.o \
377	km_openssl/soft_keymaster_enforcement.o \
378	km_openssl/software_random_source.o \
379	km_openssl/symmetric_key.o \
380	km_openssl/triple_des_key.o \
381	km_openssl/triple_des_operation.o \
382	km_openssl/wrapped_key.o \
383	legacy_support/ec_keymaster0_key.o \
384	legacy_support/ec_keymaster1_key.o \
385	legacy_support/ecdsa_keymaster1_operation.o \
386	legacy_support/keymaster0_engine.o \
387	legacy_support/keymaster1_engine.o \
388	legacy_support/rsa_keymaster0_key.o \
389	legacy_support/rsa_keymaster1_key.o \
390	legacy_support/rsa_keymaster1_operation.o \
391	tests/android_keymaster_test_utils.o \
392	$(BASE)/system/security/keystore/keyblob_utils.o \
393	$(GTEST_OBJS)
394
395tests/keymaster_enforcement_test: tests/keymaster_enforcement_test.o \
396	android_keymaster/android_keymaster_messages.o \
397	tests/android_keymaster_test_utils.o \
398	android_keymaster/android_keymaster_utils.o \
399	android_keymaster/authorization_set.o \
400	android_keymaster/keymaster_enforcement.o \
401	km_openssl/ckdf.o \
402	km_openssl/openssl_err.o \
403	km_openssl/soft_keymaster_enforcement.o \
404	android_keymaster/keymaster_tags.o \
405	android_keymaster/logger.o \
406	android_keymaster/serializable.o \
407	$(GTEST_OBJS)
408
409tests/attestation_record_test: tests/attestation_record_test.o \
410	tests/android_keymaster_test_utils.o \
411	android_keymaster/android_keymaster_utils.o \
412	km_openssl/attestation_record.o \
413	android_keymaster/authorization_set.o \
414	android_keymaster/keymaster_tags.o \
415	android_keymaster/logger.o \
416	km_openssl/openssl_err.o \
417	android_keymaster/serializable.o \
418	$(GTEST_OBJS)
419
420tests/wrapped_key_test: tests/wrapped_key_test.o \
421	tests/android_keymaster_test_utils.o \
422	android_keymaster/android_keymaster_utils.o \
423	km_openssl/attestation_record.o \
424	android_keymaster/authorization_set.o \
425	android_keymaster/keymaster_tags.o \
426	android_keymaster/logger.o \
427	km_openssl/openssl_err.o \
428	android_keymaster/serializable.o \
429	km_openssl/wrapped_key.o \
430	$(GTEST_OBJS)
431
432$(GTEST)/src/gtest-all.o: CXXFLAGS:=$(subst -Wmissing-declarations,,$(CXXFLAGS))
433
434clean:
435	rm -f $(OBJS) $(DEPS) $(BINARIES) \
436		$(BINARIES:=.run) $(BINARIES:=.memcheck) $(BINARIES:=.massif) \
437		*gcov *gcno *gcda coverage.info
438	rm -rf coverage
439
440-include $(CPPSRCS:.cpp=.d)
441-include $(CCSRCS:.cc=.d)
442