| 
									
										
										
										
											2017-06-19 15:49:18 +02:00
										 |  |  | /* LibTomCrypt, modular cryptographic library -- Tom St Denis
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * LibTomCrypt is a library that provides various cryptographic | 
					
						
							|  |  |  |  * algorithms in a highly modular and flexible manner. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The library is free for all purposes without any express | 
					
						
							|  |  |  |  * guarantee it works. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2005-04-17 11:37:13 +00:00
										 |  |  | #include  <tomcrypt_test.h>
 | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | #if defined(LTC_BASE64) || defined(LTC_BASE64_URL)
 | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  | int base64_test(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |    unsigned char in[64], out[256], tmp[64]; | 
					
						
							| 
									
										
										
										
											2014-03-04 18:04:29 +01:00
										 |  |  |    unsigned long x, l1, l2, slen1; | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-09 18:31:14 +02:00
										 |  |  |    const unsigned char special_case[] = { | 
					
						
							| 
									
										
										
										
											2016-09-24 16:45:48 +02:00
										 |  |  |          0xbe, 0xe8, 0x92, 0x3c, 0xa2, 0x25, 0xf0, 0xf8, | 
					
						
							|  |  |  |          0x91, 0xe4, 0xef, 0xab, 0x0b, 0x8c, 0xfd, 0xff, | 
					
						
							|  |  |  |          0x14, 0xd0, 0x29, 0x9d, 0x00 }; | 
					
						
							| 
									
										
										
										
											2014-03-04 18:04:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-11 17:42:33 +02:00
										 |  |  | #if defined(LTC_BASE64)
 | 
					
						
							| 
									
										
										
										
											2014-03-04 18:04:29 +01:00
										 |  |  |    /*
 | 
					
						
							|  |  |  |     TEST CASES SOURCE: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Network Working Group                                       S. Josefsson | 
					
						
							|  |  |  |     Request for Comments: 4648                                           SJD | 
					
						
							|  |  |  |     Obsoletes: 3548                                             October 2006 | 
					
						
							|  |  |  |     Category: Standards Track | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |    const struct { | 
					
						
							|  |  |  |      const char* s; | 
					
						
							|  |  |  |      const char* b64; | 
					
						
							|  |  |  |    } cases[] = { | 
					
						
							|  |  |  |        {"", ""              }, | 
					
						
							|  |  |  |        {"f", "Zg=="         }, | 
					
						
							|  |  |  |        {"fo", "Zm8="        }, | 
					
						
							|  |  |  |        {"foo", "Zm9v"       }, | 
					
						
							|  |  |  |        {"foob", "Zm9vYg=="  }, | 
					
						
							|  |  |  |        {"fooba", "Zm9vYmE=" }, | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |        {"foobar", "Zm9vYmFy"}, | 
					
						
							| 
									
										
										
										
											2017-05-09 18:31:14 +02:00
										 |  |  |        {(char*)special_case,"vuiSPKIl8PiR5O+rC4z9/xTQKZ0="} | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |    }; | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | #ifdef LTC_BASE64_URL
 | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |    const struct { | 
					
						
							|  |  |  |       const char* s; | 
					
						
							| 
									
										
										
										
											2017-02-17 11:18:58 +01:00
										 |  |  |       int is_strict; | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |    } url_cases[] = { | 
					
						
							|  |  |  |          {"vuiSPKIl8PiR5O-rC4z9_xTQKZ0", 0}, | 
					
						
							|  |  |  |          {"vuiSPKIl8PiR5O-rC4z9_xTQKZ0=", 1}, | 
					
						
							|  |  |  |          {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0", 0}, | 
					
						
							|  |  |  |          {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0}, | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |          {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0==", 0}, | 
					
						
							|  |  |  |          {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0===", 0}, | 
					
						
							|  |  |  |          {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0====", 0}, | 
					
						
							|  |  |  |          {"vuiS*=PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0}, | 
					
						
							|  |  |  |          {"vuiS*==PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0}, | 
					
						
							|  |  |  |          {"vuiS*===PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0}, | 
					
						
							| 
									
										
										
										
											2014-03-04 18:04:29 +01:00
										 |  |  |    }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |    for (x = 0; x < sizeof(url_cases)/sizeof(url_cases[0]); ++x) { | 
					
						
							|  |  |  |        slen1 = strlen(url_cases[x].s); | 
					
						
							|  |  |  |        l1 = sizeof(out); | 
					
						
							| 
									
										
										
										
											2017-02-17 11:18:58 +01:00
										 |  |  |        if(url_cases[x].is_strict) | 
					
						
							|  |  |  |           DO(base64url_strict_decode((unsigned char*)url_cases[x].s, slen1, out, &l1)); | 
					
						
							|  |  |  |        else | 
					
						
							|  |  |  |           DO(base64url_decode((unsigned char*)url_cases[x].s, slen1, out, &l1)); | 
					
						
							| 
									
										
										
										
											2017-05-09 18:31:14 +02:00
										 |  |  |        if (compare_testvector(out, l1, special_case, sizeof(special_case) - 1, "base64url decode", x)) { | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |            return 1; | 
					
						
							|  |  |  |        } | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |        if(x < 2) { | 
					
						
							|  |  |  |           l2 = sizeof(tmp); | 
					
						
							|  |  |  |           if(x == 0) | 
					
						
							|  |  |  |              DO(base64url_encode(out, l1, tmp, &l2)); | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |              DO(base64url_strict_encode(out, l1, tmp, &l2)); | 
					
						
							|  |  |  |           if (compare_testvector(tmp, l2, url_cases[x].s, strlen(url_cases[x].s), "base64url encode", x)) { | 
					
						
							|  |  |  |               return 1; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |        } | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  |    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |    DO(base64url_strict_decode((unsigned char*)url_cases[4].s, slen1, out, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_INVALID_PACKET); | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(LTC_BASE64)
 | 
					
						
							|  |  |  |    for (x = 0; x < sizeof(cases)/sizeof(cases[0]); ++x) { | 
					
						
							|  |  |  |        memset(out, 0, sizeof(out)); | 
					
						
							|  |  |  |        memset(tmp, 0, sizeof(tmp)); | 
					
						
							|  |  |  |        slen1 = strlen(cases[x].s); | 
					
						
							|  |  |  |        l1 = sizeof(out); | 
					
						
							|  |  |  |        DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1)); | 
					
						
							|  |  |  |        l2 = sizeof(tmp); | 
					
						
							|  |  |  |        DO(base64_strict_decode(out, l1, tmp, &l2)); | 
					
						
							|  |  |  |        if (compare_testvector(out, l1, cases[x].b64, strlen(cases[x].b64), "base64 encode", x) || | 
					
						
							|  |  |  |              compare_testvector(tmp, l2, cases[x].s, slen1, "base64 decode", x)) { | 
					
						
							|  |  |  |            return 1; | 
					
						
							|  |  |  |        } | 
					
						
							|  |  |  |    } | 
					
						
							| 
									
										
										
										
											2016-04-03 14:11:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  |    for  (x = 0; x < 64; x++) { | 
					
						
							| 
									
										
										
										
											2005-04-17 11:37:13 +00:00
										 |  |  |        yarrow_read(in, x, &yarrow_prng); | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  |        l1 = sizeof(out); | 
					
						
							|  |  |  |        DO(base64_encode(in, x, out, &l1)); | 
					
						
							|  |  |  |        l2 = sizeof(tmp); | 
					
						
							|  |  |  |        DO(base64_decode(out, l1, tmp, &l2)); | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |        if (compare_testvector(tmp, x, in, x, "random base64", x)) { | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  |            return 1; | 
					
						
							|  |  |  |        } | 
					
						
							|  |  |  |    } | 
					
						
							| 
									
										
										
										
											2016-01-23 13:14:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    x--; | 
					
						
							|  |  |  |    memmove(&out[11], &out[10], l1 - 10); | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |    out[10] = '='; | 
					
						
							| 
									
										
										
										
											2016-01-23 13:14:50 +01:00
										 |  |  |    l1++; | 
					
						
							|  |  |  |    l2 = sizeof(tmp); | 
					
						
							| 
									
										
										
										
											2017-02-17 11:18:58 +01:00
										 |  |  |    DO(base64_decode(out, l1, tmp, &l2)); | 
					
						
							| 
									
										
										
										
											2017-02-21 00:39:10 +01:00
										 |  |  |    if (compare_testvector(tmp, l2, in, l2, "relaxed base64 decoding", -1)) { | 
					
						
							| 
									
										
										
										
											2016-01-23 13:14:50 +01:00
										 |  |  |        print_hex("input ", out, l1); | 
					
						
							|  |  |  |        return 1; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |    l2 = sizeof(tmp); | 
					
						
							| 
									
										
										
										
											2017-02-17 11:18:58 +01:00
										 |  |  |    DO(base64_strict_decode(out, l1, tmp, &l2) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_INVALID_PACKET); | 
					
						
							| 
									
										
										
										
											2017-02-28 21:23:16 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 02:36:47 +00:00
										 |  |  |    return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-01 10:09:41 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-06-09 00:08:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 13:43:49 +02:00
										 |  |  | /* ref:         $Format:%D$ */ | 
					
						
							|  |  |  | /* git commit:  $Format:%H$ */ | 
					
						
							|  |  |  | /* commit time: $Format:%ai$ */ |