25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.4 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 "../test/file_test.h"
  20. #include "../test/stl_compat.h"
  21. // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
  22. #if defined(OPENSSL_WINDOWS)
  23. // MSVC doesn't support C++11 |alignas|.
  24. #define ALIGNED __declspec(align(16))
  25. #else
  26. #define ALIGNED alignas(16)
  27. #endif
  28. static bool TestPoly1305(FileTest *t, void *arg) {
  29. std::vector<uint8_t> key, in, mac;
  30. if (!t->GetBytes(&key, "Key") ||
  31. !t->GetBytes(&in, "Input") ||
  32. !t->GetBytes(&mac, "MAC")) {
  33. return false;
  34. }
  35. if (key.size() != 32 || mac.size() != 16) {
  36. t->PrintLine("Invalid test");
  37. return false;
  38. }
  39. // Test single-shot operation.
  40. poly1305_state state;
  41. CRYPTO_poly1305_init(&state, bssl::vector_data(&key));
  42. CRYPTO_poly1305_update(&state, bssl::vector_data(&in), in.size());
  43. ALIGNED uint8_t out[16];
  44. CRYPTO_poly1305_finish(&state, out);
  45. if (!t->ExpectBytesEqual(out, 16, bssl::vector_data(&mac), mac.size())) {
  46. t->PrintLine("Single-shot Poly1305 failed.");
  47. return false;
  48. }
  49. // Test streaming byte-by-byte.
  50. CRYPTO_poly1305_init(&state, bssl::vector_data(&key));
  51. for (size_t i = 0; i < in.size(); i++) {
  52. CRYPTO_poly1305_update(&state, &in[i], 1);
  53. }
  54. CRYPTO_poly1305_finish(&state, out);
  55. if (!t->ExpectBytesEqual(out, 16, bssl::vector_data(&mac), mac.size())) {
  56. t->PrintLine("Streaming Poly1305 failed.");
  57. return false;
  58. }
  59. return true;
  60. }
  61. int main(int argc, char **argv) {
  62. CRYPTO_library_init();
  63. if (argc != 2) {
  64. fprintf(stderr, "%s <test file>\n", argv[0]);
  65. return 1;
  66. }
  67. return FileTestMain(TestPoly1305, nullptr, argv[1]);
  68. }