]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/mac.c
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>
19 #define BUFSIZE 1024*8
21 typedef enum OPTION_choice
{
22 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
23 OPT_MACOPT
, OPT_BIN
, OPT_IN
, OPT_OUT
26 const OPTIONS mac_options
[] = {
27 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] mac_name\n"},
28 {OPT_HELP_STR
, 1, '-', "mac_name\t\t MAC algorithm (See list "
30 {"help", OPT_HELP
, '-', "Display this summary"},
31 {"macopt", OPT_MACOPT
, 's', "MAC algorithm control parameters in n:v form. "
32 "See 'Supported Controls' in the EVP_MAC_ docs"},
33 {"in", OPT_IN
, '<', "Input file to MAC (default is stdin)"},
34 {"out", OPT_OUT
, '>', "Output to filename rather than stdout"},
35 {"binary", OPT_BIN
, '-', "Output in binary format (Default is hexadecimal "
40 static int mac_ctrl_string(EVP_MAC_CTX
*ctx
, const char *value
)
43 char *stmp
, *vtmp
= NULL
;
45 stmp
= OPENSSL_strdup(value
);
48 vtmp
= strchr(stmp
, ':');
53 rv
= EVP_MAC_ctrl_str(ctx
, stmp
, vtmp
);
58 int mac_main(int argc
, char **argv
)
62 const EVP_MAC
*mac
= NULL
;
64 EVP_MAC_CTX
*ctx
= NULL
;
65 STACK_OF(OPENSSL_STRING
) *opts
= NULL
;
66 unsigned char *buf
= NULL
;
69 BIO
*in
= NULL
, *out
= NULL
;
70 const char *outfile
= NULL
;
71 const char *infile
= NULL
;
73 int inform
= FORMAT_BINARY
;
75 prog
= opt_init(argc
, argv
, mac_options
);
76 buf
= app_malloc(BUFSIZE
, "I/O buffer");
77 while ((o
= opt_next()) != OPT_EOF
) {
81 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
84 opt_help(mac_options
);
98 opts
= sk_OPENSSL_STRING_new_null();
99 if (opts
== NULL
|| !sk_OPENSSL_STRING_push(opts
, opt_arg()))
104 argc
= opt_num_rest();
108 BIO_printf(bio_err
, "Invalid number of extra arguments\n");
112 mac
= EVP_get_macbyname(argv
[0]);
114 BIO_printf(bio_err
, "Invalid MAC name %s\n", argv
[0]);
118 ctx
= EVP_MAC_CTX_new(mac
);
123 for (i
= 0; i
< sk_OPENSSL_STRING_num(opts
); i
++) {
124 char *opt
= sk_OPENSSL_STRING_value(opts
, i
);
125 if (mac_ctrl_string(ctx
, opt
) <= 0) {
126 BIO_printf(bio_err
, "MAC parameter error '%s'\n", opt
);
127 ERR_print_errors(bio_err
);
133 /* Use text mode for stdin */
134 if (infile
== NULL
|| strcmp(infile
, "-") == 0)
135 inform
= FORMAT_TEXT
;
136 in
= bio_open_default(infile
, 'r', inform
);
140 out
= bio_open_default(outfile
, 'w', out_bin
? FORMAT_BINARY
: FORMAT_TEXT
);
144 if (!EVP_MAC_init(ctx
)) {
145 BIO_printf(bio_err
, "EVP_MAC_Init failed\n");
151 i
= BIO_read(in
, (char *)buf
, BUFSIZE
);
153 BIO_printf(bio_err
, "Read Error in '%s'\n", infile
);
158 if (!EVP_MAC_update(ctx
, buf
, i
)) {
159 BIO_printf(bio_err
, "EVP_MAC_update failed\n");
164 if (!EVP_MAC_final(ctx
, NULL
, &len
)) {
165 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
169 BIO_printf(bio_err
, "output len is too large\n");
173 if (!EVP_MAC_final(ctx
, buf
, &len
)) {
174 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
179 BIO_write(out
, buf
, len
);
182 BIO_printf(out
,"\n");
183 for (i
= 0; i
< (int)len
; ++i
)
184 BIO_printf(out
, "%02X", buf
[i
]);
186 BIO_printf(out
,"\n");
192 ERR_print_errors(bio_err
);
193 OPENSSL_clear_free(buf
, BUFSIZE
);
194 sk_OPENSSL_STRING_free(opts
);
197 EVP_MAC_CTX_free(ctx
);