SM4 optimization for ARM by HW instruction
[openssl.git] / apps / mac.c
1 /*
2  * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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 #include <openssl/params.h>
18 #include <openssl/core_names.h>
19
20 #undef BUFSIZE
21 #define BUFSIZE 1024*8
22
23 typedef enum OPTION_choice {
24     OPT_COMMON,
25     OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
26     OPT_CIPHER, OPT_DIGEST,
27     OPT_PROV_ENUM
28 } OPTION_CHOICE;
29
30 const OPTIONS mac_options[] = {
31     {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
32
33     OPT_SECTION("General"),
34     {"help", OPT_HELP, '-', "Display this summary"},
35     {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
36     {"cipher", OPT_CIPHER, 's', "Cipher"},
37     {"digest", OPT_DIGEST, 's', "Digest"},
38     {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
39
40     OPT_SECTION("Input"),
41     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
42
43     OPT_SECTION("Output"),
44     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
45     {"binary", OPT_BIN, '-',
46         "Output in binary format (default is hexadecimal)"},
47
48     OPT_PROV_OPTIONS,
49
50     OPT_PARAMETERS(),
51     {"mac_name", 0, 0, "MAC algorithm"},
52     {NULL}
53 };
54
55 static char *alloc_mac_algorithm_name(STACK_OF(OPENSSL_STRING) **optp,
56                                       const char *name, const char *arg)
57 {
58     size_t len = strlen(name) + strlen(arg) + 2;
59     char *res;
60
61     if (*optp == NULL)
62         *optp = sk_OPENSSL_STRING_new_null();
63     if (*optp == NULL)
64         return NULL;
65
66     res = app_malloc(len, "algorithm name");
67     BIO_snprintf(res, len, "%s:%s", name, arg);
68     if (sk_OPENSSL_STRING_push(*optp, res))
69         return res;
70     OPENSSL_free(res);
71     return NULL;
72 }
73
74 int mac_main(int argc, char **argv)
75 {
76     int ret = 1;
77     char *prog;
78     EVP_MAC *mac = NULL;
79     OPTION_CHOICE o;
80     EVP_MAC_CTX *ctx = NULL;
81     STACK_OF(OPENSSL_STRING) *opts = NULL;
82     unsigned char *buf = NULL;
83     size_t len;
84     int i;
85     BIO *in = NULL, *out = NULL;
86     const char *outfile = NULL;
87     const char *infile = NULL;
88     int out_bin = 0;
89     int inform = FORMAT_BINARY;
90     char *digest = NULL, *cipher = NULL;
91     OSSL_PARAM *params = NULL;
92
93     prog = opt_init(argc, argv, mac_options);
94     buf = app_malloc(BUFSIZE, "I/O buffer");
95     while ((o = opt_next()) != OPT_EOF) {
96         switch (o) {
97         default:
98 opthelp:
99             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
100             goto err;
101         case OPT_HELP:
102             opt_help(mac_options);
103             ret = 0;
104             goto err;
105         case OPT_BIN:
106             out_bin = 1;
107             break;
108         case OPT_IN:
109             infile = opt_arg();
110             break;
111         case OPT_OUT:
112             outfile = opt_arg();
113             break;
114         case OPT_MACOPT:
115             if (opts == NULL)
116                 opts = sk_OPENSSL_STRING_new_null();
117             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
118                 goto opthelp;
119             break;
120         case OPT_CIPHER:
121             OPENSSL_free(cipher);
122             cipher = alloc_mac_algorithm_name(&opts, "cipher", opt_arg());
123             if (cipher == NULL)
124                 goto opthelp;
125             break;
126         case OPT_DIGEST:
127             OPENSSL_free(digest);
128             digest = alloc_mac_algorithm_name(&opts, "digest", opt_arg());
129             if (digest == NULL)
130                 goto opthelp;
131             break;
132         case OPT_PROV_CASES:
133             if (!opt_provider(o))
134                 goto err;
135             break;
136         }
137     }
138
139     /* One argument, the MAC name. */
140     if (!opt_check_rest_arg("MAC name"))
141         goto opthelp;
142     argv = opt_rest();
143
144     mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq());
145     if (mac == NULL) {
146         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
147         goto opthelp;
148     }
149
150     ctx = EVP_MAC_CTX_new(mac);
151     if (ctx == NULL)
152         goto err;
153
154     if (opts != NULL) {
155         int ok = 1;
156
157         params = app_params_new_from_opts(opts,
158                                           EVP_MAC_settable_ctx_params(mac));
159         if (params == NULL)
160             goto err;
161
162         if (!EVP_MAC_CTX_set_params(ctx, params)) {
163             BIO_printf(bio_err, "MAC parameter error\n");
164             ERR_print_errors(bio_err);
165             ok = 0;
166         }
167         app_params_free(params);
168         if (!ok)
169             goto err;
170     }
171
172     /* Use text mode for stdin */
173     if (infile == NULL || strcmp(infile, "-") == 0)
174         inform = FORMAT_TEXT;
175     in = bio_open_default(infile, 'r', inform);
176     if (in == NULL)
177         goto err;
178
179     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
180     if (out == NULL)
181         goto err;
182
183     if (!EVP_MAC_init(ctx, NULL, 0, NULL)) {
184         BIO_printf(bio_err, "EVP_MAC_Init failed\n");
185         goto err;
186     }
187
188     while (BIO_pending(in) || !BIO_eof(in)) {
189         i = BIO_read(in, (char *)buf, BUFSIZE);
190         if (i < 0) {
191             BIO_printf(bio_err, "Read Error in '%s'\n", infile);
192             ERR_print_errors(bio_err);
193             goto err;
194         }
195         if (i == 0)
196             break;
197         if (!EVP_MAC_update(ctx, buf, i)) {
198             BIO_printf(bio_err, "EVP_MAC_update failed\n");
199             goto err;
200         }
201     }
202
203     if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
204         BIO_printf(bio_err, "EVP_MAC_final failed\n");
205         goto err;
206     }
207     if (len > BUFSIZE) {
208         BIO_printf(bio_err, "output len is too large\n");
209         goto err;
210     }
211
212     if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
213         BIO_printf(bio_err, "EVP_MAC_final failed\n");
214         goto err;
215     }
216
217     if (out_bin) {
218         BIO_write(out, buf, len);
219     } else {
220         for (i = 0; i < (int)len; ++i)
221             BIO_printf(out, "%02X", buf[i]);
222         if (outfile == NULL)
223             BIO_printf(out, "\n");
224     }
225
226     ret = 0;
227 err:
228     if (ret != 0)
229         ERR_print_errors(bio_err);
230     OPENSSL_clear_free(buf, BUFSIZE);
231     OPENSSL_free(cipher);
232     OPENSSL_free(digest);
233     sk_OPENSSL_STRING_free(opts);
234     BIO_free(in);
235     BIO_free(out);
236     EVP_MAC_CTX_free(ctx);
237     EVP_MAC_free(mac);
238     return ret;
239 }