2 * Copyright 2018 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>
20 #define BUFSIZE 1024*8
22 typedef enum OPTION_choice {
23 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24 OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT
27 const OPTIONS mac_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
29 {OPT_HELP_STR, 1, '-', "mac_name\t\t MAC algorithm (See list "
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
33 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
34 {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
35 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
36 {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
41 int mac_main(int argc, char **argv)
47 EVP_MAC_CTX *ctx = NULL;
48 STACK_OF(OPENSSL_STRING) *opts = NULL;
49 unsigned char *buf = NULL;
52 BIO *in = NULL, *out = NULL;
53 const char *outfile = NULL;
54 const char *infile = NULL;
56 int inform = FORMAT_BINARY;
58 prog = opt_init(argc, argv, mac_options);
59 buf = app_malloc(BUFSIZE, "I/O buffer");
60 while ((o = opt_next()) != OPT_EOF) {
64 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
67 opt_help(mac_options);
81 opts = sk_OPENSSL_STRING_new_null();
82 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
87 argc = opt_num_rest();
91 BIO_printf(bio_err, "Invalid number of extra arguments\n");
95 mac = EVP_MAC_fetch(NULL, argv[0], NULL);
97 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
101 ctx = EVP_MAC_CTX_new(mac);
108 app_params_new_from_opts(opts, EVP_MAC_CTX_settable_params(mac));
113 if (!EVP_MAC_CTX_set_params(ctx, params)) {
114 BIO_printf(bio_err, "MAC parameter error\n");
115 ERR_print_errors(bio_err);
118 app_params_free(params);
123 /* Use text mode for stdin */
124 if (infile == NULL || strcmp(infile, "-") == 0)
125 inform = FORMAT_TEXT;
126 in = bio_open_default(infile, 'r', inform);
130 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
134 if (!EVP_MAC_init(ctx)) {
135 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
140 i = BIO_read(in, (char *)buf, BUFSIZE);
142 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
147 if (!EVP_MAC_update(ctx, buf, i)) {
148 BIO_printf(bio_err, "EVP_MAC_update failed\n");
153 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
154 BIO_printf(bio_err, "EVP_MAC_final failed\n");
158 BIO_printf(bio_err, "output len is too large\n");
162 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
163 BIO_printf(bio_err, "EVP_MAC_final failed\n");
168 BIO_write(out, buf, len);
171 BIO_printf(out,"\n");
172 for (i = 0; i < (int)len; ++i)
173 BIO_printf(out, "%02X", buf[i]);
175 BIO_printf(out,"\n");
181 ERR_print_errors(bio_err);
182 OPENSSL_clear_free(buf, BUFSIZE);
183 sk_OPENSSL_STRING_free(opts);
186 EVP_MAC_CTX_free(ctx);