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