2
0

mac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. argc = opt_num_rest();
  125. argv = opt_rest();
  126. if (argc != 1)
  127. goto opthelp;
  128. mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq());
  129. if (mac == NULL) {
  130. BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
  131. goto opthelp;
  132. }
  133. ctx = EVP_MAC_CTX_new(mac);
  134. if (ctx == NULL)
  135. goto err;
  136. if (opts != NULL) {
  137. int ok = 1;
  138. params = app_params_new_from_opts(opts,
  139. EVP_MAC_settable_ctx_params(mac));
  140. if (params == NULL)
  141. goto err;
  142. if (!EVP_MAC_CTX_set_params(ctx, params)) {
  143. BIO_printf(bio_err, "MAC parameter error\n");
  144. ERR_print_errors(bio_err);
  145. ok = 0;
  146. }
  147. app_params_free(params);
  148. if (!ok)
  149. goto err;
  150. }
  151. /* Use text mode for stdin */
  152. if (infile == NULL || strcmp(infile, "-") == 0)
  153. inform = FORMAT_TEXT;
  154. in = bio_open_default(infile, 'r', inform);
  155. if (in == NULL)
  156. goto err;
  157. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  158. if (out == NULL)
  159. goto err;
  160. if (!EVP_MAC_init(ctx, NULL, 0, NULL)) {
  161. BIO_printf(bio_err, "EVP_MAC_Init failed\n");
  162. goto err;
  163. }
  164. while (BIO_pending(in) || !BIO_eof(in)) {
  165. i = BIO_read(in, (char *)buf, BUFSIZE);
  166. if (i < 0) {
  167. BIO_printf(bio_err, "Read Error in '%s'\n", infile);
  168. ERR_print_errors(bio_err);
  169. goto err;
  170. }
  171. if (i == 0)
  172. break;
  173. if (!EVP_MAC_update(ctx, buf, i)) {
  174. BIO_printf(bio_err, "EVP_MAC_update failed\n");
  175. goto err;
  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. if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
  187. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  188. goto err;
  189. }
  190. if (out_bin) {
  191. BIO_write(out, buf, len);
  192. } else {
  193. for (i = 0; i < (int)len; ++i)
  194. BIO_printf(out, "%02X", buf[i]);
  195. if (outfile == NULL)
  196. BIO_printf(out,"\n");
  197. }
  198. ret = 0;
  199. err:
  200. if (ret != 0)
  201. ERR_print_errors(bio_err);
  202. OPENSSL_clear_free(buf, BUFSIZE);
  203. OPENSSL_free(cipher);
  204. OPENSSL_free(digest);
  205. sk_OPENSSL_STRING_free(opts);
  206. BIO_free(in);
  207. BIO_free(out);
  208. EVP_MAC_CTX_free(ctx);
  209. EVP_MAC_free(mac);
  210. return ret;
  211. }