Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
marcozecchini 0:9fca2b23d0ba 3 * SPDX-License-Identifier: Apache-2.0
marcozecchini 0:9fca2b23d0ba 4 *
marcozecchini 0:9fca2b23d0ba 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
marcozecchini 0:9fca2b23d0ba 6 * not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 7 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 8 *
marcozecchini 0:9fca2b23d0ba 9 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
marcozecchini 0:9fca2b23d0ba 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 14 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 15 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 16 */
marcozecchini 0:9fca2b23d0ba 17 #include <stdio.h>
marcozecchini 0:9fca2b23d0ba 18 #include <string.h>
marcozecchini 0:9fca2b23d0ba 19 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 20 #include "greentea-client/test_env.h"
marcozecchini 0:9fca2b23d0ba 21 #include "unity/unity.h"
marcozecchini 0:9fca2b23d0ba 22 #include "utest/utest.h"
marcozecchini 0:9fca2b23d0ba 23
marcozecchini 0:9fca2b23d0ba 24 #include "mbedtls/sha256.h"
marcozecchini 0:9fca2b23d0ba 25
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 using namespace utest::v1;
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 #if defined(MBEDTLS_SHA256_C)
marcozecchini 0:9fca2b23d0ba 30 /* Tests several call to mbedtls_sha256_update function that are not modulo 64 bytes */
marcozecchini 0:9fca2b23d0ba 31 void test_case_sha256_split() {
marcozecchini 0:9fca2b23d0ba 32 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
marcozecchini 0:9fca2b23d0ba 33 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 34 const unsigned char test_sum[] =
marcozecchini 0:9fca2b23d0ba 35 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22,
marcozecchini 0:9fca2b23d0ba 36 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
marcozecchini 0:9fca2b23d0ba 37 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
marcozecchini 0:9fca2b23d0ba 38 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
marcozecchini 0:9fca2b23d0ba 39 unsigned char outsum[32];
marcozecchini 0:9fca2b23d0ba 40 int i;
marcozecchini 0:9fca2b23d0ba 41
marcozecchini 0:9fca2b23d0ba 42 mbedtls_sha256_context ctx;
marcozecchini 0:9fca2b23d0ba 43 printf("test sha256\n");
marcozecchini 0:9fca2b23d0ba 44 mbedtls_sha256_init( &ctx );
marcozecchini 0:9fca2b23d0ba 45 mbedtls_sha256_starts( &ctx, 0);
marcozecchini 0:9fca2b23d0ba 46 #if 0
marcozecchini 0:9fca2b23d0ba 47 printf("test not splitted\n");
marcozecchini 0:9fca2b23d0ba 48 mbedtls_sha256_update( &ctx, test_buf, 168 );
marcozecchini 0:9fca2b23d0ba 49 #else
marcozecchini 0:9fca2b23d0ba 50 printf("test splitted into 3 pieces\n");
marcozecchini 0:9fca2b23d0ba 51 mbedtls_sha256_update( &ctx, test_buf, 2 );
marcozecchini 0:9fca2b23d0ba 52 mbedtls_sha256_update( &ctx, test_buf+2, 66 );
marcozecchini 0:9fca2b23d0ba 53 mbedtls_sha256_update( &ctx, test_buf+68, 100 );
marcozecchini 0:9fca2b23d0ba 54 #endif
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 mbedtls_sha256_finish( &ctx, outsum );
marcozecchini 0:9fca2b23d0ba 57 mbedtls_sha256_free( &ctx );
marcozecchini 0:9fca2b23d0ba 58
marcozecchini 0:9fca2b23d0ba 59 printf("\nreceived result : ");
marcozecchini 0:9fca2b23d0ba 60 for (i=0;i<32;i++) { printf("%02X",outsum[i]);}
marcozecchini 0:9fca2b23d0ba 61 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 62
marcozecchini 0:9fca2b23d0ba 63 printf("\nend of test sha256\n");
marcozecchini 0:9fca2b23d0ba 64 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum, test_sum,32);
marcozecchini 0:9fca2b23d0ba 65 }
marcozecchini 0:9fca2b23d0ba 66
marcozecchini 0:9fca2b23d0ba 67 /* Tests that treating 2 sha256 objects in // does not impact the result */
marcozecchini 0:9fca2b23d0ba 68 void test_case_sha256_multi() {
marcozecchini 0:9fca2b23d0ba 69 const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
marcozecchini 0:9fca2b23d0ba 70 const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"};
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 73 const unsigned char test_sum1[] =
marcozecchini 0:9fca2b23d0ba 74 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
marcozecchini 0:9fca2b23d0ba 75 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
marcozecchini 0:9fca2b23d0ba 76 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
marcozecchini 0:9fca2b23d0ba 77 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 };
marcozecchini 0:9fca2b23d0ba 78 // sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 79 const unsigned char test_sum2[] =
marcozecchini 0:9fca2b23d0ba 80 { 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22,
marcozecchini 0:9fca2b23d0ba 81 0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
marcozecchini 0:9fca2b23d0ba 82 0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
marcozecchini 0:9fca2b23d0ba 83 0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
marcozecchini 0:9fca2b23d0ba 84 // sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789
marcozecchini 0:9fca2b23d0ba 85 const unsigned char test_sum3[] =
marcozecchini 0:9fca2b23d0ba 86 { 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E,
marcozecchini 0:9fca2b23d0ba 87 0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97,
marcozecchini 0:9fca2b23d0ba 88 0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22,
marcozecchini 0:9fca2b23d0ba 89 0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68};
marcozecchini 0:9fca2b23d0ba 90 unsigned char outsum1[32], outsum2[32], outsum3[32];
marcozecchini 0:9fca2b23d0ba 91 int i;
marcozecchini 0:9fca2b23d0ba 92
marcozecchini 0:9fca2b23d0ba 93 mbedtls_sha256_context ctx1;
marcozecchini 0:9fca2b23d0ba 94 mbedtls_sha256_context ctx2;
marcozecchini 0:9fca2b23d0ba 95 mbedtls_sha256_context ctx3;
marcozecchini 0:9fca2b23d0ba 96 printf("test sha256_multi\n");
marcozecchini 0:9fca2b23d0ba 97 //Init both contexts
marcozecchini 0:9fca2b23d0ba 98 mbedtls_sha256_init( &ctx1);
marcozecchini 0:9fca2b23d0ba 99 mbedtls_sha256_init( &ctx2);
marcozecchini 0:9fca2b23d0ba 100 mbedtls_sha256_init( &ctx3);
marcozecchini 0:9fca2b23d0ba 101 //Start both contexts
marcozecchini 0:9fca2b23d0ba 102 mbedtls_sha256_starts( &ctx1, 0);
marcozecchini 0:9fca2b23d0ba 103 mbedtls_sha256_starts( &ctx2, 0);
marcozecchini 0:9fca2b23d0ba 104
marcozecchini 0:9fca2b23d0ba 105 printf("upd ctx1\n");
marcozecchini 0:9fca2b23d0ba 106 mbedtls_sha256_update( &ctx1, test_buf, 56 );
marcozecchini 0:9fca2b23d0ba 107 printf("upd ctx2\n");
marcozecchini 0:9fca2b23d0ba 108 mbedtls_sha256_update( &ctx2, test_buf, 66 );
marcozecchini 0:9fca2b23d0ba 109 printf("finish ctx1\n");
marcozecchini 0:9fca2b23d0ba 110 mbedtls_sha256_finish( &ctx1, outsum1 );
marcozecchini 0:9fca2b23d0ba 111 printf("upd ctx2\n");
marcozecchini 0:9fca2b23d0ba 112 mbedtls_sha256_update( &ctx2, test_buf+66, 46 );
marcozecchini 0:9fca2b23d0ba 113 printf("clone ctx2 in ctx3\n");
marcozecchini 0:9fca2b23d0ba 114 mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2);
marcozecchini 0:9fca2b23d0ba 115 printf("free ctx1\n");
marcozecchini 0:9fca2b23d0ba 116 mbedtls_sha256_free( &ctx1 );
marcozecchini 0:9fca2b23d0ba 117 printf("upd ctx2\n");
marcozecchini 0:9fca2b23d0ba 118 mbedtls_sha256_update( &ctx2, test_buf+112, 56 );
marcozecchini 0:9fca2b23d0ba 119 printf("upd ctx3 with different values than ctx2\n");
marcozecchini 0:9fca2b23d0ba 120 mbedtls_sha256_update( &ctx3, test_buf2, 56 );
marcozecchini 0:9fca2b23d0ba 121 printf("finish ctx2\n");
marcozecchini 0:9fca2b23d0ba 122 mbedtls_sha256_finish( &ctx2, outsum2 );
marcozecchini 0:9fca2b23d0ba 123 printf("finish ctx3\n");
marcozecchini 0:9fca2b23d0ba 124 mbedtls_sha256_finish( &ctx3, outsum3 );
marcozecchini 0:9fca2b23d0ba 125 printf("free ctx2\n");
marcozecchini 0:9fca2b23d0ba 126 mbedtls_sha256_free( &ctx2 );
marcozecchini 0:9fca2b23d0ba 127 printf("free ctx3\n");
marcozecchini 0:9fca2b23d0ba 128 mbedtls_sha256_free( &ctx3 );
marcozecchini 0:9fca2b23d0ba 129
marcozecchini 0:9fca2b23d0ba 130 printf("\nreceived result ctx1 : ");
marcozecchini 0:9fca2b23d0ba 131 for (i=0;i<32;i++) { printf("%02X",outsum1[i]);}
marcozecchini 0:9fca2b23d0ba 132 printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 133 printf("\nreceived result ctx2 : ");
marcozecchini 0:9fca2b23d0ba 134 for (i=0;i<32;i++) { printf("%02X",outsum2[i]);}
marcozecchini 0:9fca2b23d0ba 135 printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
marcozecchini 0:9fca2b23d0ba 136 printf("\nreceived result ctx3 : ");
marcozecchini 0:9fca2b23d0ba 137 for (i=0;i<32;i++) { printf("%02X",outsum3[i]);}
marcozecchini 0:9fca2b23d0ba 138 printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789
marcozecchini 0:9fca2b23d0ba 139 printf("\nend of test sha256\n");
marcozecchini 0:9fca2b23d0ba 140 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32);
marcozecchini 0:9fca2b23d0ba 141 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32);
marcozecchini 0:9fca2b23d0ba 142 TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3,32);
marcozecchini 0:9fca2b23d0ba 143 }
marcozecchini 0:9fca2b23d0ba 144 #endif //MBEDTLS_SHA256_C
marcozecchini 0:9fca2b23d0ba 145
marcozecchini 0:9fca2b23d0ba 146 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
marcozecchini 0:9fca2b23d0ba 147 greentea_case_failure_abort_handler(source, reason);
marcozecchini 0:9fca2b23d0ba 148 return STATUS_CONTINUE;
marcozecchini 0:9fca2b23d0ba 149 }
marcozecchini 0:9fca2b23d0ba 150
marcozecchini 0:9fca2b23d0ba 151 Case cases[] = {
marcozecchini 0:9fca2b23d0ba 152 #if defined(MBEDTLS_SHA256_C)
marcozecchini 0:9fca2b23d0ba 153 Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler),
marcozecchini 0:9fca2b23d0ba 154 Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler),
marcozecchini 0:9fca2b23d0ba 155 #endif
marcozecchini 0:9fca2b23d0ba 156 };
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
marcozecchini 0:9fca2b23d0ba 159 GREENTEA_SETUP(10, "default_auto");
marcozecchini 0:9fca2b23d0ba 160 return greentea_test_setup_handler(number_of_cases);
marcozecchini 0:9fca2b23d0ba 161 }
marcozecchini 0:9fca2b23d0ba 162
marcozecchini 0:9fca2b23d0ba 163 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 int main() {
marcozecchini 0:9fca2b23d0ba 166 Harness::run(specification);
marcozecchini 0:9fca2b23d0ba 167 }