]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/mac.c
2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
,
28 const OPTIONS mac_options
[] = {
29 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] mac_name\n"},
31 OPT_SECTION("General"),
32 {"help", OPT_HELP
, '-', "Display this summary"},
33 {"macopt", OPT_MACOPT
, 's', "MAC algorithm parameters in n:v form"},
34 {OPT_MORE_STR
, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
37 {"in", OPT_IN
, '<', "Input file to MAC (default is stdin)"},
39 OPT_SECTION("Output"),
40 {"out", OPT_OUT
, '>', "Output to filename rather than stdout"},
41 {"binary", OPT_BIN
, '-',
42 "Output in binary format (default is hexadecimal)"},
47 {"mac_name", 0, 0, "MAC algorithm"},
51 int mac_main(int argc
, char **argv
)
57 EVP_MAC_CTX
*ctx
= NULL
;
58 STACK_OF(OPENSSL_STRING
) *opts
= NULL
;
59 unsigned char *buf
= NULL
;
62 BIO
*in
= NULL
, *out
= NULL
;
63 const char *outfile
= NULL
;
64 const char *infile
= NULL
;
66 int inform
= FORMAT_BINARY
;
68 prog
= opt_init(argc
, argv
, mac_options
);
69 buf
= app_malloc(BUFSIZE
, "I/O buffer");
70 while ((o
= opt_next()) != OPT_EOF
) {
74 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
77 opt_help(mac_options
);
91 opts
= sk_OPENSSL_STRING_new_null();
92 if (opts
== NULL
|| !sk_OPENSSL_STRING_push(opts
, opt_arg()))
101 argc
= opt_num_rest();
105 BIO_printf(bio_err
, "Invalid number of extra arguments\n");
109 mac
= EVP_MAC_fetch(NULL
, argv
[0], NULL
);
111 BIO_printf(bio_err
, "Invalid MAC name %s\n", argv
[0]);
115 ctx
= EVP_MAC_CTX_new(mac
);
122 app_params_new_from_opts(opts
, EVP_MAC_settable_ctx_params(mac
));
127 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
128 BIO_printf(bio_err
, "MAC parameter error\n");
129 ERR_print_errors(bio_err
);
132 app_params_free(params
);
137 /* Use text mode for stdin */
138 if (infile
== NULL
|| strcmp(infile
, "-") == 0)
139 inform
= FORMAT_TEXT
;
140 in
= bio_open_default(infile
, 'r', inform
);
144 out
= bio_open_default(outfile
, 'w', out_bin
? FORMAT_BINARY
: FORMAT_TEXT
);
148 if (!EVP_MAC_init(ctx
)) {
149 BIO_printf(bio_err
, "EVP_MAC_Init failed\n");
154 i
= BIO_read(in
, (char *)buf
, BUFSIZE
);
156 BIO_printf(bio_err
, "Read Error in '%s'\n", infile
);
161 if (!EVP_MAC_update(ctx
, buf
, i
)) {
162 BIO_printf(bio_err
, "EVP_MAC_update failed\n");
167 if (!EVP_MAC_final(ctx
, NULL
, &len
, 0)) {
168 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
172 BIO_printf(bio_err
, "output len is too large\n");
176 if (!EVP_MAC_final(ctx
, buf
, &len
, BUFSIZE
)) {
177 BIO_printf(bio_err
, "EVP_MAC_final failed\n");
182 BIO_write(out
, buf
, len
);
185 BIO_printf(out
,"\n");
186 for (i
= 0; i
< (int)len
; ++i
)
187 BIO_printf(out
, "%02X", buf
[i
]);
189 BIO_printf(out
,"\n");
195 ERR_print_errors(bio_err
);
196 OPENSSL_clear_free(buf
, BUFSIZE
);
197 sk_OPENSSL_STRING_free(opts
);
200 EVP_MAC_CTX_free(ctx
);