=================================================================== RCS file: /home/cvs/OpenXM/src/ox_ntl/crypt/des/destest.c,v retrieving revision 1.1 retrieving revision 1.3 diff -u -p -r1.1 -r1.3 --- OpenXM/src/ox_ntl/crypt/des/destest.c 2004/07/11 00:32:17 1.1 +++ OpenXM/src/ox_ntl/crypt/des/destest.c 2005/06/19 15:45:38 1.3 @@ -1,4 +1,4 @@ -/* $OpenXM$ */ +/* $OpenXM: OpenXM/src/ox_ntl/crypt/des/destest.c,v 1.2 2005/06/19 15:30:01 iwane Exp $ */ /* * FIPS 81 - Des Modes of Operation */ @@ -6,15 +6,18 @@ #include #include -#include "des.h" +#include "des3.h" #define N 4096 int main() { des_key dkey; + des3_key dkey3; unsigned char key[] = "\x01\x23\x45\x67\x89\xab\xcd\xef"; /* secret key */ - unsigned char *iv = (unsigned char *)"\x12\x34\x56\x78\x90\xab\xcd\xef"; /* initial vector */ + unsigned char key3[3 * 8]; + unsigned char *ivp = (unsigned char *)"\x12\x34\x56\x78\x90\xab\xcd\xef"; /* initial vector */ + unsigned char iv[8]; unsigned char enc[N], dec[N]; int i, k; int ret; @@ -75,7 +78,12 @@ main() const unsigned char *plain2 = (unsigned char *)"7654321 Now is the time for \0\0\0\0\0\0\0\0"; const unsigned char *p; + for (i = 0; i < 3; i++) { + memcpy(key3 + 8 * i, key, 8); + } + des_keyset(key, &dkey); + des3_keyset(key3, &dkey3); k = 24; ret = des_enc_ecb(&dkey, k, plain, enc); @@ -102,6 +110,8 @@ _CBC: int ks[] = {24, 32}; const unsigned char *d[] = {plain, plain2}; + memcpy(iv, ivp, 8); + k = ks[i]; p = d[i]; @@ -112,12 +122,34 @@ _CBC: goto _CFB; } + memcpy(iv, ivp, 8); + ret = des_dec_cbc(&dkey, iv, k, enc, dec); if (memcmp(p, dec, k)) { printf("CBC dec failed.\n"); printf("ret = %d, i = %d\n", ret, i); goto _CFB; } + + memcpy(iv, ivp, 8); + + ret = des3_enc_cbc(&dkey3, iv, k, p, enc); + if (memcmp(ci[i], enc, k)) { + printf("CBC enc failed. [des3]\n"); + printf("ret = %d, i = %d\n", ret, i); + goto _CFB; + } + + memcpy(iv, ivp, 8); + + ret = des3_dec_cbc(&dkey3, iv, k, enc, dec); + if (memcmp(p, dec, k)) { + printf("CBC dec failed. [des3]\n"); + printf("ret = %d, i = %d\n", ret, i); + goto _CFB; + } + + } printf("CBC ok.\n"); @@ -130,6 +162,7 @@ _CFB: int ks[] = {24, 24, 24, 24, 24, 28}; const unsigned char *d[] = {plain, plain, plain, plain, plain, plain2}; + memcpy(iv, ivp, 8); k = ks[i]; p = d[i]; @@ -157,6 +190,8 @@ _OFB: const unsigned char *ci[] = {ofb8, ofb64}; int ks[] = {10, 24}; const unsigned char *d[] = {plain, plain}; + + memcpy(iv, ivp, 8); k = ks[i]; p = d[i];