2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2010, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews/* $Id: t_hashes.c,v 1.5 2010/10/04 22:27:41 marka Exp $ */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * -d1 or larger shows hash or HMAC result even if correct
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <config.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <stdlib.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <isc/hmacmd5.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <isc/hmacsha.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <isc/md5.h>
34f010449c1c29915689f741cfb347125bdec2cdMark Andrews#include <isc/print.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <isc/sha1.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <isc/string.h>
34f010449c1c29915689f741cfb347125bdec2cdMark Andrews#include <isc/util.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#include <pk11/site.h>
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#include <tests/t_api.h>
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic int nprobs;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef void(*HASH_INIT)(void *);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef void(*HMAC_INIT)(void *, const unsigned char *, unsigned int);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef void(*UPDATE)(void *, const unsigned char *, unsigned int);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef void(*FINAL)(void *, const unsigned char *);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef void(*SIGN)(void *, const unsigned char *, unsigned int);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef struct {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const char *name;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned char *key;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned int key_len;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned char *str;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned int str_len;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews} IN_;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#define STR_INIT(s) (const unsigned char *)(s), sizeof(s)-1
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsunion {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char b[1024];
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char md5[16];
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char sha1[ISC_SHA1_DIGESTLENGTH];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char sha224[ISC_SHA224_DIGESTLENGTH];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char sha256[ISC_SHA256_DIGESTLENGTH];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char sha384[ISC_SHA384_DIGESTLENGTH];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char sha512[ISC_SHA512_DIGESTLENGTH];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews} dbuf;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews#define DIGEST_FILL 0xdf
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstypedef struct {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const char *str;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned int digest_len;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews} OUT_;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * two ad hoc hash examples
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ abc = { "\"abc\"", NULL, 0, STR_INIT("abc")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "a9993e364706816aba3e25717850c26c9cd0d89d",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_md5 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "900150983cd24fb0d6963f7d28e17f72",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 16};
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ abc_blah = { "\"abcdbc...\"", NULL, 0,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews STR_INIT("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_blah_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_blah_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ abc_blah_md5 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "8215ef0796a20bcaaae116d3876c664a",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 16};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * three HMAC-md5 examples from RFC 2104
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc2104_1_key[16] = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc2104_1 = {"RFC 2104 #1", rfc2104_1_key, sizeof(rfc2104_1_key),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews STR_INIT("Hi There")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc2104_1_hmac = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "9294727a3638bb1c13f48ef8158bfc9d",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 16};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc2104_2 = {"RFC 2104 #2", STR_INIT("Jefe"),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews STR_INIT("what do ya want for nothing?")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc2104_2_hmac = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "750c783e6ab0b503eaa86e310a5db738",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 16};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc2104_3_key[16] = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc2104_3_s[50] = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 0xDD, 0xDD};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc2104_3 = {"RFC 2104 #3", rfc2104_3_key, sizeof(rfc2104_3_key),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews rfc2104_3_s, sizeof(rfc2104_3_s)};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc2104_3_hmac = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "56be34521d144c88dbb8c733f0e8b3f6",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews 16};
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * four three HMAC-SHA tests cut-and-pasted from RFC 4634 starting on page 86
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc4634_1_key[20] = {
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc4634_1 = {"RFC 4634 #1", rfc4634_1_key, sizeof(rfc4634_1_key),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews STR_INIT("Hi There")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_1_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "B617318655057264E28BC0B6FB378C8EF146BE00",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_1_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "896FB1128ABBDF196832107CD49DF33F47B4B1169912BA4F53684B22",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_1_sha256 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "B0344C61D8DB38535CA8AFCEAF0BF12B881DC200C9833DA726E9376C2E32"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "CFF7",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA256_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_1_sha384 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "AFD03944D84895626B0825F4AB46907F15F9DADBE4101EC682AA034C7CEB"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "C59CFAEA9EA9076EDE7F4AF152E8B2FA9CB6",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA384_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_1_sha512 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "87AA7CDEA5EF619D4FF0B4241A1D6CB02379F4E2CE4EC2787AD0B30545E1"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "7CDEDAA833B7D6B8A702038B274EAEA3F4E4BE9D914EEB61F1702E696C20"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "3A126854",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA512_DIGESTLENGTH};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc4634_2 = {"RFC 4634 #2", STR_INIT("Jefe"),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews STR_INIT("what do ya want for nothing?")};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_2_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_2_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "A30E01098BC6DBBF45690F3A7E9E6D0F8BBEA2A39E6148008FD05E44",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_2_sha256 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "5BDCC146BF60754E6A042426089575C75A003F089D2739839DEC58B964EC"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "3843",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA256_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_2_sha384 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "AF45D2E376484031617F78D2B58A6B1B9C7EF464F5A01B47E42EC3736322"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "445E8E2240CA5E69E2C78B3239ECFAB21649",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA384_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_2_sha512 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "164B7A7BFCF819E2E395FBE73B56E0A387BD64222E831FD610270CD7EA25"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "05549758BF75C05A994A6D034F65F8F0E6FDCAEAB1A34D4A6B4B636E070A"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "38BCE737",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA512_DIGESTLENGTH};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc4634_3_key[20] = {
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc4634_3_s[50] = {
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc4634_3 = {"RFC 4634 #3", rfc4634_3_key, sizeof(rfc4634_3_key),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews rfc4634_3_s, sizeof(rfc4634_3_s)};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_3_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "125D7342B9AC11CD91A39AF48AA17B4F63F175D3",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_3_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "7FB3CB3588C6C1F6FFA9694D7D6AD2649365B0C1F65D69D1EC8333EA",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_3_sha256 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "773EA91E36800E46854DB8EBD09181A72959098B3EF8C122D9635514CED5"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "65FE",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA256_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_3_sha384 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "88062608D3E6AD8A0AA2ACE014C8A86F0AA635D947AC9FEBE83EF4E55966"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "144B2A5AB39DC13814B94E3AB6E101A34F27",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA384_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_3_sha512 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "FA73B0089D56A284EFB0F0756C890BE9B1B5DBDD8EE81A3655F83E33B227"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "9D39BF3E848279A722C806B485A47E67C807B946A337BEE8942674278859"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "E13292FB",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA512_DIGESTLENGTH};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc4634_4_key[25] = {
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0x15, 0x16, 0x17, 0x18, 0x19
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const unsigned char rfc4634_4_s[50] = {
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd
13dae6ff5843734df144aa6e80a7741d22526816Mark Andrews};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic IN_ rfc4634_4 = {"RFC 4634 #3", rfc4634_4_key, sizeof(rfc4634_4_key),
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews rfc4634_4_s, sizeof(rfc4634_4_s)};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_4_sha1 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "4C9007F4026250C6BC8414F9BF50C86C2D7235DA",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA1_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_4_sha224 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "6C11506874013CAC6A2ABC1BB382627CEC6A90D86EFC012DE7AFEC5A",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA224_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_4_sha256 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "82558A389A443C0EA4CC819899F2083A85F0FAA3E578F8077A2E3FF46729"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "665B",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA256_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_4_sha384 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "3E8A69B7783C25851933AB6290AF6CA77A9981480850009CC5577C6E1F57"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "3B4E6801DD23C4A7D679CCF8A386C674CFFB",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA384_DIGESTLENGTH};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsstatic OUT_ rfc4634_4_sha512 = {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "B0BA465637458C6990E5A8C5F61D4AF7E576D97FF94B872DE76F8050361E"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "E3DBA91CA5C11AA25EB4D679275CC5788063A5F19741120C4F2DE2ADEBEB"
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "10A298DD",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ISC_SHA512_DIGESTLENGTH};
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic const char *
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsd2str(char *buf, unsigned int buf_len,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const unsigned char *d, unsigned int d_len)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned int i, l;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews l = 0;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews for (i = 0; i < d_len && l < buf_len-4; ++i) {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews l += snprintf(&buf[l], buf_len-l, "%02x", d[i]);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews if (l >= buf_len-3) {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews REQUIRE(buf_len > sizeof("..."));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews strcpy(&buf[l-sizeof(" ...")], " ...");
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews return buf;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * Compare binary digest or HMAC to string of hex digits from an RFC
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsck(const char *name, const IN_ *in, const OUT_ *out)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews char buf[sizeof(dbuf)*2+1];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews const char *str_name;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned int l;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews d2str(buf, sizeof(buf), dbuf.b, out->digest_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews str_name = in->name != NULL ? in->name : (const char *)in->str;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews if (T_debug != 0)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_info("%s(%s) = %s\n", name, str_name, buf);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews if (strcasecmp(buf, out->str)) {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_info("%s(%s)\n%9s %s\n%9s %s\n",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews name, str_name,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "is", buf,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews "should be", out->str);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ++nprobs;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews return;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews /*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * check that the hash or HMAC is no longer than we think it is
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews for (l = out->digest_len; l < sizeof(dbuf); ++l) {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews if (dbuf.b[l] != DIGEST_FILL) {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_info("byte #%d after end of %s(%s) changed to %02x\n",
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews l-out->digest_len, name, str_name, dbuf.b[l]);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ++nprobs;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews break;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewst_hash(const char *hname, HASH_INIT init, UPDATE update, FINAL final,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews IN_ *in, OUT_ *out)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews union {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char b[1024];
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_sha1_t sha1;
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_md5_t md5;
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews } ctx;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews init(&ctx);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews update(&ctx, in->str, in->str_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews memset(dbuf.b, DIGEST_FILL, sizeof(dbuf));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews final(&ctx, dbuf.b);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ck(hname, in, out);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * isc_sha224_final has a different calling sequence
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewst_sha224(IN_ *in, OUT_ *out)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_sha224_t ctx;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews memset(dbuf.b, DIGEST_FILL, sizeof(dbuf));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_sha224_init(&ctx);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_sha224_update(&ctx, in->str, in->str_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews memset(dbuf.b, DIGEST_FILL, sizeof(dbuf));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_sha224_final(dbuf.b, &ctx);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ck("SHA224", in, out);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewst_hashes(IN_ *in, OUT_ *out_sha1, OUT_ *out_sha224, OUT_ *out_md5)
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#else
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrewst_hashes(IN_ *in, OUT_ *out_sha1, OUT_ *out_sha224)
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hash("SHA1", (HASH_INIT)isc_sha1_init, (UPDATE)isc_sha1_update,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (FINAL)isc_sha1_final, in, out_sha1);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_sha224(in, out_sha224);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hash("md5", (HASH_INIT)isc_md5_init, (UPDATE)isc_md5_update,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (FINAL)isc_md5_final, in, out_md5);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * isc_hmacmd5_sign has a different calling sequence
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewst_md5hmac(IN_ *in, OUT_ *out)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacmd5_t ctx;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacmd5_init(&ctx, in->key, in->key_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacmd5_update(&ctx, in->str, in->str_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews memset(dbuf.b, DIGEST_FILL, sizeof(dbuf));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacmd5_sign(&ctx, dbuf.b);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ck("HMAC-md5", in, out);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewst_hmac(const char *hname, HMAC_INIT init, UPDATE update, SIGN sign,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews IN_ *in, OUT_ *out)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews union {
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews unsigned char b[1024];
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacmd5_t hmacmd5;
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacsha1_t hmacsha1;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacsha224_t hmacsha224;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacsha256_t hmacsha256;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacsha384_t hmacsha384;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews isc_hmacsha512_t hmacsha512;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews } ctx;
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews init(&ctx, in->key, in->key_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews update(&ctx, in->str, in->str_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews memset(dbuf.b, DIGEST_FILL, sizeof(dbuf));
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews sign(&ctx, dbuf.b, out->digest_len);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews ck(hname, in, out);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewst_hmacs(IN_ *in, OUT_ *out_sha1, OUT_ *out_sha224, OUT_ *out_sha256,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews OUT_ *out_sha384, OUT_ *out_sha512)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmac("HMAC-SHA1", (HMAC_INIT)isc_hmacsha1_init,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (UPDATE)isc_hmacsha1_update, (SIGN)isc_hmacsha1_sign,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews in, out_sha1);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmac("HMAC-SHA224", (HMAC_INIT)isc_hmacsha224_init,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (UPDATE)isc_hmacsha224_update, (SIGN)isc_hmacsha224_sign,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews in, out_sha224);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmac("HMAC-SHA256", (HMAC_INIT)isc_hmacsha256_init,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (UPDATE)isc_hmacsha256_update, (SIGN)isc_hmacsha256_sign,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews in, out_sha256);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmac("HMAC-SHA384", (HMAC_INIT)isc_hmacsha384_init,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (UPDATE)isc_hmacsha384_update, (SIGN)isc_hmacsha384_sign,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews in, out_sha384);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmac("HMAC-SHA512", (HMAC_INIT)isc_hmacsha512_init,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews (UPDATE)isc_hmacsha512_update, (SIGN)isc_hmacsha512_sign,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews in, out_sha512);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews/*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * This will almost never fail, and so there is no need for the extra noise
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * that would come from breaking it into several tests.
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewsstatic void
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewst1(void)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews{
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews /*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * two ad hoc hash examples
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hashes(&abc, &abc_sha1, &abc_sha224, &abc_md5);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hashes(&abc_blah, &abc_blah_sha1, &abc_blah_sha224, &abc_blah_md5);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#else
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews t_hashes(&abc, &abc_sha1, &abc_sha224);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews t_hashes(&abc_blah, &abc_blah_sha1, &abc_blah_sha224);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews /*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * three HMAC-md5 examples from RFC 2104
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_md5hmac(&rfc2104_1, &rfc2104_1_hmac);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_md5hmac(&rfc2104_2, &rfc2104_2_hmac);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_md5hmac(&rfc2104_3, &rfc2104_3_hmac);
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews /*
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews * four HMAC-SHA tests from RFC 4634 starting on page 86
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews */
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmacs(&rfc4634_1, &rfc4634_1_sha1, &rfc4634_1_sha224,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews &rfc4634_1_sha256, &rfc4634_1_sha384, &rfc4634_1_sha512);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmacs(&rfc4634_2, &rfc4634_2_sha1, &rfc4634_2_sha224,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews &rfc4634_2_sha256, &rfc4634_2_sha384, &rfc4634_2_sha512);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmacs(&rfc4634_3, &rfc4634_3_sha1, &rfc4634_3_sha224,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews &rfc4634_3_sha256, &rfc4634_3_sha384, &rfc4634_3_sha512);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_hmacs(&rfc4634_4, &rfc4634_4_sha1, &rfc4634_4_sha224,
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews &rfc4634_4_sha256, &rfc4634_4_sha384, &rfc4634_4_sha512);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews if (nprobs != 0)
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_result(T_FAIL);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews else
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews t_result(T_PASS);
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews}
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrewstestspec_t T_testlist[] = {
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews { (PFV) t1, "hashes" },
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews { (PFV) 0, NULL }
2015023399657c36e9dcc8b5dc35ce20dfc876f0Mark Andrews};
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#ifdef WIN32
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsint
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrewsmain(int argc, char **argv) {
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews t_settests(T_testlist);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews return (t_main(argc, argv));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews}
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#endif