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 "
32 OPT_SECTION("General"),
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
35 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
38 {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
40 OPT_SECTION("Output"),
41 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
42 {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
47 int mac_main(int argc, char **argv)
53 EVP_MAC_CTX *ctx = NULL;
54 STACK_OF(OPENSSL_STRING) *opts = NULL;
55 unsigned char *buf = NULL;
58 BIO *in = NULL, *out = NULL;
59 const char *outfile = NULL;
60 const char *infile = NULL;
62 int inform = FORMAT_BINARY;
64 prog = opt_init(argc, argv, mac_options);
65 buf = app_malloc(BUFSIZE, "I/O buffer");
66 while ((o = opt_next()) != OPT_EOF) {
70 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
73 opt_help(mac_options);
87 opts = sk_OPENSSL_STRING_new_null();
88 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
93 argc = opt_num_rest();
97 BIO_printf(bio_err, "Invalid number of extra arguments\n");
101 mac = EVP_MAC_fetch(NULL, argv[0], NULL);
103 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
107 ctx = EVP_MAC_CTX_new(mac);
114 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
119 if (!EVP_MAC_CTX_set_params(ctx, params)) {
120 BIO_printf(bio_err, "MAC parameter error\n");
121 ERR_print_errors(bio_err);
124 app_params_free(params);
129 /* Use text mode for stdin */
130 if (infile == NULL || strcmp(infile, "-") == 0)
131 inform = FORMAT_TEXT;
132 in = bio_open_default(infile, 'r', inform);
136 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
140 if (!EVP_MAC_init(ctx)) {
141 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
146 i = BIO_read(in, (char *)buf, BUFSIZE);
148 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
153 if (!EVP_MAC_update(ctx, buf, i)) {
154 BIO_printf(bio_err, "EVP_MAC_update failed\n");
159 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
160 BIO_printf(bio_err, "EVP_MAC_final failed\n");
164 BIO_printf(bio_err, "output len is too large\n");
168 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
169 BIO_printf(bio_err, "EVP_MAC_final failed\n");
174 BIO_write(out, buf, len);
177 BIO_printf(out,"\n");
178 for (i = 0; i < (int)len; ++i)
179 BIO_printf(out, "%02X", buf[i]);
181 BIO_printf(out,"\n");
187 ERR_print_errors(bio_err);
188 OPENSSL_clear_free(buf, BUFSIZE);
189 sk_OPENSSL_STRING_free(opts);
192 EVP_MAC_CTX_free(ctx);