etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tools / isc-hmac-fixup.c
blob9debde5cef5de662d7eafc3edae507fb7f17466d
1 /* $NetBSD: isc-hmac-fixup.c,v 1.7 2014/12/10 04:37:54 christos Exp $ */
3 /*
4 * Copyright (C) 2010, 2014 Internet Systems Consortium, Inc. ("ISC")
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.
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.
19 /* Id: isc-hmac-fixup.c,v 1.4 2010/03/10 02:17:52 marka Exp */
21 #include <config.h>
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>
33 #define HMAC_LEN 64
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;
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);
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);
57 isc_buffer_usedregion(&buf, &r);
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);
67 r.base = key;
68 r.length = ISC_MD5_DIGESTLENGTH;
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);
78 r.base = key;
79 r.length = ISC_SHA1_DIGESTLENGTH;
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);
89 r.base = key;
90 r.length = ISC_SHA224_DIGESTLENGTH;
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);
100 r.base = key;
101 r.length = ISC_SHA256_DIGESTLENGTH;
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);
111 r.base = key;
112 r.length = ISC_SHA384_DIGESTLENGTH;
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);
122 r.base = key;
123 r.length = ISC_SHA512_DIGESTLENGTH;
125 } else {
126 fprintf(stderr, "unknown hmac/digest algorithm: %s\n", argv[1]);
127 return (1);
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);
136 fprintf(stdout, "%.*s\n", (int)isc_buffer_usedlength(&buf), base64);
137 return (0);