2
0

mac.c 5.2 KB

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