Adapt apps/mac.c to use provider based MACs
[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 #include <openssl/params.h>
18
19 #undef BUFSIZE
20 #define BUFSIZE 1024*8
21
22 typedef 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
27 const OPTIONS mac_options[] = {
28     {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
29     {OPT_HELP_STR, 1, '-', "mac_name\t\t MAC algorithm (See list "
30                            "-mac-algorithms)"},
31     {"help", OPT_HELP, '-', "Display this summary"},
32     {"macopt", OPT_MACOPT, 's', "MAC algorithm control parameters in n:v form. "
33                                 "See 'Supported Controls' in the EVP_MAC_ docs"},
34     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
35     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
36     {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
37                              "output)"},
38     {NULL}
39 };
40
41 int mac_main(int argc, char **argv)
42 {
43     int ret = 1;
44     char *prog;
45     EVP_MAC *mac = NULL;
46     OPTION_CHOICE o;
47     EVP_MAC_CTX *ctx = NULL;
48     STACK_OF(OPENSSL_STRING) *opts = NULL;
49     unsigned char *buf = NULL;
50     size_t len;
51     int i;
52     BIO *in = NULL, *out = NULL;
53     const char *outfile = NULL;
54     const char *infile = NULL;
55     int out_bin = 0;
56     int inform = FORMAT_BINARY;
57
58     prog = opt_init(argc, argv, mac_options);
59     buf = app_malloc(BUFSIZE, "I/O buffer");
60     while ((o = opt_next()) != OPT_EOF) {
61         switch (o) {
62         default:
63 opthelp:
64             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
65             goto err;
66         case OPT_HELP:
67             opt_help(mac_options);
68             ret = 0;
69             goto err;
70         case OPT_BIN:
71             out_bin = 1;
72             break;
73         case OPT_IN:
74             infile = opt_arg();
75             break;
76         case OPT_OUT:
77             outfile = opt_arg();
78             break;
79         case OPT_MACOPT:
80             if (opts == NULL)
81                 opts = sk_OPENSSL_STRING_new_null();
82             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
83                 goto opthelp;
84             break;
85         }
86     }
87     argc = opt_num_rest();
88     argv = opt_rest();
89
90     if (argc != 1) {
91         BIO_printf(bio_err, "Invalid number of extra arguments\n");
92         goto opthelp;
93     }
94
95     mac = EVP_MAC_fetch(NULL, argv[0], NULL);
96     if (mac == NULL) {
97         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
98         goto opthelp;
99     }
100
101     ctx = EVP_MAC_CTX_new(mac);
102     if (ctx == NULL)
103         goto err;
104
105     if (opts != NULL) {
106         OSSL_PARAM *params =
107             OPENSSL_zalloc(sizeof(OSSL_PARAM)
108                            * (sk_OPENSSL_STRING_num(opts) + 1));
109         const OSSL_PARAM *paramdefs = EVP_MAC_CTX_settable_params(mac);
110         size_t params_n;
111         int ok = 1;
112
113         for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
114              params_n++) {
115             char *opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
116             char *stmp, *vtmp = NULL;
117
118             if ((stmp = OPENSSL_strdup(opt)) == NULL
119                 || (vtmp = strchr(stmp, ':')) == NULL
120                 || (*vtmp++ = 0) /* Always zero */
121                 || !OSSL_PARAM_allocate_from_text(&params[params_n], paramdefs,
122                                                   stmp, vtmp, strlen(vtmp))) {
123                 BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
124                 ERR_print_errors(bio_err);
125                 ok = 0;
126             }
127             OPENSSL_free(stmp);
128             if (!ok)
129                 break;
130         }
131         if (ok) {
132             params[params_n] = OSSL_PARAM_construct_end();
133             if (!EVP_MAC_CTX_set_params(ctx, params)) {
134                 BIO_printf(bio_err, "MAC parameter error\n");
135                 ERR_print_errors(bio_err);
136                 goto err;
137             }
138         }
139         for (; params_n-- > 0;) {
140             OPENSSL_free(params[params_n].data);
141         }
142         OPENSSL_free(params);
143         if (!ok)
144             goto err;
145     }
146
147     /* Use text mode for stdin */
148     if (infile == NULL || strcmp(infile, "-") == 0)
149         inform = FORMAT_TEXT;
150     in = bio_open_default(infile, 'r', inform);
151     if (in == NULL)
152         goto err;
153
154     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
155     if (out == NULL)
156         goto err;
157
158     if (!EVP_MAC_init(ctx)) {
159         BIO_printf(bio_err, "EVP_MAC_Init failed\n");
160         goto err;
161     }
162
163
164     for (;;) {
165         i = BIO_read(in, (char *)buf, BUFSIZE);
166         if (i < 0) {
167             BIO_printf(bio_err, "Read Error in '%s'\n", infile);
168             goto err;
169         }
170         if (i == 0)
171             break;
172         if (!EVP_MAC_update(ctx, buf, i)) {
173             BIO_printf(bio_err, "EVP_MAC_update failed\n");
174             goto err;
175         }
176     }
177
178     if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
179         BIO_printf(bio_err, "EVP_MAC_final failed\n");
180         goto err;
181     }
182     if (len > BUFSIZE) {
183         BIO_printf(bio_err, "output len is too large\n");
184         goto err;
185     }
186
187     if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
188         BIO_printf(bio_err, "EVP_MAC_final failed\n");
189         goto err;
190     }
191
192     if (out_bin) {
193         BIO_write(out, buf, len);
194     } else {
195         if (outfile == NULL)
196             BIO_printf(out,"\n");
197         for (i = 0; i < (int)len; ++i)
198             BIO_printf(out, "%02X", buf[i]);
199         if (outfile == NULL)
200             BIO_printf(out,"\n");
201     }
202
203     ret = 0;
204 err:
205     if (ret != 0)
206         ERR_print_errors(bio_err);
207     OPENSSL_clear_free(buf, BUFSIZE);
208     sk_OPENSSL_STRING_free(opts);
209     BIO_free(in);
210     BIO_free(out);
211     EVP_MAC_CTX_free(ctx);
212     EVP_MAC_free(mac);
213     return ret;
214 }