| 1 |
/* |
| 2 |
* Copyright (C) 2024 Tianjia Zhang |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* 3. Neither the name of the author nor the names of other contributors |
| 14 |
* may be used to endorse or promote products derived from this software |
| 15 |
* without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
* SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "crypt-port.h" |
| 31 |
#include "alg-sm3.h" |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
#if INCLUDE_sm3crypt |
| 36 |
|
| 37 |
static const struct |
| 38 |
{ |
| 39 |
const char *input; |
| 40 |
const char result[33]; |
| 41 |
} tests[] = |
| 42 |
{ |
| 43 |
/* Test vectors from OSCCA GM/T 0004-2012: appendix A. */ |
| 44 |
{ |
| 45 |
"abc", |
| 46 |
"\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2" |
| 47 |
"\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0" |
| 48 |
}, |
| 49 |
{ |
| 50 |
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 51 |
"\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1\xea" |
| 52 |
"\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b\x8c\x05" |
| 53 |
}, |
| 54 |
{ |
| 55 |
"", |
| 56 |
"\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8E\x61\x19\x48\x31\xe8\x1a\x8f" |
| 57 |
"\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82\xaa\x2b" |
| 58 |
}, |
| 59 |
{ |
| 60 |
"a", |
| 61 |
"\x62\x34\x76\xac\x18\xf6\x5a\x29\x09\xe4\x3c\x7f\xec\x61\xb4\x9c" |
| 62 |
"\x7e\x76\x4a\x91\xa1\x8c\xcb\x82\xf1\x91\x7a\x29\xc8\x6c\x5e\x88" |
| 63 |
}, |
| 64 |
{ |
| 65 |
"message digest", |
| 66 |
"\xc5\x22\xa9\x42\xe8\x9b\xd8\x0d\x97\xdd\x66\x6e\x7a\x55\x31\xb3" |
| 67 |
"\x61\x88\xc9\x81\x71\x49\xe9\xb2\x58\xdf\xe5\x1e\xce\x98\xed\x77" |
| 68 |
}, |
| 69 |
{ |
| 70 |
"abcdefghijklmnopqrstuvwxyz", |
| 71 |
"\xb8\x0f\xe9\x7a\x4d\xa2\x4a\xfc\x27\x75\x64\xf6\x6a\x35\x9e\xf4" |
| 72 |
"\x40\x46\x2a\xd2\x8d\xcc\x6d\x63\xad\xb2\x4d\x5c\x20\xa6\x15\x95" |
| 73 |
}, |
| 74 |
{ |
| 75 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
| 76 |
"\x29\x71\xd1\x0c\x88\x42\xb7\x0c\x97\x9e\x55\x06\x34\x80\xc5\x0b" |
| 77 |
"\xac\xff\xd9\x0e\x98\xe2\xe6\x0d\x25\x12\xab\x8a\xbf\xdf\xce\xc5" |
| 78 |
}, |
| 79 |
{ |
| 80 |
"123456789012345678901234567890123456789012345678901234567890" |
| 81 |
"12345678901234567890", |
| 82 |
"\xad\x81\x80\x53\x21\xf3\xe6\x9d\x25\x12\x35\xbf\x88\x6a\x56\x48" |
| 83 |
"\x44\x87\x3b\x56\xdd\x7d\xde\x40\x0f\x05\x5b\x7d\xde\x39\x30\x7a" |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
static void |
| 89 |
report_failure(int n, const char *tag, |
| 90 |
const char expected[32], uint8_t actual[32]) |
| 91 |
{ |
| 92 |
int i; |
| 93 |
printf ("FAIL: test %d (%s):\n exp:", n, tag); |
| 94 |
for (i = 0; i < 32; i++) |
| 95 |
{ |
| 96 |
if (i % 4 == 0) |
| 97 |
putchar (' '); |
| 98 |
printf ("%02x", (unsigned int)(unsigned char)expected[i]); |
| 99 |
} |
| 100 |
printf ("\n got:"); |
| 101 |
for (i = 0; i < 32; i++) |
| 102 |
{ |
| 103 |
if (i % 4 == 0) |
| 104 |
putchar (' '); |
| 105 |
printf ("%02x", (unsigned int)(unsigned char)actual[i]); |
| 106 |
} |
| 107 |
putchar ('\n'); |
| 108 |
putchar ('\n'); |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
main (void) |
| 113 |
{ |
| 114 |
sm3_ctx ctx; |
| 115 |
uint8_t sum[32]; |
| 116 |
int result = 0; |
| 117 |
int cnt; |
| 118 |
int i; |
| 119 |
|
| 120 |
for (cnt = 0; cnt < (int) ARRAY_SIZE (tests); ++cnt) |
| 121 |
{ |
| 122 |
sm3_buf (tests[cnt].input, strlen (tests[cnt].input), sum); |
| 123 |
if (memcmp (tests[cnt].result, sum, 32) != 0) |
| 124 |
{ |
| 125 |
report_failure (cnt, "all at once", tests[cnt].result, sum); |
| 126 |
result = 1; |
| 127 |
} |
| 128 |
|
| 129 |
sm3_init (&ctx); |
| 130 |
for (i = 0; tests[cnt].input[i] != '\0'; ++i) |
| 131 |
sm3_update (&ctx, &tests[cnt].input[i], 1); |
| 132 |
sm3_final (sum, &ctx); |
| 133 |
if (memcmp (tests[cnt].result, sum, 32) != 0) |
| 134 |
{ |
| 135 |
report_failure (cnt, "byte by byte", tests[cnt].result, sum); |
| 136 |
result = 1; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* Test vector from FIPS 180-2: appendix B.3. */ |
| 141 |
char buf[1000]; |
| 142 |
memset (buf, 'a', sizeof (buf)); |
| 143 |
sm3_init (&ctx); |
| 144 |
for (i = 0; i < 1000; ++i) |
| 145 |
sm3_update (&ctx, buf, sizeof (buf)); |
| 146 |
sm3_final (sum, &ctx); |
| 147 |
static const char expected[33] = |
| 148 |
"\xc8\xaa\xf8\x94\x29\x55\x40\x29\xe2\x31\x94\x1a\x2a\xcc\x0a\xd6" |
| 149 |
"\x1f\xf2\xa5\xac\xd8\xfa\xdd\x25\x84\x7a\x3a\x73\x2b\x3b\x02\xc3"; |
| 150 |
|
| 151 |
if (memcmp (expected, sum, 32) != 0) |
| 152 |
{ |
| 153 |
report_failure (cnt, "block by block", expected, sum); |
| 154 |
result = 1; |
| 155 |
} |
| 156 |
|
| 157 |
return result; |
| 158 |
} |
| 159 |
|
| 160 |
#else |
| 161 |
|
| 162 |
int |
| 163 |
main (void) |
| 164 |
{ |
| 165 |
return 77; /* UNSUPPORTED */ |
| 166 |
} |
| 167 |
|
| 168 |
#endif |