1/* 2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 2000, 2001 Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 * PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/* $Id: hash_test.c,v 1.19 2007/06/19 23:46:59 tbox Exp $ */ 19 20/*! \file */ 21#include <config.h> 22 23#include <stdio.h> 24#include <string.h> 25 26#include <isc/hmacmd5.h> 27#include <isc/hmacsha.h> 28#include <isc/md5.h> 29#include <isc/sha1.h> 30#include <isc/util.h> 31#include <isc/string.h> 32 33static void 34print_digest(const char *s, const char *hash, unsigned char *d, 35 unsigned int words) 36{ 37 unsigned int i, j; 38 39 printf("hash (%s) %s:\n\t", hash, s); 40 for (i = 0; i < words; i++) { 41 printf(" "); 42 for (j = 0; j < 4; j++) 43 printf("%02x", d[i * 4 + j]); 44 } 45 printf("\n"); 46} 47 48int 49main(int argc, char **argv) { 50 isc_sha1_t sha1; 51 isc_sha224_t sha224; 52 isc_md5_t md5; 53 isc_hmacmd5_t hmacmd5; 54 isc_hmacsha1_t hmacsha1; 55 isc_hmacsha224_t hmacsha224; 56 isc_hmacsha256_t hmacsha256; 57 isc_hmacsha384_t hmacsha384; 58 isc_hmacsha512_t hmacsha512; 59 unsigned char digest[ISC_SHA512_DIGESTLENGTH]; 60 unsigned char buffer[1024]; 61 const char *s; 62 unsigned char key[20]; 63 64 UNUSED(argc); 65 UNUSED(argv); 66 67 s = "abc"; 68 isc_sha1_init(&sha1); 69 memcpy(buffer, s, strlen(s)); 70 isc_sha1_update(&sha1, buffer, strlen(s)); 71 isc_sha1_final(&sha1, digest); 72 print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4); 73 74 s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 75 isc_sha1_init(&sha1); 76 memcpy(buffer, s, strlen(s)); 77 isc_sha1_update(&sha1, buffer, strlen(s)); 78 isc_sha1_final(&sha1, digest); 79 print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4); 80 81 s = "abc"; 82 isc_sha224_init(&sha224); 83 memcpy(buffer, s, strlen(s)); 84 isc_sha224_update(&sha224, buffer, strlen(s)); 85 isc_sha224_final(digest, &sha224); 86 print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4); 87 88 s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 89 isc_sha224_init(&sha224); 90 memcpy(buffer, s, strlen(s)); 91 isc_sha224_update(&sha224, buffer, strlen(s)); 92 isc_sha224_final(digest, &sha224); 93 print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4); 94 95 s = "abc"; 96 isc_md5_init(&md5); 97 memcpy(buffer, s, strlen(s)); 98 isc_md5_update(&md5, buffer, strlen(s)); 99 isc_md5_final(&md5, digest); 100 print_digest(s, "md5", digest, 4); 101 102 /* 103 * The 3 HMAC-MD5 examples from RFC2104 104 */ 105 s = "Hi There"; 106 memset(key, 0x0b, 16); 107 isc_hmacmd5_init(&hmacmd5, key, 16); 108 memcpy(buffer, s, strlen(s)); 109 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s)); 110 isc_hmacmd5_sign(&hmacmd5, digest); 111 print_digest(s, "hmacmd5", digest, 4); 112 113 s = "what do ya want for nothing?"; 114 strcpy((char *)key, "Jefe"); 115 isc_hmacmd5_init(&hmacmd5, key, 4); 116 memcpy(buffer, s, strlen(s)); 117 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s)); 118 isc_hmacmd5_sign(&hmacmd5, digest); 119 print_digest(s, "hmacmd5", digest, 4); 120 121 s = "\335\335\335\335\335\335\335\335\335\335" 122 "\335\335\335\335\335\335\335\335\335\335" 123 "\335\335\335\335\335\335\335\335\335\335" 124 "\335\335\335\335\335\335\335\335\335\335" 125 "\335\335\335\335\335\335\335\335\335\335"; 126 memset(key, 0xaa, 16); 127 isc_hmacmd5_init(&hmacmd5, key, 16); 128 memcpy(buffer, s, strlen(s)); 129 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s)); 130 isc_hmacmd5_sign(&hmacmd5, digest); 131 print_digest(s, "hmacmd5", digest, 4); 132 133 /* 134 * The 3 HMAC-SHA1 examples from RFC4634. 135 */ 136 s = "Hi There"; 137 memset(key, 0x0b, 20); 138 isc_hmacsha1_init(&hmacsha1, key, 20); 139 memcpy(buffer, s, strlen(s)); 140 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s)); 141 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH); 142 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4); 143 144 s = "what do ya want for nothing?"; 145 strcpy((char *)key, "Jefe"); 146 isc_hmacsha1_init(&hmacsha1, key, 4); 147 memcpy(buffer, s, strlen(s)); 148 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s)); 149 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH); 150 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4); 151 152 s = "\335\335\335\335\335\335\335\335\335\335" 153 "\335\335\335\335\335\335\335\335\335\335" 154 "\335\335\335\335\335\335\335\335\335\335" 155 "\335\335\335\335\335\335\335\335\335\335" 156 "\335\335\335\335\335\335\335\335\335\335"; 157 memset(key, 0xaa, 20); 158 isc_hmacsha1_init(&hmacsha1, key, 20); 159 memcpy(buffer, s, strlen(s)); 160 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s)); 161 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH); 162 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4); 163 164 /* 165 * The 3 HMAC-SHA224 examples from RFC4634. 166 */ 167 s = "Hi There"; 168 memset(key, 0x0b, 20); 169 isc_hmacsha224_init(&hmacsha224, key, 20); 170 memcpy(buffer, s, strlen(s)); 171 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s)); 172 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH); 173 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4); 174 175 s = "what do ya want for nothing?"; 176 strcpy((char *)key, "Jefe"); 177 isc_hmacsha224_init(&hmacsha224, key, 4); 178 memcpy(buffer, s, strlen(s)); 179 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s)); 180 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH); 181 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4); 182 183 s = "\335\335\335\335\335\335\335\335\335\335" 184 "\335\335\335\335\335\335\335\335\335\335" 185 "\335\335\335\335\335\335\335\335\335\335" 186 "\335\335\335\335\335\335\335\335\335\335" 187 "\335\335\335\335\335\335\335\335\335\335"; 188 memset(key, 0xaa, 20); 189 isc_hmacsha224_init(&hmacsha224, key, 20); 190 memcpy(buffer, s, strlen(s)); 191 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s)); 192 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH); 193 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4); 194 195 /* 196 * The 3 HMAC-SHA256 examples from RFC4634. 197 */ 198 s = "Hi There"; 199 memset(key, 0x0b, 20); 200 isc_hmacsha256_init(&hmacsha256, key, 20); 201 memcpy(buffer, s, strlen(s)); 202 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s)); 203 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH); 204 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4); 205 206 s = "what do ya want for nothing?"; 207 strcpy((char *)key, "Jefe"); 208 isc_hmacsha256_init(&hmacsha256, key, 4); 209 memcpy(buffer, s, strlen(s)); 210 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s)); 211 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH); 212 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4); 213 214 s = "\335\335\335\335\335\335\335\335\335\335" 215 "\335\335\335\335\335\335\335\335\335\335" 216 "\335\335\335\335\335\335\335\335\335\335" 217 "\335\335\335\335\335\335\335\335\335\335" 218 "\335\335\335\335\335\335\335\335\335\335"; 219 memset(key, 0xaa, 20); 220 isc_hmacsha256_init(&hmacsha256, key, 20); 221 memcpy(buffer, s, strlen(s)); 222 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s)); 223 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH); 224 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4); 225 226 /* 227 * The 3 HMAC-SHA384 examples from RFC4634. 228 */ 229 s = "Hi There"; 230 memset(key, 0x0b, 20); 231 isc_hmacsha384_init(&hmacsha384, key, 20); 232 memcpy(buffer, s, strlen(s)); 233 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s)); 234 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH); 235 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4); 236 237 s = "what do ya want for nothing?"; 238 strcpy((char *)key, "Jefe"); 239 isc_hmacsha384_init(&hmacsha384, key, 4); 240 memcpy(buffer, s, strlen(s)); 241 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s)); 242 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH); 243 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4); 244 245 s = "\335\335\335\335\335\335\335\335\335\335" 246 "\335\335\335\335\335\335\335\335\335\335" 247 "\335\335\335\335\335\335\335\335\335\335" 248 "\335\335\335\335\335\335\335\335\335\335" 249 "\335\335\335\335\335\335\335\335\335\335"; 250 memset(key, 0xaa, 20); 251 isc_hmacsha384_init(&hmacsha384, key, 20); 252 memcpy(buffer, s, strlen(s)); 253 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s)); 254 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH); 255 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4); 256 257 /* 258 * The 3 HMAC-SHA512 examples from RFC4634. 259 */ 260 s = "Hi There"; 261 memset(key, 0x0b, 20); 262 isc_hmacsha512_init(&hmacsha512, key, 20); 263 memcpy(buffer, s, strlen(s)); 264 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s)); 265 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH); 266 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4); 267 268 s = "what do ya want for nothing?"; 269 strcpy((char *)key, "Jefe"); 270 isc_hmacsha512_init(&hmacsha512, key, 4); 271 memcpy(buffer, s, strlen(s)); 272 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s)); 273 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH); 274 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4); 275 276 s = "\335\335\335\335\335\335\335\335\335\335" 277 "\335\335\335\335\335\335\335\335\335\335" 278 "\335\335\335\335\335\335\335\335\335\335" 279 "\335\335\335\335\335\335\335\335\335\335" 280 "\335\335\335\335\335\335\335\335\335\335"; 281 memset(key, 0xaa, 20); 282 isc_hmacsha512_init(&hmacsha512, key, 20); 283 memcpy(buffer, s, strlen(s)); 284 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s)); 285 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH); 286 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4); 287 288 return (0); 289} 290