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 *  CCGCMTest.c
26 *  CommonCrypto
27 */
28
29#include "capabilities.h"
30#include <stdio.h>
31#include <CommonCrypto/CommonCryptor.h>
32#include <CommonCrypto/CommonCryptorSPI.h>
33#include "CCCryptorTestFuncs.h"
34#include "testbyteBuffer.h"
35#include "testmore.h"
36
37#if (CCSYMGCM == 0)
38entryPoint(CommonCryptoSymGCM,"CommonCrypto Symmetric GCM Testing")
39#else
40
41
42
43
44static int kTestTestCount = 16;
45
46int CommonCryptoSymGCM(int argc, char *const *argv) {
47	char *keyStr;
48	char *iv;
49	char *plainText;
50	char *cipherText;
51    char *adata;
52    char *tag;
53    CCAlgorithm alg;
54	int retval, accum = 0;
55
56    alg		   = kCCAlgorithmAES128;
57
58	plan_tests(kTestTestCount);
59
60    /* testcase #1 */
61
62    keyStr =     "00000000000000000000000000000000";
63    adata =      "";
64    iv =         "000000000000000000000000";
65    plainText =  "";
66    cipherText = "";
67    tag =        "58e2fccefa7e3061367f1d57a4e7455a";
68
69    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
70
71    ok(retval == 0, "AES-GCM Testcase 1.1");
72    accum += retval;
73
74    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
75
76    ok(retval == 0, "AES-GCM Testcase 1.2");
77    accum += retval;
78
79    /* testcase #2 */
80
81    keyStr =     "00000000000000000000000000000000";
82    adata =      "";
83    iv =         "000000000000000000000000";
84    plainText =  "00000000000000000000000000000000";
85    cipherText = "0388dace60b6a392f328c2b971b2fe78";
86    tag =        "ab6e47d42cec13bdf53a67b21257bddf";
87
88    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
89    ok(retval == 0, "AES-GCM Testcase 2.1");
90    accum += retval;
91    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
92    ok(retval == 0, "AES-GCM Testcase 2.2");
93    accum += retval;
94
95    /* testcase #3 */
96
97    keyStr =     "feffe9928665731c6d6a8f9467308308";
98    adata =      "";
99    iv =         "cafebabefacedbaddecaf888";
100    plainText =  "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255";
101    cipherText = "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985";
102    tag =        "4d5c2af327cd64a62cf35abd2ba6fab4";
103
104    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
105    ok(retval == 0, "AES-GCM Testcase 3.1");
106    accum += retval;
107    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
108    ok(retval == 0, "AES-GCM Testcase 3.2");
109    accum += retval;
110
111    /* testcase #4 */
112
113    keyStr =     "feffe9928665731c6d6a8f9467308308";
114    adata =      "feedfacedeadbeeffeedfacedeadbeefabaddad2";
115    iv =         "cafebabefacedbaddecaf888";
116    plainText =  "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39";
117    cipherText = "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091";
118    tag =        "5bc94fbc3221a5db94fae95ae7121a47";
119
120    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
121    ok(retval == 0, "AES-GCM Testcase 4.1");
122    accum += retval;
123    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
124    ok(retval == 0, "AES-GCM Testcase 4.2");
125    accum += retval;
126
127    /* testcase #5 */
128
129    keyStr =     "feffe9928665731c6d6a8f9467308308";
130    adata =      "feedfacedeadbeeffeedfacedeadbeefabaddad2";
131    iv =         "cafebabefacedbad";
132    plainText =  "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39";
133    cipherText = "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598";
134    tag =        "3612d2e79e3b0785561be14aaca2fccb";
135
136    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
137    ok(retval == 0, "AES-GCM Testcase 5.1");
138    accum += retval;
139    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
140    ok(retval == 0, "AES-GCM Testcase 5.2");
141    accum += retval;
142
143    /* testcase #6 */
144
145    keyStr =     "feffe9928665731c6d6a8f9467308308";
146    adata = "feedfacedeadbeeffeedfacedeadbeefabaddad2";
147    iv = "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b";
148    plainText = "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39";
149    cipherText = "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5";
150    tag = "619cc5aefffe0bfa462af43c1699d050";
151
152    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
153    ok(retval == 0, "AES-GCM Testcase 6.1");
154    accum += retval;
155    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
156    ok(retval == 0, "AES-GCM Testcase 6.2");
157    accum += retval;
158
159    /* testcase #46 from BG (catchestheLTCbugofv1.15) */
160    keyStr = "00000000000000000000000000000000";
161    adata =  "688e1aa984de926dc7b4c47f44";
162    iv =     "b72138b5a05ff5070e8cd94183f761d8";
163    plainText =  "a2aab3ad8b17acdda288426cd7c429b7ca86b7aca05809c70ce82db25711cb5302eb2743b036f3d750d6cf0dc0acb92950d546db308f93b4ff244afa9dc72bcd758d2c";
164    cipherText = "cbc8d2f15481a4cc7dd1e19aaa83de5678483ec359ae7dec2ab8d534e0906f4b4663faff58a8b2d733b845eef7c9b331e9e10eb2612c995feb1ac15a6286cce8b297a8";
165    tag =    "8d2d2a9372626f6bee8580276a6366bf";
166
167    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
168    ok(retval == 0, "AES-GCM Testcase 7.1");
169    accum += retval;
170    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
171    ok(retval == 0, "AES-GCM Testcase 7.2");
172    accum += retval;
173
174    /* testcase #8 - #1 with NULL IV and AAD */
175
176    keyStr =     "00000000000000000000000000000000";
177    adata =      "";
178    iv =         "";
179    plainText =  "";
180    cipherText = "";
181    tag =        "66e94bd4ef8a2c3b884cfa59ca342b2e";
182
183    retval = CCCryptorGCMTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
184    ok(retval == 0, "AES-GCM Testcase 8.1");
185    accum += retval;
186    retval = CCCryptorGCMDiscreetTestCase(keyStr, iv, adata, tag, alg, cipherText, plainText);
187    ok(retval == 0, "AES-GCM Testcase 8.2");
188    accum += retval;
189
190
191    return accum != 0;
192}
193#endif
194
195