1 /********************************************************************** 2 Copyright(c) 2024 Intel Corporation All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in 11 the documentation and/or other materials provided with the 12 distribution. 13 * Neither the name of Intel Corporation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **********************************************************************/ 29 30 /* 31 * SHA self tests 32 */ 33 34 #include <stdlib.h> 35 #include <stdio.h> 36 #include <stdint.h> 37 #include <string.h> 38 39 #include "sha1_mb.h" 40 #include "sha256_mb.h" 41 #include "sha512_mb.h" 42 43 #include "internal_fips.h" 44 #include "types.h" 45 #include "test.h" 46 47 typedef uint32_t DigestSHA1[SHA1_DIGEST_NWORDS]; 48 typedef uint32_t DigestSHA256[SHA256_DIGEST_NWORDS]; 49 typedef uint64_t DigestSHA512[SHA512_DIGEST_NWORDS]; 50 51 static const uint8_t msg[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 52 static const DigestSHA1 expResultDigest_sha1 = { 0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, 0xF95129E5, 53 0xE54670F1 }; 54 55 static const DigestSHA256 expResultDigest_sha256 = { 56 0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039, 57 0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1 58 }; 59 60 static uint8_t msg_sha512[] = "The quick brown fox jumps over the lazy dog"; 61 static DigestSHA512 expResultDigest_sha512 = { 0x07e547d9586f6a73, 0xf73fbac0435ed769, 62 0x51218fb7d0c8d788, 0xa309d785436bbb64, 63 0x2e93a252a954f239, 0x12547d1e8a3b5ed6, 64 0xe1bfd7097821233f, 0xa0538f3db854fee6 }; 65 66 static int 67 _sha1_self_test(void) 68 { 69 70 SHA1_HASH_CTX_MGR mgr; 71 SHA1_HASH_CTX ctxpool, *ctx = NULL; 72 uint32_t j; 73 74 sha1_ctx_mgr_init(&mgr); 75 76 // Init context before first use 77 hash_ctx_init(&ctxpool); 78 79 ctx = sha1_ctx_mgr_submit(&mgr, &ctxpool, msg, (uint32_t) strlen((char *) msg), 80 HASH_ENTIRE); 81 82 if (ctx == NULL) 83 ctx = sha1_ctx_mgr_flush(&mgr); 84 85 if (ctx) { 86 for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { 87 if (expResultDigest_sha1[j] != ctxpool.job.result_digest[j]) 88 return -1; 89 } 90 } else 91 return -1; 92 93 return 0; 94 } 95 96 static int 97 _sha256_self_test(void) 98 { 99 100 SHA256_HASH_CTX_MGR mgr; 101 SHA256_HASH_CTX ctxpool, *ctx = NULL; 102 uint32_t j; 103 104 sha256_ctx_mgr_init(&mgr); 105 106 // Init context before first use 107 hash_ctx_init(&ctxpool); 108 109 ctx = sha256_ctx_mgr_submit(&mgr, &ctxpool, msg, (uint32_t) strlen((char *) msg), 110 HASH_ENTIRE); 111 112 if (ctx == NULL) 113 ctx = sha256_ctx_mgr_flush(&mgr); 114 115 if (ctx) { 116 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { 117 if (expResultDigest_sha256[j] != ctxpool.job.result_digest[j]) 118 return -1; 119 } 120 } else 121 return -1; 122 123 return 0; 124 } 125 126 static int 127 _sha512_self_test(void) 128 { 129 130 SHA512_HASH_CTX_MGR mgr; 131 SHA512_HASH_CTX ctxpool, *ctx = NULL; 132 uint32_t j; 133 134 sha512_ctx_mgr_init(&mgr); 135 136 // Init context before first use 137 hash_ctx_init(&ctxpool); 138 139 ctx = sha512_ctx_mgr_submit(&mgr, &ctxpool, msg_sha512, 140 (uint32_t) strlen((char *) msg_sha512), HASH_ENTIRE); 141 142 if (ctx == NULL) 143 ctx = sha512_ctx_mgr_flush(&mgr); 144 145 if (ctx) { 146 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) { 147 if (expResultDigest_sha512[j] != ctxpool.job.result_digest[j]) 148 return -1; 149 } 150 } else 151 return -1; 152 153 return 0; 154 } 155 156 int 157 _sha_self_tests(void) 158 { 159 int ret; 160 161 ret = _sha1_self_test(); 162 ret |= _sha256_self_test(); 163 ret |= _sha512_self_test(); 164 165 return ret; 166 } 167