test_hash_sha256.c (4116B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "endianness.h" 17 #include "hash.h" 18 #include "math.h" 19 #include "mem_allocator.h" 20 #include <string.h> 21 22 struct test_str { 23 const char* str; 24 const char* sha256sum; 25 }; 26 struct test_data { 27 const void* mem; 28 const size_t len; 29 const char* sha256sum; 30 }; 31 32 struct buf { 33 const char* mem; 34 size_t len; 35 }; 36 37 static void 38 chk_hash(const void* data, const size_t data_len, const char* sha256sum) 39 { 40 hash256_T hash; 41 char hash_str[65]; 42 ASSERT(sha256sum); 43 44 hash_sha256(data, data_len, hash); 45 hash256_to_cstr(hash, hash_str); 46 CHK(!strcmp(hash_str, sha256sum)); 47 } 48 49 int 50 main(int argc, char** argv) 51 { 52 char* data = NULL; 53 hash256_T hash0, hash1; 54 (void)argc, (void)argv; 55 56 data = mem_alloc(0x6000003e); 57 CHK(data); 58 59 data[0] = '\0'; 60 chk_hash(data, 0, 61 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); 62 63 hash_sha256(data, 0, hash0); 64 ((uint32_t*)hash1)[0] = big_endian_32(0xe3b0c442); 65 ((uint32_t*)hash1)[1] = big_endian_32(0x98fc1c14); 66 ((uint32_t*)hash1)[2] = big_endian_32(0x9afbf4c8); 67 ((uint32_t*)hash1)[3] = big_endian_32(0x996fb924); 68 ((uint32_t*)hash1)[4] = big_endian_32(0x27ae41e4); 69 ((uint32_t*)hash1)[5] = big_endian_32(0x649b934c); 70 ((uint32_t*)hash1)[6] = big_endian_32(0xa495991b); 71 ((uint32_t*)hash1)[7] = big_endian_32(0x7852b855); 72 CHK(hash256_eq(hash0, hash1)); 73 74 sprintf(data, "abc"); 75 chk_hash(data, strlen(data), 76 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); 77 78 sprintf(data, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); 79 chk_hash(data, strlen(data), 80 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); 81 82 data[0] = (char)0xbdu; 83 chk_hash(data, 1, 84 "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b"); 85 86 data[0] = (char)0xc9u; 87 data[1] = (char)0x8cu; 88 data[2] = (char)0x8eu; 89 data[3] = (char)0x55u; 90 chk_hash(data, 4, 91 "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504"); 92 93 memset(data, 0, 55); 94 chk_hash(data, 55, 95 "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7"); 96 97 memset(data, 0, 56); 98 chk_hash(data, 56, 99 "d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb"); 100 101 memset(data, 0, 57); 102 chk_hash(data, 57, 103 "65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785"); 104 105 memset(data, 0, 64); 106 chk_hash(data, 64, 107 "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"); 108 109 memset(data, 0, 1000); 110 chk_hash(data, 1000, 111 "541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53"); 112 113 memset(data, 'A', 1000); 114 chk_hash(data, 1000, 115 "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4"); 116 117 memset(data, 'U', 1005); 118 chk_hash(data, 1005, 119 "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0"); 120 121 memset(data, 0, 1000000); 122 chk_hash(data, 1000000, 123 "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025"); 124 125 memset(data, 'Z', 0x20000000); 126 chk_hash(data, 0x20000000, 127 "15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd"); 128 129 memset(data, 0, 0x41000000); 130 chk_hash(data, 0x41000000, 131 "461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53"); 132 133 memset(data, 'B', 0x6000003e); 134 chk_hash(data, 0x6000003e, 135 "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea"); 136 137 mem_rm(data); 138 CHK(mem_allocated_size() == 0); 139 return 0; 140 }