mac.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2018-2020 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. DEFINE_STACK_OF_STRING()
  17. #undef BUFSIZE
  18. #define BUFSIZE 1024*8
  19. typedef enum OPTION_choice {
  20. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  21. OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
  22. OPT_PROV_ENUM
  23. } OPTION_CHOICE;
  24. const OPTIONS mac_options[] = {
  25. {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
  26. OPT_SECTION("General"),
  27. {"help", OPT_HELP, '-', "Display this summary"},
  28. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
  29. {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
  30. OPT_SECTION("Input"),
  31. {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
  32. OPT_SECTION("Output"),
  33. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  34. {"binary", OPT_BIN, '-',
  35. "Output in binary format (default is hexadecimal)"},
  36. OPT_PROV_OPTIONS,
  37. OPT_PARAMETERS(),
  38. {"mac_name", 0, 0, "MAC algorithm"},
  39. {NULL}
  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. prog = opt_init(argc, argv, mac_options);
  58. buf = app_malloc(BUFSIZE, "I/O buffer");
  59. while ((o = opt_next()) != OPT_EOF) {
  60. switch (o) {
  61. default:
  62. opthelp:
  63. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  64. goto err;
  65. case OPT_HELP:
  66. opt_help(mac_options);
  67. ret = 0;
  68. goto err;
  69. case OPT_BIN:
  70. out_bin = 1;
  71. break;
  72. case OPT_IN:
  73. infile = opt_arg();
  74. break;
  75. case OPT_OUT:
  76. outfile = opt_arg();
  77. break;
  78. case OPT_MACOPT:
  79. if (opts == NULL)
  80. opts = sk_OPENSSL_STRING_new_null();
  81. if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
  82. goto opthelp;
  83. break;
  84. case OPT_PROV_CASES:
  85. if (!opt_provider(o))
  86. goto err;
  87. break;
  88. }
  89. }
  90. argc = opt_num_rest();
  91. argv = opt_rest();
  92. if (argc != 1) {
  93. BIO_printf(bio_err, "Invalid number of extra arguments\n");
  94. goto opthelp;
  95. }
  96. mac = EVP_MAC_fetch(NULL, argv[0], NULL);
  97. if (mac == NULL) {
  98. BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
  99. goto opthelp;
  100. }
  101. ctx = EVP_MAC_new_ctx(mac);
  102. if (ctx == NULL)
  103. goto err;
  104. if (opts != NULL) {
  105. int ok = 1;
  106. OSSL_PARAM *params =
  107. app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
  108. if (params == NULL)
  109. goto err;
  110. if (!EVP_MAC_set_ctx_params(ctx, params)) {
  111. BIO_printf(bio_err, "MAC parameter error\n");
  112. ERR_print_errors(bio_err);
  113. ok = 0;
  114. }
  115. app_params_free(params);
  116. if (!ok)
  117. goto err;
  118. }
  119. /* Use text mode for stdin */
  120. if (infile == NULL || strcmp(infile, "-") == 0)
  121. inform = FORMAT_TEXT;
  122. in = bio_open_default(infile, 'r', inform);
  123. if (in == NULL)
  124. goto err;
  125. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  126. if (out == NULL)
  127. goto err;
  128. if (!EVP_MAC_init(ctx)) {
  129. BIO_printf(bio_err, "EVP_MAC_Init failed\n");
  130. goto err;
  131. }
  132. for (;;) {
  133. i = BIO_read(in, (char *)buf, BUFSIZE);
  134. if (i < 0) {
  135. BIO_printf(bio_err, "Read Error in '%s'\n", infile);
  136. goto err;
  137. }
  138. if (i == 0)
  139. break;
  140. if (!EVP_MAC_update(ctx, buf, i)) {
  141. BIO_printf(bio_err, "EVP_MAC_update failed\n");
  142. goto err;
  143. }
  144. }
  145. if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
  146. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  147. goto err;
  148. }
  149. if (len > BUFSIZE) {
  150. BIO_printf(bio_err, "output len is too large\n");
  151. goto err;
  152. }
  153. if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
  154. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  155. goto err;
  156. }
  157. if (out_bin) {
  158. BIO_write(out, buf, len);
  159. } else {
  160. if (outfile == NULL)
  161. BIO_printf(out,"\n");
  162. for (i = 0; i < (int)len; ++i)
  163. BIO_printf(out, "%02X", buf[i]);
  164. if (outfile == NULL)
  165. BIO_printf(out,"\n");
  166. }
  167. ret = 0;
  168. err:
  169. if (ret != 0)
  170. ERR_print_errors(bio_err);
  171. OPENSSL_clear_free(buf, BUFSIZE);
  172. sk_OPENSSL_STRING_free(opts);
  173. BIO_free(in);
  174. BIO_free(out);
  175. EVP_MAC_free_ctx(ctx);
  176. EVP_MAC_free(mac);
  177. return ret;
  178. }