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 control parameters in n:v form. "
33 "See 'Supported Controls' 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);
107 OPENSSL_zalloc(sizeof(OSSL_PARAM)
108 * (sk_OPENSSL_STRING_num(opts) + 1));
109 const OSSL_PARAM *paramdefs = EVP_MAC_CTX_settable_params(mac);
113 for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
115 char *opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
116 char *stmp, *vtmp = NULL;
118 if ((stmp = OPENSSL_strdup(opt)) == NULL
119 || (vtmp = strchr(stmp, ':')) == NULL
120 || (*vtmp++ = 0) /* Always zero */
121 || !OSSL_PARAM_allocate_from_text(¶ms[params_n], paramdefs,
122 stmp, vtmp, strlen(vtmp))) {
123 BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
124 ERR_print_errors(bio_err);
132 params[params_n] = OSSL_PARAM_construct_end();
133 if (!EVP_MAC_CTX_set_params(ctx, params)) {
134 BIO_printf(bio_err, "MAC parameter error\n");
135 ERR_print_errors(bio_err);
139 for (; params_n-- > 0;) {
140 OPENSSL_free(params[params_n].data);
142 OPENSSL_free(params);
147 /* Use text mode for stdin */
148 if (infile == NULL || strcmp(infile, "-") == 0)
149 inform = FORMAT_TEXT;
150 in = bio_open_default(infile, 'r', inform);
154 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
158 if (!EVP_MAC_init(ctx)) {
159 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
165 i = BIO_read(in, (char *)buf, BUFSIZE);
167 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
172 if (!EVP_MAC_update(ctx, buf, i)) {
173 BIO_printf(bio_err, "EVP_MAC_update failed\n");
178 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
179 BIO_printf(bio_err, "EVP_MAC_final failed\n");
183 BIO_printf(bio_err, "output len is too large\n");
187 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
188 BIO_printf(bio_err, "EVP_MAC_final failed\n");
193 BIO_write(out, buf, len);
196 BIO_printf(out,"\n");
197 for (i = 0; i < (int)len; ++i)
198 BIO_printf(out, "%02X", buf[i]);
200 BIO_printf(out,"\n");
206 ERR_print_errors(bio_err);
207 OPENSSL_clear_free(buf, BUFSIZE);
208 sk_OPENSSL_STRING_free(opts);
211 EVP_MAC_CTX_free(ctx);