Add hmac test vector test program
This commit is contained in:
		
							parent
							
								
									c126fa6430
								
							
						
					
					
						commit
						1292829a07
					
				
					 3 changed files with 255 additions and 0 deletions
				
			
		
							
								
								
									
										128
									
								
								examples/tests/hmac_test_vectors/FreeRTOSConfig.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								examples/tests/hmac_test_vectors/FreeRTOSConfig.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,128 @@ | |||
| /*
 | ||||
|     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd. | ||||
| 
 | ||||
|     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
 | ||||
| 
 | ||||
|     *************************************************************************** | ||||
|      *                                                                       * | ||||
|      *    FreeRTOS provides completely free yet professionally developed,    * | ||||
|      *    robust, strictly quality controlled, supported, and cross          * | ||||
|      *    platform software that has become a de facto standard.             * | ||||
|      *                                                                       * | ||||
|      *    Help yourself get started quickly and support the FreeRTOS         * | ||||
|      *    project by purchasing a FreeRTOS tutorial book, reference          * | ||||
|      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *
 | ||||
|      *                                                                       * | ||||
|      *    Thank you!                                                         * | ||||
|      *                                                                       * | ||||
|     *************************************************************************** | ||||
| 
 | ||||
|     This file is part of the FreeRTOS distribution. | ||||
| 
 | ||||
|     FreeRTOS is free software; you can redistribute it and/or modify it under | ||||
|     the terms of the GNU General Public License (version 2) as published by the | ||||
|     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. | ||||
| 
 | ||||
|     >>! NOTE: The modification to the GPL is included to allow you to distribute | ||||
|     >>! a combined work that includes FreeRTOS without being obliged to provide | ||||
|     >>! the source code for proprietary components outside of the FreeRTOS | ||||
|     >>! kernel. | ||||
| 
 | ||||
|     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY | ||||
|     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||
|     FOR A PARTICULAR PURPOSE.  Full license text is available from the following | ||||
|     link: http://www.freertos.org/a00114.html
 | ||||
| 
 | ||||
|     1 tab == 4 spaces! | ||||
| 
 | ||||
|     *************************************************************************** | ||||
|      *                                                                       * | ||||
|      *    Having a problem?  Start by reading the FAQ "My application does   * | ||||
|      *    not run, what could be wrong?"                                     * | ||||
|      *                                                                       * | ||||
|      *    http://www.FreeRTOS.org/FAQHelp.html                               *
 | ||||
|      *                                                                       * | ||||
|     *************************************************************************** | ||||
| 
 | ||||
|     http://www.FreeRTOS.org - Documentation, books, training, latest versions,
 | ||||
|     license and Real Time Engineers Ltd. contact details. | ||||
| 
 | ||||
|     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
 | ||||
|     including FreeRTOS+Trace - an indispensable productivity tool, a DOS | ||||
|     compatible FAT file system, and our tiny thread aware UDP/IP stack. | ||||
| 
 | ||||
|     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
 | ||||
|     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS | ||||
|     licenses offer ticketed support, indemnification and middleware. | ||||
| 
 | ||||
|     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
 | ||||
|     engineered and independently SIL3 certified version for use in safety and | ||||
|     mission critical applications that require provable dependability. | ||||
| 
 | ||||
|     1 tab == 4 spaces! | ||||
| */ | ||||
| 
 | ||||
| #ifndef FREERTOS_CONFIG_H | ||||
| #define FREERTOS_CONFIG_H | ||||
| 
 | ||||
| /*-----------------------------------------------------------
 | ||||
|  * Application specific definitions. | ||||
|  * | ||||
|  * These definitions should be adjusted for your particular hardware and | ||||
|  * application requirements. | ||||
|  * | ||||
|  * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE | ||||
|  * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.  | ||||
|  * | ||||
|  * See http://www.freertos.org/a00110.html.
 | ||||
|  *----------------------------------------------------------*/ | ||||
| 
 | ||||
