xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/wanboot/encr/encr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <stdio.h>
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <fcntl.h>
32*0Sstevel@tonic-gate #include <libintl.h>
33*0Sstevel@tonic-gate #include <locale.h>
34*0Sstevel@tonic-gate #include <sys/des.h>
35*0Sstevel@tonic-gate #include <strings.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <wanbootutil.h>
38*0Sstevel@tonic-gate #include <sys/sysmacros.h>
39*0Sstevel@tonic-gate #include <sys/wanboot_impl.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /* Return codes */
42*0Sstevel@tonic-gate #define	ENCR_SUCCESS	0
43*0Sstevel@tonic-gate #define	ENCR_NOKEY	1
44*0Sstevel@tonic-gate #define	ENCR_ERROR	2
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /* Private buffer length */
47*0Sstevel@tonic-gate #define	ENCR_BUF_LEN		1024
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /* Encryption algorithm suboption. */
50*0Sstevel@tonic-gate #define	TYPE	0
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static char *opts[] = { "type", NULL };
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * This routine is used to parse the suboptions of '-o' option.
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  * The option should be of the form: type=<3des|aes>
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * This routine will pass the value of the suboption back in the
60*0Sstevel@tonic-gate  * supplied arguments, 'ka'.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * Returns:
63*0Sstevel@tonic-gate  *	ENCR_SUCCESS or ENCR_ERROR.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate static int
process_option(char * arg,wbku_key_attr_t * ka)66*0Sstevel@tonic-gate process_option(char *arg, wbku_key_attr_t *ka)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	char *value;
69*0Sstevel@tonic-gate 	wbku_retcode_t ret;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	while (*arg != '\0') {
72*0Sstevel@tonic-gate 		switch (getsubopt(&arg, opts, &value)) {
73*0Sstevel@tonic-gate 		case TYPE:
74*0Sstevel@tonic-gate 			/*
75*0Sstevel@tonic-gate 			 * Key type.
76*0Sstevel@tonic-gate 			 */
77*0Sstevel@tonic-gate 			ret = wbku_str_to_keyattr(value, ka, WBKU_ENCR_KEY);
78*0Sstevel@tonic-gate 			if (ret != WBKU_SUCCESS) {
79*0Sstevel@tonic-gate 				wbku_printerr("%s\n", wbku_retmsg(ret));
80*0Sstevel@tonic-gate 				return (ENCR_ERROR);
81*0Sstevel@tonic-gate 			}
82*0Sstevel@tonic-gate 			break;
83*0Sstevel@tonic-gate 		default:
84*0Sstevel@tonic-gate 			wbku_printerr("Invalid option %s\n", value);
85*0Sstevel@tonic-gate 			return (ENCR_ERROR);
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	return (ENCR_SUCCESS);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  * This routine is used to find the key of type defined by 'ka' and
94*0Sstevel@tonic-gate  * return it in 'key'. The key file should have been opened by the
95*0Sstevel@tonic-gate  * caller and the handle passed in 'key_fp'.
96*0Sstevel@tonic-gate  *
97*0Sstevel@tonic-gate  * Returns:
98*0Sstevel@tonic-gate  *	ENCR_SUCCESS, ENCR_ERROR or ENCR_NOKEY.
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate static int
get_key(FILE * key_fp,wbku_key_attr_t * ka,uint8_t * key)101*0Sstevel@tonic-gate get_key(FILE *key_fp, wbku_key_attr_t *ka, uint8_t *key)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	wbku_retcode_t ret;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/*
106*0Sstevel@tonic-gate 	 * Find the client key, if it exists.
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 	ret = wbku_find_key(key_fp, NULL, ka, key, B_FALSE);
109*0Sstevel@tonic-gate 	if (ret != WBKU_SUCCESS) {
110*0Sstevel@tonic-gate 		wbku_printerr("%s\n", wbku_retmsg(ret));
111*0Sstevel@tonic-gate 		if (ret == WBKU_NOKEY)
112*0Sstevel@tonic-gate 			return (ENCR_NOKEY);
113*0Sstevel@tonic-gate 		else
114*0Sstevel@tonic-gate 			return (ENCR_ERROR);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 	return (ENCR_SUCCESS);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /*
120*0Sstevel@tonic-gate  * This routine is the common encryption routine used to encrypt data
121*0Sstevel@tonic-gate  * using the CBC handle initialized by the calling routine.  The data
122*0Sstevel@tonic-gate  * to be encrypted is read from stdin and the encrypted data is written to
123*0Sstevel@tonic-gate  * stdout.
124*0Sstevel@tonic-gate  *
125*0Sstevel@tonic-gate  * Returns:
126*0Sstevel@tonic-gate  *	ENCR_SUCCESS or ENCR_ERROR.
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate static int
encr_gen(cbc_handle_t * ch)129*0Sstevel@tonic-gate encr_gen(cbc_handle_t *ch)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	uint8_t iv[WANBOOT_MAXBLOCKLEN];
132*0Sstevel@tonic-gate 	uint8_t buf[ENCR_BUF_LEN];
133*0Sstevel@tonic-gate 	uint8_t *bufp;
134*0Sstevel@tonic-gate 	int read_size;
135*0Sstevel@tonic-gate 	ssize_t i, j, k;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	/*
138*0Sstevel@tonic-gate 	 * Use a random number as the IV
139*0Sstevel@tonic-gate 	 */
140*0Sstevel@tonic-gate 	if (wbio_nread_rand(iv, ch->blocklen) != 0) {
141*0Sstevel@tonic-gate 		wbku_printerr("Cannot generate initialization vector");
142*0Sstevel@tonic-gate 		return (ENCR_ERROR);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * Output the IV to stdout.
147*0Sstevel@tonic-gate 	 */
148*0Sstevel@tonic-gate 	if (wbio_nwrite(STDOUT_FILENO, iv, ch->blocklen) != 0) {
149*0Sstevel@tonic-gate 		wbku_printerr("Write error encountered\n");
150*0Sstevel@tonic-gate 		return (ENCR_ERROR);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	/*
154*0Sstevel@tonic-gate 	 * Try to read in multiple of block_size as CBC requires
155*0Sstevel@tonic-gate 	 * that data be encrypted in block_size chunks.
156*0Sstevel@tonic-gate 	 */
157*0Sstevel@tonic-gate 	read_size = ENCR_BUF_LEN / ch->blocklen * ch->blocklen;
158*0Sstevel@tonic-gate 	while ((i = read(STDIN_FILENO, buf, read_size)) > 0) {
159*0Sstevel@tonic-gate 		/*
160*0Sstevel@tonic-gate 		 * If data received is not a multiple of the block size,
161*0Sstevel@tonic-gate 		 * try to receive more.  If reach EOF, pad the rest with
162*0Sstevel@tonic-gate 		 * 0.
163*0Sstevel@tonic-gate 		 */
164*0Sstevel@tonic-gate 		if ((j = i % ch->blocklen) != 0) {
165*0Sstevel@tonic-gate 			/*
166*0Sstevel@tonic-gate 			 * Determine how more data need to be received to
167*0Sstevel@tonic-gate 			 * fill out the buffer so that it contains a
168*0Sstevel@tonic-gate 			 * multiple of block_size chunks.
169*0Sstevel@tonic-gate 			 */
170*0Sstevel@tonic-gate 			j = ch->blocklen - j;
171*0Sstevel@tonic-gate 			bufp = buf + i;
172*0Sstevel@tonic-gate 			k = j;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 			/*
175*0Sstevel@tonic-gate 			 * Try to fill the gap.
176*0Sstevel@tonic-gate 			 *
177*0Sstevel@tonic-gate 			 */
178*0Sstevel@tonic-gate 			while ((j = read(STDIN_FILENO, bufp, j)) != k &&
179*0Sstevel@tonic-gate 			    j != 0) {
180*0Sstevel@tonic-gate 				bufp += j;
181*0Sstevel@tonic-gate 				k -= j;
182*0Sstevel@tonic-gate 				j = k;
183*0Sstevel@tonic-gate 			}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 			/*
186*0Sstevel@tonic-gate 			 * This is the total length of the buffer.
187*0Sstevel@tonic-gate 			 */
188*0Sstevel@tonic-gate 			i = (i + ch->blocklen) - (i % ch->blocklen);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 			if (j == 0) {
191*0Sstevel@tonic-gate 				/* EOF, do padding. */
192*0Sstevel@tonic-gate 				(void) memset(bufp, 0, k);
193*0Sstevel@tonic-gate 				(void) cbc_encrypt(ch, buf, i, iv);
194*0Sstevel@tonic-gate 			} else if (j > 0) {
195*0Sstevel@tonic-gate 				/* The gap has been filled in */
196*0Sstevel@tonic-gate 				(void) cbc_encrypt(ch, buf, i, iv);
197*0Sstevel@tonic-gate 			} else {
198*0Sstevel@tonic-gate 				/* Oops. */
199*0Sstevel@tonic-gate 				wbku_printerr("Input error");
200*0Sstevel@tonic-gate 				return (ENCR_ERROR);
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 		} else {
203*0Sstevel@tonic-gate 			/* A multiple of the block size was received */
204*0Sstevel@tonic-gate 			(void) cbc_encrypt(ch, buf, i, iv);
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		if (wbio_nwrite(STDOUT_FILENO, buf, i) != 0) {
207*0Sstevel@tonic-gate 			wbku_printerr("Write error encountered\n");
208*0Sstevel@tonic-gate 			return (ENCR_ERROR);
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	return (ENCR_SUCCESS);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*
216*0Sstevel@tonic-gate  * This routine initializes a CBC handle for 3DES and calls the
217*0Sstevel@tonic-gate  * common encryption routine to encrypt data.
218*0Sstevel@tonic-gate  *
219*0Sstevel@tonic-gate  * Returns:
220*0Sstevel@tonic-gate  *	ENCR_SUCCESS or ENCR_ERROR.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate static int
encr_gen_3des(const wbku_key_attr_t * ka,const uint8_t * key)223*0Sstevel@tonic-gate encr_gen_3des(const wbku_key_attr_t *ka, const uint8_t *key)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	cbc_handle_t ch;
226*0Sstevel@tonic-gate 	void *eh;
227*0Sstevel@tonic-gate 	int ret;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	/*
230*0Sstevel@tonic-gate 	 * Initialize a 3DES handle.
231*0Sstevel@tonic-gate 	 */
232*0Sstevel@tonic-gate 	if (des3_init(&eh) != 0) {
233*0Sstevel@tonic-gate 		return (ENCR_ERROR);
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	des3_key(eh, key);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	/*
238*0Sstevel@tonic-gate 	 * Initialize the CBC handle.
239*0Sstevel@tonic-gate 	 */
240*0Sstevel@tonic-gate 	cbc_makehandle(&ch, eh, ka->ka_len, DES3_BLOCK_SIZE,
241*0Sstevel@tonic-gate 	    DES3_IV_SIZE, des3_encrypt, des3_decrypt);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * Encrypt the data.
245*0Sstevel@tonic-gate 	 */
246*0Sstevel@tonic-gate 	ret = encr_gen(&ch);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	/*
249*0Sstevel@tonic-gate 	 *  Free the 3DES resources.
250*0Sstevel@tonic-gate 	 */
251*0Sstevel@tonic-gate 	des3_fini(eh);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	return (ret);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate /*
257*0Sstevel@tonic-gate  * This routine initializes a CBC handle for AES and calls the
258*0Sstevel@tonic-gate  * common encryption routine to encrypt data.
259*0Sstevel@tonic-gate  *
260*0Sstevel@tonic-gate  * Returns:
261*0Sstevel@tonic-gate  *	ENCR_SUCCESS or ENCR_ERROR.
262*0Sstevel@tonic-gate  */
263*0Sstevel@tonic-gate static int
encr_gen_aes(const wbku_key_attr_t * ka,const uint8_t * key)264*0Sstevel@tonic-gate encr_gen_aes(const wbku_key_attr_t *ka, const uint8_t *key)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	cbc_handle_t ch;
267*0Sstevel@tonic-gate 	void *eh;
268*0Sstevel@tonic-gate 	int ret;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * Initialize an AES handle.
272*0Sstevel@tonic-gate 	 */
273*0Sstevel@tonic-gate 	if (aes_init(&eh) != 0) {
274*0Sstevel@tonic-gate 		return (ENCR_ERROR);
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 	aes_key(eh, key, ka->ka_len);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	/*
279*0Sstevel@tonic-gate 	 * Initialize the CBC handle.
280*0Sstevel@tonic-gate 	 */
281*0Sstevel@tonic-gate 	cbc_makehandle(&ch, eh, ka->ka_len, AES_BLOCK_SIZE,
282*0Sstevel@tonic-gate 	    AES_IV_SIZE, aes_encrypt, aes_decrypt);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	/*
285*0Sstevel@tonic-gate 	 * Encrypt the data.
286*0Sstevel@tonic-gate 	 */
287*0Sstevel@tonic-gate 	ret = encr_gen(&ch);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	/*
290*0Sstevel@tonic-gate 	 *  Free the AES resources.
291*0Sstevel@tonic-gate 	 */
292*0Sstevel@tonic-gate 	aes_fini(eh);
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	return (ret);
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate /*
298*0Sstevel@tonic-gate  * Prints usage().
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate static void
usage(const char * cmd)301*0Sstevel@tonic-gate usage(const char *cmd)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate 	(void) fprintf(stderr,
304*0Sstevel@tonic-gate 	    gettext("Usage: %s -o type=<%s|%s> -k key_file\n"),
305*0Sstevel@tonic-gate 	    cmd, WBKU_KW_3DES, WBKU_KW_AES_128);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate  * This program is used to encrypt data read from stdin and print it to
310*0Sstevel@tonic-gate  * stdout. The path to the key file and the algorithm to use are
311*0Sstevel@tonic-gate  * provided by the user.
312*0Sstevel@tonic-gate  *
313*0Sstevel@tonic-gate  * Returns:
314*0Sstevel@tonic-gate  *	ENCR_SUCCESS, ENCR_ERROR or ENCR_NOKEY.
315*0Sstevel@tonic-gate  */
316*0Sstevel@tonic-gate int
main(int argc,char ** argv)317*0Sstevel@tonic-gate main(int argc, char **argv)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	uint8_t key[WANBOOT_MAXKEYLEN];
320*0Sstevel@tonic-gate 	int c;
321*0Sstevel@tonic-gate 	char *keyfile_name = NULL;
322*0Sstevel@tonic-gate 	wbku_key_attr_t ka;
323*0Sstevel@tonic-gate 	FILE *key_fp;
324*0Sstevel@tonic-gate 	int ret;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	/*
327*0Sstevel@tonic-gate 	 * Do the necessary magic for localization support.
328*0Sstevel@tonic-gate 	 */
329*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
330*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
331*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
332*0Sstevel@tonic-gate #endif
333*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	/*
336*0Sstevel@tonic-gate 	 * Initialize program name for use by wbku_printerr().
337*0Sstevel@tonic-gate 	 */
338*0Sstevel@tonic-gate 	wbku_errinit(argv[0]);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/*
341*0Sstevel@tonic-gate 	 * Should be five arguments.
342*0Sstevel@tonic-gate 	 */
343*0Sstevel@tonic-gate 	if (argc < 5) {
344*0Sstevel@tonic-gate 		usage(argv[0]);
345*0Sstevel@tonic-gate 		return (ENCR_ERROR);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	/*
349*0Sstevel@tonic-gate 	 * Parse the options.
350*0Sstevel@tonic-gate 	 */
351*0Sstevel@tonic-gate 	ka.ka_type = WBKU_KEY_UNKNOWN;
352*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:k:")) != EOF) {
353*0Sstevel@tonic-gate 		switch (c) {
354*0Sstevel@tonic-gate 		case 'o':
355*0Sstevel@tonic-gate 			/*
356*0Sstevel@tonic-gate 			 * Suboptions.
357*0Sstevel@tonic-gate 			 */
358*0Sstevel@tonic-gate 			ret = process_option(optarg, &ka);
359*0Sstevel@tonic-gate 			if (ret != ENCR_SUCCESS) {
360*0Sstevel@tonic-gate 				usage(argv[0]);
361*0Sstevel@tonic-gate 				return (ret);
362*0Sstevel@tonic-gate 			}
363*0Sstevel@tonic-gate 			break;
364*0Sstevel@tonic-gate 		case 'k':
365*0Sstevel@tonic-gate 			/*
366*0Sstevel@tonic-gate 			 * Path to key file.
367*0Sstevel@tonic-gate 			 */
368*0Sstevel@tonic-gate 			keyfile_name = optarg;
369*0Sstevel@tonic-gate 			break;
370*0Sstevel@tonic-gate 		default:
371*0Sstevel@tonic-gate 			usage(argv[0]);
372*0Sstevel@tonic-gate 			return (ENCR_ERROR);
373*0Sstevel@tonic-gate 		}
374*0Sstevel@tonic-gate 	}
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	/*
377*0Sstevel@tonic-gate 	 * Gotta have a key file.
378*0Sstevel@tonic-gate 	 */
379*0Sstevel@tonic-gate 	if (keyfile_name == NULL) {
380*0Sstevel@tonic-gate 		wbku_printerr("Must specify the key_file\n");
381*0Sstevel@tonic-gate 		return (ENCR_ERROR);
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * Gotta have a key type.
386*0Sstevel@tonic-gate 	 */
387*0Sstevel@tonic-gate 	if (ka.ka_type == WBKU_KEY_UNKNOWN) {
388*0Sstevel@tonic-gate 		wbku_printerr("Unsupported encryption algorithm\n");
389*0Sstevel@tonic-gate 		return (ENCR_ERROR);
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	/*
393*0Sstevel@tonic-gate 	 * Open the key file for reading.
394*0Sstevel@tonic-gate 	 */
395*0Sstevel@tonic-gate 	if ((key_fp = fopen(keyfile_name, "r")) == NULL) {
396*0Sstevel@tonic-gate 		wbku_printerr("Cannot open %s", keyfile_name);
397*0Sstevel@tonic-gate 		return (ENCR_ERROR);
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/*
401*0Sstevel@tonic-gate 	 * Get the key from the key file and call the right
402*0Sstevel@tonic-gate 	 * encryption routine.
403*0Sstevel@tonic-gate 	 */
404*0Sstevel@tonic-gate 	ret = get_key(key_fp, &ka, key);
405*0Sstevel@tonic-gate 	if (ret == ENCR_SUCCESS) {
406*0Sstevel@tonic-gate 		switch (ka.ka_type) {
407*0Sstevel@tonic-gate 		case WBKU_KEY_3DES:
408*0Sstevel@tonic-gate 			ret = encr_gen_3des(&ka, key);
409*0Sstevel@tonic-gate 			break;
410*0Sstevel@tonic-gate 		case WBKU_KEY_AES_128:
411*0Sstevel@tonic-gate 			ret = encr_gen_aes(&ka, key);
412*0Sstevel@tonic-gate 			break;
413*0Sstevel@tonic-gate 		default:
414*0Sstevel@tonic-gate 			ret = ENCR_ERROR;	/* Internal error only */
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	(void) fclose(key_fp);
419*0Sstevel@tonic-gate 	return (ret);
420*0Sstevel@tonic-gate }
421