init
Embed:
(wiki syntax)
Show/hide line numbers
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 /** 00027 * \name SECTION: Compatibility code 00028 * 00029 * Depending on whether the alternative (hatdware accelerated) hashing 00030 * functions are provided or not, different API should be used for hashing. 00031 * \{ 00032 */ 00033 00034 #if defined(MBEDTLS_SHA256_ALT) 00035 00036 /** 00037 * \brief This function starts a SHA-256 checksum calculation. 00038 * 00039 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0. 00040 * 00041 * \param ctx The SHA-256 context to initialize. 00042 * \param is224 Determines which function to use. 00043 * <ul><li>0: Use SHA-256.</li> 00044 * <li>1: Use SHA-224.</li></ul> 00045 */ 00046 void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, 00047 int is224 ) 00048 { 00049 mbedtls_sha256_starts_ret( ctx, is224 ); 00050 } 00051 00052 /** 00053 * \brief This function feeds an input buffer into an ongoing 00054 * SHA-256 checksum calculation. 00055 * 00056 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0. 00057 * 00058 * \param ctx The SHA-256 context to initialize. 00059 * \param input The buffer holding the data. 00060 * \param ilen The length of the input data. 00061 */ 00062 void mbedtls_sha256_update( mbedtls_sha256_context *ctx, 00063 const unsigned char *input, 00064 size_t ilen ) 00065 { 00066 mbedtls_sha256_update_ret( ctx, input, ilen ); 00067 } 00068 00069 /** 00070 * \brief This function finishes the SHA-256 operation, and writes 00071 * the result to the output buffer. 00072 * 00073 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0. 00074 * 00075 * \param ctx The SHA-256 context. 00076 * \param output The SHA-224or SHA-256 checksum result. 00077 */ 00078 void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, 00079 unsigned char output[32] ) 00080 { 00081 mbedtls_sha256_finish_ret( ctx, output ); 00082 } 00083 00084 #endif /* defined(MBEDTLS_SHA256_ALT) */ 00085 00086 /* \} name SECTION: Compatibility code */ 00087 00088 using namespace utest::v1; 00089 00090 #if defined(MBEDTLS_SHA256_C) 00091 /* Tests several call to mbedtls_sha256_update function that are not modulo 64 bytes */ 00092 void test_case_sha256_split() { 00093 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00094 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00095 const unsigned char test_sum[] = 00096 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00097 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00098 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00099 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C}; 00100 unsigned char outsum[32]; 00101 int i; 00102 00103 mbedtls_sha256_context ctx; 00104 printf("test sha256\n"); 00105 mbedtls_sha256_init( &ctx ); 00106 mbedtls_sha256_starts( &ctx, 0); 00107 #if 0 00108 printf("test not splitted\n"); 00109 mbedtls_sha256_update( &ctx, test_buf, 168 ); 00110 #else 00111 printf("test splitted into 3 pieces\n"); 00112 mbedtls_sha256_update( &ctx, test_buf, 2 ); 00113 mbedtls_sha256_update( &ctx, test_buf+2, 66 ); 00114 mbedtls_sha256_update( &ctx, test_buf+68, 100 ); 00115 #endif 00116 00117 mbedtls_sha256_finish( &ctx, outsum ); 00118 mbedtls_sha256_free( &ctx ); 00119 00120 printf("\nreceived result : "); 00121 for (i=0;i<32;i++) { printf("%02X",outsum[i]);} 00122 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00123 00124 printf("\nend of test sha256\n"); 00125 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum, test_sum,32); 00126 } 00127 00128 /* Tests that treating 2 sha256 objects in // does not impact the result */ 00129 void test_case_sha256_multi() { 00130 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; 00131 const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"}; 00132 00133 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00134 const unsigned char test_sum1[] = 00135 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 00136 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 00137 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 00138 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }; 00139 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00140 const unsigned char test_sum2[] = 00141 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22, 00142 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93, 00143 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0, 00144 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C}; 00145 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789 00146 const unsigned char test_sum3[] = 00147 { 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E, 00148 0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97, 00149 0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22, 00150 0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68}; 00151 unsigned char outsum1[32], outsum2[32], outsum3[32]; 00152 int i; 00153 00154 mbedtls_sha256_context ctx1; 00155 mbedtls_sha256_context ctx2; 00156 mbedtls_sha256_context ctx3; 00157 printf("test sha256_multi\n"); 00158 //Init both contexts 00159 mbedtls_sha256_init( &ctx1); 00160 mbedtls_sha256_init( &ctx2); 00161 mbedtls_sha256_init( &ctx3); 00162 //Start both contexts 00163 mbedtls_sha256_starts( &ctx1, 0); 00164 mbedtls_sha256_starts( &ctx2, 0); 00165 00166 printf("upd ctx1\n"); 00167 mbedtls_sha256_update( &ctx1, test_buf, 56 ); 00168 printf("upd ctx2\n"); 00169 mbedtls_sha256_update( &ctx2, test_buf, 66 ); 00170 printf("finish ctx1\n"); 00171 mbedtls_sha256_finish( &ctx1, outsum1 ); 00172 printf("upd ctx2\n"); 00173 mbedtls_sha256_update( &ctx2, test_buf+66, 46 ); 00174 printf("clone ctx2 in ctx3\n"); 00175 mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2); 00176 printf("free ctx1\n"); 00177 mbedtls_sha256_free( &ctx1 ); 00178 printf("upd ctx2\n"); 00179 mbedtls_sha256_update( &ctx2, test_buf+112, 56 ); 00180 printf("upd ctx3 with different values than ctx2\n"); 00181 mbedtls_sha256_update( &ctx3, test_buf2, 56 ); 00182 printf("finish ctx2\n"); 00183 mbedtls_sha256_finish( &ctx2, outsum2 ); 00184 printf("finish ctx3\n"); 00185 mbedtls_sha256_finish( &ctx3, outsum3 ); 00186 printf("free ctx2\n"); 00187 mbedtls_sha256_free( &ctx2 ); 00188 printf("free ctx3\n"); 00189 mbedtls_sha256_free( &ctx3 ); 00190 00191 printf("\nreceived result ctx1 : "); 00192 for (i=0;i<32;i++) { printf("%02X",outsum1[i]);} 00193 printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00194 printf("\nreceived result ctx2 : "); 00195 for (i=0;i<32;i++) { printf("%02X",outsum2[i]);} 00196 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 00197 printf("\nreceived result ctx3 : "); 00198 for (i=0;i<32;i++) { printf("%02X",outsum3[i]);} 00199 printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789 00200 printf("\nend of test sha256\n"); 00201 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32); 00202 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32); 00203 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3,32); 00204 } 00205 #endif //MBEDTLS_SHA256_C 00206 00207 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { 00208 greentea_case_failure_abort_handler(source, reason); 00209 return STATUS_CONTINUE; 00210 } 00211 00212 Case cases[] = { 00213 #if defined(MBEDTLS_SHA256_C) 00214 Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler), 00215 Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler), 00216 #endif 00217 }; 00218 00219 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { 00220 GREENTEA_SETUP(10, "default_auto"); 00221 return greentea_test_setup_handler(number_of_cases); 00222 } 00223 00224 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00225 00226 int main() { 00227 Harness::run(specification); 00228 }
Generated on Tue Jul 12 2022 13:24:56 by
1.7.2