David Benjamin | 9da9035 | 2014-11-04 21:23:33 -0500 | [diff] [blame^] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include <openssl/crypto.h> |
| 20 | #include <openssl/digest.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/md4.h> |
| 23 | #include <openssl/md5.h> |
| 24 | #include <openssl/sha.h> |
| 25 | |
| 26 | |
| 27 | typedef struct { |
| 28 | /* md_func is the digest to test. */ |
| 29 | const EVP_MD *(*md_func)(void); |
| 30 | /* one_shot_func is the convenience one-shot version of the |
| 31 | * digest. */ |
| 32 | uint8_t *(*one_shot_func)(const uint8_t *, size_t, uint8_t *); |
| 33 | /* input is a NUL-terminated string to hash. */ |
| 34 | const char *input; |
| 35 | /* repeat is the number of times to repeat input. */ |
| 36 | size_t repeat; |
| 37 | /* expected_hex is the expected digest in hexadecimal. */ |
| 38 | const char *expected_hex; |
| 39 | } TEST_VECTOR; |
| 40 | |
| 41 | static const TEST_VECTOR kTestVectors[] = { |
| 42 | /* MD4 tests, from RFC 1320. (crypto/md4 does not provide a |
| 43 | * one-shot MD4 function.) */ |
| 44 | { &EVP_md4, NULL, "", 1, "31d6cfe0d16ae931b73c59d7e0c089c0" }, |
| 45 | { &EVP_md4, NULL, "a", 1, "bde52cb31de33e46245e05fbdbd6fb24" }, |
| 46 | { &EVP_md4, NULL, "abc", 1, "a448017aaf21d8525fc10ae87aa6729d" }, |
| 47 | { &EVP_md4, NULL, "message digest", 1, |
| 48 | "d9130a8164549fe818874806e1c7014b" }, |
| 49 | { &EVP_md4, NULL, "abcdefghijklmnopqrstuvwxyz", 1, |
| 50 | "d79e1c308aa5bbcdeea8ed63df412da9" }, |
| 51 | { &EVP_md4, NULL, |
| 52 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1, |
| 53 | "043f8582f241db351ce627e153e7f0e4" }, |
| 54 | { &EVP_md4, NULL, "1234567890", 8, "e33b4ddc9c38f2199c3e7b164fcc0536" }, |
| 55 | |
| 56 | /* MD5 tests, from RFC 1321. */ |
| 57 | { &EVP_md5, &MD5, "", 1, "d41d8cd98f00b204e9800998ecf8427e" }, |
| 58 | { &EVP_md5, &MD5, "a", 1, "0cc175b9c0f1b6a831c399e269772661" }, |
| 59 | { &EVP_md5, &MD5, "abc", 1, "900150983cd24fb0d6963f7d28e17f72" }, |
| 60 | { &EVP_md5, &MD5, "message digest", 1, "f96b697d7cb7938d525a2f31aaf161d0" }, |
| 61 | { &EVP_md5, &MD5, "abcdefghijklmnopqrstuvwxyz", 1, |
| 62 | "c3fcd3d76192e4007dfb496cca67e13b" }, |
| 63 | { &EVP_md5, &MD5, |
| 64 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1, |
| 65 | "d174ab98d277d9f5a5611c2c9f419d9f" }, |
| 66 | { &EVP_md5, &MD5, "1234567890", 8, "57edf4a22be3c955ac49da2e2107b67a" }, |
| 67 | |
| 68 | /* SHA-1 tests, from RFC 3174. */ |
| 69 | { &EVP_sha1, &SHA1, "abc", 1, "a9993e364706816aba3e25717850c26c9cd0d89d" }, |
| 70 | { &EVP_sha1, &SHA1, |
| 71 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, |
| 72 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1" }, |
| 73 | { &EVP_sha1, &SHA1, "a", 1000000, |
| 74 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f" }, |
| 75 | { &EVP_sha1, &SHA1, |
| 76 | "0123456701234567012345670123456701234567012345670123456701234567", 10, |
| 77 | "dea356a2cddd90c7a7ecedc5ebb563934f460452" }, |
| 78 | |
| 79 | /* SHA-224 tests, from RFC 3874. */ |
| 80 | { &EVP_sha224, &SHA224, "abc", 1, |
| 81 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" }, |
| 82 | { &EVP_sha224, &SHA224, |
| 83 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, |
| 84 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" }, |
| 85 | { &EVP_sha224, &SHA224, |
| 86 | "a", 1000000, |
| 87 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" }, |
| 88 | |
| 89 | /* SHA-256 tests, from NIST. */ |
| 90 | { &EVP_sha256, &SHA256, "abc", 1, |
| 91 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" }, |
| 92 | { &EVP_sha256, &SHA256, |
| 93 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, |
| 94 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" }, |
| 95 | |
| 96 | /* SHA-384 tests, from NIST. */ |
| 97 | { &EVP_sha384, &SHA384, "abc", 1, |
| 98 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" |
| 99 | "8086072ba1e7cc2358baeca134c825a7" }, |
| 100 | { &EVP_sha384, &SHA384, |
| 101 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
| 102 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1, |
| 103 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" |
| 104 | "fcc7c71a557e2db966c3e9fa91746039" }, |
| 105 | |
| 106 | /* SHA-512 tests, from NIST. */ |
| 107 | { &EVP_sha512, &SHA512, "abc", 1, |
| 108 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" |
| 109 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" }, |
| 110 | { &EVP_sha512, &SHA512, |
| 111 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
| 112 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1, |
| 113 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" |
| 114 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" }, |
| 115 | }; |
| 116 | |
| 117 | static int compare_digest(const TEST_VECTOR *test, |
| 118 | const uint8_t *digest, |
| 119 | size_t digest_len) { |
| 120 | static const char kHexTable[] = "0123456789abcdef"; |
| 121 | size_t i; |
| 122 | char digest_hex[2*EVP_MAX_MD_SIZE + 1]; |
| 123 | |
| 124 | for (i = 0; i < digest_len; i++) { |
| 125 | digest_hex[2*i] = kHexTable[digest[i] >> 4]; |
| 126 | digest_hex[2*i + 1] = kHexTable[digest[i] & 0xf]; |
| 127 | } |
| 128 | digest_hex[2*digest_len] = '\0'; |
| 129 | |
| 130 | if (strcmp(digest_hex, test->expected_hex) != 0) { |
| 131 | fprintf(stderr, "%s(\"%s\" * %d) = %s; want %s\n", |
| 132 | EVP_MD_name(test->md_func()), test->input, (int)test->repeat, |
| 133 | digest_hex, test->expected_hex); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | static int test_digest(const TEST_VECTOR *test) { |
| 141 | int ret = 0; |
| 142 | EVP_MD_CTX ctx; |
| 143 | size_t i; |
| 144 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 145 | unsigned digest_len; |
| 146 | |
| 147 | EVP_MD_CTX_init(&ctx); |
| 148 | |
| 149 | /* Test the input provided. */ |
| 150 | if (!EVP_DigestInit_ex(&ctx, test->md_func(), NULL)) { |
| 151 | fprintf(stderr, "EVP_DigestInit_ex failed\n"); |
| 152 | goto done; |
| 153 | } |
| 154 | for (i = 0; i < test->repeat; i++) { |
| 155 | if (!EVP_DigestUpdate(&ctx, test->input, strlen(test->input))) { |
| 156 | fprintf(stderr, "EVP_DigestUpdate failed\n"); |
| 157 | goto done; |
| 158 | } |
| 159 | } |
| 160 | if (!EVP_DigestFinal_ex(&ctx, digest, &digest_len)) { |
| 161 | fprintf(stderr, "EVP_DigestFinal_ex failed\n"); |
| 162 | goto done; |
| 163 | } |
| 164 | if (!compare_digest(test, digest, digest_len)) { |
| 165 | goto done; |
| 166 | } |
| 167 | |
| 168 | /* Test the input one character at a time. */ |
| 169 | if (!EVP_DigestInit_ex(&ctx, test->md_func(), NULL)) { |
| 170 | fprintf(stderr, "EVP_DigestInit_ex failed\n"); |
| 171 | goto done; |
| 172 | } |
| 173 | if (!EVP_DigestUpdate(&ctx, NULL, 0)) { |
| 174 | fprintf(stderr, "EVP_DigestUpdate failed\n"); |
| 175 | goto done; |
| 176 | } |
| 177 | for (i = 0; i < test->repeat; i++) { |
| 178 | const char *p; |
| 179 | for (p = test->input; *p; p++) { |
| 180 | if (!EVP_DigestUpdate(&ctx, p, 1)) { |
| 181 | fprintf(stderr, "EVP_DigestUpdate failed\n"); |
| 182 | goto done; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | if (!EVP_DigestFinal_ex(&ctx, digest, &digest_len)) { |
| 187 | fprintf(stderr, "EVP_DigestFinal_ex failed\n"); |
| 188 | goto done; |
| 189 | } |
| 190 | if (digest_len != EVP_MD_size(test->md_func())) { |
| 191 | fprintf(stderr, "EVP_MD_size output incorrect\n"); |
| 192 | goto done; |
| 193 | } |
| 194 | if (!compare_digest(test, digest, digest_len)) { |
| 195 | goto done; |
| 196 | } |
| 197 | |
| 198 | /* Test the one-shot function. */ |
| 199 | if (test->one_shot_func && test->repeat == 1) { |
| 200 | uint8_t *out = test->one_shot_func((const uint8_t *)test->input, |
| 201 | strlen(test->input), digest); |
| 202 | if (out != digest) { |
| 203 | fprintf(stderr, "one_shot_func gave incorrect return\n"); |
| 204 | goto done; |
| 205 | } |
| 206 | if (!compare_digest(test, digest, EVP_MD_size(test->md_func()))) { |
| 207 | goto done; |
| 208 | } |
| 209 | |
| 210 | /* Test the deprecated static buffer variant, until it's removed. */ |
| 211 | out = test->one_shot_func((const uint8_t *)test->input, strlen(test->input), |
| 212 | NULL); |
| 213 | if (!compare_digest(test, out, EVP_MD_size(test->md_func()))) { |
| 214 | goto done; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | ret = 1; |
| 219 | |
| 220 | done: |
| 221 | EVP_MD_CTX_cleanup(&ctx); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | int main(void) { |
| 226 | size_t i; |
| 227 | |
| 228 | CRYPTO_library_init(); |
| 229 | ERR_load_crypto_strings(); |
| 230 | |
| 231 | for (i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) { |
| 232 | if (!test_digest(&kTestVectors[i])) { |
| 233 | fprintf(stderr, "Test %d failed\n", (int)i); |
| 234 | return 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | printf("PASS\n"); |
| 239 | return 0; |
| 240 | } |