1/*
2 * Copyright (c) 2010 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 *  CBCTest.c
26 *  CommonCrypto
27 */
28
29#include <stdio.h>
30#include "CCCryptorTestFuncs.h"
31#include "testbyteBuffer.h"
32#include "testmore.h"
33#include "capabilities.h"
34
35#if (CCSYMCBC == 0)
36entryPoint(CommonCryptoSymCBC,"CommonCrypto Symmetric CBC Testing")
37#else
38
39
40static int kTestTestCount = 30;
41
42int CommonCryptoSymCBC(int argc, char *const *argv) {
43	char *keyStr;
44	char *iv;
45	char *plainText;
46	char *cipherText;
47    CCAlgorithm alg;
48    CCOptions options;
49	int retval;
50    int accum = 0;
51
52	keyStr 	   = "000102030405060708090a0b0c0d0e0f";
53	iv         = "0f0e0d0c0b0a09080706050403020100";
54    alg		   = kCCAlgorithmAES128;
55    options    = kCCOptionPKCS7Padding;
56
57	plan_tests(kTestTestCount);
58
59    accum = (int) genRandomSize(1,10);
60	// 1
61	plainText  = "0a";
62	cipherText = "a385b047a4108a8748bf96b435738213";
63    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
64    ok(retval == 0, "CBC with Padding 1 byte CCCrypt");
65    accum |= retval;
66    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
67    ok(retval == 0, "CBC with Padding 1 byte Multiple Updates");
68    accum |= retval;
69
70	// 15
71	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
72	cipherText = "324a44cf3395b14214861084019f9257";
73    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
74    ok(retval == 0, "CBC with Padding 15 byte CCCrypt");
75    accum |= retval;
76    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
77    ok(retval == 0, "CBC with Padding 15 byte Multiple Updates");
78    accum |= retval;
79
80	// 16
81    plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
82	cipherText = "16d67a52c1e8384f7ed887c2011605346544febcf84574c334f1145d17567047";
83    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
84    ok(retval == 0, "CBC with Padding 16 byte CCCrypt");
85    accum |= retval;
86    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
87    ok(retval == 0, "CBC with Padding 16 byte Multiple Updates");
88    accum |= retval;
89
90	// 17
91	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
92	cipherText = "16d67a52c1e8384f7ed887c2011605348b72cecb00bbc00f328af6bb69085b02";
93    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
94    ok(retval == 0, "CBC with Padding 17 byte CCCrypt");
95    accum |= retval;
96    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
97    ok(retval == 0, "CBC with Padding 17 byte Multiple Updates");
98    accum |= retval;
99
100	// 31
101	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
102	cipherText = "16d67a52c1e8384f7ed887c2011605347175cf878a75bc1947ae79c6c6835030";
103    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
104    ok(retval == 0, "CBC with Padding 31 byte CCCrypt");
105    accum |= retval;
106    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
107    ok(retval == 0, "CBC with Padding 31 byte Multiple Updates");
108    accum |= retval;
109
110	// 32
111	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
112	cipherText = "16d67a52c1e8384f7ed887c20116053486869f3b83f3b3a83531e4169e97b7244a49199daa033fa88f07dd4be52ae78e";
113    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
114    ok(retval == 0, "CBC with Padding 32 byte CCCrypt");
115    accum |= retval;
116    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
117    ok(retval == 0, "CBC with Padding 32 byte Multiple Updates");
118    accum |= retval;
119
120	// 33
121	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
122	cipherText = "16d67a52c1e8384f7ed887c20116053486869f3b83f3b3a83531e4169e97b724d0080fb874dd556fa86b314acc4f597b";
123    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
124    ok(retval == 0, "CBC with Padding 33 byte CCCrypt");
125    accum |= retval;
126    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
127    ok(retval == 0, "CBC with Padding 33 byte Multiple Updates");
128    accum |= retval;
129
130    iv = NULL;
131	// 1
132	plainText  = "0a";
133	cipherText = "27cae51ac763b250945fd805c937119b";
134    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
135    ok(retval == 0, "CBC with Padding 1 byte CCCrypt NULL IV");
136    accum |= retval;
137    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
138    ok(retval == 0, "CBC with Padding 1 byte Multiple Updates NULL IV");
139    accum |= retval;
140
141	// 15
142	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
143	cipherText = "feb9c3a005dcbd1e2630af742e988e81";
144    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
145    ok(retval == 0, "CBC with Padding 15 byte CCCrypt NULL IV");
146    accum |= retval;
147    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
148    ok(retval == 0, "CBC with Padding 15 byte Multiple Updates NULL IV");
149    accum |= retval;
150
151	// 16
152    plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
153	cipherText = "d307b25d3abaf87c0053e8188152992a8b002a94911ee1e157d815a026cfadeb";
154    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
155    ok(retval == 0, "CBC with Padding 16 byte CCCrypt NULL IV");
156    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
157    ok(retval == 0, "CBC with Padding 16 byte Multiple Updates NULL IV");
158    accum |= retval;
159
160	// 17
161	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
162	cipherText = "d307b25d3abaf87c0053e8188152992ab8fe4130b613e93617b2eda2e0c5c678";
163    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
164    ok(retval == 0, "CBC with Padding 17 byte CCCrypt NULL IV");
165    accum |= retval;
166    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
167    ok(retval == 0, "CBC with Padding 17 byte Multiple Updates NULL IV");
168    accum |= retval;
169
170	// 31
171	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
172	cipherText = "d307b25d3abaf87c0053e8188152992a4157ad665141a79481f463357707f759";
173    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
174    ok(retval == 0, "CBC with Padding 31 byte CCCrypt NULL IV");
175    accum |= retval;
176    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
177    ok(retval == 0, "CBC with Padding 31 byte Multiple Updates NULL IV");
178    accum |= retval;
179
180	// 32
181	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
182	cipherText = "d307b25d3abaf87c0053e8188152992a923832530aa268661a6c1fa3c69d6a23dc6d5c0d7fa8127cfd601cae71b4c14f";
183    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
184    ok(retval == 0, "CBC with Padding 32 byte CCCrypt NULL IV");
185    accum |= retval;
186    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
187    ok(retval == 0, "CBC with Padding 32 byte Multiple Updates NULL IV");
188    accum |= retval;
189
190	// 33
191	plainText  = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a";
192	cipherText = "d307b25d3abaf87c0053e8188152992a923832530aa268661a6c1fa3c69d6a2382178b537aa2946f7a4124ee33744edd";
193    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
194    ok(retval == 0, "CBC with Padding 33 byte CCCrypt NULL IV");
195    accum |= retval;
196    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
197    ok(retval == 0, "CBC with Padding 33 byte Multiple Updates NULL IV");
198    accum |= retval;
199
200    // Blowfish vector that was failing for Jim
201
202    alg = kCCAlgorithmBlowfish;
203    options = 0;
204    keyStr = "0123456789ABCDEFF0E1D2C3B4A59687";
205    iv = "FEDCBA9876543210";
206    plainText =  "37363534333231204E6F77206973207468652074696D6520666F722000000000";
207    cipherText = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC";
208    retval = CCCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
209    ok(retval == 0, "CBC-blowfish vector 1");
210    accum |= retval;
211    retval = CCMultiCryptTestCase(keyStr, iv, alg, options, cipherText, plainText);
212    ok(retval == 0, "CBC-blowfish vector 1");
213    accum |= retval;
214
215
216    return accum != 0;
217}
218#endif
219
220