2
0

mac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /* One argument, the MAC name. */
  90. argc = opt_num_rest();
  91. argv = opt_rest();
  92. if (argc != 1)
  93. goto opthelp;
  94. mac = EVP_MAC_fetch(NULL, argv[0], NULL);
  95. if (mac == NULL) {
  96. BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
  97. goto opthelp;
  98. }
  99. ctx = EVP_MAC_CTX_new(mac);
  100. if (ctx == NULL)
  101. goto err;
  102. if (opts != NULL) {
  103. int ok = 1;
  104. OSSL_PARAM *params =
  105. app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
  106. if (params == NULL)
  107. goto err;
  108. if (!EVP_MAC_CTX_set_params(ctx, params)) {
  109. BIO_printf(bio_err, "MAC parameter error\n");
  110. ERR_print_errors(bio_err);
  111. ok = 0;
  112. }
  113. app_params_free(params);
  114. if (!ok)
  115. goto err;
  116. }
  117. /* Use text mode for stdin */
  118. if (infile == NULL || strcmp(infile, "-") == 0)
  119. inform = FORMAT_TEXT;
  120. in = bio_open_default(infile, 'r', inform);
  121. if (in == NULL)
  122. goto err;
  123. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  124. if (out == NULL)
  125. goto err;
  126. if (!EVP_MAC_init(ctx)) {
  127. BIO_printf(bio_err, "EVP_MAC_Init failed\n");
  128. goto err;
  129. }
  130. for (;;) {
  131. i = BIO_read(in, (char *)buf, BUFSIZE);
  132. if (i < 0) {
  133. BIO_printf(bio_err, "Read Error in '%s'\n", infile);
  134. goto err;
  135. }
  136. if (i == 0)
  137. break;
  138. if (!EVP_MAC_update(ctx, buf, i)) {
  139. BIO_printf(bio_err, "EVP_MAC_update failed\n");
  140. goto err;
  141. }
  142. }
  143. if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
  144. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  145. goto err;
  146. }
  147. if (len > BUFSIZE) {
  148. BIO_printf(bio_err, "output len is too large\n");
  149. goto err;
  150. }
  151. if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
  152. BIO_printf(bio_err, "EVP_MAC_final failed\n");
  153. goto err;
  154. }
  155. if (out_bin) {
  156. BIO_write(out, buf, len);
  157. } else {
  158. if (outfile == NULL)
  159. BIO_printf(out,"\n");
  160. for (i = 0; i < (int)len; ++i)
  161. BIO_printf(out, "%02X", buf[i]);
  162. if (outfile == NULL)
  163. BIO_printf(out,"\n");
  164. }
  165. ret = 0;
  166. err:
  167. if (ret != 0)
  168. ERR_print_errors(bio_err);
  169. OPENSSL_clear_free(buf, BUFSIZE);
  170. sk_OPENSSL_STRING_free(opts);
  171. BIO_free(in);
  172. BIO_free(out);
  173. EVP_MAC_CTX_free(ctx);
  174. EVP_MAC_free(mac);
  175. return ret;
  176. }