Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

128 рядки
3.8 KiB

  1. /* Copyright (c) 2014, 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 <openssl/base64.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/err.h>
  19. struct TestVector {
  20. const char *decoded;
  21. const char *encoded;
  22. };
  23. // Test vectors from RFC 4648.
  24. static const TestVector kTestVectors[] = {
  25. { "", "" },
  26. { "f" , "Zg==" },
  27. { "fo", "Zm8=" },
  28. { "foo", "Zm9v" },
  29. { "foob", "Zm9vYg==" },
  30. { "fooba", "Zm9vYmE=" },
  31. { "foobar", "Zm9vYmFy" },
  32. };
  33. static const size_t kNumTests = sizeof(kTestVectors) / sizeof(kTestVectors[0]);
  34. static bool TestEncode() {
  35. for (size_t i = 0; i < kNumTests; i++) {
  36. const TestVector *t = &kTestVectors[i];
  37. uint8_t out[9];
  38. size_t len = EVP_EncodeBlock(out, (const uint8_t*)t->decoded,
  39. strlen(t->decoded));
  40. if (len != strlen(t->encoded) ||
  41. memcmp(out, t->encoded, len) != 0) {
  42. fprintf(stderr, "encode(\"%s\") = \"%.*s\", want \"%s\"\n",
  43. t->decoded, (int)len, (const char*)out, t->encoded);
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. static bool TestDecode() {
  50. uint8_t out[6];
  51. size_t len;
  52. for (size_t i = 0; i < kNumTests; i++) {
  53. // Test the normal API.
  54. const TestVector *t = &kTestVectors[i];
  55. size_t expected_len = strlen(t->decoded);
  56. if (!EVP_DecodeBase64(out, &len, sizeof(out),
  57. (const uint8_t*)t->encoded, strlen(t->encoded))) {
  58. fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
  59. return false;
  60. }
  61. if (len != strlen(t->decoded) ||
  62. memcmp(out, t->decoded, len) != 0) {
  63. fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
  64. t->encoded, (int)len, (const char*)out, t->decoded);
  65. return false;
  66. }
  67. // Test that the padding behavior of the deprecated API is preserved.
  68. int ret = EVP_DecodeBlock(out, (const uint8_t*)t->encoded,
  69. strlen(t->encoded));
  70. if (ret < 0) {
  71. fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
  72. return false;
  73. }
  74. if (ret % 3 != 0) {
  75. fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n");
  76. return false;
  77. }
  78. if (expected_len % 3 != 0) {
  79. ret -= 3 - (expected_len % 3);
  80. }
  81. if (static_cast<size_t>(ret) != strlen(t->decoded) ||
  82. memcmp(out, t->decoded, ret) != 0) {
  83. fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
  84. t->encoded, ret, (const char*)out, t->decoded);
  85. return false;
  86. }
  87. }
  88. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a!bc", 4)) {
  89. fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
  90. return false;
  91. }
  92. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a=bc", 4)) {
  93. fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
  94. return false;
  95. }
  96. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"abc", 4)) {
  97. fprintf(stderr, "Failed to reject invalid input length.\n");
  98. return false;
  99. }
  100. return true;
  101. }
  102. int main(void) {
  103. CRYPTO_library_init();
  104. if (!TestEncode() ||
  105. !TestDecode()) {
  106. return 1;
  107. }
  108. printf("PASS\n");
  109. return 0;
  110. }