www.pudn.com > serpent.rar > ecb_tbl.c


/* 
  $Id: ecb_tbl.c,v 1.3 1998/06/07 12:26:10 fms Exp $ 
 
  # This file is part of the C reference implementation of Serpent. 
  # 
  # Written by Frank Stajano, 
  # Olivetti Oracle Research Laboratory  and 
  # Cambridge University Computer Laboratory . 
  #  
  # (c) 1998 Olivetti Oracle Research Laboratory (ORL) 
  # 
  # Original (Python) Serpent reference development started on 1998 02 12. 
  # C implementation development started on 1998 03 04. 
  # 
  # Serpent cipher invented by Ross Anderson, Eli Biham, Lars Knudsen. 
  # Serpent is a candidate for the Advanced Encryption Standard. 
 
*/ 
 
/* -------------------------------------------------- */ 
#include "serpent-api.h" 
#include "serpent-aux.h" 
/* -------------------------------------------------- */ 
EMBED_RCS(ecb_tbl_c, 
          "$Id: ecb_tbl.c,v 1.3 1998/06/07 12:26:10 fms Exp $") 
 
int main(void) { 
  int i, bitsPerShortKey, result, round, value; 
  BLOCK plainText, cipherText; 
  char asciiKey[HEX_DIGITS_PER_KEY+1]; 
  char buffer[MAX_CHARS_PER_LINE+1]; 
  keyInstance key; 
  cipherInstance cipher; 
 
  printf( 
         "/* \n" 
         "\n" 
         "For each key size, make a key of all 0s and try out some\n" 
         "'magic' plaintexts, coming from stdin, that have been\n" 
         "precomputed in such a way that they will present specific\n" 
         "inputs to the S-boxes.\n" 
         "\n" 
         "All the rounds, from 0 to 31, are exercised in turn. For each\n" 
         "round, all the possible nibble inputs from 0 to f are presented\n" 
         "in turn to each S-box in that round. The plaintexts with these\n" 
         "properties have been precomputed in advance by working\n" 
         "backwards through the cipher.\n" 
         "\n" 
         "*/\n" 
         ); 
 
  printHeader("ecb_tbl", "Electronic Codebook (ECB) Mode", 
              "Tables Known Answer Tests"); 
 
   
  result = cipherInit(&cipher, MODE_ECB, 0); 
  if (result != TRUE) goto error; 
 
  for(bitsPerShortKey=BITS_PER_SHORTEST_KEY; bitsPerShortKey<=BITS_PER_KEY; 
      bitsPerShortKey+=BITS_PER_KEY_STEP) { 
 
    printf("KEYSIZE=%d\n\n", bitsPerShortKey); 
 
    /* Construct (backwards) an ascii key of all 0s, of length 
       bitsPerShortKey bits. */ 
    /* XXX It would be neater to read the key from the input too, instead 
       of hardcoding 0000...0 */ 
    i=bitsPerShortKey/BITS_PER_HEX_DIGIT; 
    asciiKey[i] = 0; /* terminating null */  
    for (i--; i >=0; i--) { 
      asciiKey[i] = '0'; 
    } 
    printf("KEY=%s\n\n", asciiKey); 
 
    result = makeKey(&key, DIR_ENCRYPT, bitsPerShortKey, asciiKey); 
    if (result != TRUE) goto error; 
       
    i=1; 
    for(round=0; round < r; round++) { 
      for(value=0; value < (1<