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 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00039 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00040 const unsigned char test_sum[] = 00041 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00042 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00043 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00044 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C}; 00045 unsigned char outsum[32]; 00046 int i; 00047 00048 mbedtls_sha256_context ctx; 00049 printf("test sha256\n"); 00050 mbedtls_sha256_init( &ctx ); 00051 mbedtls_sha256_starts( &ctx, 0); 00052 #if 0 00053 printf("test not splitted\n"); 00054 mbedtls_sha256_update( &ctx, test_buf, 168 ); 00055 #else 00056 printf("test splitted into 3 pieces\n"); 00057 mbedtls_sha256_update( &ctx, test_buf, 2 ); 00058 mbedtls_sha256_update( &ctx, test_buf+2, 66 ); 00059 mbedtls_sha256_update( &ctx, test_buf+68, 100 ); 00060 #endif 00061 00062 mbedtls_sha256_finish( &ctx, outsum ); 00063 mbedtls_sha256_free( &ctx ); 00064 00065 printf("\nreceived result : "); 00066 for (i=0;i<32;i++) { printf("%02X",outsum[i]);} 00067 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00068 00069 printf("\nend of test sha256\n"); 00070 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum, test_sum,32); 00071 } 00072 00073 /* Tests that treating 2 sha256 objects in // does not impact the result */ 00074 void test_case_sha256_multi() { 00075 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00076 const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"}; 00077 00078 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00079 const unsigned char test_sum1[] = 00080 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 00081 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 00082 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 00083 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }; 00084 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00085 const unsigned char test_sum2[] = 00086 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00087 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00088 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00089 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C}; 00090 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789 00091 const unsigned char test_sum3[] = 00092 { 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E, 00093 0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97, 00094 0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22, 00095 0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68}; 00096 unsigned char outsum1[32], outsum2[32], outsum3[32]; 00097 int i; 00098 00099 mbedtls_sha256_context ctx1; 00100 mbedtls_sha256_context ctx2; 00101 mbedtls_sha256_context ctx3; 00102 printf("test sha256_multi\n"); 00103 //Init both contexts 00104 mbedtls_sha256_init( &ctx1); 00105 mbedtls_sha256_init( &ctx2); 00106 mbedtls_sha256_init( &ctx3); 00107 //Start both contexts 00108 mbedtls_sha256_starts( &ctx1, 0); 00109 mbedtls_sha256_starts( &ctx2, 0); 00110 00111 printf("upd ctx1\n"); 00112 mbedtls_sha256_update( &ctx1, test_buf, 56 ); 00113 printf("upd ctx2\n"); 00114 mbedtls_sha256_update( &ctx2, test_buf, 66 ); 00115 printf("finish ctx1\n"); 00116 mbedtls_sha256_finish( &ctx1, outsum1 ); 00117 printf("upd ctx2\n"); 00118 mbedtls_sha256_update( &ctx2, test_buf+66, 46 ); 00119 printf("clone ctx2 in ctx3\n"); 00120 mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2); 00121 printf("free ctx1\n"); 00122 mbedtls_sha256_free( &ctx1 ); 00123 printf("upd ctx2\n"); 00124 mbedtls_sha256_update( &ctx2, test_buf+112, 56 ); 00125 printf("upd ctx3 with different values than ctx2\n"); 00126 mbedtls_sha256_update( &ctx3, test_buf2, 56 ); 00127 printf("finish ctx2\n"); 00128 mbedtls_sha256_finish( &ctx2, outsum2 ); 00129 printf("finish ctx3\n"); 00130 mbedtls_sha256_finish( &ctx3, outsum3 ); 00131 printf("free ctx2\n"); 00132 mbedtls_sha256_free( &ctx2 ); 00133 printf("free ctx3\n"); 00134 mbedtls_sha256_free( &ctx3 ); 00135 00136 printf("\nreceived result ctx1 : "); 00137 for (i=0;i<32;i++) { printf("%02X",outsum1[i]);} 00138 printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00139 printf("\nreceived result ctx2 : "); 00140 for (i=0;i<32;i++) { printf("%02X",outsum2[i]);} 00141 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00142 printf("\nreceived result ctx3 : "); 00143 for (i=0;i<32;i++) { printf("%02X",outsum3[i]);} 00144 printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789 00145 printf("\nend of test sha256\n"); 00146 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32); 00147 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32); 00148 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3,32); 00149 } 00150 #endif //MBEDTLS_SHA256_C 00151 00152 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { 00153 greentea_case_failure_abort_handler(source, reason); 00154 return STATUS_CONTINUE; 00155 } 00156 00157 Case cases[] = { 00158 #if defined(MBEDTLS_SHA256_C) 00159 Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler), 00160 Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler), 00161 #endif 00162 }; 00163 00164 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { 00165 GREENTEA_SETUP(10, "default_auto"); 00166 return greentea_test_setup_handler(number_of_cases); 00167 } 00168 00169 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00170 00171 int main() { 00172 int ret = 0; 00173 #if defined(MBEDTLS_PLATFORM_C) 00174 mbedtls_platform_context platform_ctx; 00175 if((ret = mbedtls_platform_setup(&platform_ctx))!= 0) 00176 { 00177 mbedtls_printf("Mbed TLS multitest failed! mbedtls_platform_setup returned %d\n", ret); 00178 return 1; 00179 } 00180 #endif 00181 ret = (Harness::run(specification) ? 0 : 1); 00182 #if defined(MBEDTLS_PLATFORM_C) 00183 mbedtls_platform_teardown(&platform_ctx); 00184 #endif 00185 return ret; 00186 }
Generated on Tue Jul 12 2022 12:45:24 by
1.7.2