Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

132 wiersze
3.9 KiB

  1. /* Copyright (c) 2015, 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. #include <stdio.h>
  15. #include <string.h>
  16. #include <vector>
  17. #include <openssl/crypto.h>
  18. #include <openssl/poly1305.h>
  19. #include "../internal.h"
  20. #include "../test/file_test.h"
  21. static bool TestSIMD(FileTest *t, unsigned excess,
  22. const std::vector<uint8_t> &key,
  23. const std::vector<uint8_t> &in,
  24. const std::vector<uint8_t> &mac) {
  25. poly1305_state state;
  26. CRYPTO_poly1305_init(&state, key.data());
  27. size_t done = 0;
  28. // Feed 16 bytes in. Some implementations begin in non-SIMD mode and upgrade
  29. // on-demand. Stress the upgrade path.
  30. size_t todo = 16;
  31. if (todo > in.size()) {
  32. todo = in.size();
  33. }
  34. CRYPTO_poly1305_update(&state, in.data(), todo);
  35. done += todo;
  36. for (;;) {
  37. // Feed 128 + |excess| bytes to test SIMD mode.
  38. if (done + 128 + excess > in.size()) {
  39. break;
  40. }
  41. CRYPTO_poly1305_update(&state, in.data() + done, 128 + excess);
  42. done += 128 + excess;
  43. // Feed |excess| bytes to ensure SIMD mode can handle short inputs.
  44. if (done + excess > in.size()) {
  45. break;
  46. }
  47. CRYPTO_poly1305_update(&state, in.data() + done, excess);
  48. done += excess;
  49. }
  50. // Consume the remainder and finish.
  51. CRYPTO_poly1305_update(&state, in.data() + done, in.size() - done);
  52. // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
  53. alignas(16) uint8_t out[16];
  54. CRYPTO_poly1305_finish(&state, out);
  55. if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) {
  56. t->PrintLine("SIMD pattern %u failed.", excess);
  57. return false;
  58. }
  59. return true;
  60. }
  61. static bool TestPoly1305(FileTest *t, void *arg) {
  62. std::vector<uint8_t> key, in, mac;
  63. if (!t->GetBytes(&key, "Key") ||
  64. !t->GetBytes(&in, "Input") ||
  65. !t->GetBytes(&mac, "MAC")) {
  66. return false;
  67. }
  68. if (key.size() != 32 || mac.size() != 16) {
  69. t->PrintLine("Invalid test");
  70. return false;
  71. }
  72. // Test single-shot operation.
  73. poly1305_state state;
  74. CRYPTO_poly1305_init(&state, key.data());
  75. CRYPTO_poly1305_update(&state, in.data(), in.size());
  76. // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
  77. alignas(16) uint8_t out[16];
  78. CRYPTO_poly1305_finish(&state, out);
  79. if (!t->ExpectBytesEqual(out, 16, mac.data(), mac.size())) {
  80. t->PrintLine("Single-shot Poly1305 failed.");
  81. return false;
  82. }
  83. // Test streaming byte-by-byte.
  84. CRYPTO_poly1305_init(&state, key.data());
  85. for (size_t i = 0; i < in.size(); i++) {
  86. CRYPTO_poly1305_update(&state, &in[i], 1);
  87. }
  88. CRYPTO_poly1305_finish(&state, out);
  89. if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) {
  90. t->PrintLine("Streaming Poly1305 failed.");
  91. return false;
  92. }
  93. // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of
  94. // four blocks, so test up to three blocks of excess.
  95. if (!TestSIMD(t, 0, key, in, mac) ||
  96. !TestSIMD(t, 16, key, in, mac) ||
  97. !TestSIMD(t, 32, key, in, mac) ||
  98. !TestSIMD(t, 48, key, in, mac)) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. int main(int argc, char **argv) {
  104. CRYPTO_library_init();
  105. if (argc != 2) {
  106. fprintf(stderr, "%s <test file>\n", argv[0]);
  107. return 1;
  108. }
  109. return FileTestMain(TestPoly1305, nullptr, argv[1]);
  110. }