xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/wanboot/hmac/hmac.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
32*7c478bd9Sstevel@tonic-gate #include <libintl.h>
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <wanbootutil.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/wanboot_impl.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /* Return codes */
40*7c478bd9Sstevel@tonic-gate #define	HMAC_SUCCESS	0
41*7c478bd9Sstevel@tonic-gate #define	HMAC_NOKEY	1
42*7c478bd9Sstevel@tonic-gate #define	HMAC_ERROR	2
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /* Private buffer length */
45*7c478bd9Sstevel@tonic-gate #define	HMAC_BUF_LEN	1024
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * This routine is used to compute a hash digest for the file represented
49*7c478bd9Sstevel@tonic-gate  * by the file descirptor, 'fd'. The key, 'hmac_key', and key type, 'ka',
50*7c478bd9Sstevel@tonic-gate  * will be provided by the caller. The resulting hash digest will be
51*7c478bd9Sstevel@tonic-gate  * written to stdout.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * Returns:
54*7c478bd9Sstevel@tonic-gate  *	HMAC_SUCCESS or HMAC_ERROR.
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate static int
hash_gen(int in_fd,const wbku_key_attr_t * ka,const uint8_t * hmac_key)57*7c478bd9Sstevel@tonic-gate hash_gen(int in_fd, const wbku_key_attr_t *ka, const uint8_t *hmac_key)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	SHA1_CTX ctx;
60*7c478bd9Sstevel@tonic-gate 	uint8_t buf[HMAC_BUF_LEN];
61*7c478bd9Sstevel@tonic-gate 	ssize_t i;
62*7c478bd9Sstevel@tonic-gate 	uint8_t digest[HMAC_DIGEST_LEN];
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/*
65*7c478bd9Sstevel@tonic-gate 	 * Initialize the computation.
66*7c478bd9Sstevel@tonic-gate 	 */
67*7c478bd9Sstevel@tonic-gate 	HMACInit(&ctx, hmac_key, ka->ka_len);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/*
70*7c478bd9Sstevel@tonic-gate 	 * Read the data to hash.
71*7c478bd9Sstevel@tonic-gate 	 */
72*7c478bd9Sstevel@tonic-gate 	while ((i = read(in_fd, buf, HMAC_BUF_LEN)) > 0) {
73*7c478bd9Sstevel@tonic-gate 		HMACUpdate(&ctx, buf, i);
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	if (i < 0) {
76*7c478bd9Sstevel@tonic-gate 		wbku_printerr("Cannot read input_file");
77*7c478bd9Sstevel@tonic-gate 		return (HMAC_ERROR);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/*
81*7c478bd9Sstevel@tonic-gate 	 * Finalize the digest.
82*7c478bd9Sstevel@tonic-gate 	 */
83*7c478bd9Sstevel@tonic-gate 	HMACFinal(&ctx, hmac_key, ka->ka_len, digest);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/*
86*7c478bd9Sstevel@tonic-gate 	 * Write the digest to stdout.
87*7c478bd9Sstevel@tonic-gate 	 */
88*7c478bd9Sstevel@tonic-gate 	if (wbio_nwrite(STDOUT_FILENO, digest, sizeof (digest)) != 0) {
89*7c478bd9Sstevel@tonic-gate 		wbku_printerr("Cannot output digest");
90*7c478bd9Sstevel@tonic-gate 		return (HMAC_ERROR);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Success.
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	return (HMAC_SUCCESS);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Prints usage().
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate static void
usage(const char * cmd)103*7c478bd9Sstevel@tonic-gate usage(const char *cmd)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
106*7c478bd9Sstevel@tonic-gate 	    gettext("Usage: %s [-i input_file] -k key_file\n"), cmd);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate  * This program is used to compute a hash digest for data read in from
111*7c478bd9Sstevel@tonic-gate  * stdin or optionally, a file. The resulting hash digest will be written
112*7c478bd9Sstevel@tonic-gate  * to stdout.
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  * Returns:
115*7c478bd9Sstevel@tonic-gate  *	HMAC_SUCCESS, HMAC_ERROR or HMAC_NOKEY.
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)118*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	uint8_t hmac_key[WANBOOT_HMAC_KEY_SIZE];
121*7c478bd9Sstevel@tonic-gate 	int c;
122*7c478bd9Sstevel@tonic-gate 	char *infile_name = NULL;
123*7c478bd9Sstevel@tonic-gate 	char *keyfile_name = NULL;
124*7c478bd9Sstevel@tonic-gate 	int in_fd = -1;
125*7c478bd9Sstevel@tonic-gate 	FILE *key_fp = NULL;
126*7c478bd9Sstevel@tonic-gate 	wbku_key_attr_t ka;
127*7c478bd9Sstevel@tonic-gate 	wbku_retcode_t wbkuret;
128*7c478bd9Sstevel@tonic-gate 	int ret = HMAC_ERROR;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/*
131*7c478bd9Sstevel@tonic-gate 	 * Do the necessary magic for localization support.
132*7c478bd9Sstevel@tonic-gate 	 */
133*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
134*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
135*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
136*7c478bd9Sstevel@tonic-gate #endif
137*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	 * Initialize program name for use by wbku_printerr().
141*7c478bd9Sstevel@tonic-gate 	 */
142*7c478bd9Sstevel@tonic-gate 	wbku_errinit(argv[0]);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Should be at least three arguments.
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	if (argc < 3) {
148*7c478bd9Sstevel@tonic-gate 		usage(argv[0]);
149*7c478bd9Sstevel@tonic-gate 		return (HMAC_ERROR);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/*
153*7c478bd9Sstevel@tonic-gate 	 * Parse the options.
154*7c478bd9Sstevel@tonic-gate 	 */
155*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "i:k:")) != EOF) {
156*7c478bd9Sstevel@tonic-gate 		switch (c) {
157*7c478bd9Sstevel@tonic-gate 		case 'i':
158*7c478bd9Sstevel@tonic-gate 			/*
159*7c478bd9Sstevel@tonic-gate 			 * Optional input file.
160*7c478bd9Sstevel@tonic-gate 			 */
161*7c478bd9Sstevel@tonic-gate 			infile_name = optarg;
162*7c478bd9Sstevel@tonic-gate 			break;
163*7c478bd9Sstevel@tonic-gate 		case 'k':
164*7c478bd9Sstevel@tonic-gate 			/*
165*7c478bd9Sstevel@tonic-gate 			 * Path to key file.
166*7c478bd9Sstevel@tonic-gate 			 */
167*7c478bd9Sstevel@tonic-gate 			keyfile_name = optarg;
168*7c478bd9Sstevel@tonic-gate 			break;
169*7c478bd9Sstevel@tonic-gate 		default:
170*7c478bd9Sstevel@tonic-gate 			usage(argv[0]);
171*7c478bd9Sstevel@tonic-gate 			return (HMAC_ERROR);
172*7c478bd9Sstevel@tonic-gate 		}
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	/*
176*7c478bd9Sstevel@tonic-gate 	 * A key file must be defined.
177*7c478bd9Sstevel@tonic-gate 	 */
178*7c478bd9Sstevel@tonic-gate 	if (keyfile_name == NULL) {
179*7c478bd9Sstevel@tonic-gate 		wbku_printerr("Must specify the key_file\n");
180*7c478bd9Sstevel@tonic-gate 		return (HMAC_ERROR);
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	/*
184*7c478bd9Sstevel@tonic-gate 	 * If the user did not provide an input file for the data,
185*7c478bd9Sstevel@tonic-gate 	 * then use stdin as the source.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	if (infile_name == NULL) {
188*7c478bd9Sstevel@tonic-gate 		in_fd = STDIN_FILENO;
189*7c478bd9Sstevel@tonic-gate 	} else {
190*7c478bd9Sstevel@tonic-gate 		in_fd = open(infile_name, O_RDONLY);
191*7c478bd9Sstevel@tonic-gate 		if (in_fd < 0) {
192*7c478bd9Sstevel@tonic-gate 			wbku_printerr("Cannot open input_file");
193*7c478bd9Sstevel@tonic-gate 			return (HMAC_ERROR);
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/*
198*7c478bd9Sstevel@tonic-gate 	 * Open the key file for reading.
199*7c478bd9Sstevel@tonic-gate 	 */
200*7c478bd9Sstevel@tonic-gate 	if ((key_fp = fopen(keyfile_name, "r")) == NULL) {
201*7c478bd9Sstevel@tonic-gate 		wbku_printerr("Cannot open %s", keyfile_name);
202*7c478bd9Sstevel@tonic-gate 		goto out;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/*
206*7c478bd9Sstevel@tonic-gate 	 * Create a SHA1 key attribute structure. It's the only hash
207*7c478bd9Sstevel@tonic-gate 	 * type we support.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	wbkuret = wbku_str_to_keyattr(WBKU_KW_HMAC_SHA1, &ka, WBKU_HASH_KEY);
210*7c478bd9Sstevel@tonic-gate 	if (wbkuret != WBKU_SUCCESS) {
211*7c478bd9Sstevel@tonic-gate 		wbku_printerr("%s\n", wbku_retmsg(wbkuret));
212*7c478bd9Sstevel@tonic-gate 		goto out;
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	/*
216*7c478bd9Sstevel@tonic-gate 	 * Find the client key, if it exists.
217*7c478bd9Sstevel@tonic-gate 	 */
218*7c478bd9Sstevel@tonic-gate 	wbkuret = wbku_find_key(key_fp, NULL, &ka, hmac_key, B_FALSE);
219*7c478bd9Sstevel@tonic-gate 	if (wbkuret != WBKU_SUCCESS) {
220*7c478bd9Sstevel@tonic-gate 		wbku_printerr("%s\n", wbku_retmsg(wbkuret));
221*7c478bd9Sstevel@tonic-gate 		ret = (wbkuret == WBKU_NOKEY) ? HMAC_NOKEY : HMAC_ERROR;
222*7c478bd9Sstevel@tonic-gate 	} else {
223*7c478bd9Sstevel@tonic-gate 		ret = hash_gen(in_fd, &ka, hmac_key);
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate out:
226*7c478bd9Sstevel@tonic-gate 	/*
227*7c478bd9Sstevel@tonic-gate 	 * Cleanup.
228*7c478bd9Sstevel@tonic-gate 	 */
229*7c478bd9Sstevel@tonic-gate 	if (in_fd != -1) {
230*7c478bd9Sstevel@tonic-gate 		(void) close(in_fd);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	if (key_fp != NULL) {
233*7c478bd9Sstevel@tonic-gate 		(void) fclose(key_fp);
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	return (ret);
237*7c478bd9Sstevel@tonic-gate }
238