1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * CAST low level APIs are deprecated for public use, but still ok for
12*e0c4386eSCy Schubert * internal use.
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert #include "internal/deprecated.h"
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <string.h>
18*e0c4386eSCy Schubert #include <stdlib.h>
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */
21*e0c4386eSCy Schubert #include "internal/nelem.h"
22*e0c4386eSCy Schubert #include "testutil.h"
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CAST
25*e0c4386eSCy Schubert # include <openssl/cast.h>
26*e0c4386eSCy Schubert
27*e0c4386eSCy Schubert static unsigned char k[16] = {
28*e0c4386eSCy Schubert 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
29*e0c4386eSCy Schubert 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
30*e0c4386eSCy Schubert };
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert static unsigned char in[8] =
33*e0c4386eSCy Schubert { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert static int k_len[3] = { 16, 10, 5 };
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert static unsigned char c[3][8] = {
38*e0c4386eSCy Schubert {0x23, 0x8B, 0x4F, 0xE5, 0x84, 0x7E, 0x44, 0xB2},
39*e0c4386eSCy Schubert {0xEB, 0x6A, 0x71, 0x1A, 0x2C, 0x02, 0x27, 0x1B},
40*e0c4386eSCy Schubert {0x7A, 0xC8, 0x16, 0xD1, 0x6E, 0x9B, 0x30, 0x2E},
41*e0c4386eSCy Schubert };
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert static unsigned char in_a[16] = {
44*e0c4386eSCy Schubert 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
45*e0c4386eSCy Schubert 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
46*e0c4386eSCy Schubert };
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert static unsigned char in_b[16] = {
49*e0c4386eSCy Schubert 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
50*e0c4386eSCy Schubert 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
51*e0c4386eSCy Schubert };
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert static unsigned char c_a[16] = {
54*e0c4386eSCy Schubert 0xEE, 0xA9, 0xD0, 0xA2, 0x49, 0xFD, 0x3B, 0xA6,
55*e0c4386eSCy Schubert 0xB3, 0x43, 0x6F, 0xB8, 0x9D, 0x6D, 0xCA, 0x92
56*e0c4386eSCy Schubert };
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert static unsigned char c_b[16] = {
59*e0c4386eSCy Schubert 0xB2, 0xC9, 0x5E, 0xB0, 0x0C, 0x31, 0xAD, 0x71,
60*e0c4386eSCy Schubert 0x80, 0xAC, 0x05, 0xB8, 0xE8, 0x3D, 0x69, 0x6E
61*e0c4386eSCy Schubert };
62*e0c4386eSCy Schubert
cast_test_vector(int z)63*e0c4386eSCy Schubert static int cast_test_vector(int z)
64*e0c4386eSCy Schubert {
65*e0c4386eSCy Schubert int testresult = 1;
66*e0c4386eSCy Schubert CAST_KEY key;
67*e0c4386eSCy Schubert unsigned char out[80];
68*e0c4386eSCy Schubert
69*e0c4386eSCy Schubert CAST_set_key(&key, k_len[z], k);
70*e0c4386eSCy Schubert CAST_ecb_encrypt(in, out, &key, CAST_ENCRYPT);
71*e0c4386eSCy Schubert if (!TEST_mem_eq(out, sizeof(c[z]), c[z], sizeof(c[z]))) {
72*e0c4386eSCy Schubert TEST_info("CAST_ENCRYPT iteration %d failed (len=%d)", z, k_len[z]);
73*e0c4386eSCy Schubert testresult = 0;
74*e0c4386eSCy Schubert }
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert CAST_ecb_encrypt(out, out, &key, CAST_DECRYPT);
77*e0c4386eSCy Schubert if (!TEST_mem_eq(out, sizeof(in), in, sizeof(in))) {
78*e0c4386eSCy Schubert TEST_info("CAST_DECRYPT iteration %d failed (len=%d)", z, k_len[z]);
79*e0c4386eSCy Schubert testresult = 0;
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert return testresult;
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert
cast_test_iterations(void)84*e0c4386eSCy Schubert static int cast_test_iterations(void)
85*e0c4386eSCy Schubert {
86*e0c4386eSCy Schubert long l;
87*e0c4386eSCy Schubert int testresult = 1;
88*e0c4386eSCy Schubert CAST_KEY key, key_b;
89*e0c4386eSCy Schubert unsigned char out_a[16], out_b[16];
90*e0c4386eSCy Schubert
91*e0c4386eSCy Schubert memcpy(out_a, in_a, sizeof(in_a));
92*e0c4386eSCy Schubert memcpy(out_b, in_b, sizeof(in_b));
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert for (l = 0; l < 1000000L; l++) {
95*e0c4386eSCy Schubert CAST_set_key(&key_b, 16, out_b);
96*e0c4386eSCy Schubert CAST_ecb_encrypt(&(out_a[0]), &(out_a[0]), &key_b, CAST_ENCRYPT);
97*e0c4386eSCy Schubert CAST_ecb_encrypt(&(out_a[8]), &(out_a[8]), &key_b, CAST_ENCRYPT);
98*e0c4386eSCy Schubert CAST_set_key(&key, 16, out_a);
99*e0c4386eSCy Schubert CAST_ecb_encrypt(&(out_b[0]), &(out_b[0]), &key, CAST_ENCRYPT);
100*e0c4386eSCy Schubert CAST_ecb_encrypt(&(out_b[8]), &(out_b[8]), &key, CAST_ENCRYPT);
101*e0c4386eSCy Schubert }
102*e0c4386eSCy Schubert
103*e0c4386eSCy Schubert if (!TEST_mem_eq(out_a, sizeof(c_a), c_a, sizeof(c_a))
104*e0c4386eSCy Schubert || !TEST_mem_eq(out_b, sizeof(c_b), c_b, sizeof(c_b)))
105*e0c4386eSCy Schubert testresult = 0;
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert return testresult;
108*e0c4386eSCy Schubert }
109*e0c4386eSCy Schubert #endif
110*e0c4386eSCy Schubert
setup_tests(void)111*e0c4386eSCy Schubert int setup_tests(void)
112*e0c4386eSCy Schubert {
113*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CAST
114*e0c4386eSCy Schubert ADD_ALL_TESTS(cast_test_vector, OSSL_NELEM(k_len));
115*e0c4386eSCy Schubert ADD_TEST(cast_test_iterations);
116*e0c4386eSCy Schubert #endif
117*e0c4386eSCy Schubert return 1;
118*e0c4386eSCy Schubert }
119