]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/mac.c
a02779b29f4198847008d37b8463a12b2a8c851e
[thirdparty/openssl.git] / apps / mac.c
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>
17
18 #undef BUFSIZE
19 #define BUFSIZE 1024*8
20
21 typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23 OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT
24 } OPTION_CHOICE;
25
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 "
29 "-mac-algorithms)"},
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 "
36 "output)"},
37 {NULL}
38 };
39
40 static int mac_ctrl_string(EVP_MAC_CTX *ctx, const char *value)
41 {
42 int rv;
43 char *stmp, *vtmp = NULL;
44
45 stmp = OPENSSL_strdup(value);
46 if (stmp == NULL)
47 return -1;
48 vtmp = strchr(stmp, ':');
49 if (vtmp != NULL) {
50 *vtmp = 0;
51 vtmp++;
52 }
53 rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp);
54 OPENSSL_free(stmp);
55 return rv;
56 }
57
58 int mac_main(int argc, char **argv)
59 {
60 int ret = 1;
61 char *prog;
62 const EVP_MAC *mac = NULL;
63 OPTION_CHOICE o;
64 EVP_MAC_CTX *ctx = NULL;
65 STACK_OF(OPENSSL_STRING) *opts = NULL;
66 unsigned char *buf = NULL;
67 size_t len;
68 int i;
69 BIO *in = NULL, *out = NULL;
70 const char *outfile = NULL;
71 const char *infile = NULL;
72 int out_bin = 0;
73 int inform = FORMAT_BINARY;
74
75 prog = opt_init(argc, argv, mac_options);
76 buf = app_malloc(BUFSIZE, "I/O buffer");
77 while ((o = opt_next()) != OPT_EOF) {
78 switch (o) {
79 case OPT_EOF:
80 case OPT_ERR:
81 opthelp:
82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
83 goto err;
84 case OPT_HELP:
85 opt_help(mac_options);
86 ret = 0;
87 goto err;
88 case OPT_BIN:
89 out_bin = 1;
90 break;
91 case OPT_IN:
92 infile = opt_arg();
93 break;
94 case OPT_OUT:
95 outfile = opt_arg();
96 break;
97 case OPT_MACOPT:
98 if (opts == NULL)
99 opts = sk_OPENSSL_STRING_new_null();
100 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
101 goto opthelp;
102 break;
103 }
104 }
105 argc = opt_num_rest();
106 argv = opt_rest();
107
108 if (argc != 1) {
109 BIO_printf(bio_err, "Invalid number of extra arguments\n");
110 goto opthelp;
111 }
112
113 mac = EVP_get_macbyname(argv[0]);
114 if (mac == NULL) {
115 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
116 goto opthelp;
117 }
118
119 ctx = EVP_MAC_CTX_new(mac);
120 if (ctx == NULL)
121 goto err;
122
123 if (opts != NULL) {
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);
129 goto err;
130 }
131 }
132 }
133
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);
138 if (in == NULL)
139 goto err;
140
141 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
142 if (out == NULL)
143 goto err;
144
145 if (!EVP_MAC_init(ctx)) {
146 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
147 goto err;
148 }
149
150
151 for (;;) {
152 i = BIO_read(in, (char *)buf, BUFSIZE);
153 if (i < 0) {
154 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
155 goto err;
156 }
157 if (i == 0)
158 break;
159 if (!EVP_MAC_update(ctx, buf, i)) {
160 BIO_printf(bio_err, "EVP_MAC_update failed\n");
161 goto err;
162 }
163 }
164
165 if (!EVP_MAC_final(ctx, NULL, &len)) {
166 BIO_printf(bio_err, "EVP_MAC_final failed\n");
167 goto err;
168 }
169 if (len > BUFSIZE) {
170 BIO_printf(bio_err, "output len is too large\n");
171 goto err;
172 }
173
174 if (!EVP_MAC_final(ctx, buf, &len)) {
175 BIO_printf(bio_err, "EVP_MAC_final failed\n");
176 goto err;
177 }
178
179 if (out_bin) {
180 BIO_write(out, buf, len);
181 } else {
182 if (outfile == NULL)
183 BIO_printf(out,"\n");
184 for (i = 0; i < (int)len; ++i)
185 BIO_printf(out, "%02X", buf[i]);
186 if (outfile == NULL)
187 BIO_printf(out,"\n");
188 }
189
190 ret = 0;
191 err:
192 if (ret != 0)
193 ERR_print_errors(bio_err);
194 OPENSSL_clear_free(buf, BUFSIZE);
195 sk_OPENSSL_STRING_free(opts);
196 BIO_free(in);
197 BIO_free(out);
198 EVP_MAC_CTX_free(ctx);
199 return ret;
200 }