4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
36 #include <wanbootutil.h>
37 #include <sys/wanboot_impl.h>
40 #define HMAC_SUCCESS 0
44 /* Private buffer length */
45 #define HMAC_BUF_LEN 1024
48 * This routine is used to compute a hash digest for the file represented
49 * by the file descirptor, 'fd'. The key, 'hmac_key', and key type, 'ka',
50 * will be provided by the caller. The resulting hash digest will be
54 * HMAC_SUCCESS or HMAC_ERROR.
57 hash_gen(int in_fd
, const wbku_key_attr_t
*ka
, const uint8_t *hmac_key
)
60 uint8_t buf
[HMAC_BUF_LEN
];
62 uint8_t digest
[HMAC_DIGEST_LEN
];
65 * Initialize the computation.
67 HMACInit(&ctx
, hmac_key
, ka
->ka_len
);
70 * Read the data to hash.
72 while ((i
= read(in_fd
, buf
, HMAC_BUF_LEN
)) > 0) {
73 HMACUpdate(&ctx
, buf
, i
);
76 wbku_printerr("Cannot read input_file");
81 * Finalize the digest.
83 HMACFinal(&ctx
, hmac_key
, ka
->ka_len
, digest
);
86 * Write the digest to stdout.
88 if (wbio_nwrite(STDOUT_FILENO
, digest
, sizeof (digest
)) != 0) {
89 wbku_printerr("Cannot output digest");
96 return (HMAC_SUCCESS
);
103 usage(const char *cmd
)
105 (void) fprintf(stderr
,
106 gettext("Usage: %s [-i input_file] -k key_file\n"), cmd
);
110 * This program is used to compute a hash digest for data read in from
111 * stdin or optionally, a file. The resulting hash digest will be written
115 * HMAC_SUCCESS, HMAC_ERROR or HMAC_NOKEY.
118 main(int argc
, char **argv
)
120 uint8_t hmac_key
[WANBOOT_HMAC_KEY_SIZE
];
122 char *infile_name
= NULL
;
123 char *keyfile_name
= NULL
;
127 wbku_retcode_t wbkuret
;
128 int ret
= HMAC_ERROR
;
131 * Do the necessary magic for localization support.
133 (void) setlocale(LC_ALL
, "");
134 #if !defined(TEXT_DOMAIN)
135 #define TEXT_DOMAIN "SYS_TEST"
137 (void) textdomain(TEXT_DOMAIN
);
140 * Initialize program name for use by wbku_printerr().
142 wbku_errinit(argv
[0]);
145 * Should be at least three arguments.
155 while ((c
= getopt(argc
, argv
, "i:k:")) != EOF
) {
159 * Optional input file.
161 infile_name
= optarg
;
167 keyfile_name
= optarg
;
176 * A key file must be defined.
178 if (keyfile_name
== NULL
) {
179 wbku_printerr("Must specify the key_file\n");
184 * If the user did not provide an input file for the data,
185 * then use stdin as the source.
187 if (infile_name
== NULL
) {
188 in_fd
= STDIN_FILENO
;
190 in_fd
= open(infile_name
, O_RDONLY
);
192 wbku_printerr("Cannot open input_file");
198 * Open the key file for reading.
200 if ((key_fp
= fopen(keyfile_name
, "r")) == NULL
) {
201 wbku_printerr("Cannot open %s", keyfile_name
);
206 * Create a SHA1 key attribute structure. It's the only hash
209 wbkuret
= wbku_str_to_keyattr(WBKU_KW_HMAC_SHA1
, &ka
, WBKU_HASH_KEY
);
210 if (wbkuret
!= WBKU_SUCCESS
) {
211 wbku_printerr("%s\n", wbku_retmsg(wbkuret
));
216 * Find the client key, if it exists.
218 wbkuret
= wbku_find_key(key_fp
, NULL
, &ka
, hmac_key
, B_FALSE
);
219 if (wbkuret
!= WBKU_SUCCESS
) {
220 wbku_printerr("%s\n", wbku_retmsg(wbkuret
));
221 ret
= (wbkuret
== WBKU_NOKEY
) ? HMAC_NOKEY
: HMAC_ERROR
;
223 ret
= hash_gen(in_fd
, &ka
, hmac_key
);
232 if (key_fp
!= NULL
) {
233 (void) fclose(key_fp
);