]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/mac.c
Document command parameters.
[thirdparty/openssl.git] / apps / mac.c
CommitLineData
4d768e96
SL
1/*
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <string.h>
11
12#include "apps.h"
13#include "progs.h"
14#include <openssl/bio.h>
15#include <openssl/err.h>
16#include <openssl/evp.h>
d747fb2e 17#include <openssl/params.h>
4d768e96
SL
18
19#undef BUFSIZE
20#define BUFSIZE 1024*8
21
22typedef enum OPTION_choice {
23 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24 OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT
25} OPTION_CHOICE;
26
27const OPTIONS mac_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
5388f986
RS
29
30 OPT_SECTION("General"),
4d768e96 31 {"help", OPT_HELP, '-', "Display this summary"},
92de469f
RS
32 {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
33 {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
5388f986
RS
34
35 OPT_SECTION("Input"),
4d768e96 36 {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
5388f986
RS
37
38 OPT_SECTION("Output"),
4d768e96 39 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
92de469f
RS
40 {"binary", OPT_BIN, '-',
41 "Output in binary format (default is hexadecimal)"},
42
43 OPT_PARAMETERS(),
44 {"mac_name", 0, 0, "MAC algorithm"},
4d768e96
SL
45 {NULL}
46};
47
4d768e96
SL
48int mac_main(int argc, char **argv)
49{
50 int ret = 1;
51 char *prog;
d747fb2e 52 EVP_MAC *mac = NULL;
4d768e96
SL
53 OPTION_CHOICE o;
54 EVP_MAC_CTX *ctx = NULL;
55 STACK_OF(OPENSSL_STRING) *opts = NULL;
56 unsigned char *buf = NULL;
57 size_t len;
58 int i;
59 BIO *in = NULL, *out = NULL;
60 const char *outfile = NULL;
61 const char *infile = NULL;
62 int out_bin = 0;
63 int inform = FORMAT_BINARY;
64
65 prog = opt_init(argc, argv, mac_options);
66 buf = app_malloc(BUFSIZE, "I/O buffer");
67 while ((o = opt_next()) != OPT_EOF) {
68 switch (o) {
a3c62426 69 default:
4d768e96
SL
70opthelp:
71 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
72 goto err;
73 case OPT_HELP:
74 opt_help(mac_options);
75 ret = 0;
76 goto err;
77 case OPT_BIN:
78 out_bin = 1;
79 break;
80 case OPT_IN:
81 infile = opt_arg();
82 break;
83 case OPT_OUT:
84 outfile = opt_arg();
85 break;
86 case OPT_MACOPT:
87 if (opts == NULL)
88 opts = sk_OPENSSL_STRING_new_null();
89 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
90 goto opthelp;
91 break;
92 }
93 }
94 argc = opt_num_rest();
95 argv = opt_rest();
96
97 if (argc != 1) {
98 BIO_printf(bio_err, "Invalid number of extra arguments\n");
99 goto opthelp;
100 }
101
d747fb2e 102 mac = EVP_MAC_fetch(NULL, argv[0], NULL);
4d768e96
SL
103 if (mac == NULL) {
104 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
105 goto opthelp;
106 }
107
108 ctx = EVP_MAC_CTX_new(mac);
109 if (ctx == NULL)
110 goto err;
111
112 if (opts != NULL) {
d747fb2e 113 int ok = 1;
95214b43 114 OSSL_PARAM *params =
41f7ecf3 115 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
d747fb2e 116
95214b43
SL
117 if (params == NULL)
118 goto err;
119
120 if (!EVP_MAC_CTX_set_params(ctx, params)) {
121 BIO_printf(bio_err, "MAC parameter error\n");
122 ERR_print_errors(bio_err);
123 ok = 0;
d747fb2e 124 }
95214b43 125 app_params_free(params);
d747fb2e
RL
126 if (!ok)
127 goto err;
4d768e96
SL
128 }
129
130 /* Use text mode for stdin */
131 if (infile == NULL || strcmp(infile, "-") == 0)
132 inform = FORMAT_TEXT;
133 in = bio_open_default(infile, 'r', inform);
134 if (in == NULL)
135 goto err;
136
137 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
138 if (out == NULL)
139 goto err;
140
141 if (!EVP_MAC_init(ctx)) {
142 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
143 goto err;
144 }
145
4d768e96
SL
146 for (;;) {
147 i = BIO_read(in, (char *)buf, BUFSIZE);
148 if (i < 0) {
149 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
150 goto err;
151 }
152 if (i == 0)
153 break;
154 if (!EVP_MAC_update(ctx, buf, i)) {
155 BIO_printf(bio_err, "EVP_MAC_update failed\n");
156 goto err;
157 }
158 }
159
d747fb2e 160 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
4d768e96
SL
161 BIO_printf(bio_err, "EVP_MAC_final failed\n");
162 goto err;
163 }
164 if (len > BUFSIZE) {
165 BIO_printf(bio_err, "output len is too large\n");
166 goto err;
167 }
168
d747fb2e 169 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
4d768e96
SL
170 BIO_printf(bio_err, "EVP_MAC_final failed\n");
171 goto err;
172 }
173
174 if (out_bin) {
175 BIO_write(out, buf, len);
176 } else {
177 if (outfile == NULL)
178 BIO_printf(out,"\n");
179 for (i = 0; i < (int)len; ++i)
180 BIO_printf(out, "%02X", buf[i]);
181 if (outfile == NULL)
182 BIO_printf(out,"\n");
183 }
184
185 ret = 0;
186err:
187 if (ret != 0)
188 ERR_print_errors(bio_err);
189 OPENSSL_clear_free(buf, BUFSIZE);
190 sk_OPENSSL_STRING_free(opts);
191 BIO_free(in);
192 BIO_free(out);
193 EVP_MAC_CTX_free(ctx);
d747fb2e 194 EVP_MAC_free(mac);
4d768e96
SL
195 return ret;
196}