You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 line
3.3 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_keywrap_test processes a NIST CAVP AES test vector request file and
  15. // emits the corresponding response.
  16. #include <stdlib.h>
  17. #include <openssl/aes.h>
  18. #include <openssl/crypto.h>
  19. #include "../crypto/test/file_test.h"
  20. #include "cavp_test_util.h"
  21. namespace {
  22. struct TestCtx {
  23. bool encrypt;
  24. };
  25. }
  26. static bool AESKeyWrap(std::vector<uint8_t> *out, bool encrypt,
  27. const std::vector<uint8_t> &key,
  28. const std::vector<uint8_t> &in) {
  29. size_t key_bits = key.size() * 8;
  30. if (key_bits != 128 && key_bits != 256) {
  31. return false;
  32. }
  33. AES_KEY aes_key;
  34. if (encrypt) {
  35. out->resize(in.size() + 8);
  36. if (AES_set_encrypt_key(key.data(), key_bits, &aes_key) ||
  37. AES_wrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
  38. -1) {
  39. return false;
  40. }
  41. } else {
  42. out->resize(in.size() - 8);
  43. if (AES_set_decrypt_key(key.data(), key_bits, &aes_key) ||
  44. AES_unwrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
  45. -1) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. static bool TestCipher(FileTest *t, void *arg) {
  52. TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
  53. std::string count, unused, in_label = ctx->encrypt ? "P" : "C",
  54. result_label = ctx->encrypt ? "C" : "P";
  55. std::vector<uint8_t> key, in, result;
  56. // clang-format off
  57. if (!t->GetInstruction(&unused, "PLAINTEXT LENGTH") ||
  58. !t->GetAttribute(&count, "COUNT") ||
  59. !t->GetBytes(&key, "K") ||
  60. !t->GetBytes(&in, in_label)) {
  61. return false;
  62. }
  63. // clang-format on
  64. printf("%s", t->CurrentTestToString().c_str());
  65. if (!AESKeyWrap(&result, ctx->encrypt, key, in)) {
  66. if (ctx->encrypt) {
  67. return false;
  68. } else {
  69. printf("FAIL\r\n\r\n");
  70. }
  71. } else {
  72. printf("%s = %s\r\n\r\n", result_label.c_str(),
  73. EncodeHex(result.data(), result.size()).c_str());
  74. }
  75. return true;
  76. }
  77. static int usage(char *arg) {
  78. fprintf(
  79. stderr,
  80. "usage: %s (enc|dec) (128|256) <test file>\n",
  81. arg);
  82. return 1;
  83. }
  84. int cavp_keywrap_test_main(int argc, char **argv) {
  85. if (argc != 4) {
  86. return usage(argv[0]);
  87. }
  88. const std::string op(argv[1]);
  89. bool encrypt;
  90. if (op == "enc") {
  91. encrypt = true;
  92. } else if (op == "dec") {
  93. encrypt = false;
  94. } else {
  95. return usage(argv[0]);
  96. }
  97. TestCtx ctx = {encrypt};
  98. FileTest::Options opts;
  99. opts.path = argv[3];
  100. opts.callback = TestCipher;
  101. opts.arg = &ctx;
  102. opts.silent = true;
  103. opts.comment_callback = EchoComment;
  104. return FileTestMain(opts);
  105. }