add OPENSSL_FUNC.pod documenting OPENSSL_MSTR, OPENSSL_FUNC, and friends
[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
32     OPT_SECTION("General"),
33     {"help", OPT_HELP, '-', "Display this summary"},
34     {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
35                                 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
36
37     OPT_SECTION("Input"),
38     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
39
40     OPT_SECTION("Output"),
41     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
42     {"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
43                              "output)"},
44     {NULL}
45 };
46
47 int mac_main(int argc, char **argv)
48 {
49     int ret = 1;
50     char *prog;
51     EVP_MAC *mac = NULL;
52     OPTION_CHOICE o;
53     EVP_MAC_CTX *ctx = NULL;
54     STACK_OF(OPENSSL_STRING) *opts = NULL;
55     unsigned char *buf = NULL;
56     size_t len;
57     int i;
58     BIO *in = NULL, *out = NULL;
59     const char *outfile = NULL;
60     const char *infile = NULL;
61     int out_bin = 0;
62     int inform = FORMAT_BINARY;
63
64     prog = opt_init(argc, argv, mac_options);
65     buf = app_malloc(BUFSIZE, "I/O buffer");
66     while ((o = opt_next()) != OPT_EOF) {
67         switch (o) {
68         default:
69 opthelp:
70             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
71             goto err;
72         case OPT_HELP:
73             opt_help(mac_options);
74             ret = 0;
75             goto err;
76         case OPT_BIN:
77             out_bin = 1;
78             break;
79         case OPT_IN:
80             infile = opt_arg();
81             break;
82         case OPT_OUT:
83             outfile = opt_arg();
84             break;
85         case OPT_MACOPT:
86             if (opts == NULL)
87                 opts = sk_OPENSSL_STRING_new_null();
88             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
89                 goto opthelp;
90             break;
91         }
92     }
93     argc = opt_num_rest();
94     argv = opt_rest();
95
96     if (argc != 1) {
97         BIO_printf(bio_err, "Invalid number of extra arguments\n");
98         goto opthelp;
99     }
100
101     mac = EVP_MAC_fetch(NULL, argv[0], NULL);
102     if (mac == NULL) {
103         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
104         goto opthelp;
105     }
106
107     ctx = EVP_MAC_CTX_new(mac);
108     if (ctx == NULL)
109         goto err;
110
111     if (opts != NULL) {
112         int ok = 1;
113         OSSL_PARAM *params =
114             app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
115
116         if (params == NULL)
117             goto err;
118
119         if (!EVP_MAC_CTX_set_params(ctx, params)) {
120             BIO_printf(bio_err, "MAC parameter error\n");
121             ERR_print_errors(bio_err);
122             ok = 0;
123         }
124         app_params_free(params);
125         if (!ok)
126             goto err;
127     }
128
129     /* Use text mode for stdin */
130     if (infile == NULL || strcmp(infile, "-") == 0)
131         inform = FORMAT_TEXT;
132     in = bio_open_default(infile, 'r', inform);
133     if (in == NULL)
134         goto err;
135
136     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
137     if (out == NULL)
138         goto err;
139
140     if (!EVP_MAC_init(ctx)) {
141         BIO_printf(bio_err, "EVP_MAC_Init failed\n");
142         goto err;
143     }
144
145     for (;;) {
146         i = BIO_read(in, (char *)buf, BUFSIZE);
147         if (i < 0) {
148             BIO_printf(bio_err, "Read Error in '%s'\n", infile);
149             goto err;
150         }
151         if (i == 0)
152             break;
153         if (!EVP_MAC_update(ctx, buf, i)) {
154             BIO_printf(bio_err, "EVP_MAC_update failed\n");
155             goto err;
156         }
157     }
158
159     if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
160         BIO_printf(bio_err, "EVP_MAC_final failed\n");
161         goto err;
162     }
163     if (len > BUFSIZE) {
164         BIO_printf(bio_err, "output len is too large\n");
165         goto err;
166     }
167
168     if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
169         BIO_printf(bio_err, "EVP_MAC_final failed\n");
170         goto err;
171     }
172
173     if (out_bin) {
174         BIO_write(out, buf, len);
175     } else {
176         if (outfile == NULL)
177             BIO_printf(out,"\n");
178         for (i = 0; i < (int)len; ++i)
179             BIO_printf(out, "%02X", buf[i]);
180         if (outfile == NULL)
181             BIO_printf(out,"\n");
182     }
183
184     ret = 0;
185 err:
186     if (ret != 0)
187         ERR_print_errors(bio_err);
188     OPENSSL_clear_free(buf, BUFSIZE);
189     sk_OPENSSL_STRING_free(opts);
190     BIO_free(in);
191     BIO_free(out);
192     EVP_MAC_CTX_free(ctx);
193     EVP_MAC_free(mac);
194     return ret;
195 }