mbedtls_common.mk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #
  2. # Copyright (c) 2015-2024, Arm Limited. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. ifneq (${MBEDTLS_COMMON_MK},1)
  7. MBEDTLS_COMMON_MK := 1
  8. # MBEDTLS_DIR must be set to the mbed TLS main directory (it must contain
  9. # the 'include' and 'library' subdirectories).
  10. ifeq (${MBEDTLS_DIR},)
  11. $(error Error: MBEDTLS_DIR not set)
  12. endif
  13. MBEDTLS_INC = -I${MBEDTLS_DIR}/include
  14. MBEDTLS_MAJOR=$(shell grep -hP "define MBEDTLS_VERSION_MAJOR" ${MBEDTLS_DIR}/include/mbedtls/*.h | grep -oe '\([0-9.]*\)')
  15. MBEDTLS_MINOR=$(shell grep -hP "define MBEDTLS_VERSION_MINOR" ${MBEDTLS_DIR}/include/mbedtls/*.h | grep -oe '\([0-9.]*\)')
  16. $(info MBEDTLS_VERSION_MAJOR is [${MBEDTLS_MAJOR}] MBEDTLS_VERSION_MINOR is [${MBEDTLS_MINOR}])
  17. ifneq (${MBEDTLS_MAJOR}, 3)
  18. $(error Error: TF-A only supports MbedTLS versions > 3.x)
  19. endif
  20. # Specify mbed TLS configuration file
  21. ifeq (${PSA_CRYPTO},1)
  22. MBEDTLS_CONFIG_FILE ?= "<drivers/auth/mbedtls/psa_mbedtls_config.h>"
  23. else
  24. MBEDTLS_CONFIG_FILE ?= "<drivers/auth/mbedtls/mbedtls_config-3.h>"
  25. endif
  26. $(eval $(call add_define,MBEDTLS_CONFIG_FILE))
  27. MBEDTLS_SOURCES += drivers/auth/mbedtls/mbedtls_common.c
  28. LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \
  29. aes.c \
  30. asn1parse.c \
  31. asn1write.c \
  32. cipher.c \
  33. cipher_wrap.c \
  34. constant_time.c \
  35. memory_buffer_alloc.c \
  36. oid.c \
  37. platform.c \
  38. platform_util.c \
  39. bignum.c \
  40. bignum_core.c \
  41. gcm.c \
  42. md.c \
  43. pk.c \
  44. pk_ecc.c \
  45. pk_wrap.c \
  46. pkparse.c \
  47. pkwrite.c \
  48. sha256.c \
  49. sha512.c \
  50. ecdsa.c \
  51. ecp_curves.c \
  52. ecp.c \
  53. rsa.c \
  54. rsa_alt_helpers.c \
  55. x509.c \
  56. x509_crt.c \
  57. )
  58. ifeq (${PSA_CRYPTO},1)
  59. LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \
  60. psa_crypto.c \
  61. psa_crypto_client.c \
  62. psa_crypto_hash.c \
  63. psa_crypto_rsa.c \
  64. psa_crypto_ecp.c \
  65. psa_crypto_slot_management.c \
  66. psa_crypto_aead.c \
  67. psa_crypto_cipher.c \
  68. psa_util.c \
  69. )
  70. endif
  71. # The platform may define the variable 'TF_MBEDTLS_KEY_ALG' to select the key
  72. # algorithm to use. If the variable is not defined, select it based on
  73. # algorithm used for key generation `KEY_ALG`. If `KEY_ALG` is not defined,
  74. # then it is set to `rsa`.
  75. ifeq (${TF_MBEDTLS_KEY_ALG},)
  76. ifeq (${KEY_ALG}, ecdsa)
  77. TF_MBEDTLS_KEY_ALG := ecdsa
  78. else
  79. TF_MBEDTLS_KEY_ALG := rsa
  80. endif
  81. endif
  82. ifeq (${TF_MBEDTLS_KEY_SIZE},)
  83. ifneq ($(findstring rsa,${TF_MBEDTLS_KEY_ALG}),)
  84. ifeq (${KEY_SIZE},)
  85. TF_MBEDTLS_KEY_SIZE := 2048
  86. else ifneq ($(filter $(KEY_SIZE), 1024 2048 3072 4096),)
  87. TF_MBEDTLS_KEY_SIZE := ${KEY_SIZE}
  88. else
  89. $(error "Invalid value for KEY_SIZE: ${KEY_SIZE}")
  90. endif
  91. else ifneq ($(findstring ecdsa,${TF_MBEDTLS_KEY_ALG}),)
  92. ifeq (${KEY_SIZE},)
  93. TF_MBEDTLS_KEY_SIZE := 256
  94. else ifneq ($(filter $(KEY_SIZE), 256 384),)
  95. TF_MBEDTLS_KEY_SIZE := ${KEY_SIZE}
  96. else
  97. $(error "Invalid value for KEY_SIZE: ${KEY_SIZE}")
  98. endif
  99. endif
  100. endif
  101. ifeq (${HASH_ALG}, sha384)
  102. TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA384
  103. else ifeq (${HASH_ALG}, sha512)
  104. TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA512
  105. else
  106. TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA256
  107. endif
  108. ifeq (${MBOOT_EL_HASH_ALG}, sha256)
  109. $(eval $(call add_define,TF_MBEDTLS_MBOOT_USE_SHA256))
  110. else ifeq (${MBOOT_EL_HASH_ALG}, sha384)
  111. $(eval $(call add_define,TF_MBEDTLS_MBOOT_USE_SHA384))
  112. else ifeq (${MBOOT_EL_HASH_ALG}, sha512)
  113. $(eval $(call add_define,TF_MBEDTLS_MBOOT_USE_SHA512))
  114. endif
  115. ifeq (${TF_MBEDTLS_KEY_ALG},ecdsa)
  116. TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_ECDSA
  117. else ifeq (${TF_MBEDTLS_KEY_ALG},rsa)
  118. TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_RSA
  119. else ifeq (${TF_MBEDTLS_KEY_ALG},rsa+ecdsa)
  120. TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_RSA_AND_ECDSA
  121. else
  122. $(error "TF_MBEDTLS_KEY_ALG=${TF_MBEDTLS_KEY_ALG} not supported on mbed TLS")
  123. endif
  124. ifeq (${DECRYPTION_SUPPORT}, aes_gcm)
  125. TF_MBEDTLS_USE_AES_GCM := 1
  126. else
  127. TF_MBEDTLS_USE_AES_GCM := 0
  128. endif
  129. # Needs to be set to drive mbed TLS configuration correctly
  130. $(eval $(call add_defines,\
  131. $(sort \
  132. TF_MBEDTLS_KEY_ALG_ID \
  133. TF_MBEDTLS_KEY_SIZE \
  134. TF_MBEDTLS_HASH_ALG_ID \
  135. TF_MBEDTLS_USE_AES_GCM \
  136. )))
  137. $(eval $(call MAKE_LIB,mbedtls))
  138. endif