1d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Copyright (c) 2014, Google Inc. 2d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 3d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Permission to use, copy, modify, and/or distribute this software for any 4d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * purpose with or without fee is hereby granted, provided that the above 5d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * copyright notice and this permission notice appear in all copies. 6d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 7d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 15d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <stdio.h> 16d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <string.h> 17d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 18d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/base64.h> 19d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/crypto.h> 20d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/err.h> 21d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 22d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 23e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystruct TestVector { 24d9e397b599b13d642138480a28c14db7a136bf0Adam Langley const char *decoded; 25d9e397b599b13d642138480a28c14db7a136bf0Adam Langley const char *encoded; 26e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}; 27d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 28e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// Test vectors from RFC 4648. 29e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic const TestVector kTestVectors[] = { 30d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "", "" }, 31d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "f" , "Zg==" }, 32d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "fo", "Zm8=" }, 33d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "foo", "Zm9v" }, 34d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "foob", "Zm9vYg==" }, 35d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "fooba", "Zm9vYmE=" }, 36d9e397b599b13d642138480a28c14db7a136bf0Adam Langley { "foobar", "Zm9vYmFy" }, 37d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}; 38d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 39e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic const size_t kNumTests = sizeof(kTestVectors) / sizeof(kTestVectors[0]); 40d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 41e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool TestEncode() { 42e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley for (size_t i = 0; i < kNumTests; i++) { 43e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley const TestVector *t = &kTestVectors[i]; 44e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley uint8_t out[9]; 45e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley size_t len = EVP_EncodeBlock(out, (const uint8_t*)t->decoded, 46e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley strlen(t->decoded)); 47d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (len != strlen(t->encoded) || 48d9e397b599b13d642138480a28c14db7a136bf0Adam Langley memcmp(out, t->encoded, len) != 0) { 49d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "encode(\"%s\") = \"%.*s\", want \"%s\"\n", 50d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t->decoded, (int)len, (const char*)out, t->encoded); 51e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 52d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 53d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 54e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return true; 55d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 56d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 57e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool TestDecode() { 58d9e397b599b13d642138480a28c14db7a136bf0Adam Langley uint8_t out[6]; 59e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley size_t len; 60d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 61e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley for (size_t i = 0; i < kNumTests; i++) { 62e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley // Test the normal API. 63e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley const TestVector *t = &kTestVectors[i]; 64d9e397b599b13d642138480a28c14db7a136bf0Adam Langley size_t expected_len = strlen(t->decoded); 65d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (!EVP_DecodeBase64(out, &len, sizeof(out), 66d9e397b599b13d642138480a28c14db7a136bf0Adam Langley (const uint8_t*)t->encoded, strlen(t->encoded))) { 67d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "decode(\"%s\") failed\n", t->encoded); 68e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 69d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 70d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (len != strlen(t->decoded) || 71d9e397b599b13d642138480a28c14db7a136bf0Adam Langley memcmp(out, t->decoded, len) != 0) { 72d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n", 73d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t->encoded, (int)len, (const char*)out, t->decoded); 74e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 75d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 76d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 77e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley // Test that the padding behavior of the deprecated API is preserved. 78e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley int ret = EVP_DecodeBlock(out, (const uint8_t*)t->encoded, 79e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley strlen(t->encoded)); 80d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (ret < 0) { 81d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "decode(\"%s\") failed\n", t->encoded); 82e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 83d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 84d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (ret % 3 != 0) { 85d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n"); 86e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 87d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 88d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (expected_len % 3 != 0) { 89d9e397b599b13d642138480a28c14db7a136bf0Adam Langley ret -= 3 - (expected_len % 3); 90d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 91e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley if (static_cast<size_t>(ret) != strlen(t->decoded) || 92d9e397b599b13d642138480a28c14db7a136bf0Adam Langley memcmp(out, t->decoded, ret) != 0) { 93d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n", 94d9e397b599b13d642138480a28c14db7a136bf0Adam Langley t->encoded, ret, (const char*)out, t->decoded); 95e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 96d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 97d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 98d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 99d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a!bc", 4)) { 100d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "Failed to reject invalid characters in the middle.\n"); 101e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 102d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 103d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 104d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a=bc", 4)) { 105d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "Failed to reject invalid characters in the middle.\n"); 106e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 107d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 108d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 109d9e397b599b13d642138480a28c14db7a136bf0Adam Langley if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"abc", 4)) { 110d9e397b599b13d642138480a28c14db7a136bf0Adam Langley fprintf(stderr, "Failed to reject invalid input length.\n"); 111e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return false; 112d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 113d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 114e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley return true; 115d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 116d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 117d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint main(void) { 118d9e397b599b13d642138480a28c14db7a136bf0Adam Langley CRYPTO_library_init(); 119d9e397b599b13d642138480a28c14db7a136bf0Adam Langley ERR_load_crypto_strings(); 120d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 121e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley if (!TestEncode() || 122e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley !TestDecode()) { 123d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 1; 124d9e397b599b13d642138480a28c14db7a136bf0Adam Langley } 125d9e397b599b13d642138480a28c14db7a136bf0Adam Langley 126d9e397b599b13d642138480a28c14db7a136bf0Adam Langley printf("PASS\n"); 127d9e397b599b13d642138480a28c14db7a136bf0Adam Langley return 0; 128d9e397b599b13d642138480a28c14db7a136bf0Adam Langley} 129