Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

164 rader
5.0 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_aes_gcm_test processes a NIST CAVP AES GCM test vector request file and
  15. // emits the corresponding response.
  16. #include <stdlib.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/cipher.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/err.h>
  21. #include "../crypto/test/file_test.h"
  22. #include "cavp_test_util.h"
  23. namespace {
  24. struct TestCtx {
  25. const EVP_AEAD *aead;
  26. };
  27. }
  28. static const EVP_AEAD *GetAEAD(const std::string &name, const bool enc) {
  29. if (name == "aes-128-gcm") {
  30. return EVP_aead_aes_128_gcm();
  31. } else if (name == "aes-256-gcm") {
  32. return EVP_aead_aes_256_gcm();
  33. }
  34. return nullptr;
  35. }
  36. static bool TestAEADEncrypt(FileTest *t, void *arg) {
  37. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  38. std::string key_len_str, iv_len_str, pt_len_str, aad_len_str, tag_len_str;
  39. if (!t->GetInstruction(&key_len_str, "Keylen") ||
  40. !t->GetInstruction(&iv_len_str, "IVlen") ||
  41. !t->GetInstruction(&pt_len_str, "PTlen") ||
  42. !t->GetInstruction(&aad_len_str, "AADlen") ||
  43. !t->GetInstruction(&tag_len_str, "Taglen")) {
  44. return false;
  45. }
  46. std::string count;
  47. std::vector<uint8_t> key, iv, pt, aad, tag, ct;
  48. if (!t->GetAttribute(&count, "Count") ||
  49. !t->GetBytes(&key, "Key") ||
  50. !t->GetBytes(&iv, "IV") ||
  51. !t->GetBytes(&pt, "PT") ||
  52. !t->GetBytes(&aad, "AAD") ||
  53. key.size() * 8 != strtoul(key_len_str.c_str(), nullptr, 0) ||
  54. iv.size() * 8 != strtoul(iv_len_str.c_str(), nullptr, 0) ||
  55. pt.size() * 8 != strtoul(pt_len_str.c_str(), nullptr, 0) ||
  56. aad.size() * 8 != strtoul(aad_len_str.c_str(), nullptr, 0) ||
  57. iv.size() != 12) {
  58. return false;
  59. }
  60. const size_t tag_len = strtoul(tag_len_str.c_str(), nullptr, 0) / 8;
  61. if (!AEADEncrypt(ctx->aead, &ct, &tag, tag_len, key, pt, aad, iv)) {
  62. return false;
  63. }
  64. printf("%s", t->CurrentTestToString().c_str());
  65. printf("CT = %s\r\n", EncodeHex(ct.data(), ct.size()).c_str());
  66. printf("Tag = %s\r\n\r\n", EncodeHex(tag.data(), tag.size()).c_str());
  67. return true;
  68. }
  69. static bool TestAEADDecrypt(FileTest *t, void *arg) {
  70. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  71. std::string key_len, iv_len, pt_len_str, aad_len_str, tag_len;
  72. if (!t->GetInstruction(&key_len, "Keylen") ||
  73. !t->GetInstruction(&iv_len, "IVlen") ||
  74. !t->GetInstruction(&pt_len_str, "PTlen") ||
  75. !t->GetInstruction(&aad_len_str, "AADlen") ||
  76. !t->GetInstruction(&tag_len, "Taglen")) {
  77. t->PrintLine("Invalid instruction block.");
  78. return false;
  79. }
  80. size_t aad_len = strtoul(aad_len_str.c_str(), nullptr, 0) / 8;
  81. size_t pt_len = strtoul(pt_len_str.c_str(), nullptr, 0) / 8;
  82. std::string count;
  83. std::vector<uint8_t> key, iv, ct, aad, tag, pt;
  84. if (!t->GetAttribute(&count, "Count") ||
  85. !t->GetBytes(&key, "Key") ||
  86. !t->GetBytes(&aad, "AAD") ||
  87. !t->GetBytes(&tag, "Tag") ||
  88. !t->GetBytes(&iv, "IV") ||
  89. !t->GetBytes(&ct, "CT") ||
  90. key.size() * 8 != strtoul(key_len.c_str(), nullptr, 0) ||
  91. iv.size() * 8 != strtoul(iv_len.c_str(), nullptr, 0) ||
  92. ct.size() != pt_len ||
  93. aad.size() != aad_len ||
  94. tag.size() * 8 != strtoul(tag_len.c_str(), nullptr, 0)) {
  95. t->PrintLine("Invalid test case");
  96. return false;
  97. }
  98. printf("%s", t->CurrentTestToString().c_str());
  99. bool aead_result =
  100. AEADDecrypt(ctx->aead, &pt, pt_len, key, aad, ct, tag, iv);
  101. if (aead_result) {
  102. printf("PT = %s\r\n\r\n", EncodeHex(pt.data(), pt.size()).c_str());
  103. } else {
  104. printf("FAIL\r\n\r\n");
  105. }
  106. return true;
  107. }
  108. static int usage(char *arg) {
  109. fprintf(stderr, "usage: %s (enc|dec) <cipher> <test file>\n", arg);
  110. return 1;
  111. }
  112. int cavp_aes_gcm_test_main(int argc, char **argv) {
  113. if (argc != 4) {
  114. return usage(argv[0]);
  115. }
  116. const std::string mode(argv[1]);
  117. bool (*test_fn)(FileTest * t, void *arg);
  118. if (mode == "enc") {
  119. test_fn = &TestAEADEncrypt;
  120. } else if (mode == "dec") {
  121. test_fn = &TestAEADDecrypt;
  122. } else {
  123. return usage(argv[0]);
  124. }
  125. const EVP_AEAD *aead = GetAEAD(argv[2], mode == "enc");
  126. if (aead == nullptr) {
  127. fprintf(stderr, "invalid aead: %s\n", argv[2]);
  128. return 1;
  129. }
  130. TestCtx ctx = {aead};
  131. FileTest::Options opts;
  132. opts.path = argv[3];
  133. opts.callback = test_fn;
  134. opts.arg = &ctx;
  135. opts.silent = true;
  136. opts.comment_callback = EchoComment;
  137. return FileTestMain(opts);
  138. }