122 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <stdbool.h>
 | |
| #include <stdint.h>
 | |
| #include <string.h>
 | |
| 
 | |
| #include "hash.h"
 | |
| #include "log.h"
 | |
| #include "global.h"
 | |
| 
 | |
| static bool
 | |
| _test_sha256_fail() {
 | |
|     static const char * test_data = "Data for hash creation ...";
 | |
|     static const uint8_t need_hash[] = {
 | |
|         0xFf, 0x25, 0x84, 0xbc, 0x6f, 0xaf, 0x4a, 0x77,
 | |
|         0xff, 0x32, 0xe7, 0x45, 0x82, 0x62, 0xef, 0x89,
 | |
|         0x08, 0x8e, 0x93, 0x88, 0x64, 0x67, 0xa2, 0xc8,
 | |
|         0x19, 0xbd, 0x99, 0x60, 0xb8, 0x6e, 0xfb, 0x16
 | |
|     };
 | |
| 
 | |
|     uint8_t hash[32];
 | |
|     size_t hash_sz;
 | |
| 
 | |
|     if (IOTELIC_OK != hash_create(HASH_SHA_256, (const uint8_t *)test_data, strlen(test_data),
 | |
|                                   hash, sizeof(hash), &hash_sz)) {
 | |
|         LOG("Can't create hash (%s)", hash_name(HASH_SHA_256));
 | |
|         return false;
 | |
|     }
 | |
|     return 0 == memcmp(hash, need_hash, hash_sz);
 | |
| }
 | |
| 
 | |
| static bool
 | |
| _test_sha256_pass() {
 | |
|     static const char * test_data = "Data for hash creation ...";
 | |
|     static const uint8_t need_hash[] = {
 | |
|             0xef, 0x25, 0x84, 0xbc, 0x6f, 0xaf, 0x4a, 0x77,
 | |
|             0xff, 0x32, 0xe7, 0x45, 0x82, 0x62, 0xef, 0x89,
 | |
|             0x08, 0x8e, 0x93, 0x88, 0x64, 0x67, 0xa2, 0xc8,
 | |
|             0x19, 0xbd, 0x99, 0x60, 0xb8, 0x6e, 0xfb, 0x16
 | |
|     };
 | |
| 
 | |
|     uint8_t hash[32];
 | |
|     size_t hash_sz;
 | |
| 
 | |
|     if (IOTELIC_OK != hash_create(HASH_SHA_256, (const uint8_t *)test_data, strlen(test_data),
 | |
|                                   hash, sizeof(hash), &hash_sz)) {
 | |
|         LOG("Can't create hash (%s)", hash_name(HASH_SHA_256));
 | |
|         return false;
 | |
|     }
 | |
|     return 0 == memcmp(hash, need_hash, hash_sz);
 | |
| }
 | |
| 
 | |
| static bool
 | |
| _test_sha384_pass() {
 | |
|     static const char * test_data = "Data for hash creation ...";
 | |
|     static const uint8_t need_hash[] = {
 | |
|         0x61, 0xf0, 0xbb, 0x30, 0xa9, 0xca, 0x9a, 0xec,
 | |
|         0x94, 0x21, 0xb5, 0xfb, 0xe2, 0x98, 0x0d, 0x60,
 | |
|         0xf2, 0xe3, 0x35, 0x70, 0x8b, 0xf2, 0x14, 0x4b,
 | |
|         0x85, 0x9f, 0xdb, 0x3e, 0xa0, 0xbf, 0x46, 0x2a,
 | |
|         0x6f, 0x5b, 0xc2, 0x1a, 0x44, 0xf7, 0x7c, 0xf2,
 | |
|         0x3b, 0x47, 0xe0, 0x56, 0x27, 0xb9, 0xa5, 0x7b
 | |
|     };
 | |
| 
 | |
|     uint8_t hash[48];
 | |
|     size_t hash_sz;
 | |
| 
 | |
|     if (IOTELIC_OK != hash_create(HASH_SHA_384, (const uint8_t *)test_data, strlen(test_data),
 | |
|                                   hash, sizeof(hash), &hash_sz)) {
 | |
|         LOG("Can't create hash (%s)", hash_name(HASH_SHA_384));
 | |
|         return false;
 | |
|     }
 | |
|     return 0 == memcmp(hash, need_hash, hash_sz);
 | |
| }
 | |
| 
 | |
| static bool
 | |
| _test_sha512_pass() {
 | |
|     static const char * test_data = "Data for hash creation ...";
 | |
|     static const uint8_t need_hash[] = {
 | |
|         0xe7, 0x3c, 0xa0, 0x66, 0xc1, 0x1f, 0x56, 0xf5,
 | |
|         0xd8, 0x35, 0x93, 0x2b, 0xaa, 0xdd, 0xbf, 0x71,
 | |
|         0x0a, 0xb2, 0xbd, 0x1b, 0x51, 0x86, 0xf3, 0x2b,
 | |
|         0x5b, 0xdf, 0xaf, 0x20, 0x50, 0xfe, 0xeb, 0x13,
 | |
|         0x39, 0x17, 0xb1, 0x58, 0xf7, 0x51, 0x4f, 0xd4,
 | |
|         0x61, 0x2e, 0x75, 0xe7, 0x74, 0x8f, 0x59, 0x2a,
 | |
|         0x80, 0xde, 0x87, 0x50, 0x7c, 0x21, 0xae, 0x72,
 | |
|         0x34, 0x16, 0x9f, 0x89, 0x41, 0x1c, 0x34, 0xda
 | |
|     };
 | |
| 
 | |
|     uint8_t hash[64];
 | |
|     size_t hash_sz;
 | |
| 
 | |
|     if (IOTELIC_OK != hash_create(HASH_SHA_512, (const uint8_t *)test_data, strlen(test_data),
 | |
|                                   hash, sizeof(hash), &hash_sz)) {
 | |
|         LOG("Can't create hash (%s)", hash_name(HASH_SHA_512));
 | |
|         return false;
 | |
|     }
 | |
|     return 0 == memcmp(hash, need_hash, hash_sz);
 | |
| }
 | |
| 
 | |
| bool
 | |
| test_hash(void)
 | |
| {
 | |
|     if (!_test_sha256_pass()) {
 | |
|         LOG("%s fail", hash_name(HASH_SHA_256));
 | |
|         return false;
 | |
|     }
 | |
|     if (!_test_sha384_pass()) {
 | |
|         LOG("%s fail", hash_name(HASH_SHA_384));
 | |
|         return false;
 | |
|     }
 | |
|     if (!_test_sha512_pass()) {
 | |
|         LOG("%s fail", hash_name(HASH_SHA_512));
 | |
|         return false;
 | |
|     }
 | |
|     if (_test_sha256_fail()) {
 | |
|         LOG("%s passed on wrong data. So, it's bug.", hash_name(HASH_SHA_256));
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     return true;
 | |
| }
 |