Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2017, Cypress Semiconductor Corporation or a subsidiary of
- * Cypress Semiconductor Corporation. All Rights Reserved.
- *
- * This software, associated documentation and materials ("Software"),
- * is owned by Cypress Semiconductor Corporation
- * or one of its subsidiaries ("Cypress") and is protected by and subject to
- * worldwide patent protection (United States and foreign),
- * United States copyright laws and international treaty provisions.
- * Therefore, you may use this Software only as provided in the license
- * agreement accompanying the software package from which you
- * obtained this Software ("EULA").
- * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
- * non-transferable license to copy, modify, and compile the Software
- * source code solely for use in connection with Cypress's
- * integrated circuit products. Any reproduction, modification, translation,
- * compilation, or representation of this Software except as specified
- * above is prohibited without the express written permission of Cypress.
- *
- * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
- * reserves the right to make changes to the Software without notice. Cypress
- * does not assume any liability arising out of the application or use of the
- * Software or any product or circuit described in the Software. Cypress does
- * not authorize its products for use in any products where a malfunction or
- * failure of the Cypress product may reasonably be expected to result in
- * significant property damage, injury or death ("High Risk Product"). By
- * including Cypress's product in a High Risk Product, the manufacturer
- * of such system or application assumes all risk of such use and in doing
- * so agrees to indemnify Cypress against all liability.
- */
- /** @file
- *
- * Crypto Test Application
- *
- * This application demonstrates how to use crypto functions available
- * in the WICED crypto library
- *
- * Features demonstrated
- * - AES encrypt/decrypt
- * - MD5, SHA1 and SHA256 Hashing and HMAC functionality
- *
- * Application Instructions
- * Connect a PC terminal to the serial port of the WICED Eval board,
- * then build and download the application as described in the WICED
- * Quick Start Guide
- *
- * The app exercises various crypto functions and prints
- * the results to the UART.
- *
- */
- /*
- AES-CBC test vectors from http://www.ietf.org/rfc/rfc3602.txt
- Test Vectors (Trailing '\0' of a character string not included in test):
- Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
- Key : 0x06a9214036b8a15b512e03d534120006
- IV : 0x3dafba429d9eb430b422da802c9fac41
- Plaintext : "Single block msg"
- Ciphertext: 0xe353779c1079aeb82708942dbe77181a
- Case #2: Encrypting 32 bytes (2 blocks) using AES-CBC with 128-bit key
- Key : 0xc286696d887c9aa0611bbb3e2025a45a
- IV : 0x562e17996d093d28ddb3ba695a2e6f58
- Plaintext : 0x000102030405060708090a0b0c0d0e0f
- 101112131415161718191a1b1c1d1e1f
- Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
- 7586602d253cfff91b8266bea6d61ab1
- Case #3: Encrypting 48 bytes (3 blocks) using AES-CBC with 128-bit key
- Key : 0x6c3ea0477630ce21a2ce334aa746c2cd
- IV : 0xc782dc4c098c66cbd9cd27d825682c81
- Plaintext : "This is a 48-byte message (exactly 3 AES blocks)"
- Ciphertext: 0xd0a02b3836451753d493665d33f0e886
- 2dea54cdb293abc7506939276772f8d5
- 021c19216bad525c8579695d83ba2684
- Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key
- Key : 0x56e47a38c5598974bc46903dba290349
- IV : 0x8ce82eefbea0da3c44699ed7db51b7d9
- Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
- b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
- c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
- d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
- Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
- 0f3af07a9a31a9c684db207eb0ef8e4e
- 35907aa632c3ffdf868bb7b29d3d46ad
- 83ce9f9a102ee99d49a53e87f4c3da55
- */
- #include <stdlib.h>
- #include "wiced.h"
- #include "mbedtls/sha1.h"
- #include "mbedtls/aes.h"
- #include "mbedtls/md5.h"
- #include "wwd_debug.h"
- #include "string.h"
- #include "wiced_time.h"
- #include <crypto_core.h>
- #include <crypto_api.h>
- /******************************************************
- * Macros
- ******************************************************/
- /******************************************************
- * Constants
- ******************************************************/
- #define NUM_OF_AES_CBC_TESTS 4
- #define AES_CBC_IV_LENGTH 16 /* Length of the AES-CBC initialization vector in octets */
- #define AES_CBC_KEY_LENGTH 128 /* Length of the AES-CBC key in bits */
- /******************************************************
- * Enumerations
- ******************************************************/
- /******************************************************
- * Type Definitions
- ******************************************************/
- /******************************************************
- * Structures
- ******************************************************/
- /******************************************************
- * Static Function Declarations
- ******************************************************/
- void aes_cbc_test (void);
- void dump_bytes (const uint8_t* bptr, uint32_t len);
- /******************************************************
- * Variable Definitions
- ******************************************************/
- aes_context_t context_aes;
- sha1_context context_sha1;
- sha2_context context_sha2;
- md5_context context_md5;
- hw_aes_context_t hw_context_aes;
- int pass_count = 0, fail_count = 0;
- struct aes_cbc_test_case {
- int num;
- char label[32];
- uint8_t hex_key[16];
- uint8_t iv[16];
- int key_len;
- uint8_t hex_plain_text[128];
- char* char_plain_text;
- int data_len;
- uint8_t cipher_text[128];
- } aes_cbc_test_cases[NUM_OF_AES_CBC_TESTS] = {
- {
- .num = 1,
- .label = "Test Case AES-CBC-1",
- .hex_key = {
- 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
- 0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06,
- },
- .iv = {
- 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
- 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41,
- },
- .key_len = 16,
- .hex_plain_text = { 0 },
- .char_plain_text = "Single block msg",
- .data_len = 16,
- .cipher_text = {
- 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
- 0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a,
- },
- },
- {
- .num = 2,
- .label = "Test Case AES-CBC-2",
- .hex_key = {
- 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
- 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a,
- },
- .iv = {
- 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
- 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58,
- },
- .key_len = 16,
- .hex_plain_text = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- },
- .char_plain_text = NULL,
- .data_len = 32,
- .cipher_text = {
- 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
- 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
- 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
- 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1,
- },
- },
- {
- .num = 3,
- .label = "Test Case AES-CBC-3",
- .hex_key = {
- 0x6c, 0x3e, 0xa0, 0x47, 0x76, 0x30, 0xce, 0x21,
- 0xa2, 0xce, 0x33, 0x4a, 0xa7, 0x46, 0xc2, 0xcd,
- },
- .iv = {
- 0xc7, 0x82, 0xdc, 0x4c, 0x09, 0x8c, 0x66, 0xcb,
- 0xd9, 0xcd, 0x27, 0xd8, 0x25, 0x68, 0x2c, 0x81,
- },
- .key_len = 16,
- .hex_plain_text = { 0 },
- .char_plain_text = "This is a 48-byte message (exactly 3 AES blocks)",
- .data_len = 48,
- .cipher_text = {
- 0xd0, 0xa0, 0x2b, 0x38, 0x36, 0x45, 0x17, 0x53,
- 0xd4, 0x93, 0x66, 0x5d, 0x33, 0xf0, 0xe8, 0x86,
- 0x2d, 0xea, 0x54, 0xcd, 0xb2, 0x93, 0xab, 0xc7,
- 0x50, 0x69, 0x39, 0x27, 0x67, 0x72, 0xf8, 0xd5,
- 0x02, 0x1c, 0x19, 0x21, 0x6b, 0xad, 0x52, 0x5c,
- 0x85, 0x79, 0x69, 0x5d, 0x83, 0xba, 0x26, 0x84
- },
- },
- {
- .num = 4,
- .label = "Test Case AES-CBC-4",
- .hex_key = {
- 0x56, 0xe4, 0x7a, 0x38, 0xc5, 0x59, 0x89, 0x74,
- 0xbc, 0x46, 0x90, 0x3d, 0xba, 0x29, 0x03, 0x49,
- },
- .iv = {
- 0x8c, 0xe8, 0x2e, 0xef, 0xbe, 0xa0, 0xda, 0x3c,
- 0x44, 0x69, 0x9e, 0xd7, 0xdb, 0x51, 0xb7, 0xd9,
- },
- .key_len = 16,
- .hex_plain_text = {
- 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
- 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
- 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
- 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
- 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
- 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
- 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
- },
- .char_plain_text = NULL,
- .data_len = 64,
- .cipher_text = {
- 0xc3, 0x0e, 0x32, 0xff, 0xed, 0xc0, 0x77, 0x4e,
- 0x6a, 0xff, 0x6a, 0xf0, 0x86, 0x9f, 0x71, 0xaa,
- 0x0f, 0x3a, 0xf0, 0x7a, 0x9a, 0x31, 0xa9, 0xc6,
- 0x84, 0xdb, 0x20, 0x7e, 0xb0, 0xef, 0x8e, 0x4e,
- 0x35, 0x90, 0x7a, 0xa6, 0x32, 0xc3, 0xff, 0xdf,
- 0x86, 0x8b, 0xb7, 0xb2, 0x9d, 0x3d, 0x46, 0xad,
- 0x83, 0xce, 0x9f, 0x9a, 0x10, 0x2e, 0xe9, 0x9d,
- 0x49, 0xa5, 0x3e, 0x87, 0xf4, 0xc3, 0xda, 0x55,
- },
- },
- };
- /******************************************************
- * Function Definitions
- ******************************************************/
- void aes_cbc_test(void)
- {
- wiced_time_t t1, t2, i;
- uint8_t cipher_text[64];
- uint8_t plain_text[64];
- uint8_t iv[16];
- char *plain_text_ptr;
- for (i = 0; i < 1; i++)
- {
- /* Test encryption */
- WPRINT_APP_INFO( ( "\n%s\n", aes_cbc_test_cases[i].label ) );
- memcpy(iv, aes_cbc_test_cases[i].iv, AES_CBC_IV_LENGTH);
- memset(&hw_context_aes, 0, sizeof(hw_context_aes));
- hw_aes_setkey_enc(&hw_context_aes, aes_cbc_test_cases[i].hex_key, AES_CBC_KEY_LENGTH);
- plain_text_ptr = aes_cbc_test_cases[i].char_plain_text;
- dump_bytes( (uint8_t*)plain_text_ptr, aes_cbc_test_cases[i].data_len );
- /*
- WPRINT_APP_INFO( ( "\nPlain text: " ) );
- if ( aes_cbc_test_cases[i].char_plain_text != NULL )
- {
- plain_text_ptr = aes_cbc_test_cases[i].char_plain_text;
- WPRINT_APP_INFO( ( "\"" ) );
- WPRINT_APP_INFO( ( "%s", plain_text_ptr ) );
- WPRINT_APP_INFO( ( "\"\n" ) );
- }
- else
- {
- plain_text_ptr = (char*)aes_cbc_test_cases[i].hex_plain_text;
- dump_bytes( (uint8_t*)plain_text_ptr, aes_cbc_test_cases[i].data_len );
- }
- */
- wiced_time_get_time( &t1 );
- hw_aes_crypt_cbc(&hw_context_aes, HW_AES_ENCRYPT, aes_cbc_test_cases[i].data_len, iv, (unsigned char*)plain_text_ptr, cipher_text);
- wiced_time_get_time( &t2 );
- t2 = t2 - t1;
- WPRINT_APP_INFO( ( "\nResulting Cipher Text:" ) );
- dump_bytes( cipher_text, aes_cbc_test_cases[i].data_len);
- /* Test decryption */
- memcpy(iv, aes_cbc_test_cases[i].iv, AES_CBC_IV_LENGTH);
- memset(&hw_context_aes, 0, sizeof(hw_context_aes));
- hw_aes_setkey_dec(&hw_context_aes, aes_cbc_test_cases[i].hex_key, AES_CBC_KEY_LENGTH);
- hw_aes_crypt_cbc(&hw_context_aes, HW_AES_DECRYPT, aes_cbc_test_cases[i].data_len, iv, cipher_text, plain_text );
- WPRINT_APP_INFO(("\nCompare these:\n"));
- dump_bytes((const uint8_t*)plain_text_ptr, aes_cbc_test_cases[0].data_len);
- dump_bytes(plain_text, aes_cbc_test_cases[0].data_len);
- }
- }
- void application_start( )
- {
- /* Hash, HMAC and AES tests */
- //sw_md5_test();
- //sw_sha1_test();
- //sw_sha256_test();
- //sw_md5_hmac_test();
- //sw_sha1_hmac_test();
- //sw_hmac_sha256_test();
- platform_hwcrypto_init();
- aes_cbc_test();
- //WPRINT_APP_INFO( ( "\nPassed %d tests\n", pass_count ) );
- //WPRINT_APP_INFO( ( "Failed %d tests\n", fail_count ) );
- }
- void dump_bytes(const uint8_t* bptr, uint32_t len)
- {
- int i = 0;
- for (i = 0; i < len; )
- {
- if ((i & 0x0f) == 0)
- {
- WPRINT_APP_INFO( ( "\n" ) );
- }
- else if ((i & 0x07) == 0)
- {
- WPRINT_APP_INFO( (" ") );
- }
- WPRINT_APP_INFO( ( "%02x ", bptr[i++] ) );
- }
- WPRINT_APP_INFO( ( "\n" ) );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement