您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

103 行
2.8 KiB

  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. // cavp_sha_monte_test processes a NIST CAVP SHA-Monte test vector request file
  15. // and emits the corresponding response.
  16. #include <stdlib.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/digest.h>
  19. #include "../crypto/test/file_test.h"
  20. #include "cavp_test_util.h"
  21. namespace {
  22. struct TestCtx {
  23. std::string hash;
  24. };
  25. }
  26. static bool TestSHAMonte(FileTest *t, void *arg) {
  27. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  28. const EVP_MD *md = EVP_get_digestbyname(ctx->hash.c_str());
  29. if (md == nullptr) {
  30. return false;
  31. }
  32. const size_t md_len = EVP_MD_size(md);
  33. std::string out_len;
  34. if (!t->GetInstruction(&out_len, "L") ||
  35. md_len != strtoul(out_len.c_str(), nullptr, 0)) {
  36. return false;
  37. }
  38. std::vector<uint8_t> seed;
  39. if (!t->GetBytes(&seed, "Seed") ||
  40. seed.size() != md_len) {
  41. return false;
  42. }
  43. std::vector<uint8_t> out = seed;
  44. printf("%s\r\n", t->CurrentTestToString().c_str());
  45. for (int count = 0; count < 100; count++) {
  46. std::vector<uint8_t> msg;
  47. msg.insert(msg.end(), out.begin(), out.end());
  48. msg.insert(msg.end(), out.begin(), out.end());
  49. msg.insert(msg.end(), out.begin(), out.end());
  50. for (int i = 0; i < 1000; i++) {
  51. unsigned digest_len;
  52. if (!EVP_Digest(msg.data(), msg.size(), out.data(), &digest_len, md,
  53. nullptr) ||
  54. digest_len != out.size()) {
  55. return false;
  56. }
  57. msg.erase(msg.begin(), msg.begin() + out.size());
  58. msg.insert(msg.end(), out.begin(), out.end());
  59. }
  60. printf("COUNT = %d\r\n", count);
  61. printf("MD = %s\r\n\r\n", EncodeHex(out.data(), out.size()).c_str());
  62. }
  63. return true;
  64. }
  65. static int usage(char *arg) {
  66. fprintf(stderr, "usage: %s <hash> <test file>\n", arg);
  67. return 1;
  68. }
  69. int cavp_sha_monte_test_main(int argc, char **argv) {
  70. if (argc != 3) {
  71. return usage(argv[0]);
  72. }
  73. TestCtx ctx = {std::string(argv[1])};
  74. FileTest::Options opts;
  75. opts.path = argv[2];
  76. opts.callback = TestSHAMonte;
  77. opts.arg = &ctx;
  78. opts.silent = true;
  79. opts.comment_callback = EchoComment;
  80. return FileTestMain(opts);
  81. }