2014-07-17 06:51:59 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/base64.h>
|
2014-09-12 00:11:15 +01:00
|
|
|
#include <openssl/crypto.h>
|
2014-07-17 06:51:59 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2014-07-17 18:33:16 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
struct TestVector {
|
2014-07-17 06:51:59 +01:00
|
|
|
const char *decoded;
|
|
|
|
const char *encoded;
|
2015-03-26 05:19:49 +00:00
|
|
|
};
|
2014-07-17 06:51:59 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
// Test vectors from RFC 4648.
|
|
|
|
static const TestVector kTestVectors[] = {
|
2014-07-17 06:51:59 +01:00
|
|
|
{ "", "" },
|
|
|
|
{ "f" , "Zg==" },
|
|
|
|
{ "fo", "Zm8=" },
|
|
|
|
{ "foo", "Zm9v" },
|
|
|
|
{ "foob", "Zm9vYg==" },
|
|
|
|
{ "fooba", "Zm9vYmE=" },
|
|
|
|
{ "foobar", "Zm9vYmFy" },
|
|
|
|
};
|
2014-07-17 18:33:16 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
static const size_t kNumTests = sizeof(kTestVectors) / sizeof(kTestVectors[0]);
|
2014-07-17 06:51:59 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
static bool TestEncode() {
|
|
|
|
for (size_t i = 0; i < kNumTests; i++) {
|
|
|
|
const TestVector *t = &kTestVectors[i];
|
|
|
|
uint8_t out[9];
|
|
|
|
size_t len = EVP_EncodeBlock(out, (const uint8_t*)t->decoded,
|
|
|
|
strlen(t->decoded));
|
2014-07-17 06:51:59 +01:00
|
|
|
if (len != strlen(t->encoded) ||
|
|
|
|
memcmp(out, t->encoded, len) != 0) {
|
|
|
|
fprintf(stderr, "encode(\"%s\") = \"%.*s\", want \"%s\"\n",
|
|
|
|
t->decoded, (int)len, (const char*)out, t->encoded);
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 05:19:49 +00:00
|
|
|
return true;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
static bool TestDecode() {
|
2014-07-17 06:51:59 +01:00
|
|
|
uint8_t out[6];
|
2015-03-26 05:19:49 +00:00
|
|
|
size_t len;
|
2014-07-17 06:51:59 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
for (size_t i = 0; i < kNumTests; i++) {
|
|
|
|
// Test the normal API.
|
|
|
|
const TestVector *t = &kTestVectors[i];
|
2014-07-17 06:51:59 +01:00
|
|
|
size_t expected_len = strlen(t->decoded);
|
2014-08-24 05:49:21 +01:00
|
|
|
if (!EVP_DecodeBase64(out, &len, sizeof(out),
|
|
|
|
(const uint8_t*)t->encoded, strlen(t->encoded))) {
|
|
|
|
fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
if (len != strlen(t->decoded) ||
|
|
|
|
memcmp(out, t->decoded, len) != 0) {
|
|
|
|
fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
|
|
|
|
t->encoded, (int)len, (const char*)out, t->decoded);
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
2014-08-24 05:49:21 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
// Test that the padding behavior of the deprecated API is preserved.
|
|
|
|
int ret = EVP_DecodeBlock(out, (const uint8_t*)t->encoded,
|
|
|
|
strlen(t->encoded));
|
2014-08-24 05:49:21 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-08-24 05:49:21 +01:00
|
|
|
}
|
|
|
|
if (ret % 3 != 0) {
|
|
|
|
fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n");
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-08-24 05:49:21 +01:00
|
|
|
}
|
|
|
|
if (expected_len % 3 != 0) {
|
|
|
|
ret -= 3 - (expected_len % 3);
|
|
|
|
}
|
2015-03-26 05:19:49 +00:00
|
|
|
if (static_cast<size_t>(ret) != strlen(t->decoded) ||
|
2014-08-24 05:49:21 +01:00
|
|
|
memcmp(out, t->decoded, ret) != 0) {
|
|
|
|
fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
|
|
|
|
t->encoded, ret, (const char*)out, t->decoded);
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-08-24 05:49:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a!bc", 4)) {
|
|
|
|
fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
|
2014-08-24 05:49:21 +01:00
|
|
|
if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a=bc", 4)) {
|
2014-07-17 06:51:59 +01:00
|
|
|
fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
|
2014-08-24 05:49:21 +01:00
|
|
|
if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"abc", 4)) {
|
2014-07-17 06:51:59 +01:00
|
|
|
fprintf(stderr, "Failed to reject invalid input length.\n");
|
2015-03-26 05:19:49 +00:00
|
|
|
return false;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
return true;
|
2014-07-17 06:51:59 +01:00
|
|
|
}
|
|
|
|
|
2014-08-20 21:24:00 +01:00
|
|
|
int main(void) {
|
2014-09-12 00:11:15 +01:00
|
|
|
CRYPTO_library_init();
|
2014-07-17 06:51:59 +01:00
|
|
|
|
2015-03-26 05:19:49 +00:00
|
|
|
if (!TestEncode() ||
|
|
|
|
!TestDecode()) {
|
2014-07-17 06:51:59 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|