Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

107 řádky
3.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_ctr_drbg_test processes a NIST CAVP DRBG800-90A test vector request
  15. // file and emits the corresponding response.
  16. #include <openssl/crypto.h>
  17. #include <stdlib.h>
  18. #include "cavp_test_util.h"
  19. #include "../crypto/fipsmodule/rand/internal.h"
  20. #include "../crypto/test/file_test.h"
  21. static bool TestCTRDRBG(FileTest *t, void *arg) {
  22. std::string test_type, prediction_resistance, entropy_input_len, nonce_len,
  23. personalization_str_len, additional_input_len, returned_bits_len;
  24. if (!t->GetInstruction(&test_type, "AES-256 no df") ||
  25. !t->GetInstruction(&prediction_resistance, "PredictionResistance") ||
  26. !t->GetInstruction(&entropy_input_len, "EntropyInputLen") ||
  27. !t->GetInstruction(&nonce_len, "NonceLen") ||
  28. !t->GetInstruction(&personalization_str_len,
  29. "PersonalizationStringLen") ||
  30. !t->GetInstruction(&additional_input_len, "AdditionalInputLen") ||
  31. !t->GetInstruction(&returned_bits_len, "ReturnedBitsLen") ||
  32. !test_type.empty() ||
  33. prediction_resistance != "False" ||
  34. strtoul(entropy_input_len.c_str(), nullptr, 0) !=
  35. CTR_DRBG_ENTROPY_LEN * 8 ||
  36. nonce_len != "0") {
  37. return false;
  38. }
  39. std::string count;
  40. std::vector<uint8_t> entropy, nonce, personalization_str, ai1, ai2;
  41. if (!t->GetAttribute(&count, "COUNT") ||
  42. !t->GetBytes(&entropy, "EntropyInput") ||
  43. !t->GetBytes(&nonce, "Nonce") ||
  44. !t->GetBytes(&personalization_str, "PersonalizationString") ||
  45. !t->GetBytes(&ai1, "AdditionalInput") ||
  46. !t->GetBytes(&ai2, "AdditionalInput/2") ||
  47. entropy.size() * 8 != strtoul(entropy_input_len.c_str(), nullptr, 0) ||
  48. nonce.size() != 0 ||
  49. personalization_str.size() * 8 !=
  50. strtoul(personalization_str_len.c_str(), nullptr, 0) ||
  51. ai1.size() != ai2.size() ||
  52. ai1.size() * 8 != strtoul(additional_input_len.c_str(), nullptr, 0)) {
  53. return false;
  54. }
  55. CTR_DRBG_STATE drbg;
  56. CTR_DRBG_init(&drbg, entropy.data(),
  57. personalization_str.size() > 0 ? personalization_str.data()
  58. : nullptr,
  59. personalization_str.size());
  60. uint64_t out_len = strtoul(returned_bits_len.c_str(), nullptr, 0);
  61. if (out_len == 0 || (out_len & 7) != 0) {
  62. return false;
  63. }
  64. out_len /= 8;
  65. std::vector<uint8_t> out;
  66. out.resize(out_len);
  67. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  68. ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
  69. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  70. ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());
  71. printf("%s", t->CurrentTestToString().c_str());
  72. printf("ReturnedBits = %s\r\n\r\n",
  73. EncodeHex(out.data(), out.size()).c_str());
  74. return true;
  75. }
  76. static int usage(char *arg) {
  77. fprintf(stderr, "usage: %s <test file>\n", arg);
  78. return 1;
  79. }
  80. int cavp_ctr_drbg_test_main(int argc, char **argv) {
  81. if (argc != 2) {
  82. return usage(argv[0]);
  83. }
  84. FileTest::Options opts;
  85. opts.path = argv[1];
  86. opts.callback = TestCTRDRBG;
  87. opts.silent = true;
  88. opts.comment_callback = EchoComment;
  89. return FileTestMain(opts);
  90. }