| #define configUSE_PREEMPTION		1 | ||||
| #define configUSE_IDLE_HOOK			0 | ||||
| #define configUSE_TICK_HOOK			0 | ||||
| #define configCPU_CLOCK_HZ			( ( unsigned long ) 80000000 ) | ||||
| #define configTICK_RATE_HZ			( ( portTickType ) 100 ) | ||||
| #define configMAX_PRIORITIES		( ( unsigned portBASE_TYPE ) 15 ) | ||||
| #define configMINIMAL_STACK_SIZE	( ( unsigned short )256 ) | ||||
| #define configTOTAL_HEAP_SIZE		( ( size_t ) ( 32 * 1024 ) ) | ||||
| #define configMAX_TASK_NAME_LEN		( 16 ) | ||||
| #define configUSE_TRACE_FACILITY	0 | ||||
| #define configUSE_STATS_FORMATTING_FUNCTIONS 0 | ||||
| #define configUSE_16_BIT_TICKS		0 | ||||
| #define configIDLE_SHOULD_YIELD		1 | ||||
| #define configUSE_NEWLIB_REENTRANT      0 | ||||
| 
 | ||||
| #define INCLUDE_xTaskGetIdleTaskHandle 1 | ||||
| #define INCLUDE_xTimerGetTimerDaemonTaskHandle 1 | ||||
| 
 | ||||
| #define configCHECK_FOR_STACK_OVERFLOW  2 | ||||
| #define configUSE_MUTEXES  1 | ||||
| #define configUSE_TIMERS    1 | ||||
| 
 | ||||
| #if configUSE_TIMERS | ||||
| #define configTIMER_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) | ||||
| #define configTIMER_QUEUE_LENGTH (10) | ||||
| #define configTIMER_TASK_STACK_DEPTH  ( ( unsigned short ) 512 ) | ||||
| #endif | ||||
| 
 | ||||
| /* Co-routine definitions. */ | ||||
| #define configUSE_CO_ROUTINES 		0 | ||||
| #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) | ||||
| 
 | ||||
| /* Set the following definitions to 1 to include the API function, or zero
 | ||||
| to exclude the API function. */ | ||||
| 
 | ||||
| #define INCLUDE_vTaskPrioritySet		1 | ||||
| #define INCLUDE_uxTaskPriorityGet		1 | ||||
| #define INCLUDE_vTaskDelete				1 | ||||
| #define INCLUDE_vTaskCleanUpResources	0 | ||||
| #define INCLUDE_vTaskSuspend			1 | ||||
| #define INCLUDE_vTaskDelayUntil			1 | ||||
| #define INCLUDE_vTaskDelay				1 | ||||
| 
 | ||||
| /*set the #define for debug info*/ | ||||
| #define INCLUDE_xTaskGetCurrentTaskHandle 1 | ||||
| #define INCLUDE_uxTaskGetStackHighWaterMark 1 | ||||
| 
 | ||||
| #endif /* FREERTOS_CONFIG_H */ | ||||
| 
 | ||||
							
								
								
									
										2
									
								
								examples/tests/hmac_test_vectors/Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/tests/hmac_test_vectors/Makefile
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | |||
| TARGET=hmac_test | ||||
| include ../../../common.mk | ||||
							
								
								
									
										125
									
								
								examples/tests/hmac_test_vectors/main.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								examples/tests/hmac_test_vectors/main.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,125 @@ | |||
| /*
 | ||||
|  * | ||||
|  * Test program to verify HMAC test vectors from RFC2202 | ||||
|  * http://www.rfc-editor.org/rfc/rfc2202.txt
 | ||||
|  * | ||||
|  * Currently only does MD5 vectors not SHA1. | ||||
|  * | ||||
|  * This sample code is in the public domain., | ||||
|  */ | ||||
| #include "espressif/esp_common.h" | ||||
| #include "FreeRTOS.h" | ||||
| #include "ssl.h" | ||||
| #include "espressif/sdk_prototypes.h" | ||||
| 
 | ||||
| struct test_vector { | ||||
|     const uint8_t *key; | ||||
|     const uint8_t key_len; | ||||
|     const uint8_t *data; | ||||
|     const uint8_t data_len; | ||||
|     const uint8_t *digest; | ||||
| }; | ||||
| 
 | ||||
| static const uint8_t aa_80_times[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | ||||
| 				      0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa}; | ||||
| 
 | ||||
| /* NOTE: Vectors 6 & 7 currently cause a fault as the axTLS
 | ||||
|    routines don't support keys longer than the block size. */ | ||||
| const uint8_t NUM_MD5_VECTORS = 5; | ||||
| 
 | ||||
