comparison libtomcrypt/tests/base64_test.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include <tomcrypt_test.h>
10
11 #if defined(LTC_BASE64) || defined(LTC_BASE64_URL)
12 int base64_test(void)
13 {
14 unsigned char in[64], out[256], tmp[64];
15 unsigned long x, l1, l2, slen1;
16
17 const unsigned char special_case[] = {
18 0xbe, 0xe8, 0x92, 0x3c, 0xa2, 0x25, 0xf0, 0xf8,
19 0x91, 0xe4, 0xef, 0xab, 0x0b, 0x8c, 0xfd, 0xff,
20 0x14, 0xd0, 0x29, 0x9d, 0x00 };
21
22 #if defined(LTC_BASE64)
23 /*
24 TEST CASES SOURCE:
25
26 Network Working Group S. Josefsson
27 Request for Comments: 4648 SJD
28 Obsoletes: 3548 October 2006
29 Category: Standards Track
30 */
31 const struct {
32 const char* s;
33 const char* b64;
34 } cases[] = {
35 {"", "" },
36 {"f", "Zg==" },
37 {"fo", "Zm8=" },
38 {"foo", "Zm9v" },
39 {"foob", "Zm9vYg==" },
40 {"fooba", "Zm9vYmE=" },
41 {"foobar", "Zm9vYmFy"},
42 {(char*)special_case,"vuiSPKIl8PiR5O+rC4z9/xTQKZ0="}
43 };
44 #endif
45
46 #ifdef LTC_BASE64_URL
47 const struct {
48 const char* s;
49 int is_strict;
50 } url_cases[] = {
51 {"vuiSPKIl8PiR5O-rC4z9_xTQKZ0", 0},
52 {"vuiSPKIl8PiR5O-rC4z9_xTQKZ0=", 1},
53 {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0", 0},
54 {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
55 {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0==", 0},
56 {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0===", 0},
57 {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0====", 0},
58 {"vuiS*=PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
59 {"vuiS*==PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
60 {"vuiS*===PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
61 };
62
63 for (x = 0; x < sizeof(url_cases)/sizeof(url_cases[0]); ++x) {
64 slen1 = strlen(url_cases[x].s);
65 l1 = sizeof(out);
66 if(url_cases[x].is_strict)
67 DO(base64url_strict_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
68 else
69 DO(base64url_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
70 if (compare_testvector(out, l1, special_case, sizeof(special_case) - 1, "base64url decode", x)) {
71 return 1;
72 }
73 if(x < 2) {
74 l2 = sizeof(tmp);
75 if(x == 0)
76 DO(base64url_encode(out, l1, tmp, &l2));
77 else
78 DO(base64url_strict_encode(out, l1, tmp, &l2));
79 if (compare_testvector(tmp, l2, url_cases[x].s, strlen(url_cases[x].s), "base64url encode", x)) {
80 return 1;
81 }
82 }
83 }
84
85 DO(base64url_strict_decode((unsigned char*)url_cases[4].s, slen1, out, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_INVALID_PACKET);
86 #endif
87
88 #if defined(LTC_BASE64)
89 for (x = 0; x < sizeof(cases)/sizeof(cases[0]); ++x) {
90 memset(out, 0, sizeof(out));
91 memset(tmp, 0, sizeof(tmp));
92 slen1 = strlen(cases[x].s);
93 l1 = sizeof(out);
94 DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1));
95 l2 = sizeof(tmp);
96 DO(base64_strict_decode(out, l1, tmp, &l2));
97 if (compare_testvector(out, l1, cases[x].b64, strlen(cases[x].b64), "base64 encode", x) ||
98 compare_testvector(tmp, l2, cases[x].s, slen1, "base64 decode", x)) {
99 return 1;
100 }
101 }
102
103 for (x = 0; x < 64; x++) {
104 yarrow_read(in, x, &yarrow_prng);
105 l1 = sizeof(out);
106 DO(base64_encode(in, x, out, &l1));
107 l2 = sizeof(tmp);
108 DO(base64_decode(out, l1, tmp, &l2));
109 if (compare_testvector(tmp, x, in, x, "random base64", x)) {
110 return 1;
111 }
112 }
113
114 x--;
115 memmove(&out[11], &out[10], l1 - 10);
116 out[10] = '=';
117 l1++;
118 l2 = sizeof(tmp);
119 DO(base64_decode(out, l1, tmp, &l2));
120 if (compare_testvector(tmp, l2, in, l2, "relaxed base64 decoding", -1)) {
121 print_hex("input ", out, l1);
122 return 1;
123 }
124 l2 = sizeof(tmp);
125 DO(base64_strict_decode(out, l1, tmp, &l2) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_INVALID_PACKET);
126 #endif
127
128 return 0;
129 }
130 #endif
131
132 /* ref: $Format:%D$ */
133 /* git commit: $Format:%H$ */
134 /* commit time: $Format:%ai$ */