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.
 
 
 
 
 
 

134 lines
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. typedef struct {
  20. const char *decoded;
  21. const char *encoded;
  22. } TEST_VECTOR;
  23. /* Test vectors from RFC 4648. */
  24. static const TEST_VECTOR test_vectors[] = {
  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(test_vectors) / sizeof(test_vectors[0]);
  34. static int test_encode(void) {
  35. uint8_t out[9];
  36. size_t i, len;
  37. for (i = 0; i < kNumTests; i++) {
  38. const TEST_VECTOR *t = &test_vectors[i];
  39. len = EVP_EncodeBlock(out, (const uint8_t*)t->decoded, 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 0;
  45. }
  46. }
  47. return 1;
  48. }
  49. static int test_decode(void) {
  50. uint8_t out[6];
  51. size_t i, len;
  52. int ret;
  53. for (i = 0; i < kNumTests; i++) {
  54. /* Test the normal API. */
  55. const TEST_VECTOR *t = &test_vectors[i];
  56. size_t expected_len = strlen(t->decoded);
  57. if (!EVP_DecodeBase64(out, &len, sizeof(out),
  58. (const uint8_t*)t->encoded, strlen(t->encoded))) {
  59. fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
  60. return 0;
  61. }
  62. if (len != strlen(t->decoded) ||
  63. memcmp(out, t->decoded, len) != 0) {
  64. fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
  65. t->encoded, (int)len, (const char*)out, t->decoded);
  66. return 0;
  67. }
  68. /* Test that the padding behavior of the deprecated API is
  69. * preserved. */
  70. ret = EVP_DecodeBlock(out, (const uint8_t*)t->encoded, strlen(t->encoded));
  71. if (ret < 0) {
  72. fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
  73. return 0;
  74. }
  75. if (ret % 3 != 0) {
  76. fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n");
  77. return 0;
  78. }
  79. if (expected_len % 3 != 0) {
  80. ret -= 3 - (expected_len % 3);
  81. }
  82. if (ret != strlen(t->decoded) ||
  83. memcmp(out, t->decoded, ret) != 0) {
  84. fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
  85. t->encoded, ret, (const char*)out, t->decoded);
  86. return 0;
  87. }
  88. }
  89. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a!bc", 4)) {
  90. fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
  91. return 0;
  92. }
  93. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a=bc", 4)) {
  94. fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
  95. return 0;
  96. }
  97. if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"abc", 4)) {
  98. fprintf(stderr, "Failed to reject invalid input length.\n");
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. int main(void) {
  104. CRYPTO_library_init();
  105. ERR_load_crypto_strings();
  106. if (!test_encode()) {
  107. return 1;
  108. }
  109. if (!test_decode()) {
  110. return 1;
  111. }
  112. printf("PASS\n");
  113. return 0;
  114. }