]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/mac.c
a02779b29f4198847008d37b8463a12b2a8c851e
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
) {
82 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
85 opt_help(mac_options
);
99 opts
= sk_OPENSSL_STRING_new_null();
100 if (opts
== NULL
|| !sk_OPENSSL_STRING_push(opts
, opt_arg()))
105 argc
= opt_num_rest();
109 BIO_printf(bio_err
, "Invalid number of extra arguments\n");
113 mac
= EVP_get_macbyname(argv
[0]);
115 BIO_printf(bio_err
, "Invalid MAC name %s\n", argv
[0]);
119 ctx
= EVP_MAC_CTX_new(mac
);
124 for (i
= 0; i
< sk_OPENSSL_STRING_num(opts
); i
++) {
125 char *opt
= sk_OPENSSL_STRING_value(opts
, i
);
126 if (mac_ctrl_string(ctx
, opt
) <= 0) {
127 BIO_printf(bio_err
, "MAC parameter error '%s'\n", opt
);
128 ERR_print_errors(bio_err
);
134 /* Use text mode for stdin */
135 if (infile
== NULL
|| strcmp(infile
, "-") == 0)
136 inform
= FORMAT_TEXT
;
137 in
= bio_open_default(infile
, 'r', inform
);
141 out
= bio_open_default(outfile
, 'w', out_bin
? FORMAT_BINARY
: FORMAT_TEXT
);
145 if (!EVP_MAC_init(ctx
)) {
146 BIO_printf(bio_err
, "EVP_MAC_Init failed\n");
152 i
= BIO_read(in
, (char *)buf
, BUFSIZE
);
154 BIO_printf(bio_err
, "Read Error in '%s'\n", infile
);
159 if (!EVP_MAC_update(ctx
, buf
, i
)) {
160 BIO_printf(bio_err
, "EVP_MAC_update failed\n");
165 if (!EVP_MAC_final(ctx
, NULL
, &len
)) {
166 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
170 BIO_printf(bio_err
, "output len is too large\n");
174 if (!EVP_MAC_final(ctx
, buf
, &len
)) {
175 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
180 BIO_write(out
, buf
, len
);
183 BIO_printf(out
,"\n");
184 for (i
= 0; i
< (int)len
; ++i
)
185 BIO_printf(out
, "%02X", buf
[i
]);
187 BIO_printf(out
,"\n");
193 ERR_print_errors(bio_err
);
194 OPENSSL_clear_free(buf
, BUFSIZE
);
195 sk_OPENSSL_STRING_free(opts
);
198 EVP_MAC_CTX_free(ctx
);