Makefile 14 KB

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