summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.lib/wanboot/encr/encr.c
blob: 730f5b8101fef0f0505bf9e0aaef9cd5515b743e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <sys/des.h>
#include <strings.h>
#include <errno.h>
#include <wanbootutil.h>
#include <sys/sysmacros.h>
#include <sys/wanboot_impl.h>

/* Return codes */
#define	ENCR_SUCCESS	0
#define	ENCR_NOKEY	1
#define	ENCR_ERROR	2

/* Private buffer length */
#define	ENCR_BUF_LEN		1024

/* Encryption algorithm suboption. */
#define	TYPE	0

static char *opts[] = { "type", NULL };

/*
 * This routine is used to parse the suboptions of '-o' option.
 *
 * The option should be of the form: type=<3des|aes>
 *
 * This routine will pass the value of the suboption back in the
 * supplied arguments, 'ka'.
 *
 * Returns:
 *	ENCR_SUCCESS or ENCR_ERROR.
 */
static int
process_option(char *arg, wbku_key_attr_t *ka)
{
	char *value;
	wbku_retcode_t ret;

	while (*arg != '\0') {
		switch (getsubopt(&arg, opts, &value)) {
		case TYPE:
			/*
			 * Key type.
			 */
			ret = wbku_str_to_keyattr(value, ka, WBKU_ENCR_KEY);
			if (ret != WBKU_SUCCESS) {
				wbku_printerr("%s\n", wbku_retmsg(ret));
				return (ENCR_ERROR);
			}
			break;
		default:
			wbku_printerr("Invalid option %s\n", value);
			return (ENCR_ERROR);
		}
	}

	return (ENCR_SUCCESS);
}

/*
 * This routine is used to find the key of type defined by 'ka' and
 * return it in 'key'. The key file should have been opened by the
 * caller and the handle passed in 'key_fp'.
 *
 * Returns:
 *	ENCR_SUCCESS, ENCR_ERROR or ENCR_NOKEY.
 */
static int
get_key(FILE *key_fp, wbku_key_attr_t *ka, uint8_t *key)
{
	wbku_retcode_t ret;

	/*
	 * Find the client key, if it exists.
	 */
	ret = wbku_find_key(key_fp, NULL, ka, key, B_FALSE);
	if (ret != WBKU_SUCCESS) {
		wbku_printerr("%s\n", wbku_retmsg(ret));
		if (ret == WBKU_NOKEY)
			return (ENCR_NOKEY);
		else
			return (ENCR_ERROR);
	}
	return (ENCR_SUCCESS);
}

/*
 * This routine is the common encryption routine used to encrypt data
 * using the CBC handle initialized by the calling routine.  The data
 * to be encrypted is read from stdin and the encrypted data is written to
 * stdout.
 *
 * Returns:
 *	ENCR_SUCCESS or ENCR_ERROR.
 */
static int
encr_gen(cbc_handle_t *ch)
{
	uint8_t iv[WANBOOT_MAXBLOCKLEN];
	uint8_t buf[ENCR_BUF_LEN];
	uint8_t *bufp;
	int read_size;
	ssize_t i, j, k;

	/*
	 * Use a random number as the IV
	 */
	if (wbio_nread_rand(iv, ch->blocklen) != 0) {
		wbku_printerr("Cannot generate initialization vector");
		return (ENCR_ERROR);
	}

	/*
	 * Output the IV to stdout.
	 */
	if (wbio_nwrite(STDOUT_FILENO, iv, ch->blocklen) != 0) {
		wbku_printerr("Write error encountered\n");
		return (ENCR_ERROR);
	}

	/*
	 * Try to read in multiple of block_size as CBC requires
	 * that data be encrypted in block_size chunks.
	 */
	read_size = ENCR_BUF_LEN / ch->blocklen * ch->blocklen;
	while ((i = read(STDIN_FILENO, buf, read_size)) > 0) {
		/*
		 * If data received is not a multiple of the block size,
		 * try to receive more.  If reach EOF, pad the rest with
		 * 0.
		 */
		if ((j = i % ch->blocklen) != 0) {
			/*
			 * Determine how more data need to be received to
			 * fill out the buffer so that it contains a
			 * multiple of block_size chunks.
			 */
			j = ch->blocklen - j;
			bufp = buf + i;
			k = j;

			/*
			 * Try to fill the gap.
			 *
			 */
			while ((j = read(STDIN_FILENO, bufp, j)) != k &&
			    j != 0) {
				bufp += j;
				k -= j;
				j = k;
			}

			/*
			 * This is the total length of the buffer.
			 */
			i = (i + ch->blocklen) - (i % ch->blocklen);

			if (j == 0) {
				/* EOF, do padding. */
				(void) memset(bufp, 0, k);
				(void) cbc_encrypt(ch, buf, i, iv);
			} else if (j > 0) {
				/* The gap has been filled in */
				(void) cbc_encrypt(ch, buf, i, iv);
			} else {
				/* Oops. */
				wbku_printerr("Input error");
				return (ENCR_ERROR);
			}
		} else {
			/* A multiple of the block size was received */
			(void) cbc_encrypt(ch, buf, i, iv);
		}
		if (wbio_nwrite(STDOUT_FILENO, buf, i) != 0) {
			wbku_printerr("Write error encountered\n");
			return (ENCR_ERROR);
		}
	}

	return (ENCR_SUCCESS);
}