| static const struct test_vector md5_vectors[] = { | ||||
|     { /* vector 1*/ | ||||
| 	.key =           (const uint8_t *)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", | ||||
| 	.key_len =       16, | ||||
| 	.data =          (const uint8_t *)"Hi There", | ||||
| 	.data_len =      8, | ||||
| 	.digest =        (const uint8_t *)"\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d", | ||||
|     }, | ||||
|     { /* vector 2 */ | ||||
| 	.key =           (const uint8_t *)"Jefe", | ||||
| 	.key_len =       4, | ||||
| 	.data =          (const uint8_t *)"what do ya want for nothing?", | ||||
| 	.data_len =      28, | ||||
| 	.digest =        (const uint8_t *)"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38", | ||||
|     }, | ||||
|     { /* vector 3 */ | ||||
| 	.key =           (const uint8_t *)"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", | ||||
| 	.key_len =       16, | ||||
| 	.data =          (const uint8_t *)"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", | ||||
| 	.data_len =      50, | ||||
| 	.digest =        (const uint8_t *)"\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6", | ||||
|     }, | ||||
|     { /* vector 4*/ | ||||
| 	.key =           (const uint8_t *)"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", | ||||
| 	.key_len =         25, | ||||
| 	.data =          (const uint8_t *)"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd", | ||||
| 	.data_len =      50, | ||||
| 	.digest =        (const uint8_t *)"\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79", | ||||
|     }, | ||||
|     { /* test case 5 */ | ||||
| 	.key =           (const uint8_t *)"\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c", | ||||
| 	.key_len =       16, | ||||
| 	.data =          (const uint8_t *)"Test With Truncation", | ||||
| 	.data_len =      20, | ||||
| 	.digest =        (const uint8_t *)"\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c", | ||||
|     }, | ||||
|     { /* test case 6 */ | ||||
| 	.key =           aa_80_times, | ||||
| 	.key_len =       80, | ||||
| 	.data =          (const uint8_t *)"Test Using Larger Than Block-Size Key - Hash Key First", | ||||
| 	.data_len =      54, | ||||
| 	.digest =        (const uint8_t *)"\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd", | ||||
|     }, | ||||
|     { | ||||
| 	/* test case 7 */ | ||||
| 	.key =           aa_80_times, | ||||
| 	.key_len =       80, | ||||
| 	.data =          (const uint8_t *)"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", | ||||
| 	.data_len =      73, | ||||
| 	.digest =        (const uint8_t *)"\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e", | ||||
|     }, | ||||
| }; | ||||
| 
 | ||||
| static void test_md5(void) | ||||
| { | ||||
|     printf("\r\nTesting MD5 vectors...\r\n"); | ||||
|     uint8_t test_digest[16]; | ||||
|     for(int i = 0; i < NUM_MD5_VECTORS; i++) { | ||||
| 	const struct test_vector *vector = &md5_vectors[i]; | ||||
| 	printf("Test case %d: ", i+1); | ||||
| 	hmac_md5(vector->data, vector->data_len, vector->key, vector->key_len, test_digest); | ||||
| 	if(memcmp(test_digest, vector->digest, 16)) { | ||||
| 	    uint8_t first = 0; | ||||
| 	    for(first = 0; first < 16; first++) { | ||||
| 		if(test_digest[first] != vector->digest[first]) { | ||||
| 		    break; | ||||
| 		} | ||||
| 	    } | ||||
| 	    printf("FAIL, first difference at byte %d\r\n", first); | ||||
| 	    print_blob("key", vector->key, vector->key_len); | ||||
| 	    print_blob("data", vector->data, vector->data_len); | ||||
| 	    print_blob("expected digest", vector->digest, 16); | ||||
| 	    print_blob("actual digest", test_digest, 16); | ||||
| 	} | ||||
| 	else { | ||||
| 	    printf("PASS\r\n"); | ||||
| 	} | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void user_init(void) | ||||
| { | ||||
|     sdk_uart_div_modify(0, UART_CLK_FREQ / 115200); | ||||
| 
 | ||||
|     test_md5(); | ||||
| 
 | ||||
|     printf("Done.\r\n"); | ||||
|     while(1) { | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue