Advertisement
Guest User

HW_AES attempt based on snip/crypto/crypto.c with changes

a guest
Mar 5th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.82 KB | None | 0 0
  1. /*
  2.  * Copyright 2017, Cypress Semiconductor Corporation or a subsidiary of
  3.  * Cypress Semiconductor Corporation. All Rights Reserved.
  4.  *
  5.  * This software, associated documentation and materials ("Software"),
  6.  * is owned by Cypress Semiconductor Corporation
  7.  * or one of its subsidiaries ("Cypress") and is protected by and subject to
  8.  * worldwide patent protection (United States and foreign),
  9.  * United States copyright laws and international treaty provisions.
  10.  * Therefore, you may use this Software only as provided in the license
  11.  * agreement accompanying the software package from which you
  12.  * obtained this Software ("EULA").
  13.  * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
  14.  * non-transferable license to copy, modify, and compile the Software
  15.  * source code solely for use in connection with Cypress's
  16.  * integrated circuit products. Any reproduction, modification, translation,
  17.  * compilation, or representation of this Software except as specified
  18.  * above is prohibited without the express written permission of Cypress.
  19.  *
  20.  * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
  21.  * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
  22.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
  23.  * reserves the right to make changes to the Software without notice. Cypress
  24.  * does not assume any liability arising out of the application or use of the
  25.  * Software or any product or circuit described in the Software. Cypress does
  26.  * not authorize its products for use in any products where a malfunction or
  27.  * failure of the Cypress product may reasonably be expected to result in
  28.  * significant property damage, injury or death ("High Risk Product"). By
  29.  * including Cypress's product in a High Risk Product, the manufacturer
  30.  * of such system or application assumes all risk of such use and in doing
  31.  * so agrees to indemnify Cypress against all liability.
  32.  */
  33.  
  34. /** @file
  35.  *
  36.  * Crypto Test Application
  37.  *
  38.  * This application demonstrates how to use crypto functions available
  39.  * in the WICED crypto library
  40.  *
  41.  * Features demonstrated
  42.  *  - AES encrypt/decrypt
  43.  *  - MD5, SHA1 and SHA256 Hashing and HMAC functionality
  44.  *
  45.  * Application Instructions
  46.  *   Connect a PC terminal to the serial port of the WICED Eval board,
  47.  *   then build and download the application as described in the WICED
  48.  *   Quick Start Guide
  49.  *
  50.  *   The app exercises various crypto functions and prints
  51.  *   the results to the UART.
  52.  *
  53.  */
  54.  
  55. /*
  56.  
  57.  
  58.  
  59.     AES-CBC test vectors from http://www.ietf.org/rfc/rfc3602.txt
  60.  
  61.     Test Vectors (Trailing '\0' of a character string not included in test):
  62.  
  63.     Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
  64.  
  65.     Key       : 0x06a9214036b8a15b512e03d534120006
  66.     IV        : 0x3dafba429d9eb430b422da802c9fac41
  67.     Plaintext : "Single block msg"
  68.     Ciphertext: 0xe353779c1079aeb82708942dbe77181a
  69.  
  70.     Case #2: Encrypting 32 bytes (2 blocks) using AES-CBC with 128-bit key
  71.     Key       : 0xc286696d887c9aa0611bbb3e2025a45a
  72.     IV        : 0x562e17996d093d28ddb3ba695a2e6f58
  73.     Plaintext : 0x000102030405060708090a0b0c0d0e0f
  74.                   101112131415161718191a1b1c1d1e1f
  75.     Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
  76.                   7586602d253cfff91b8266bea6d61ab1
  77.  
  78.     Case #3: Encrypting 48 bytes (3 blocks) using AES-CBC with 128-bit key
  79.     Key       : 0x6c3ea0477630ce21a2ce334aa746c2cd
  80.     IV        : 0xc782dc4c098c66cbd9cd27d825682c81
  81.     Plaintext : "This is a 48-byte message (exactly 3 AES blocks)"
  82.     Ciphertext: 0xd0a02b3836451753d493665d33f0e886
  83.                   2dea54cdb293abc7506939276772f8d5
  84.                   021c19216bad525c8579695d83ba2684
  85.  
  86.     Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key
  87.     Key       : 0x56e47a38c5598974bc46903dba290349
  88.     IV        : 0x8ce82eefbea0da3c44699ed7db51b7d9
  89.     Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
  90.                   b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
  91.                   c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
  92.                   d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
  93.     Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
  94.                   0f3af07a9a31a9c684db207eb0ef8e4e
  95.                   35907aa632c3ffdf868bb7b29d3d46ad
  96.                   83ce9f9a102ee99d49a53e87f4c3da55
  97.  
  98. */
  99.  
  100.  
  101. #include <stdlib.h>
  102. #include "wiced.h"
  103. #include "mbedtls/sha1.h"
  104. #include "mbedtls/aes.h"
  105. #include "mbedtls/md5.h"
  106. #include "wwd_debug.h"
  107. #include "string.h"
  108. #include "wiced_time.h"
  109.  
  110. #include <crypto_core.h>
  111. #include <crypto_api.h>
  112.  
  113. /******************************************************
  114.  *                      Macros
  115.  ******************************************************/
  116.  
  117. /******************************************************
  118.  *                    Constants
  119.  ******************************************************/
  120.  
  121. #define NUM_OF_AES_CBC_TESTS     4
  122.  
  123. #define AES_CBC_IV_LENGTH        16              /* Length of the AES-CBC initialization vector in octets */
  124. #define AES_CBC_KEY_LENGTH       128             /* Length of the AES-CBC key in bits */
  125.  
  126. /******************************************************
  127.  *                   Enumerations
  128.  ******************************************************/
  129.  
  130. /******************************************************
  131.  *                 Type Definitions
  132.  ******************************************************/
  133.  
  134. /******************************************************
  135.  *                    Structures
  136.  ******************************************************/
  137.  
  138. /******************************************************
  139.  *               Static Function Declarations
  140.  ******************************************************/
  141.  
  142.  
  143. void aes_cbc_test       (void);
  144. void dump_bytes         (const uint8_t* bptr, uint32_t len);
  145.  
  146. /******************************************************
  147.  *               Variable Definitions
  148.  ******************************************************/
  149.  
  150. aes_context_t context_aes;
  151. sha1_context  context_sha1;
  152. sha2_context  context_sha2;
  153. md5_context   context_md5;
  154.  
  155. hw_aes_context_t hw_context_aes;
  156.  
  157. int pass_count = 0, fail_count = 0;
  158.  
  159.  
  160.  
  161. struct aes_cbc_test_case {
  162.     int num;
  163.     char label[32];
  164.     uint8_t hex_key[16];
  165.     uint8_t iv[16];
  166.     int key_len;
  167.     uint8_t hex_plain_text[128];
  168.     char* char_plain_text;
  169.     int data_len;
  170.     uint8_t cipher_text[128];
  171. } aes_cbc_test_cases[NUM_OF_AES_CBC_TESTS] = {
  172.     {
  173.         .num = 1,
  174.         .label = "Test Case AES-CBC-1",
  175.         .hex_key = {
  176.                 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
  177.                 0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06,
  178.             },
  179.         .iv = {
  180.                 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
  181.                 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41,
  182.             },
  183.         .key_len = 16,
  184.         .hex_plain_text = { 0 },
  185.         .char_plain_text = "Single block msg",
  186.         .data_len = 16,
  187.         .cipher_text = {
  188.                 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
  189.                 0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a,
  190.             },
  191.     },
  192.     {
  193.         .num = 2,
  194.         .label = "Test Case AES-CBC-2",
  195.         .hex_key = {
  196.                 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
  197.                 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a,
  198.             },
  199.         .iv = {
  200.                 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
  201.                 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58,
  202.             },
  203.         .key_len = 16,
  204.         .hex_plain_text = {
  205.                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  206.                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  207.                 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  208.                 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  209.             },
  210.         .char_plain_text = NULL,
  211.         .data_len = 32,
  212.         .cipher_text = {
  213.                 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
  214.                 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
  215.                 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
  216.                 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1,
  217.             },
  218.     },
  219.     {
  220.         .num = 3,
  221.         .label = "Test Case AES-CBC-3",
  222.         .hex_key = {
  223.                 0x6c, 0x3e, 0xa0, 0x47, 0x76, 0x30, 0xce, 0x21,
  224.                 0xa2, 0xce, 0x33, 0x4a, 0xa7, 0x46, 0xc2, 0xcd,
  225.             },
  226.         .iv = {
  227.                 0xc7, 0x82, 0xdc, 0x4c, 0x09, 0x8c, 0x66, 0xcb,
  228.                 0xd9, 0xcd, 0x27, 0xd8, 0x25, 0x68, 0x2c, 0x81,
  229.             },
  230.         .key_len = 16,
  231.         .hex_plain_text = { 0 },
  232.         .char_plain_text = "This is a 48-byte message (exactly 3 AES blocks)",
  233.         .data_len = 48,
  234.         .cipher_text = {
  235.                 0xd0, 0xa0, 0x2b, 0x38, 0x36, 0x45, 0x17, 0x53,
  236.                 0xd4, 0x93, 0x66, 0x5d, 0x33, 0xf0, 0xe8, 0x86,
  237.                 0x2d, 0xea, 0x54, 0xcd, 0xb2, 0x93, 0xab, 0xc7,
  238.                 0x50, 0x69, 0x39, 0x27, 0x67, 0x72, 0xf8, 0xd5,
  239.                 0x02, 0x1c, 0x19, 0x21, 0x6b, 0xad, 0x52, 0x5c,
  240.                 0x85, 0x79, 0x69, 0x5d, 0x83, 0xba, 0x26, 0x84
  241.             },
  242.     },
  243.     {
  244.         .num = 4,
  245.         .label = "Test Case AES-CBC-4",
  246.         .hex_key = {
  247.                 0x56, 0xe4, 0x7a, 0x38, 0xc5, 0x59, 0x89, 0x74,
  248.                 0xbc, 0x46, 0x90, 0x3d, 0xba, 0x29, 0x03, 0x49,
  249.             },
  250.         .iv = {
  251.                 0x8c, 0xe8, 0x2e, 0xef, 0xbe, 0xa0, 0xda, 0x3c,
  252.                 0x44, 0x69, 0x9e, 0xd7, 0xdb, 0x51, 0xb7, 0xd9,
  253.             },
  254.         .key_len = 16,
  255.         .hex_plain_text = {
  256.                 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  257.                 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  258.                 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  259.                 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  260.                 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  261.                 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  262.                 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  263.                 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  264.             },
  265.         .char_plain_text = NULL,
  266.         .data_len = 64,
  267.         .cipher_text = {
  268.                 0xc3, 0x0e, 0x32, 0xff, 0xed, 0xc0, 0x77, 0x4e,
  269.                 0x6a, 0xff, 0x6a, 0xf0, 0x86, 0x9f, 0x71, 0xaa,
  270.                 0x0f, 0x3a, 0xf0, 0x7a, 0x9a, 0x31, 0xa9, 0xc6,
  271.                 0x84, 0xdb, 0x20, 0x7e, 0xb0, 0xef, 0x8e, 0x4e,
  272.                 0x35, 0x90, 0x7a, 0xa6, 0x32, 0xc3, 0xff, 0xdf,
  273.                 0x86, 0x8b, 0xb7, 0xb2, 0x9d, 0x3d, 0x46, 0xad,
  274.                 0x83, 0xce, 0x9f, 0x9a, 0x10, 0x2e, 0xe9, 0x9d,
  275.                 0x49, 0xa5, 0x3e, 0x87, 0xf4, 0xc3, 0xda, 0x55,
  276.             },
  277.     },
  278. };
  279.  
  280.  
  281. /******************************************************
  282.  *               Function Definitions
  283.  ******************************************************/
  284.  
  285.  
  286. void aes_cbc_test(void)
  287. {
  288.     wiced_time_t t1, t2, i;
  289.     uint8_t cipher_text[64];
  290.     uint8_t plain_text[64];
  291.     uint8_t iv[16];
  292.     char *plain_text_ptr;
  293.  
  294.  
  295.  
  296.     for (i = 0; i < 1; i++)
  297.     {
  298.         /* Test encryption */
  299.         WPRINT_APP_INFO( ( "\n%s\n", aes_cbc_test_cases[i].label ) );
  300.         memcpy(iv, aes_cbc_test_cases[i].iv, AES_CBC_IV_LENGTH);
  301.         memset(&hw_context_aes, 0, sizeof(hw_context_aes));
  302.         hw_aes_setkey_enc(&hw_context_aes, aes_cbc_test_cases[i].hex_key, AES_CBC_KEY_LENGTH);
  303.  
  304.         plain_text_ptr = aes_cbc_test_cases[i].char_plain_text;
  305.         dump_bytes( (uint8_t*)plain_text_ptr, aes_cbc_test_cases[i].data_len );
  306.  
  307.         /*
  308.         WPRINT_APP_INFO( ( "\nPlain text: " ) );
  309.         if ( aes_cbc_test_cases[i].char_plain_text != NULL )
  310.         {
  311.             plain_text_ptr = aes_cbc_test_cases[i].char_plain_text;
  312.             WPRINT_APP_INFO( ( "\"" ) );
  313.             WPRINT_APP_INFO( ( "%s", plain_text_ptr ) );
  314.             WPRINT_APP_INFO( ( "\"\n" ) );
  315.         }
  316.         else
  317.         {
  318.             plain_text_ptr = (char*)aes_cbc_test_cases[i].hex_plain_text;
  319.             dump_bytes( (uint8_t*)plain_text_ptr, aes_cbc_test_cases[i].data_len );
  320.         }
  321.         */
  322.  
  323.         wiced_time_get_time( &t1 );
  324.         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);
  325.         wiced_time_get_time( &t2 );
  326.         t2 = t2 - t1;
  327.  
  328.         WPRINT_APP_INFO( ( "\nResulting Cipher Text:" ) );
  329.         dump_bytes( cipher_text, aes_cbc_test_cases[i].data_len);
  330.  
  331.         /* Test decryption */
  332.         memcpy(iv, aes_cbc_test_cases[i].iv, AES_CBC_IV_LENGTH);
  333.         memset(&hw_context_aes, 0, sizeof(hw_context_aes));
  334.         hw_aes_setkey_dec(&hw_context_aes, aes_cbc_test_cases[i].hex_key, AES_CBC_KEY_LENGTH);
  335.         hw_aes_crypt_cbc(&hw_context_aes, HW_AES_DECRYPT, aes_cbc_test_cases[i].data_len, iv, cipher_text, plain_text );
  336.  
  337.         WPRINT_APP_INFO(("\nCompare these:\n"));
  338.         dump_bytes((const uint8_t*)plain_text_ptr, aes_cbc_test_cases[0].data_len);
  339.         dump_bytes(plain_text, aes_cbc_test_cases[0].data_len);
  340.     }
  341. }
  342.  
  343. void application_start( )
  344. {
  345.     /* Hash, HMAC and AES tests */
  346.     //sw_md5_test();
  347.     //sw_sha1_test();
  348.     //sw_sha256_test();
  349.     //sw_md5_hmac_test();
  350.     //sw_sha1_hmac_test();
  351.     //sw_hmac_sha256_test();
  352.  
  353.     platform_hwcrypto_init();
  354.  
  355.     aes_cbc_test();
  356.  
  357.     //WPRINT_APP_INFO( ( "\nPassed %d tests\n", pass_count ) );
  358.     //WPRINT_APP_INFO( ( "Failed %d tests\n", fail_count ) );
  359. }
  360.  
  361.  
  362. void dump_bytes(const uint8_t* bptr, uint32_t len)
  363. {
  364.     int i = 0;
  365.  
  366.     for (i = 0; i < len; )
  367.     {
  368.         if ((i & 0x0f) == 0)
  369.         {
  370.             WPRINT_APP_INFO( ( "\n" ) );
  371.         }
  372.         else if ((i & 0x07) == 0)
  373.         {
  374.             WPRINT_APP_INFO( (" ") );
  375.         }
  376.         WPRINT_APP_INFO( ( "%02x ", bptr[i++] ) );
  377.     }
  378.     WPRINT_APP_INFO( ( "\n" ) );
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement