No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tools / nsec3hash.c
blob53f9d82b20d02d33e12f77f9593d0926b10442f9
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2006, 2008, 2009 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: nsec3hash.c,v 1.6 2009/10/06 21:20:44 each Exp */
21 #include <config.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include <isc/base32.h>
27 #include <isc/buffer.h>
28 #include <isc/hex.h>
29 #include <isc/iterated_hash.h>
30 #include <isc/print.h>
31 #include <isc/result.h>
32 #include <isc/string.h>
33 #include <isc/types.h>
35 #include <dns/fixedname.h>
36 #include <dns/name.h>
37 #include <dns/nsec3.h>
38 #include <dns/types.h>
40 const char *program = "nsec3hash";
42 ISC_PLATFORM_NORETURN_PRE static void
43 fatal(const char *format, ...) ISC_PLATFORM_NORETURN_POST;
45 static void
46 fatal(const char *format, ...) {
47 va_list args;
49 fprintf(stderr, "%s: ", program);
50 va_start(args, format);
51 vfprintf(stderr, format, args);
52 va_end(args);
53 fprintf(stderr, "\n");
54 exit(1);
57 static void
58 check_result(isc_result_t result, const char *message) {
59 if (result != ISC_R_SUCCESS)
60 fatal("%s: %s", message, isc_result_totext(result));
63 static void
64 usage() {
65 fatal("salt hash iterations domain");
68 int
69 main(int argc, char **argv) {
70 dns_fixedname_t fixed;
71 dns_name_t *name;
72 isc_buffer_t buffer;
73 isc_region_t region;
74 isc_result_t result;
75 unsigned char hash[NSEC3_MAX_HASH_LENGTH];
76 unsigned char salt[DNS_NSEC3_SALTSIZE];
77 unsigned char text[1024];
78 unsigned int hash_alg;
79 unsigned int length;
80 unsigned int iterations;
81 unsigned int salt_length;
83 if (argc != 5)
84 usage();
86 if (strcmp(argv[1], "-") == 0) {
87 salt_length = 0;
88 salt[0] = 0;
89 } else {
90 isc_buffer_init(&buffer, salt, sizeof(salt));
91 result = isc_hex_decodestring(argv[1], &buffer);
92 check_result(result, "isc_hex_decodestring(salt)");
93 salt_length = isc_buffer_usedlength(&buffer);
94 if (salt_length > DNS_NSEC3_SALTSIZE)
95 fatal("salt too long");
97 hash_alg = atoi(argv[2]);
98 if (hash_alg > 255U)
99 fatal("hash algorithm too large");
100 iterations = atoi(argv[3]);
101 if (iterations > 0xffffU)
102 fatal("iterations to large");
104 dns_fixedname_init(&fixed);
105 name = dns_fixedname_name(&fixed);
106 isc_buffer_init(&buffer, argv[4], strlen(argv[4]));
107 isc_buffer_add(&buffer, strlen(argv[4]));
108 result = dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL);
109 check_result(result, "dns_name_fromtext() failed");
111 dns_name_downcase(name, name, NULL);
112 length = isc_iterated_hash(hash, hash_alg, iterations, salt,
113 salt_length, name->ndata, name->length);
114 if (length == 0)
115 fatal("isc_iterated_hash failed");
116 region.base = hash;
117 region.length = length;
118 isc_buffer_init(&buffer, text, sizeof(text));
119 isc_base32hex_totext(&region, 1, "", &buffer);
120 fprintf(stdout, "%.*s (salt=%s, hash=%u, iterations=%u)\n",
121 (int)isc_buffer_usedlength(&buffer), text, argv[1], hash_alg, iterations);
122 return(0);