Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include <stdio.h> 00018 #include <string.h> 00019 #include "mbed.h" 00020 #include "greentea-client/test_env.h" 00021 #include "unity/unity.h" 00022 #include "utest/utest.h" 00023 00024 #include "mbedtls/sha256.h" 00025 00026 #if defined(MBEDTLS_PLATFORM_C) 00027 #include "mbedtls/platform.h" 00028 #else 00029 #include <stdio.h> 00030 #define mbedtls_printf printf 00031 #endif 00032 00033 using namespace utest::v1; 00034 00035 #if defined(MBEDTLS_SHA256_C) 00036 /* Tests several call to mbedtls_sha256_update function that are not modulo 64 bytes */ 00037 void test_case_sha256_split() 00038 { 00039 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00040 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00041 const unsigned char test_sum[] = { 00042 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00043 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00044 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00045 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C 00046 }; 00047 unsigned char outsum[32]; 00048 int i; 00049 00050 mbedtls_sha256_context ctx; 00051 printf("test sha256\n"); 00052 mbedtls_sha256_init(&ctx); 00053 mbedtls_sha256_starts(&ctx, 0); 00054 #if 0 00055 printf("test not splitted\n"); 00056 mbedtls_sha256_update(&ctx, test_buf, 168); 00057 #else 00058 printf("test splitted into 3 pieces\n"); 00059 mbedtls_sha256_update(&ctx, test_buf, 2); 00060 mbedtls_sha256_update(&ctx, test_buf + 2, 66); 00061 mbedtls_sha256_update(&ctx, test_buf + 68, 100); 00062 #endif 00063 00064 mbedtls_sha256_finish(&ctx, outsum); 00065 mbedtls_sha256_free(&ctx); 00066 00067 printf("\nreceived result : "); 00068 for (i = 0; i < 32; i++) { 00069 printf("%02X", outsum[i]); 00070 } 00071 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00072 00073 printf("\nend of test sha256\n"); 00074 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum, test_sum, 32); 00075 } 00076 00077 /* Tests that treating 2 sha256 objects in // does not impact the result */ 00078 void test_case_sha256_multi() 00079 { 00080 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00081 const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"}; 00082 00083 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00084 const unsigned char test_sum1[] = { 00085 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 00086 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 00087 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 00088 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 00089 }; 00090 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00091 const unsigned char test_sum2[] = { 00092 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00093 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00094 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00095 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C 00096 }; 00097 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789 00098 const unsigned char test_sum3[] = { 00099 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E, 00100 0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97, 00101 0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22, 00102 0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68 00103 }; 00104 unsigned char outsum1[32], outsum2[32], outsum3[32]; 00105 int i; 00106 00107 mbedtls_sha256_context ctx1; 00108 mbedtls_sha256_context ctx2; 00109 mbedtls_sha256_context ctx3; 00110 printf("test sha256_multi\n"); 00111 //Init both contexts 00112 mbedtls_sha256_init(&ctx1); 00113 mbedtls_sha256_init(&ctx2); 00114 mbedtls_sha256_init(&ctx3); 00115 //Start both contexts 00116 mbedtls_sha256_starts(&ctx1, 0); 00117 mbedtls_sha256_starts(&ctx2, 0); 00118 00119 printf("upd ctx1\n"); 00120 mbedtls_sha256_update(&ctx1, test_buf, 56); 00121 printf("upd ctx2\n"); 00122 mbedtls_sha256_update(&ctx2, test_buf, 66); 00123 printf("finish ctx1\n"); 00124 mbedtls_sha256_finish(&ctx1, outsum1); 00125 printf("upd ctx2\n"); 00126 mbedtls_sha256_update(&ctx2, test_buf + 66, 46); 00127 printf("clone ctx2 in ctx3\n"); 00128 mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2); 00129 printf("free ctx1\n"); 00130 mbedtls_sha256_free(&ctx1); 00131 printf("upd ctx2\n"); 00132 mbedtls_sha256_update(&ctx2, test_buf + 112, 56); 00133 printf("upd ctx3 with different values than ctx2\n"); 00134 mbedtls_sha256_update(&ctx3, test_buf2, 56); 00135 printf("finish ctx2\n"); 00136 mbedtls_sha256_finish(&ctx2, outsum2); 00137 printf("finish ctx3\n"); 00138 mbedtls_sha256_finish(&ctx3, outsum3); 00139 printf("free ctx2\n"); 00140 mbedtls_sha256_free(&ctx2); 00141 printf("free ctx3\n"); 00142 mbedtls_sha256_free(&ctx3); 00143 00144 printf("\nreceived result ctx1 : "); 00145 for (i = 0; i < 32; i++) { 00146 printf("%02X", outsum1[i]); 00147 } 00148 printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00149 printf("\nreceived result ctx2 : "); 00150 for (i = 0; i < 32; i++) { 00151 printf("%02X", outsum2[i]); 00152 } 00153 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00154 printf("\nreceived result ctx3 : "); 00155 for (i = 0; i < 32; i++) { 00156 printf("%02X", outsum3[i]); 00157 } 00158 printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789 00159 printf("\nend of test sha256\n"); 00160 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1, 32); 00161 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2, 32); 00162 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3, 32); 00163 } 00164 #endif //MBEDTLS_SHA256_C 00165 00166 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) 00167 { 00168 greentea_case_failure_abort_handler(source, reason); 00169 return STATUS_CONTINUE; 00170 } 00171 00172 Case cases[] = { 00173 #if defined(MBEDTLS_SHA256_C) 00174 Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler), 00175 Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler), 00176 #endif 00177 }; 00178 00179 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 00180 { 00181 GREENTEA_SETUP(10, "default_auto"); 00182 return greentea_test_setup_handler(number_of_cases); 00183 } 00184 00185 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00186 00187 int main() 00188 { 00189 int ret = 0; 00190 #if defined(MBEDTLS_PLATFORM_C) 00191 if ((ret = mbedtls_platform_setup(NULL)) != 0) { 00192 mbedtls_printf("Mbed TLS multitest failed! mbedtls_platform_setup returned %d\n", ret); 00193 return 1; 00194 } 00195 #endif 00196 ret = (Harness::run(specification) ? 0 : 1); 00197 #if defined(MBEDTLS_PLATFORM_C) 00198 mbedtls_platform_teardown(NULL); 00199 #endif 00200 return ret; 00201 }
Generated on Tue Aug 9 2022 00:37:14 by
1.7.2