選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

104 行
2.7 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_hmac_test processes a NIST CAVP HMAC test vector request file and emits
  15. // the corresponding response.
  16. #include <stdlib.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/hmac.h>
  19. #include "../crypto/test/file_test.h"
  20. #include "cavp_test_util.h"
  21. static bool TestHMAC(FileTest *t, void *arg) {
  22. std::string md_len_str;
  23. if (!t->GetInstruction(&md_len_str, "L")) {
  24. return false;
  25. }
  26. const size_t md_len = strtoul(md_len_str.c_str(), nullptr, 0);
  27. const EVP_MD *md;
  28. switch (md_len) {
  29. case 20:
  30. md = EVP_sha1();
  31. break;
  32. case 28:
  33. md = EVP_sha224();
  34. break;
  35. case 32:
  36. md = EVP_sha256();
  37. break;
  38. case 48:
  39. md = EVP_sha384();
  40. break;
  41. case 64:
  42. md = EVP_sha512();
  43. break;
  44. default:
  45. return false;
  46. }
  47. std::string count_str, k_len_str, t_len_str;
  48. std::vector<uint8_t> key, msg;
  49. if (!t->GetAttribute(&count_str, "Count") ||
  50. !t->GetAttribute(&k_len_str, "Klen") ||
  51. !t->GetAttribute(&t_len_str, "Tlen") ||
  52. !t->GetBytes(&key, "Key") ||
  53. !t->GetBytes(&msg, "Msg")) {
  54. return false;
  55. }
  56. size_t k_len = strtoul(k_len_str.c_str(), nullptr, 0);
  57. size_t t_len = strtoul(t_len_str.c_str(), nullptr, 0);
  58. if (key.size() < k_len) {
  59. return false;
  60. }
  61. unsigned out_len;
  62. uint8_t out[EVP_MAX_MD_SIZE];
  63. if (HMAC(md, key.data(), k_len, msg.data(), msg.size(), out, &out_len) ==
  64. NULL) {
  65. return false;
  66. }
  67. if (out_len < t_len) {
  68. return false;
  69. }
  70. printf("%s", t->CurrentTestToString().c_str());
  71. printf("Mac = %s\r\n\r\n", EncodeHex(out, t_len).c_str());
  72. return true;
  73. }
  74. static int usage(char *arg) {
  75. fprintf(stderr, "usage: %s <test file>\n", arg);
  76. return 1;
  77. }
  78. int cavp_hmac_test_main(int argc, char **argv) {
  79. if (argc != 2) {
  80. return usage(argv[0]);
  81. }
  82. FileTest::Options opts;
  83. opts.path = argv[1];
  84. opts.callback = TestHMAC;
  85. opts.silent = true;
  86. opts.comment_callback = EchoComment;
  87. return FileTestMain(opts);
  88. }