mac.c 6.2 KB

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