home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / des / cksum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-02  |  3.6 KB  |  137 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/lib/des/RCS/cksum.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * These routines perform encryption and decryption using the DES
  12.  * private key algorithm, or else a subset of it-- fewer inner loops.
  13.  * (AUTH_DES_ITER defaults to 16, may be less.)
  14.  *
  15.  * Under U.S. law, this software may not be exported outside the US
  16.  * without license from the U.S. Commerce department.
  17.  * 
  18.  * These routines form the library interface to the DES facilities.
  19.  *
  20.  *    spm    8/85    MIT project athena
  21.  */
  22.  
  23. #ifndef    lint
  24. static char rcsid_cksum_c[] =
  25. "$Id: cksum.c,v 4.10 90/01/02 13:46:25 jtkohl Exp $";
  26. #endif    lint
  27.  
  28. #include <mit-copyright.h>
  29. #include <stdio.h>
  30. #include <strings.h>
  31.  
  32. #include <des.h>
  33. #include "des_internal.h"
  34.  
  35. extern int des_debug;
  36. extern int des_debug_print();
  37.  
  38. /*
  39.  * This routine performs DES cipher-block-chaining checksum operation,
  40.  * a.k.a.  Message Authentication Code.  It ALWAYS encrypts from input
  41.  * to a single 64 bit output MAC checksum.
  42.  *
  43.  * The key schedule is passed as an arg, as well as the cleartext or
  44.  * ciphertext. The cleartext and ciphertext should be in host order.
  45.  *
  46.  * NOTE-- the output is ALWAYS 8 bytes long.  If not enough space was
  47.  * provided, your program will get trashed.
  48.  *
  49.  * The input is null padded, at the end (highest addr), to an integral
  50.  * multiple of eight bytes.
  51.  */
  52.  
  53. unsigned long
  54. des_cbc_cksum(in,out,length,key,iv)
  55.     des_cblock *in;        /* >= length bytes of inputtext */
  56.     des_cblock *out;        /* >= length bytes of outputtext */
  57.     register long length;    /* in bytes */
  58.     des_key_schedule key;        /* precomputed key schedule */
  59.     des_cblock *iv;        /* 8 bytes of ivec */
  60. {
  61.     register unsigned long *input = (unsigned long *) in;
  62.     register unsigned long *output = (unsigned long *) out;
  63.     unsigned long *ivec = (unsigned long *) iv;
  64.  
  65.     unsigned long i,j;
  66.     static unsigned long t_input[2];
  67.     static unsigned long t_output[8];
  68.     static unsigned char *t_in_p;
  69.  
  70.     t_in_p = (unsigned char *) t_input;
  71. #ifdef MUSTALIGN
  72.     if ((long) ivec & 3) {
  73.     bcopy((char *)ivec++,(char *)&t_output[0],sizeof(t_output[0]));
  74.     bcopy((char *)ivec,(char *)&t_output[1],sizeof(t_output[1]));
  75.     }
  76.     else
  77. #endif
  78.     {
  79.     t_output[0] = *ivec++;
  80.     t_output[1] = *ivec;
  81.     }
  82.  
  83.     for (i = 0; length > 0; i++, length -= 8) {
  84.     /* get input */
  85. #ifdef MUSTALIGN
  86.     if ((long) input & 3) {
  87.         bcopy((char *)input++,(char *)&t_input[0],sizeof(t_input[0]));
  88.         bcopy((char *)input++,(char *)&t_input[1],sizeof(t_input[1]));
  89.     }
  90.     else
  91. #endif
  92.     {
  93.         t_input[0] = *input++;
  94.         t_input[1] = *input++;
  95.     }
  96.  
  97.     /* zero pad */
  98.     if (length < 8)
  99.         for (j = length; j <= 7; j++)
  100.         *(t_in_p+j)= 0;
  101.  
  102. #ifdef DEBUG
  103.     if (des_debug)
  104.         des_debug_print("clear",length,t_input[0],t_input[1]);
  105. #endif
  106.     /* do the xor for cbc into the temp */
  107.     t_input[0] ^= t_output[0] ;
  108.     t_input[1] ^= t_output[1] ;
  109.     /* encrypt */
  110.     (void) des_ecb_encrypt(t_input,t_output,key,1);
  111. #ifdef DEBUG
  112.     if (des_debug) {
  113.         des_debug_print("xor'ed",i,t_input[0],t_input[1]);
  114.         des_debug_print("cipher",i,t_output[0],t_output[1]);
  115.     }
  116. #else
  117. #ifdef lint
  118.     i = i;
  119. #endif
  120. #endif
  121.     }
  122.     /* copy temp output and save it for checksum */
  123. #ifdef MUSTALIGN
  124.     if ((long) output & 3) {
  125.     bcopy((char *)&t_output[0],(char *)output++,sizeof(t_output[0]));
  126.     bcopy((char *)&t_output[1],(char *)output,sizeof(t_output[1]));
  127.     }
  128.     else
  129. #endif
  130.     {
  131.     *output++ = t_output[0];
  132.     *output = t_output[1];
  133.     }
  134.  
  135.     return (unsigned long) t_output[1];
  136. }
  137.