2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/params.h>
19 DEFINE_STACK_OF_STRING()
22 #define BUFSIZE 1024*8
24 typedef enum OPTION_choice {
25 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
26 OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
30 const OPTIONS mac_options[] = {
31 {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
33 OPT_SECTION("General"),
34 {"help", OPT_HELP, '-', "Display this summary"},
35 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
36 {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
39 {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
41 OPT_SECTION("Output"),
42 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
43 {"binary", OPT_BIN, '-',
44 "Output in binary format (default is hexadecimal)"},
49 {"mac_name", 0, 0, "MAC algorithm"},
53 int mac_main(int argc, char **argv)
59 EVP_MAC_CTX *ctx = NULL;
60 STACK_OF(OPENSSL_STRING) *opts = NULL;
61 unsigned char *buf = NULL;
64 BIO *in = NULL, *out = NULL;
65 const char *outfile = NULL;
66 const char *infile = NULL;
68 int inform = FORMAT_BINARY;
70 prog = opt_init(argc, argv, mac_options);
71 buf = app_malloc(BUFSIZE, "I/O buffer");
72 while ((o = opt_next()) != OPT_EOF) {
76 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
79 opt_help(mac_options);
93 opts = sk_OPENSSL_STRING_new_null();
94 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
103 argc = opt_num_rest();
107 BIO_printf(bio_err, "Invalid number of extra arguments\n");
111 mac = EVP_MAC_fetch(NULL, argv[0], NULL);
113 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
117 ctx = EVP_MAC_CTX_new(mac);
124 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
129 if (!EVP_MAC_CTX_set_params(ctx, params)) {
130 BIO_printf(bio_err, "MAC parameter error\n");
131 ERR_print_errors(bio_err);
134 app_params_free(params);
139 /* Use text mode for stdin */
140 if (infile == NULL || strcmp(infile, "-") == 0)
141 inform = FORMAT_TEXT;
142 in = bio_open_default(infile, 'r', inform);
146 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
150 if (!EVP_MAC_init(ctx)) {
151 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
156 i = BIO_read(in, (char *)buf, BUFSIZE);
158 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
163 if (!EVP_MAC_update(ctx, buf, i)) {
164 BIO_printf(bio_err, "EVP_MAC_update failed\n");
169 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
170 BIO_printf(bio_err, "EVP_MAC_final failed\n");
174 BIO_printf(bio_err, "output len is too large\n");
178 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
179 BIO_printf(bio_err, "EVP_MAC_final failed\n");
184 BIO_write(out, buf, len);
187 BIO_printf(out,"\n");
188 for (i = 0; i < (int)len; ++i)
189 BIO_printf(out, "%02X", buf[i]);
191 BIO_printf(out,"\n");
197 ERR_print_errors(bio_err);
198 OPENSSL_clear_free(buf, BUFSIZE);
199 sk_OPENSSL_STRING_free(opts);
202 EVP_MAC_CTX_free(ctx);