1 /* $NetBSD: isc-hmac-fixup.c,v 1.7 2014/12/10 04:37:54 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2010, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* Id: isc-hmac-fixup.c,v 1.4 2010/03/10 02:17:52 marka Exp */ 20 21 #include <config.h> 22 23 #include <isc/base64.h> 24 #include <isc/buffer.h> 25 #include <isc/md5.h> 26 #include <isc/region.h> 27 #include <isc/result.h> 28 #include <isc/sha1.h> 29 #include <isc/sha2.h> 30 #include <isc/stdio.h> 31 #include <isc/string.h> 32 33 #define HMAC_LEN 64 34 35 int 36 main(int argc, char **argv) { 37 isc_buffer_t buf; 38 unsigned char key[1024]; 39 char secret[1024]; 40 char base64[(1024*4)/3]; 41 isc_region_t r; 42 isc_result_t result; 43 44 if (argc != 3) { 45 fprintf(stderr, "Usage:\t%s algorithm secret\n", argv[0]); 46 fprintf(stderr, "\talgorithm: (MD5 | SHA1 | SHA224 | " 47 "SHA256 | SHA384 | SHA512)\n"); 48 return (1); 49 } 50 51 isc_buffer_init(&buf, secret, sizeof(secret)); 52 result = isc_base64_decodestring(argv[2], &buf); 53 if (result != ISC_R_SUCCESS) { 54 fprintf(stderr, "error: %s\n", isc_result_totext(result)); 55 return (1); 56 } 57 isc_buffer_usedregion(&buf, &r); 58 59 if (!strcasecmp(argv[1], "md5") || 60 !strcasecmp(argv[1], "hmac-md5")) { 61 if (r.length > HMAC_LEN) { 62 isc_md5_t md5ctx; 63 isc_md5_init(&md5ctx); 64 isc_md5_update(&md5ctx, r.base, r.length); 65 isc_md5_final(&md5ctx, key); 66 67 r.base = key; 68 r.length = ISC_MD5_DIGESTLENGTH; 69 } 70 } else if (!strcasecmp(argv[1], "sha1") || 71 !strcasecmp(argv[1], "hmac-sha1")) { 72 if (r.length > ISC_SHA1_DIGESTLENGTH) { 73 isc_sha1_t sha1ctx; 74 isc_sha1_init(&sha1ctx); 75 isc_sha1_update(&sha1ctx, r.base, r.length); 76 isc_sha1_final(&sha1ctx, key); 77 78 r.base = key; 79 r.length = ISC_SHA1_DIGESTLENGTH; 80 } 81 } else if (!strcasecmp(argv[1], "sha224") || 82 !strcasecmp(argv[1], "hmac-sha224")) { 83 if (r.length > ISC_SHA224_DIGESTLENGTH) { 84 isc_sha224_t sha224ctx; 85 isc_sha224_init(&sha224ctx); 86 isc_sha224_update(&sha224ctx, r.base, r.length); 87 isc_sha224_final(key, &sha224ctx); 88 89 r.base = key; 90 r.length = ISC_SHA224_DIGESTLENGTH; 91 } 92 } else if (!strcasecmp(argv[1], "sha256") || 93 !strcasecmp(argv[1], "hmac-sha256")) { 94 if (r.length > ISC_SHA256_DIGESTLENGTH) { 95 isc_sha256_t sha256ctx; 96 isc_sha256_init(&sha256ctx); 97 isc_sha256_update(&sha256ctx, r.base, r.length); 98 isc_sha256_final(key, &sha256ctx); 99 100 r.base = key; 101 r.length = ISC_SHA256_DIGESTLENGTH; 102 } 103 } else if (!strcasecmp(argv[1], "sha384") || 104 !strcasecmp(argv[1], "hmac-sha384")) { 105 if (r.length > ISC_SHA384_DIGESTLENGTH) { 106 isc_sha384_t sha384ctx; 107 isc_sha384_init(&sha384ctx); 108 isc_sha384_update(&sha384ctx, r.base, r.length); 109 isc_sha384_final(key, &sha384ctx); 110 111 r.base = key; 112 r.length = ISC_SHA384_DIGESTLENGTH; 113 } 114 } else if (!strcasecmp(argv[1], "sha512") || 115 !strcasecmp(argv[1], "hmac-sha512")) { 116 if (r.length > ISC_SHA512_DIGESTLENGTH) { 117 isc_sha512_t sha512ctx; 118 isc_sha512_init(&sha512ctx); 119 isc_sha512_update(&sha512ctx, r.base, r.length); 120 isc_sha512_final(key, &sha512ctx); 121 122 r.base = key; 123 r.length = ISC_SHA512_DIGESTLENGTH; 124 } 125 } else { 126 fprintf(stderr, "unknown hmac/digest algorithm: %s\n", argv[1]); 127 return (1); 128 } 129 130 isc_buffer_init(&buf, base64, sizeof(base64)); 131 result = isc_base64_totext(&r, 0, "", &buf); 132 if (result != ISC_R_SUCCESS) { 133 fprintf(stderr, "error: %s\n", isc_result_totext(result)); 134 return (1); 135 } 136 fprintf(stdout, "%.*s\n", (int)isc_buffer_usedlength(&buf), base64); 137 return (0); 138 } 139