home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / des3_os2.zip / TESTCBC.C < prev    next >
C/C++ Source or Header  |  1996-02-20  |  2KB  |  56 lines

  1. /* 
  2.  * TESTCBC.C - validates the DES in Cipher Block Chaining (CBC) mode     
  3.  *
  4.  *  In Cipher Block Chaining (CBC) mode the first plain text data block
  5.  * is XORed with a block of pseudo-random data prior to being processed
  6.  * through the DES. The resulting cipher text block is then XORed with
  7.  * the next plain text data block to form the next input block to the 
  8.  * DES, thus chaining together blocks of ciphertext.
  9.  * The CBC mode produces the same cipher text whenever the same plain 
  10.  * text encrypted using the same key and IV.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "des3.h"
  16.  
  17. /* for validation keybits and patterns taken from FIPS PUB 81 Appendix C */
  18. /* initialization vector = 1234567890abcdef */
  19. static unsigned char ivec[8]   = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
  20. /* cryptographic key = 0123456789abcdef */
  21. static unsigned char key[8]    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
  22. /* plain text: "Now is the time for all ." */
  23. static unsigned char plain[3][8] = {
  24.    { 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74, },
  25.    { 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20, },
  26.    { 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20, },
  27. };
  28.  
  29. int main(void)
  30. {
  31.   unsigned char results[3][8], clrtext[3][8], iv[8];
  32.   int i;
  33.   desinit(key);
  34.   memcpy(iv,ivec,8);
  35.   for (i = 0; i < 3; i++) {
  36.     cbcencode(plain[i],results[i],iv);
  37.     memcpy(iv,results[i],8);
  38.   }
  39.   for (i = 0; i < 3; i++)
  40.   printf("CBC encrypt: %02x%02x%02x%02x%02x%02x%02x%02x -> %02x%02x%02x%02x%02x%02x%02x%02x \n",
  41.     plain[i][0], plain[i][1], plain[i][2], plain[i][3], plain[i][4], plain[i][5],
  42.     plain[i][6], plain[i][7], results[i][0], results[i][1], results[i][2],
  43.     results[i][3], results[i][4], results[i][5], results[i][6], results[i][7]);
  44.   memcpy(iv,ivec,8);
  45.   for (i = 0; i < 3; i++) {
  46.     cbcdecode(results[i],clrtext[i],iv);
  47.     memcpy(iv,results[i],8);
  48.   }
  49.   for (i = 0; i < 3; i++)
  50.   printf("CBC decrypt: %02x%02x%02x%02x%02x%02x%02x%02x -> %02x%02x%02x%02x%02x%02x%02x%02x \n",
  51.     results[i][0], results[i][1], results[i][2], results[i][3], results[i][4], results[i][5], 
  52.     results[i][6], results[i][7], clrtext[i][0], clrtext[i][1], clrtext[i][2], clrtext[i][3], 
  53.     clrtext[i][4], clrtext[i][5], clrtext[i][6], clrtext[i][7]);
  54.   return 0;
  55. }
  56.