Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | # |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 2 | # Copyright (c) 2015-2023, Arm Limited. All rights reserved. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 3 | # |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | # SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | # |
| 6 | |
| 7 | ifneq (${MBEDTLS_COMMON_MK},1) |
| 8 | MBEDTLS_COMMON_MK := 1 |
| 9 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 10 | # MBEDTLS_DIR must be set to the mbed TLS main directory (it must contain |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 11 | # the 'include' and 'library' subdirectories). |
| 12 | ifeq (${MBEDTLS_DIR},) |
| 13 | $(error Error: MBEDTLS_DIR not set) |
| 14 | endif |
| 15 | |
Roberto Vargas | d51207e | 2018-06-04 15:15:04 +0100 | [diff] [blame] | 16 | MBEDTLS_INC = -I${MBEDTLS_DIR}/include |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 17 | |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 18 | MBEDTLS_MAJOR=$(shell grep -hP "define MBEDTLS_VERSION_MAJOR" ${MBEDTLS_DIR}/include/mbedtls/*.h | grep -oe '\([0-9.]*\)') |
| 19 | MBEDTLS_MINOR=$(shell grep -hP "define MBEDTLS_VERSION_MINOR" ${MBEDTLS_DIR}/include/mbedtls/*.h | grep -oe '\([0-9.]*\)') |
| 20 | $(info MBEDTLS_VERSION_MAJOR is [${MBEDTLS_MAJOR}] MBEDTLS_VERSION_MINOR is [${MBEDTLS_MINOR}]) |
| 21 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 22 | # Specify mbed TLS configuration file |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 23 | ifeq (${MBEDTLS_MAJOR}, 2) |
| 24 | MBEDTLS_CONFIG_FILE ?= "<drivers/auth/mbedtls/mbedtls_config-2.h>" |
| 25 | else ifeq (${MBEDTLS_MAJOR}, 3) |
Manish V Badarkhe | 78e14f8 | 2023-09-06 09:08:28 +0100 | [diff] [blame] | 26 | ifeq (${PSA_CRYPTO},1) |
| 27 | MBEDTLS_CONFIG_FILE ?= "<drivers/auth/mbedtls/psa_mbedtls_config.h>" |
| 28 | else |
| 29 | MBEDTLS_CONFIG_FILE ?= "<drivers/auth/mbedtls/mbedtls_config-3.h>" |
| 30 | endif |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 31 | endif |
| 32 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 33 | $(eval $(call add_define,MBEDTLS_CONFIG_FILE)) |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 34 | |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 35 | MBEDTLS_SOURCES += drivers/auth/mbedtls/mbedtls_common.c |
| 36 | |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 37 | LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \ |
| 38 | aes.c \ |
| 39 | asn1parse.c \ |
| 40 | asn1write.c \ |
| 41 | cipher.c \ |
| 42 | cipher_wrap.c \ |
| 43 | constant_time.c \ |
| 44 | memory_buffer_alloc.c \ |
| 45 | oid.c \ |
| 46 | platform.c \ |
| 47 | platform_util.c \ |
| 48 | bignum.c \ |
| 49 | gcm.c \ |
| 50 | md.c \ |
| 51 | pk.c \ |
| 52 | pk_wrap.c \ |
| 53 | pkparse.c \ |
| 54 | pkwrite.c \ |
| 55 | sha256.c \ |
| 56 | sha512.c \ |
| 57 | ecdsa.c \ |
| 58 | ecp_curves.c \ |
| 59 | ecp.c \ |
| 60 | rsa.c \ |
| 61 | x509.c \ |
| 62 | x509_crt.c \ |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 63 | ) |
| 64 | |
Govindraj Raja | dee8b6f | 2023-01-12 15:34:12 +0000 | [diff] [blame] | 65 | ifeq (${MBEDTLS_MAJOR}, 2) |
| 66 | LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \ |
| 67 | rsa_internal.c \ |
| 68 | ) |
| 69 | else ifeq (${MBEDTLS_MAJOR}, 3) |
| 70 | LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \ |
| 71 | bignum_core.c \ |
| 72 | rsa_alt_helpers.c \ |
| 73 | hash_info.c \ |
| 74 | ) |
| 75 | |
| 76 | # Currently on Mbedtls-3 there is outstanding bug due to usage |
| 77 | # of redundant declaration[1], So disable redundant-decls |
| 78 | # compilation flag to avoid compilation error when compiling with |
| 79 | # Mbedtls-3. |
| 80 | # [1]: https://github.com/Mbed-TLS/mbedtls/issues/6910 |
| 81 | LIBMBEDTLS_CFLAGS += -Wno-error=redundant-decls |
| 82 | endif |
| 83 | |
Manish V Badarkhe | 78e14f8 | 2023-09-06 09:08:28 +0100 | [diff] [blame] | 84 | ifeq (${PSA_CRYPTO},1) |
| 85 | LIBMBEDTLS_SRCS += $(addprefix ${MBEDTLS_DIR}/library/, \ |
| 86 | psa_crypto.c \ |
| 87 | psa_crypto_client.c \ |
| 88 | psa_crypto_driver_wrappers.c \ |
| 89 | psa_crypto_hash.c \ |
| 90 | psa_crypto_rsa.c \ |
| 91 | psa_crypto_ecp.c \ |
| 92 | psa_crypto_slot_management.c \ |
| 93 | ) |
| 94 | endif |
| 95 | |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 96 | # The platform may define the variable 'TF_MBEDTLS_KEY_ALG' to select the key |
Justin Chadwell | 3168a20 | 2019-09-09 15:24:31 +0100 | [diff] [blame] | 97 | # algorithm to use. If the variable is not defined, select it based on |
| 98 | # algorithm used for key generation `KEY_ALG`. If `KEY_ALG` is not defined, |
| 99 | # then it is set to `rsa`. |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 100 | ifeq (${TF_MBEDTLS_KEY_ALG},) |
| 101 | ifeq (${KEY_ALG}, ecdsa) |
| 102 | TF_MBEDTLS_KEY_ALG := ecdsa |
| 103 | else |
| 104 | TF_MBEDTLS_KEY_ALG := rsa |
| 105 | endif |
| 106 | endif |
| 107 | |
Justin Chadwell | f9b32c1 | 2019-07-29 17:13:10 +0100 | [diff] [blame] | 108 | ifeq (${TF_MBEDTLS_KEY_SIZE},) |
| 109 | ifneq ($(findstring rsa,${TF_MBEDTLS_KEY_ALG}),) |
laurenw-arm | f709c27 | 2023-08-15 14:56:46 -0500 | [diff] [blame] | 110 | ifeq (${KEY_SIZE},) |
Justin Chadwell | f9b32c1 | 2019-07-29 17:13:10 +0100 | [diff] [blame] | 111 | TF_MBEDTLS_KEY_SIZE := 2048 |
laurenw-arm | f709c27 | 2023-08-15 14:56:46 -0500 | [diff] [blame] | 112 | else ifneq ($(filter $(KEY_SIZE), 1024 2048 3072 4096),) |
| 113 | TF_MBEDTLS_KEY_SIZE := ${KEY_SIZE} |
| 114 | else |
| 115 | $(error "Invalid value for KEY_SIZE: ${KEY_SIZE}") |
| 116 | endif |
| 117 | else ifneq ($(findstring ecdsa,${TF_MBEDTLS_KEY_ALG}),) |
| 118 | ifeq (${KEY_SIZE},) |
| 119 | TF_MBEDTLS_KEY_SIZE := 256 |
| 120 | else ifneq ($(filter $(KEY_SIZE), 256 384),) |
Justin Chadwell | f9b32c1 | 2019-07-29 17:13:10 +0100 | [diff] [blame] | 121 | TF_MBEDTLS_KEY_SIZE := ${KEY_SIZE} |
laurenw-arm | f709c27 | 2023-08-15 14:56:46 -0500 | [diff] [blame] | 122 | else |
| 123 | $(error "Invalid value for KEY_SIZE: ${KEY_SIZE}") |
| 124 | endif |
Justin Chadwell | f9b32c1 | 2019-07-29 17:13:10 +0100 | [diff] [blame] | 125 | endif |
| 126 | endif |
| 127 | |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 128 | ifeq (${HASH_ALG}, sha384) |
| 129 | TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA384 |
| 130 | else ifeq (${HASH_ALG}, sha512) |
Alexei Fedorov | 25d7c88 | 2020-03-20 18:38:55 +0000 | [diff] [blame] | 131 | TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA512 |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 132 | else |
| 133 | TF_MBEDTLS_HASH_ALG_ID := TF_MBEDTLS_SHA256 |
| 134 | endif |
| 135 | |
| 136 | ifeq (${TF_MBEDTLS_KEY_ALG},ecdsa) |
| 137 | TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_ECDSA |
| 138 | else ifeq (${TF_MBEDTLS_KEY_ALG},rsa) |
| 139 | TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_RSA |
| 140 | else ifeq (${TF_MBEDTLS_KEY_ALG},rsa+ecdsa) |
| 141 | TF_MBEDTLS_KEY_ALG_ID := TF_MBEDTLS_RSA_AND_ECDSA |
| 142 | else |
| 143 | $(error "TF_MBEDTLS_KEY_ALG=${TF_MBEDTLS_KEY_ALG} not supported on mbed TLS") |
| 144 | endif |
| 145 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 146 | ifeq (${DECRYPTION_SUPPORT}, aes_gcm) |
| 147 | TF_MBEDTLS_USE_AES_GCM := 1 |
| 148 | else |
| 149 | TF_MBEDTLS_USE_AES_GCM := 0 |
| 150 | endif |
| 151 | |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 152 | # Needs to be set to drive mbed TLS configuration correctly |
Leonardo Sandoval | 65fca7c | 2020-09-10 12:18:27 -0500 | [diff] [blame] | 153 | $(eval $(call add_defines,\ |
| 154 | $(sort \ |
| 155 | TF_MBEDTLS_KEY_ALG_ID \ |
| 156 | TF_MBEDTLS_KEY_SIZE \ |
| 157 | TF_MBEDTLS_HASH_ALG_ID \ |
| 158 | TF_MBEDTLS_USE_AES_GCM \ |
| 159 | ))) |
Roberto Vargas | 502290b | 2018-05-08 10:27:10 +0100 | [diff] [blame] | 160 | |
| 161 | $(eval $(call MAKE_LIB,mbedtls)) |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 162 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 163 | endif |