/*
 * This routine initializes a CBC handle for 3DES and calls the
 * common encryption routine to encrypt data.
 *
 * Returns:
 *	ENCR_SUCCESS or ENCR_ERROR.
 */
static int
encr_gen_3des(const wbku_key_attr_t *ka, const uint8_t *key)
{
	cbc_handle_t ch;
	void *eh;
	int ret;

	/*
	 * Initialize a 3DES handle.
	 */
	if (des3_init(&eh) != 0) {
		return (ENCR_ERROR);
	}
	des3_key(eh, key);

	/*
	 * Initialize the CBC handle.
	 */
	cbc_makehandle(&ch, eh, ka->ka_len, DES3_BLOCK_SIZE,
	    DES3_IV_SIZE, des3_encrypt, des3_decrypt);

	/*
	 * Encrypt the data.
	 */
	ret = encr_gen(&ch);

	/*
	 *  Free the 3DES resources.
	 */
	des3_fini(eh);

	return (ret);
}

/*
 * This routine initializes a CBC handle for AES and calls the
 * common encryption routine to encrypt data.
 *
 * Returns:
 *	ENCR_SUCCESS or ENCR_ERROR.
 */
static int
encr_gen_aes(const wbku_key_attr_t *ka, const uint8_t *key)
{
	cbc_handle_t ch;
	void *eh;
	int ret;

	/*
	 * Initialize an AES handle.
	 */
	if (aes_init(&eh) != 0) {
		return (ENCR_ERROR);
	}
	aes_key(eh, key, ka->ka_len);

	/*
	 * Initialize the CBC handle.
	 */
	cbc_makehandle(&ch, eh, ka->ka_len, AES_BLOCK_SIZE,
	    AES_IV_SIZE, aes_encrypt, aes_decrypt);

	/*
	 * Encrypt the data.
	 */
	ret = encr_gen(&ch);

	/*
	 *  Free the AES resources.
	 */
	aes_fini(eh);

	return (ret);
}

/*
 * Prints usage().
 */
static void
usage(const char *cmd)
{
	(void) fprintf(stderr,
	    gettext("Usage: %s -o type=<%s|%s> -k key_file\n"),
	    cmd, WBKU_KW_3DES, WBKU_KW_AES_128);
}

/*
 * This program is used to encrypt data read from stdin and print it to
 * stdout. The path to the key file and the algorithm to use are
 * provided by the user.
 *
 * Returns:
 *	ENCR_SUCCESS, ENCR_ERROR or ENCR_NOKEY.
 */
int
main(int argc, char **argv)
{
	uint8_t key[WANBOOT_MAXKEYLEN];
	int c;
	char *keyfile_name = NULL;
	wbku_key_attr_t ka;
	FILE *key_fp;
	int ret;

	/*
	 * Do the necessary magic for localization support.
	 */
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	/*
	 * Initialize program name for use by wbku_printerr().
	 */
	wbku_errinit(argv[0]);

	/*
	 * Should be five arguments.
	 */
	if (argc < 5) {
		usage(argv[0]);
		return (ENCR_ERROR);
	}

	/*
	 * Parse the options.
	 */
	ka.ka_type = WBKU_KEY_UNKNOWN;
	while ((c = getopt(argc, argv, "o:k:")) != EOF) {
		switch (c) {
		case 'o':
			/*
			 * Suboptions.
			 */
			ret = process_option(optarg, &ka);
			if (ret != ENCR_SUCCESS) {
				usage(argv[0]);
				return (ret);
			}
			break;
		case 'k':
			/*
			 * Path to key file.
			 */
			keyfile_name = optarg;
			break;
		default:
			usage(argv[0]);
			return (ENCR_ERROR);
		}
	}

	/*
	 * Gotta have a key file.
	 */
	if (keyfile_name == NULL) {
		wbku_printerr("Must specify the key_file\n");
		return (ENCR_ERROR);
	}

	/*
	 * Gotta have a key type.
	 */
	if (ka.ka_type == WBKU_KEY_UNKNOWN) {
		wbku_printerr("Unsupported encryption algorithm\n");
		return (ENCR_ERROR);
	}

	/*
	 * Open the key file for reading.
	 */
	if ((key_fp = fopen(keyfile_name, "r")) == NULL) {
		wbku_printerr("Cannot open %s", keyfile_name);
		return (ENCR_ERROR);
	}

	/*
	 * Get the key from the key file and call the right
	 * encryption routine.
	 */
	ret = get_key(key_fp, &ka, key);
	if (ret == ENCR_SUCCESS) {
		switch (ka.ka_type) {
		case WBKU_KEY_3DES:
			ret = encr_gen_3des(&ka, key);
			break;
		case WBKU_KEY_AES_128:
			ret = encr_gen_aes(&ka, key);
			break;
		default:
			ret = ENCR_ERROR;	/* Internal error only */
		}
	}

	(void) fclose(key_fp);
	return (ret);
}