2014-11-05 02:23:33 +00:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-10 01:48:22 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2014-11-05 02:23:33 +00:00
|
|
|
#include <openssl/crypto.h>
|
2016-09-06 23:10:19 +01:00
|
|
|
#include <openssl/digest.h>
|
2014-11-05 02:23:33 +00:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/md4.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2016-08-17 21:56:14 +01:00
|
|
|
#include "../internal.h"
|
|
|
|
|
|
|
|
|
2015-03-02 01:47:32 +00:00
|
|
|
struct MD {
|
|
|
|
// name is the name of the digest.
|
|
|
|
const char* name;
|
2015-03-22 23:46:59 +00:00
|
|
|
// md_func is the digest to test.
|
2015-03-02 01:47:32 +00:00
|
|
|
const EVP_MD *(*func)(void);
|
2015-03-22 23:46:59 +00:00
|
|
|
// one_shot_func is the convenience one-shot version of the
|
|
|
|
// digest.
|
2014-11-05 02:23:33 +00:00
|
|
|
uint8_t *(*one_shot_func)(const uint8_t *, size_t, uint8_t *);
|
2015-03-02 01:47:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const MD md4 = { "MD4", &EVP_md4, nullptr };
|
|
|
|
static const MD md5 = { "MD5", &EVP_md5, &MD5 };
|
|
|
|
static const MD sha1 = { "SHA1", &EVP_sha1, &SHA1 };
|
|
|
|
static const MD sha224 = { "SHA224", &EVP_sha224, &SHA224 };
|
|
|
|
static const MD sha256 = { "SHA256", &EVP_sha256, &SHA256 };
|
|
|
|
static const MD sha384 = { "SHA384", &EVP_sha384, &SHA384 };
|
|
|
|
static const MD sha512 = { "SHA512", &EVP_sha512, &SHA512 };
|
|
|
|
static const MD md5_sha1 = { "MD5-SHA1", &EVP_md5_sha1, nullptr };
|
|
|
|
|
|
|
|
struct TestVector {
|
|
|
|
// md is the digest to test.
|
|
|
|
const MD &md;
|
2015-03-22 23:46:59 +00:00
|
|
|
// input is a NUL-terminated string to hash.
|
2014-11-05 02:23:33 +00:00
|
|
|
const char *input;
|
2015-03-22 23:46:59 +00:00
|
|
|
// repeat is the number of times to repeat input.
|
2014-11-05 02:23:33 +00:00
|
|
|
size_t repeat;
|
2015-03-22 23:46:59 +00:00
|
|
|
// expected_hex is the expected digest in hexadecimal.
|
2014-11-05 02:23:33 +00:00
|
|
|
const char *expected_hex;
|
2015-03-22 23:46:59 +00:00
|
|
|
};
|
2014-11-05 02:23:33 +00:00
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
static const TestVector kTestVectors[] = {
|
|
|
|
// MD4 tests, from RFC 1320. (crypto/md4 does not provide a
|
|
|
|
// one-shot MD4 function.)
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md4, "", 1, "31d6cfe0d16ae931b73c59d7e0c089c0" },
|
|
|
|
{ md4, "a", 1, "bde52cb31de33e46245e05fbdbd6fb24" },
|
|
|
|
{ md4, "abc", 1, "a448017aaf21d8525fc10ae87aa6729d" },
|
|
|
|
{ md4, "message digest", 1, "d9130a8164549fe818874806e1c7014b" },
|
|
|
|
{ md4, "abcdefghijklmnopqrstuvwxyz", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"d79e1c308aa5bbcdeea8ed63df412da9" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md4,
|
2014-11-05 02:23:33 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
|
|
|
|
"043f8582f241db351ce627e153e7f0e4" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md4, "1234567890", 8, "e33b4ddc9c38f2199c3e7b164fcc0536" },
|
2014-11-05 02:23:33 +00:00
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// MD5 tests, from RFC 1321.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md5, "", 1, "d41d8cd98f00b204e9800998ecf8427e" },
|
|
|
|
{ md5, "a", 1, "0cc175b9c0f1b6a831c399e269772661" },
|
|
|
|
{ md5, "abc", 1, "900150983cd24fb0d6963f7d28e17f72" },
|
|
|
|
{ md5, "message digest", 1, "f96b697d7cb7938d525a2f31aaf161d0" },
|
|
|
|
{ md5, "abcdefghijklmnopqrstuvwxyz", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"c3fcd3d76192e4007dfb496cca67e13b" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md5,
|
2014-11-05 02:23:33 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
|
|
|
|
"d174ab98d277d9f5a5611c2c9f419d9f" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md5, "1234567890", 8, "57edf4a22be3c955ac49da2e2107b67a" },
|
2014-11-05 02:23:33 +00:00
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// SHA-1 tests, from RFC 3174.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha1, "abc", 1, "a9993e364706816aba3e25717850c26c9cd0d89d" },
|
|
|
|
{ sha1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
|
|
|
"84983e441c3bd26ebaae4aa1f95129e5e54670f1" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha1, "a", 1000000, "34aa973cd4c4daa4f61eeb2bdbad27316534016f" },
|
|
|
|
{ sha1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"0123456701234567012345670123456701234567012345670123456701234567", 10,
|
|
|
|
"dea356a2cddd90c7a7ecedc5ebb563934f460452" },
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// SHA-224 tests, from RFC 3874.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha224, "abc", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha224,
|
2014-11-05 02:23:33 +00:00
|
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
|
|
|
"75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha224,
|
2014-11-05 02:23:33 +00:00
|
|
|
"a", 1000000,
|
|
|
|
"20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" },
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// SHA-256 tests, from NIST.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha256, "abc", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha256,
|
2014-11-05 02:23:33 +00:00
|
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
|
|
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// SHA-384 tests, from NIST.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha384, "abc", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
|
|
|
|
"8086072ba1e7cc2358baeca134c825a7" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha384,
|
2014-11-05 02:23:33 +00:00
|
|
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
|
|
|
|
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
|
|
|
|
"09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"
|
|
|
|
"fcc7c71a557e2db966c3e9fa91746039" },
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// SHA-512 tests, from NIST.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha512, "abc", 1,
|
2014-11-05 02:23:33 +00:00
|
|
|
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
|
|
|
|
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" },
|
2015-03-02 01:47:32 +00:00
|
|
|
{ sha512,
|
2014-11-05 02:23:33 +00:00
|
|
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
|
|
|
|
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
|
|
|
|
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
|
|
|
|
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" },
|
2014-11-26 21:38:00 +00:00
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// MD5-SHA1 tests.
|
2015-03-02 01:47:32 +00:00
|
|
|
{ md5_sha1, "abc", 1,
|
2014-11-26 21:38:00 +00:00
|
|
|
"900150983cd24fb0d6963f7d28e17f72a9993e364706816aba3e25717850c26c9cd0d89d" },
|
2014-11-05 02:23:33 +00:00
|
|
|
};
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
static bool CompareDigest(const TestVector *test,
|
2014-11-05 02:23:33 +00:00
|
|
|
const uint8_t *digest,
|
|
|
|
size_t digest_len) {
|
|
|
|
static const char kHexTable[] = "0123456789abcdef";
|
|
|
|
char digest_hex[2*EVP_MAX_MD_SIZE + 1];
|
|
|
|
|
2016-09-05 17:47:25 +01:00
|
|
|
for (size_t i = 0; i < digest_len; i++) {
|
2014-11-05 02:23:33 +00:00
|
|
|
digest_hex[2*i] = kHexTable[digest[i] >> 4];
|
|
|
|
digest_hex[2*i + 1] = kHexTable[digest[i] & 0xf];
|
|
|
|
}
|
|
|
|
digest_hex[2*digest_len] = '\0';
|
|
|
|
|
|
|
|
if (strcmp(digest_hex, test->expected_hex) != 0) {
|
|
|
|
fprintf(stderr, "%s(\"%s\" * %d) = %s; want %s\n",
|
2015-03-02 01:47:32 +00:00
|
|
|
test->md.name, test->input, (int)test->repeat,
|
2014-11-05 02:23:33 +00:00
|
|
|
digest_hex, test->expected_hex);
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
return true;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
static int TestDigest(const TestVector *test) {
|
2016-12-07 03:35:41 +00:00
|
|
|
bssl::ScopedEVP_MD_CTX ctx;
|
2014-11-05 02:23:33 +00:00
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// Test the input provided.
|
2015-03-02 01:47:32 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx.get(), test->md.func(), NULL)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2015-03-22 23:46:59 +00:00
|
|
|
for (size_t i = 0; i < test->repeat; i++) {
|
|
|
|
if (!EVP_DigestUpdate(ctx.get(), test->input, strlen(test->input))) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-01 23:25:52 +01:00
|
|
|
std::unique_ptr<uint8_t[]> digest(new uint8_t[EVP_MD_size(test->md.func())]);
|
2015-03-22 23:46:59 +00:00
|
|
|
unsigned digest_len;
|
2016-08-01 23:25:52 +01:00
|
|
|
if (!EVP_DigestFinal_ex(ctx.get(), digest.get(), &digest_len)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2016-08-01 23:25:52 +01:00
|
|
|
if (!CompareDigest(test, digest.get(), digest_len)) {
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// Test the input one character at a time.
|
2015-03-02 01:47:32 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx.get(), test->md.func(), NULL)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2015-03-22 23:46:59 +00:00
|
|
|
if (!EVP_DigestUpdate(ctx.get(), NULL, 0)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2015-03-22 23:46:59 +00:00
|
|
|
for (size_t i = 0; i < test->repeat; i++) {
|
|
|
|
for (const char *p = test->input; *p; p++) {
|
|
|
|
if (!EVP_DigestUpdate(ctx.get(), p, 1)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 23:25:52 +01:00
|
|
|
if (!EVP_DigestFinal_ex(ctx.get(), digest.get(), &digest_len)) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2015-03-02 01:47:32 +00:00
|
|
|
if (digest_len != EVP_MD_size(test->md.func())) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "EVP_MD_size output incorrect\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2016-08-01 23:25:52 +01:00
|
|
|
if (!CompareDigest(test, digest.get(), digest_len)) {
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// Test the one-shot function.
|
2015-03-02 01:47:32 +00:00
|
|
|
if (test->md.one_shot_func && test->repeat == 1) {
|
|
|
|
uint8_t *out = test->md.one_shot_func((const uint8_t *)test->input,
|
2016-08-01 23:25:52 +01:00
|
|
|
strlen(test->input), digest.get());
|
|
|
|
if (out != digest.get()) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "one_shot_func gave incorrect return\n");
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
2016-08-01 23:25:52 +01:00
|
|
|
if (!CompareDigest(test, digest.get(), EVP_MD_size(test->md.func()))) {
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
// Test the deprecated static buffer variant, until it's removed.
|
2015-03-02 01:47:32 +00:00
|
|
|
out = test->md.one_shot_func((const uint8_t *)test->input,
|
|
|
|
strlen(test->input), NULL);
|
|
|
|
if (!CompareDigest(test, out, EVP_MD_size(test->md.func()))) {
|
2015-03-22 23:46:59 +00:00
|
|
|
return false;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:46:59 +00:00
|
|
|
return true;
|
2014-11-05 02:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 03:38:03 +01:00
|
|
|
static int TestGetters() {
|
|
|
|
if (EVP_get_digestbyname("RSA-SHA512") == NULL ||
|
|
|
|
EVP_get_digestbyname("sha512WithRSAEncryption") == NULL ||
|
|
|
|
EVP_get_digestbyname("nonsense") != NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 03:35:41 +00:00
|
|
|
int main() {
|
2014-11-05 02:23:33 +00:00
|
|
|
CRYPTO_library_init();
|
|
|
|
|
2016-08-17 21:56:14 +01:00
|
|
|
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTestVectors); i++) {
|
2015-03-22 23:46:59 +00:00
|
|
|
if (!TestDigest(&kTestVectors[i])) {
|
2014-11-05 02:23:33 +00:00
|
|
|
fprintf(stderr, "Test %d failed\n", (int)i);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 03:38:03 +01:00
|
|
|
if (!TestGetters()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-05 02:23:33 +00:00
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|