diff --git a/UNITTESTS/stubs/connectivity/CMakeLists.txt b/UNITTESTS/stubs/connectivity/CMakeLists.txt index 895212b..df8dbd5 100644 --- a/UNITTESTS/stubs/connectivity/CMakeLists.txt +++ b/UNITTESTS/stubs/connectivity/CMakeLists.txt @@ -18,12 +18,8 @@ target_sources(mbed-stubs-connectivity PRIVATE - aes_stub.c - cipher_stub.c - cmac_stub.c ip4tos_stub.c stoip4_stub.c - ${mbed-os_SOURCE_DIR}/connectivity/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.c ) target_link_libraries(mbed-stubs-connectivity diff --git a/UNITTESTS/stubs/connectivity/aes_stub.c b/UNITTESTS/stubs/connectivity/aes_stub.c deleted file mode 100644 index 2aa4fae..0000000 --- a/UNITTESTS/stubs/connectivity/aes_stub.c +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#if !defined(MBEDTLS_CONFIG_FILE) -#include "mbedtls/config.h" -#else -#include MBEDTLS_CONFIG_FILE -#endif - -#include - -#include "mbedtls/aes.h" - -#include "aes_stub.h" -aes_stub_def aes_stub; - - -void mbedtls_aes_init(mbedtls_aes_context *ctx) -{ -} - -void mbedtls_aes_free(mbedtls_aes_context *ctx) -{ -} - -#if defined(MBEDTLS_CIPHER_MODE_XTS) -void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx) -{ -} - -void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx) -{ -} -#endif - -#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT) -int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, - unsigned int keybits) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} -#endif - -#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT) -int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, - unsigned int keybits) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} -#endif - -#if defined(MBEDTLS_CIPHER_MODE_XTS) -int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx, - const unsigned char *key, - unsigned int keybits) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx, - const unsigned char *key, - unsigned int keybits) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} -#endif - -int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, - const unsigned char input[16], - unsigned char output[16]) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -void mbedtls_aes_encrypt(mbedtls_aes_context *ctx, - const unsigned char input[16], - unsigned char output[16]) -{ -} - -int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, - const unsigned char input[16], - unsigned char output[16]) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -void mbedtls_aes_decrypt(mbedtls_aes_context *ctx, - const unsigned char input[16], - unsigned char output[16]) -{ -} - -int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, - int mode, - const unsigned char input[16], - unsigned char output[16]) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, - int mode, - size_t length, - unsigned char iv[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - - -#if defined(MBEDTLS_CIPHER_MODE_XTS) -int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx, - int mode, - size_t length, - const unsigned char data_unit[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} -#endif - -int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, - int mode, - size_t length, - size_t *iv_off, - unsigned char iv[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, - int mode, - size_t length, - unsigned char iv[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx, - size_t length, - size_t *iv_off, - unsigned char iv[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} - -int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, - size_t length, - size_t *nc_off, - unsigned char nonce_counter[16], - unsigned char stream_block[16], - const unsigned char *input, - unsigned char *output) -{ - if (aes_stub.int_zero_counter) { - aes_stub.int_zero_counter--; - return 0; - } - return aes_stub.int_value; -} diff --git a/UNITTESTS/stubs/connectivity/cipher_stub.c b/UNITTESTS/stubs/connectivity/cipher_stub.c deleted file mode 100644 index d6fc38f..0000000 --- a/UNITTESTS/stubs/connectivity/cipher_stub.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#if !defined(MBEDTLS_CONFIG_FILE) -#include "mbedtls/config.h" -#else -#include MBEDTLS_CONFIG_FILE -#endif - -#include "mbedtls/cipher.h" -#include "mbedtls/cipher_internal.h" -#include "mbedtls/platform_util.h" - -#include -#include - -#if defined(MBEDTLS_GCM_C) -#include "mbedtls/gcm.h" -#endif - -#if defined(MBEDTLS_CCM_C) -#include "mbedtls/ccm.h" -#endif - -#if defined(MBEDTLS_CMAC_C) -#include "mbedtls/cmac.h" -#endif - -#include "cipher_stub.h" - -cipher_stub_def cipher_stub; - -const int *mbedtls_cipher_list(void) -{ - return cipher_stub.int_ptr; -} - -const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type) -{ - return cipher_stub.info_value; -} - -const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name) -{ - return cipher_stub.info_value; -} - -const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id, - int key_bitlen, - const mbedtls_cipher_mode_t mode) -{ - return cipher_stub.info_value; -} - -void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) -{ -} - -void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) -{ -} - -int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, - int key_bitlen, const mbedtls_operation_t operation) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, - const unsigned char *iv, size_t iv_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, - const unsigned char *ad, size_t ad_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, - size_t ilen, unsigned char *output, size_t *olen) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, - unsigned char *output, size_t *olen) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, - unsigned char *tag, size_t tag_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, - const unsigned char *tag, size_t tag_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx, - const unsigned char *iv, size_t iv_len, - const unsigned char *input, size_t ilen, - unsigned char *output, size_t *olen) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx, - const unsigned char *iv, size_t iv_len, - const unsigned char *ad, size_t ad_len, - const unsigned char *input, size_t ilen, - unsigned char *output, size_t *olen, - unsigned char *tag, size_t tag_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - -int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx, - const unsigned char *iv, size_t iv_len, - const unsigned char *ad, size_t ad_len, - const unsigned char *input, size_t ilen, - unsigned char *output, size_t *olen, - const unsigned char *tag, size_t tag_len) -{ - if (cipher_stub.int_zero_counter) { - cipher_stub.int_zero_counter--; - return 0; - } - return cipher_stub.int_value; -} - diff --git a/UNITTESTS/stubs/connectivity/cmac_stub.c b/UNITTESTS/stubs/connectivity/cmac_stub.c deleted file mode 100644 index 8ecd570..0000000 --- a/UNITTESTS/stubs/connectivity/cmac_stub.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#if !defined(MBEDTLS_CONFIG_FILE) -#include "mbedtls/config.h" -#else -#include MBEDTLS_CONFIG_FILE -#endif - - -#include "mbedtls/cmac.h" -#include "mbedtls/platform_util.h" -#include - -#include "cmac_stub.h" - -cmac_stub_def cmac_stub; - -int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx, - const unsigned char *key, size_t keybits) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - -int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx, - const unsigned char *input, size_t ilen) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - -int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx, - unsigned char *output) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - -int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - -int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info, - const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char *output) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - -int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length, - const unsigned char *input, size_t in_len, - unsigned char *output) -{ - if (cmac_stub.int_zero_counter) { - cmac_stub.int_zero_counter--; - return 0; - } - return cmac_stub.int_value; -} - diff --git a/connectivity/CMakeLists.txt b/connectivity/CMakeLists.txt index e011ad2..68a16b1 100644 --- a/connectivity/CMakeLists.txt +++ b/connectivity/CMakeLists.txt @@ -41,4 +41,5 @@ add_subdirectory(cellular) add_subdirectory(lorawan) add_subdirectory(netsocket) + add_subdirectory(mbedtls) endif() diff --git a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/loramaccrypto/CMakeLists.txt b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/loramaccrypto/CMakeLists.txt index edca76f..aa70031 100644 --- a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/loramaccrypto/CMakeLists.txt +++ b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/loramaccrypto/CMakeLists.txt @@ -22,6 +22,7 @@ mbed-headers-lorawan mbed-stubs mbed-stubs-headers + mbed-stubs-mbedtls gmock_main ) diff --git a/connectivity/mbedtls/CMakeLists.txt b/connectivity/mbedtls/CMakeLists.txt index 51938fe..f000967 100644 --- a/connectivity/mbedtls/CMakeLists.txt +++ b/connectivity/mbedtls/CMakeLists.txt @@ -1,6 +1,10 @@ # Copyright (c) 2020 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) + add_subdirectory(tests/UNITTESTS) +endif() + target_include_directories(mbed-mbedtls INTERFACE . diff --git a/connectivity/mbedtls/tests/UNITTESTS/.mbedignore b/connectivity/mbedtls/tests/UNITTESTS/.mbedignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/.mbedignore @@ -0,0 +1 @@ +* diff --git a/connectivity/mbedtls/tests/UNITTESTS/CMakeLists.txt b/connectivity/mbedtls/tests/UNITTESTS/CMakeLists.txt new file mode 100644 index 0000000..fa602ed --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/CMakeLists.txt @@ -0,0 +1,3 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +add_subdirectory(doubles) diff --git a/connectivity/mbedtls/tests/UNITTESTS/doubles/CMakeLists.txt b/connectivity/mbedtls/tests/UNITTESTS/doubles/CMakeLists.txt new file mode 100644 index 0000000..fad943c --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/doubles/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +add_library(mbed-stubs-mbedtls) + +target_sources(mbed-stubs-mbedtls + PRIVATE + aes_stub.c + cipher_stub.c + cmac_stub.c + ${mbed-os_SOURCE_DIR}/connectivity/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.c +) + +target_link_libraries(mbed-stubs-mbedtls + PRIVATE + mbed-headers + mbed-stubs-headers + mbed-stubs-rtos + gtest +) diff --git a/connectivity/mbedtls/tests/UNITTESTS/doubles/aes_stub.c b/connectivity/mbedtls/tests/UNITTESTS/doubles/aes_stub.c new file mode 100644 index 0000000..2aa4fae --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/doubles/aes_stub.c @@ -0,0 +1,232 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include + +#include "mbedtls/aes.h" + +#include "aes_stub.h" +aes_stub_def aes_stub; + + +void mbedtls_aes_init(mbedtls_aes_context *ctx) +{ +} + +void mbedtls_aes_free(mbedtls_aes_context *ctx) +{ +} + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx) +{ +} + +void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx) +{ +} +#endif + +#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT) +int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT) +int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16]) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +void mbedtls_aes_encrypt(mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16]) +{ +} + +int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16]) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +void mbedtls_aes_decrypt(mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16]) +{ +} + +int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, + int mode, + const unsigned char input[16], + unsigned char output[16]) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx, + int mode, + size_t length, + const unsigned char data_unit[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, + int mode, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, + size_t length, + size_t *nc_off, + unsigned char nonce_counter[16], + unsigned char stream_block[16], + const unsigned char *input, + unsigned char *output) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} diff --git a/connectivity/mbedtls/tests/UNITTESTS/doubles/cipher_stub.c b/connectivity/mbedtls/tests/UNITTESTS/doubles/cipher_stub.c new file mode 100644 index 0000000..d6fc38f --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/doubles/cipher_stub.c @@ -0,0 +1,213 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include "mbedtls/cipher.h" +#include "mbedtls/cipher_internal.h" +#include "mbedtls/platform_util.h" + +#include +#include + +#if defined(MBEDTLS_GCM_C) +#include "mbedtls/gcm.h" +#endif + +#if defined(MBEDTLS_CCM_C) +#include "mbedtls/ccm.h" +#endif + +#if defined(MBEDTLS_CMAC_C) +#include "mbedtls/cmac.h" +#endif + +#include "cipher_stub.h" + +cipher_stub_def cipher_stub; + +const int *mbedtls_cipher_list(void) +{ + return cipher_stub.int_ptr; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type) +{ + return cipher_stub.info_value; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name) +{ + return cipher_stub.info_value; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id, + int key_bitlen, + const mbedtls_cipher_mode_t mode) +{ + return cipher_stub.info_value; +} + +void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) +{ +} + +void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) +{ +} + +int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, + int key_bitlen, const mbedtls_operation_t operation) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, + const unsigned char *ad, size_t ad_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, + size_t ilen, unsigned char *output, size_t *olen) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, + unsigned char *output, size_t *olen) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, + unsigned char *tag, size_t tag_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, + const unsigned char *tag, size_t tag_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *ad, size_t ad_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, + unsigned char *tag, size_t tag_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *ad, size_t ad_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, + const unsigned char *tag, size_t tag_len) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + diff --git a/connectivity/mbedtls/tests/UNITTESTS/doubles/cmac_stub.c b/connectivity/mbedtls/tests/UNITTESTS/doubles/cmac_stub.c new file mode 100644 index 0000000..8ecd570 --- /dev/null +++ b/connectivity/mbedtls/tests/UNITTESTS/doubles/cmac_stub.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + + +#include "mbedtls/cmac.h" +#include "mbedtls/platform_util.h" +#include + +#include "cmac_stub.h" + +cmac_stub_def cmac_stub; + +int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx, + const unsigned char *key, size_t keybits) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx, + const unsigned char *input, size_t ilen) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx, + unsigned char *output) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info, + const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char *output) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length, + const unsigned char *input, size_t in_len, + unsigned char *output) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocket/CMakeLists.txt b/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocket/CMakeLists.txt index 9564055..9573c2c 100644 --- a/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocket/CMakeLists.txt +++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocket/CMakeLists.txt @@ -34,6 +34,7 @@ mbed-headers-netsocket mbed-stubs mbed-stubs-headers + mbed-stubs-mbedtls mbed-stubs-netsocket gmock_main ) diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocketWrapper/CMakeLists.txt b/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocketWrapper/CMakeLists.txt index bfc358b..a7fc88d 100644 --- a/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocketWrapper/CMakeLists.txt +++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/DTLSSocketWrapper/CMakeLists.txt @@ -33,6 +33,7 @@ mbed-headers-netsocket mbed-stubs mbed-stubs-headers + mbed-stubs-mbedtls mbed-stubs-netsocket gmock_main ) diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocket/CMakeLists.txt b/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocket/CMakeLists.txt index 8d1fc33..71f0dd5 100644 --- a/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocket/CMakeLists.txt +++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocket/CMakeLists.txt @@ -32,6 +32,7 @@ mbed-headers-netsocket mbed-stubs mbed-stubs-headers + mbed-stubs-mbedtls mbed-stubs-netsocket gmock_main ) diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/CMakeLists.txt b/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/CMakeLists.txt index 56248a3..15e6afb 100644 --- a/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/CMakeLists.txt +++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/CMakeLists.txt @@ -31,6 +31,7 @@ mbed-headers-netsocket mbed-stubs mbed-stubs-headers + mbed-stubs-mbedtls mbed-stubs-netsocket gmock_main )