xref: /aoo41x/main/sal/workben/t_cipher.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include <sal/types.h>
28*cdf0e10cSrcweir #include <osl/diagnose.h>
29*cdf0e10cSrcweir #include <rtl/cipher.h>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #define NUM_VARIABLE_KEY_TESTS	34
35*cdf0e10cSrcweir #define NUM_SET_KEY_TESTS		24
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /* plaintext bytes -- left halves */
38*cdf0e10cSrcweir unsigned long plaintext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
39*cdf0e10cSrcweir    0x00000000l, 0xFFFFFFFFl, 0x10000000l, 0x11111111l, 0x11111111l,
40*cdf0e10cSrcweir    0x01234567l, 0x00000000l, 0x01234567l, 0x01A1D6D0l, 0x5CD54CA8l,
41*cdf0e10cSrcweir    0x0248D438l, 0x51454B58l, 0x42FD4430l, 0x059B5E08l, 0x0756D8E0l,
42*cdf0e10cSrcweir    0x762514B8l, 0x3BDD1190l, 0x26955F68l, 0x164D5E40l, 0x6B056E18l,
43*cdf0e10cSrcweir    0x004BD6EFl, 0x480D3900l, 0x437540C8l, 0x072D43A0l, 0x02FE5577l,
44*cdf0e10cSrcweir    0x1D9D5C50l, 0x30553228l, 0x01234567l, 0x01234567l, 0x01234567l,
45*cdf0e10cSrcweir    0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0xFEDCBA98l,
46*cdf0e10cSrcweir    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
47*cdf0e10cSrcweir    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
48*cdf0e10cSrcweir    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
49*cdf0e10cSrcweir    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
50*cdf0e10cSrcweir    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l };
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir /* plaintext bytes -- right halves */
53*cdf0e10cSrcweir unsigned long plaintext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
54*cdf0e10cSrcweir    0x00000000l, 0xFFFFFFFFl, 0x00000001l, 0x11111111l, 0x11111111l,
55*cdf0e10cSrcweir    0x89ABCDEFl, 0x00000000l, 0x89ABCDEFl, 0x39776742l, 0x3DEF57DAl,
56*cdf0e10cSrcweir    0x06F67172l, 0x2DDF440Al, 0x59577FA2l, 0x51CF143Al, 0x774761D2l,
57*cdf0e10cSrcweir    0x29BF486Al, 0x49372802l, 0x35AF609Al, 0x4F275232l, 0x759F5CCAl,
58*cdf0e10cSrcweir    0x09176062l, 0x6EE762F2l, 0x698F3CFAl, 0x77075292l, 0x8117F12Al,
59*cdf0e10cSrcweir    0x18F728C2l, 0x6D6F295Al, 0x89ABCDEFl, 0x89ABCDEFl, 0x89ABCDEFl,
60*cdf0e10cSrcweir    0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0x76543210l,
61*cdf0e10cSrcweir    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
62*cdf0e10cSrcweir    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
63*cdf0e10cSrcweir    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
64*cdf0e10cSrcweir    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
65*cdf0e10cSrcweir    0x76543210l, 0x76543210l, 0x76543210l };
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir /* key bytes for variable key tests */
68*cdf0e10cSrcweir unsigned char variable_key[NUM_VARIABLE_KEY_TESTS][8] = {
69*cdf0e10cSrcweir    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
70*cdf0e10cSrcweir    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
71*cdf0e10cSrcweir    { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
72*cdf0e10cSrcweir    { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
73*cdf0e10cSrcweir    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
74*cdf0e10cSrcweir    { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
75*cdf0e10cSrcweir    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
76*cdf0e10cSrcweir    { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 },
77*cdf0e10cSrcweir    { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 },
78*cdf0e10cSrcweir    { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E },
79*cdf0e10cSrcweir    { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 },
80*cdf0e10cSrcweir    { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E },
81*cdf0e10cSrcweir    { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 },
82*cdf0e10cSrcweir    { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE },
83*cdf0e10cSrcweir    { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 },
84*cdf0e10cSrcweir    { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE },
85*cdf0e10cSrcweir    { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 },
86*cdf0e10cSrcweir    { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F },
87*cdf0e10cSrcweir    { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 },
88*cdf0e10cSrcweir    { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E },
89*cdf0e10cSrcweir    { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
90*cdf0e10cSrcweir    { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 },
91*cdf0e10cSrcweir    { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F },
92*cdf0e10cSrcweir    { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 },
93*cdf0e10cSrcweir    { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF },
94*cdf0e10cSrcweir    { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 },
95*cdf0e10cSrcweir    { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF },
96*cdf0e10cSrcweir    { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
97*cdf0e10cSrcweir    { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
98*cdf0e10cSrcweir    { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
99*cdf0e10cSrcweir    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
100*cdf0e10cSrcweir    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
101*cdf0e10cSrcweir    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
102*cdf0e10cSrcweir    { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }};
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir /* key bytes for set key tests */
105*cdf0e10cSrcweir unsigned char set_key[24] = {
106*cdf0e10cSrcweir    0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
107*cdf0e10cSrcweir    0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F,
108*cdf0e10cSrcweir    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir /* ciphertext bytes -- left halves */
111*cdf0e10cSrcweir unsigned long ciphertext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
112*cdf0e10cSrcweir    0x4EF99745l, 0x51866FD5l, 0x7D856F9Al, 0x2466DD87l, 0x61F9C380l,
113*cdf0e10cSrcweir    0x7D0CC630l, 0x4EF99745l, 0x0ACEAB0Fl, 0x59C68245l, 0xB1B8CC0Bl,
114*cdf0e10cSrcweir    0x1730E577l, 0xA25E7856l, 0x353882B1l, 0x48F4D088l, 0x432193B7l,
115*cdf0e10cSrcweir    0x13F04154l, 0x2EEDDA93l, 0xD887E039l, 0x5F99D04Fl, 0x4A057A3Bl,
116*cdf0e10cSrcweir    0x452031C1l, 0x7555AE39l, 0x53C55F9Cl, 0x7A8E7BFAl, 0xCF9C5D7Al,
117*cdf0e10cSrcweir    0xD1ABB290l, 0x55CB3774l, 0xFA34EC48l, 0xA7907951l, 0xC39E072Dl,
118*cdf0e10cSrcweir    0x014933E0l, 0xF21E9A77l, 0x24594688l, 0x6B5C5A9Cl, 0xF9AD597Cl,
119*cdf0e10cSrcweir    0xE91D21C1l, 0xE9C2B70Al, 0xBE1E6394l, 0xB39E4448l, 0x9457AA83l,
120*cdf0e10cSrcweir    0x8BB77032l, 0xE87A244El, 0x15750E7Al, 0x122BA70Bl, 0x3A833C9Al,
121*cdf0e10cSrcweir    0x9409DA87l, 0x884F8062l, 0x1F85031Cl, 0x79D9373Al, 0x93142887l,
122*cdf0e10cSrcweir    0x03429E83l, 0xA4299E27l, 0xAFD5AED1l, 0x10851C0El, 0xE6F51ED7l,
123*cdf0e10cSrcweir    0x64A6E14Al, 0x80C7D7D4l, 0x05044B62l };
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir /* ciphertext bytes -- right halves */
126*cdf0e10cSrcweir unsigned long ciphertext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
127*cdf0e10cSrcweir    0x6198DD78l, 0xB85ECB8Al, 0x613063F2l, 0x8B963C9Dl, 0x2281B096l,
128*cdf0e10cSrcweir    0xAFDA1EC7l, 0x6198DD78l, 0xC6A0A28Dl, 0xEB05282Bl, 0x250F09A0l,
129*cdf0e10cSrcweir    0x8BEA1DA4l, 0xCF2651EBl, 0x09CE8F1Al, 0x4C379918l, 0x8951FC98l,
130*cdf0e10cSrcweir    0xD69D1AE5l, 0xFFD39C79l, 0x3C2DA6E3l, 0x5B163969l, 0x24D3977Bl,
131*cdf0e10cSrcweir    0xE4FADA8El, 0xF59B87BDl, 0xB49FC019l, 0x937E89A3l, 0x4986ADB5l,
132*cdf0e10cSrcweir    0x658BC778l, 0xD13EF201l, 0x47B268B2l, 0x08EA3CAEl, 0x9FAC631Dl,
133*cdf0e10cSrcweir    0xCDAFF6E4l, 0xB71C49BCl, 0x5754369Al, 0x5D9E0A5Al, 0x49DB005El,
134*cdf0e10cSrcweir    0xD961A6D6l, 0x1BC65CF3l, 0x08640F05l, 0x1BDB1E6El, 0xB1928C0Dl,
135*cdf0e10cSrcweir    0xF960629Dl, 0x2CC85E82l, 0x4F4EC577l, 0x3AB64AE0l, 0xFFC537F6l,
136*cdf0e10cSrcweir    0xA90F6BF2l, 0x5060B8B4l, 0x19E11968l, 0x714CA34Fl, 0xEE3BE15Cl,
137*cdf0e10cSrcweir    0x8CE2D14Bl, 0x469FF67Bl, 0xC1BC96A8l, 0x3858DA9Fl, 0x9B9DB21Fl,
138*cdf0e10cSrcweir    0xFD36B46Fl, 0x5A5479ADl, 0xFA52D080l };
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir static sal_uInt8 cbc_key[16] =
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 	0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
144*cdf0e10cSrcweir 	0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87
145*cdf0e10cSrcweir };
146*cdf0e10cSrcweir static sal_uInt8 cbc_iv[8] =
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
149*cdf0e10cSrcweir };
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir static sal_Char cbc_data[40] = "7654321 Now is the time for ";
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir static sal_uInt8 ecb_ok[32] =
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir 	0x2A, 0xFD, 0x7D, 0xAA, 0x60, 0x62, 0x6B, 0xA3,
156*cdf0e10cSrcweir 	0x86, 0x16, 0x46, 0x8C, 0xC2, 0x9C, 0xF6, 0xE1,
157*cdf0e10cSrcweir 	0x29, 0x1E, 0x81, 0x7C, 0xC7, 0x40, 0x98, 0x2D,
158*cdf0e10cSrcweir 	0x6F, 0x87, 0xAC, 0x5F, 0x17, 0x1A, 0xAB, 0xEA
159*cdf0e10cSrcweir };
160*cdf0e10cSrcweir static sal_uInt8 cbc_ok[32] =
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir 	0x6B, 0x77, 0xB4, 0xD6, 0x30, 0x06, 0xDE, 0xE6,
163*cdf0e10cSrcweir 	0x05, 0xB1, 0x56, 0xE2, 0x74, 0x03, 0x97, 0x93,
164*cdf0e10cSrcweir 	0x58, 0xDE, 0xB9, 0xE7, 0x15, 0x46, 0x16, 0xD9,
165*cdf0e10cSrcweir 	0x59, 0xF1, 0x65, 0x2B, 0xD5, 0xFF, 0x92, 0xCC
166*cdf0e10cSrcweir };
167*cdf0e10cSrcweir static sal_uInt8 cfb_ok[] =
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir 	0xE7, 0x32, 0x14, 0xA2, 0x82, 0x21, 0x39, 0xCA,
170*cdf0e10cSrcweir 	0xF2, 0x6E, 0xCF, 0x6D, 0x2E, 0xB9, 0xE7, 0x6E,
171*cdf0e10cSrcweir 	0x3D, 0xA3, 0xDE, 0x04, 0xD1, 0x51, 0x72, 0x00,
172*cdf0e10cSrcweir 	0x51, 0x9D, 0x57, 0xA6, 0xC3
173*cdf0e10cSrcweir };
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir static sal_uInt8 arcfour_key[6][30] =
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	{ 8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
178*cdf0e10cSrcweir 	{ 8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
179*cdf0e10cSrcweir 	{ 8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
180*cdf0e10cSrcweir 	{ 4, 0xef, 0x01, 0x23, 0x45 },
181*cdf0e10cSrcweir 	{ 8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
182*cdf0e10cSrcweir 	{ 4, 0xef, 0x01, 0x23, 0x45 }
183*cdf0e10cSrcweir };
184*cdf0e10cSrcweir static sal_uInt8 arcfour_data_len[6] =
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir 	8, 8, 8, 20, 28, 10
187*cdf0e10cSrcweir };
188*cdf0e10cSrcweir static sal_uInt8 arcfour_data[6][30] =
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff },
191*cdf0e10cSrcweir 	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
192*cdf0e10cSrcweir 	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
193*cdf0e10cSrcweir 	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194*cdf0e10cSrcweir 	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195*cdf0e10cSrcweir 	  0x00, 0x00, 0x00, 0x00, 0xff },
196*cdf0e10cSrcweir 	{ 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
197*cdf0e10cSrcweir 	  0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
198*cdf0e10cSrcweir 	  0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
199*cdf0e10cSrcweir 	  0x12, 0x34, 0x56, 0x78, 0xff },
200*cdf0e10cSrcweir 	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201*cdf0e10cSrcweir 	  0x00, 0x00, 0xff }
202*cdf0e10cSrcweir };
203*cdf0e10cSrcweir static sal_uInt8 arcfour_ok[6][30] =
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir 	{ 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00},
206*cdf0e10cSrcweir 	{ 0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00},
207*cdf0e10cSrcweir 	{ 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00},
208*cdf0e10cSrcweir 	{ 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
209*cdf0e10cSrcweir 	  0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba,
210*cdf0e10cSrcweir 	  0x36, 0xb6, 0x78, 0x58, 0x00 },
211*cdf0e10cSrcweir 	{ 0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89,
212*cdf0e10cSrcweir 	  0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c,
213*cdf0e10cSrcweir 	  0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87,
214*cdf0e10cSrcweir 	  0x40, 0x01, 0x1e, 0xcf, 0x00 },
215*cdf0e10cSrcweir 	{ 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
216*cdf0e10cSrcweir 	  0xbd, 0x61, 0x00}
217*cdf0e10cSrcweir };
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir int SAL_CALL main (int argc, char *argv)
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir 	rtlCipher cipher;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	/* ECB */
224*cdf0e10cSrcweir 	cipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeECB);
225*cdf0e10cSrcweir 	OSL_ASSERT(cipher != 0);
226*cdf0e10cSrcweir 	if (cipher != 0)
227*cdf0e10cSrcweir 	{
228*cdf0e10cSrcweir 		rtlCipherError result;
229*cdf0e10cSrcweir 		sal_uInt8      ecb_in[40], ecb_out[40];
230*cdf0e10cSrcweir 		sal_uInt32     length = strlen(cbc_data) + 1;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 		result = rtl_cipher_init (
233*cdf0e10cSrcweir 			cipher, rtl_Cipher_DirectionBoth,
234*cdf0e10cSrcweir 			cbc_key, sizeof(cbc_key), NULL, 0);
235*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 		memset (ecb_out, 0, sizeof(ecb_out));
238*cdf0e10cSrcweir 		result = rtl_cipher_encode (
239*cdf0e10cSrcweir 			cipher, cbc_data, length, ecb_out, sizeof(ecb_out));
240*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
241*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (ecb_out, ecb_ok, sizeof(ecb_ok)) == 0);
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 		memset (ecb_in,  0, sizeof(ecb_in));
244*cdf0e10cSrcweir 		result = rtl_cipher_decode (
245*cdf0e10cSrcweir 			cipher, ecb_out, length, ecb_in, sizeof(ecb_in));
246*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
247*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (ecb_in, cbc_data, length) == 0);
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 		rtl_cipher_destroy (cipher);
250*cdf0e10cSrcweir 	}
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	/* CBC */
253*cdf0e10cSrcweir 	cipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeCBC);
254*cdf0e10cSrcweir 	OSL_ASSERT(cipher != 0);
255*cdf0e10cSrcweir 	if (cipher != 0)
256*cdf0e10cSrcweir 	{
257*cdf0e10cSrcweir 		rtlCipherError result;
258*cdf0e10cSrcweir 		sal_uInt8      cbc_in[40], cbc_out[40];
259*cdf0e10cSrcweir 		sal_uInt32     length = strlen(cbc_data) + 1;
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 		result = rtl_cipher_init (
262*cdf0e10cSrcweir 			cipher, rtl_Cipher_DirectionEncode,
263*cdf0e10cSrcweir 			cbc_key, sizeof(cbc_key), cbc_iv, sizeof(cbc_iv));
264*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 		memset (cbc_out, 0, sizeof(cbc_out));
267*cdf0e10cSrcweir 		result = rtl_cipher_encode (
268*cdf0e10cSrcweir 			cipher, cbc_data, length, cbc_out, sizeof(cbc_out));
269*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
270*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (cbc_out, cbc_ok, sizeof(cbc_ok)) == 0);
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 		result = rtl_cipher_init (
273*cdf0e10cSrcweir 			cipher, rtl_Cipher_DirectionDecode,
274*cdf0e10cSrcweir 			cbc_key, sizeof(cbc_key), cbc_iv, sizeof(cbc_iv));
275*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 		memset (cbc_in,  0, sizeof(cbc_in));
278*cdf0e10cSrcweir 		result = rtl_cipher_decode (
279*cdf0e10cSrcweir 			cipher, cbc_out, length, cbc_in, sizeof(cbc_in));
280*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
281*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (cbc_in, cbc_data, length) == 0);
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 		rtl_cipher_destroy (cipher);
284*cdf0e10cSrcweir  	}
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	/* CFB */
287*cdf0e10cSrcweir 	cipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream);
288*cdf0e10cSrcweir 	OSL_ASSERT(cipher != 0);
289*cdf0e10cSrcweir 	if (cipher != 0)
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir 		rtlCipherError result;
292*cdf0e10cSrcweir 		sal_uInt8      cfb_in[40], cfb_out[40];
293*cdf0e10cSrcweir 		sal_uInt32     length = strlen(cbc_data) + 1;
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 		result = rtl_cipher_init (
296*cdf0e10cSrcweir 			cipher, rtl_Cipher_DirectionEncode,
297*cdf0e10cSrcweir 			cbc_key, sizeof(cbc_key), cbc_iv, sizeof(cbc_iv));
298*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir 		memset (cfb_out, 0, sizeof(cfb_out));
301*cdf0e10cSrcweir 		result = rtl_cipher_encode (
302*cdf0e10cSrcweir 			cipher, cbc_data, length, cfb_out, sizeof(cfb_out));
303*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
304*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (cfb_out, cfb_ok, sizeof(cfb_ok)) == 0);
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 		result = rtl_cipher_init (
307*cdf0e10cSrcweir 			cipher, rtl_Cipher_DirectionDecode,
308*cdf0e10cSrcweir 			cbc_key, sizeof(cbc_key), cbc_iv, sizeof(cbc_iv));
309*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 		memset (cfb_in,  0, sizeof(cfb_in));
312*cdf0e10cSrcweir 		result = rtl_cipher_decode (
313*cdf0e10cSrcweir 			cipher, cfb_out, length, cfb_in, sizeof(cfb_in));
314*cdf0e10cSrcweir 		OSL_ASSERT(result == rtl_Cipher_E_None);
315*cdf0e10cSrcweir 		OSL_ASSERT(memcmp (cfb_in, cbc_data, length) == 0);
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 		rtl_cipher_destroy (cipher);
318*cdf0e10cSrcweir 	}
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 	/* ARCFOUR */
321*cdf0e10cSrcweir 	cipher = rtl_cipher_create (rtl_Cipher_AlgorithmARCFOUR, rtl_Cipher_ModeStream);
322*cdf0e10cSrcweir 	OSL_ASSERT(cipher != 0);
323*cdf0e10cSrcweir 	if (cipher != 0)
324*cdf0e10cSrcweir 	{
325*cdf0e10cSrcweir 		rtlCipherError result;
326*cdf0e10cSrcweir 		sal_uInt8      arcfour_out[40];
327*cdf0e10cSrcweir 		sal_Size       length;
328*cdf0e10cSrcweir 		int            i, n;
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 		n = sizeof(arcfour_data_len) / sizeof(arcfour_data_len[0]);
331*cdf0e10cSrcweir 		for (i = 0; i < n; i++)
332*cdf0e10cSrcweir 		{
333*cdf0e10cSrcweir 			length = arcfour_data_len[i];
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 			result = rtl_cipher_init (
336*cdf0e10cSrcweir 				cipher, rtl_Cipher_DirectionBoth,
337*cdf0e10cSrcweir 				&(arcfour_key[i][1]), arcfour_key[i][0], 0, 0);
338*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 			memset (arcfour_out, 0, sizeof(arcfour_out));
341*cdf0e10cSrcweir 			result = rtl_cipher_encode (
342*cdf0e10cSrcweir 				cipher, &(arcfour_data[i][0]), length,
343*cdf0e10cSrcweir 				arcfour_out, sizeof(arcfour_out));
344*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
345*cdf0e10cSrcweir 			OSL_ASSERT(memcmp (arcfour_out, arcfour_ok[i], length) == 0);
346*cdf0e10cSrcweir 		}
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 		n = arcfour_data_len[3];
349*cdf0e10cSrcweir 		for (i = 1; i < n; i++)
350*cdf0e10cSrcweir 		{
351*cdf0e10cSrcweir 			length = i;
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 			result = rtl_cipher_init (
354*cdf0e10cSrcweir 				cipher, rtl_Cipher_DirectionBoth,
355*cdf0e10cSrcweir 				&(arcfour_key[3][1]), arcfour_key[3][0], 0, 0);
356*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 			memset (arcfour_out, 0, sizeof(arcfour_out));
359*cdf0e10cSrcweir 			result = rtl_cipher_encode (
360*cdf0e10cSrcweir 				cipher, &(arcfour_data[3][0]), length,
361*cdf0e10cSrcweir 				arcfour_out, sizeof(arcfour_out));
362*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
363*cdf0e10cSrcweir 			OSL_ASSERT(memcmp (arcfour_out, arcfour_ok[3], length) == 0);
364*cdf0e10cSrcweir 			OSL_ASSERT(arcfour_out[length] == 0);
365*cdf0e10cSrcweir 		}
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir 		n = arcfour_data_len[3];
368*cdf0e10cSrcweir 		for (i = 1; i < n; i++)
369*cdf0e10cSrcweir 		{
370*cdf0e10cSrcweir 			length = i;
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir 			result = rtl_cipher_init (
373*cdf0e10cSrcweir 				cipher, rtl_Cipher_DirectionBoth,
374*cdf0e10cSrcweir 				&(arcfour_key[3][1]), arcfour_key[3][0], 0, 0);
375*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 			memset (arcfour_out, 0, sizeof(arcfour_out));
378*cdf0e10cSrcweir 			result = rtl_cipher_encode (
379*cdf0e10cSrcweir 				cipher, &(arcfour_data[3][0]), length,
380*cdf0e10cSrcweir 				&(arcfour_out[0]), sizeof(arcfour_out));
381*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir 			result = rtl_cipher_encode (
384*cdf0e10cSrcweir 				cipher, &(arcfour_data[3][length]), n - length,
385*cdf0e10cSrcweir 				&(arcfour_out[length]), sizeof(arcfour_out) - length);
386*cdf0e10cSrcweir 			OSL_ASSERT(result == rtl_Cipher_E_None);
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir 			OSL_ASSERT(memcmp (arcfour_out, arcfour_ok[3], length) == 0);
389*cdf0e10cSrcweir 		}
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 		rtl_cipher_destroy (cipher);
392*cdf0e10cSrcweir 	}
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir 	/* Done */
395*cdf0e10cSrcweir 	return 0;
396*cdf0e10cSrcweir }
397