home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku200.zip / ck_crp.c next >
C/C++ Source or Header  |  2001-12-01  |  169KB  |  5,394 lines

  1. char *ckcrpv = "Encryption Engine, 8.0.109, 13 Sept 2000";
  2. /*
  3.   C K _ C R P . C  -  Cryptography for C-Kermit"
  4.  
  5.   Copyright (C) 1998, 2001, Trustees of Columbia University in the City of New
  6.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  7.   sold for profit as a software product itself, nor may it be included in or
  8.   distributed with commercial products or otherwise distributed by commercial
  9.   concerns to their clients or customers without written permission of the
  10.   Office of Kermit Development and Distribution, Columbia University.  This
  11.   copyright notice must not be removed, altered, or obscured.
  12.  
  13.   Author:
  14.   Jeffrey E Altman (jaltman@columbia.edu).
  15. */
  16.  
  17. #define CK_CRP_C
  18. #ifdef CK_DES
  19. #ifdef CK_SSL
  20. #ifndef LIBDES
  21. #define LIBDES
  22. #endif /* LIBDES */
  23. #endif /* CK_SSL */
  24. #endif /* CK_DES */
  25.  
  26. #ifdef CRYPT_DLL
  27. #define CK_AUTHENTICATION
  28. #define CK_ENCRYPTION
  29. #define CK_DES
  30. #define CK_CAST
  31. #ifndef LIBDES
  32. #define LIBDES
  33. #endif /* LIBDES */
  34.  
  35. #define TELCMDS                         /* to define name array */
  36. #define TELOPTS                         /* to define name array */
  37. #define ENCRYPT_NAMES
  38. #endif /* CRYPT_DLL */
  39.  
  40. #include "ckcsym.h"
  41. #include "ckcdeb.h"
  42. #include "ckcnet.h"
  43.  
  44. #ifdef DEBUG
  45. #undef DEBUG
  46. #endif /* DEBUG */
  47.  
  48. #ifdef CK_AUTHENTICATION
  49. #ifdef CK_ENCRYPTION
  50. #define ENCRYPTION
  51. #ifdef CK_DES
  52. #define DES_ENCRYPTION
  53. #endif /* CK_DES */
  54. #ifdef  CK_CAST
  55. #define CAST_ENCRYPTION
  56. #endif /* CK_CAST */
  57. #ifdef COMMENT
  58. #define CAST_EXPORT_ENCRYPTION
  59. #endif /* COMMENT */
  60. #endif /* CK_ENCRYPTION */
  61. #endif /* CK_AUTHENTICATION */
  62.  
  63. #include "ckucmd.h"                     /* For struct keytab definition */
  64. #include "ckuath.h"
  65. #include "ckuat2.h"
  66. #ifdef MIT_CURRENT
  67. #include <krb5.h>
  68. #endif /* MIT_CURRENT */
  69.  
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #ifdef OS2
  73. #include <stdarg.h>
  74. #else /* OS2 */
  75. static char * tmpstring = NULL;
  76. #endif /* OS2 */
  77.  
  78. #ifndef CAST_OR_EXPORT
  79. #ifdef CAST_ENCRYPTION
  80. #define CAST_OR_EXPORT
  81. #endif /* CAST_ENCRYPTION */
  82. #ifdef CAST_EXPORT_ENCRYPTION
  83. #define CAST_OR_EXPORT
  84. #endif /* CAST_EXPORT_ENCRYPTION */
  85. #endif /* CAST_OR_EXPORT */
  86.  
  87. #ifdef CK_ENCRYPTION
  88. #ifdef CRYPT_DLL
  89. int cmd_quoting = 0;
  90.  
  91. #ifndef TELOPT_MACRO
  92. int
  93. telopt_index(opt) int opt; {
  94.     if ( opt >= 0 && opt <= TELOPT_SEND_URL )
  95.         return(opt);
  96.     else if ( opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT )
  97.         return(opt-89);
  98.     else
  99.         return(NTELOPTS);
  100. }
  101.  
  102. int
  103. telopt_ok(opt) int opt; {
  104.     return((opt >= TELOPT_BINARY && opt <= TELOPT_SEND_URL) ||
  105.         (opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT));
  106. }
  107.  
  108. CHAR *
  109. telopt(opt) int opt; {
  110.     if ( telopt_ok(opt) )
  111.         return(telopts[telopt_index(opt)]);
  112.     else
  113.         return("UNKNOWN");
  114. }
  115. #endif /* TELOPT_MACRO */
  116.  
  117. static int (*p_ttol)(char *,int)=NULL;
  118. static int (*p_dodebug)(int,char *,char *,long)=NULL;
  119. static int (*p_dohexdump)(char *,char *,int)=NULL;
  120. static void (*p_tn_debug)(char *)=NULL;
  121. static int (*p_vscrnprintf)(char *, ...)=NULL;
  122. static void * p_k5_context=NULL;
  123.  
  124. int
  125. ttol(char * s, int n)
  126. {
  127.     if ( p_ttol )
  128.         return(p_ttol(s,n));
  129.     else
  130.         return(-1);
  131. }
  132.  
  133. int
  134. dodebug(int flag, char * s1, char * s2, long n)
  135. {
  136.     if ( p_dodebug )
  137.         return(p_dodebug(flag,s1,s2,n));
  138.     else
  139.         return(-1);
  140. }
  141.  
  142. int
  143. dohexdump( char * s1, char * s2, int n )
  144. {
  145.     if ( p_dohexdump )
  146.         p_dohexdump(s1,s2,n);
  147.     return(0);
  148. }
  149.  
  150. void
  151. tn_debug( char * s )
  152. {
  153.     if ( p_tn_debug )
  154.         p_tn_debug(s);
  155. }
  156.  
  157. static char myprtfstr[4096];
  158. int
  159. Vscrnprintf(const char * format, ...) {
  160.     int i, len, rc=0;
  161.     char *cp;
  162.     va_list ap;
  163.  
  164.     va_start(ap, format);
  165. #ifdef NT
  166.     rc = _vsnprintf(myprtfstr, sizeof(myprtfstr)-1, format, ap);
  167. #else /* NT */
  168.     rc = vsprintf(myprtfstr, format, ap);
  169. #endif /* NT */
  170.     va_end(ap);
  171.  
  172.     if ( p_vscrnprintf )
  173.         return(p_vscrnprintf(myprtfstr));
  174.     else
  175.         return(-1);
  176. }
  177.  
  178. int
  179. #ifdef CK_ANSIC
  180. tn_hex(CHAR * buf, int buflen, CHAR * data, int datalen)
  181. #else /* CK_ANSIC */
  182. tn_hex(buf, buflen, data, datalen)
  183.     CHAR * buf;
  184.     int buflen;
  185.     CHAR * data;
  186.     int datalen;
  187. #endif /* CK_ANSIC */
  188. {
  189.     int i = 0, j = 0, k = 0;
  190.     CHAR tmp[8];
  191. #ifdef COMMENT
  192.     int was_hex = 1;
  193.  
  194.     for (k=0; k < datalen; k++) {
  195.         if (data[k] < 32 || data[k] >= 127) {
  196.             sprintf(tmp,"%s%02X ",was_hex?"":"\" ",data[k]);
  197.             was_hex = 1;
  198.         } else {
  199.             sprintf(tmp,"%s%c",was_hex?"\"":"",data[k]);
  200.             was_hex = 0;
  201.         }
  202.         ckstrncat(buf,tmp,buflen);
  203.     }
  204.     if (!was_hex)
  205.         ckstrncat(buf,"\" ",buflen);
  206. #else /* COMMENT */
  207.     if (datalen <= 0 || data == NULL)
  208.         return(0);
  209.  
  210.     for (i = 0; i < datalen; i++) {
  211.         ckstrncat(buf,"\r\n  ",buflen);
  212.         for (j = 0 ; (j < 16); j++) {
  213.             if ((i + j) < datalen)
  214.           sprintf(tmp,
  215.               "%s%02x ",
  216.               (j == 8 ? "| " : ""),
  217.               (CHAR) data[i + j]
  218.               );
  219.             else
  220.           sprintf(tmp,
  221.               "%s   ",
  222.               (j == 8 ? "| " : "")
  223.               );
  224.             ckstrncat(buf,tmp,buflen);
  225.         }
  226.         ckstrncat(buf," ",buflen);
  227.         for (k = 0; (k < 16) && ((i + k) < datalen); k++) {
  228.             sprintf(tmp,
  229.                      "%s%c",
  230.                      (k == 8 ? " " : ""),
  231.                      isprint(data[i + k]) ? data[i + k] : '.'
  232.                      );
  233.             ckstrncat(buf,tmp,buflen);
  234.         }
  235.         i += j - 1;
  236.     } /* end for */
  237.     ckstrncat(buf,"\r\n  ",buflen);
  238. #endif /* COMMENT */
  239.     return(strlen(buf));
  240. }
  241.  
  242. #ifdef COMMENT
  243. #define ttol        dll_ttol
  244. #define dodebug     dll_dodebug
  245. #define dohexdump   dll_dohexdump
  246. #define tn_debug    dll_tn_debug
  247. #define Vscrnprintf dll_vscrnprintf
  248. #endif /* COMMENT */
  249.  
  250. char tn_msg[TN_MSG_LEN], hexbuf[TN_MSG_LEN];   /* from ckcnet.c */
  251. int deblog=1, debses=1, tn_deb=1;
  252. #else /* CRYPT_DLL */
  253. extern char tn_msg[], hexbuf[];         /* from ckcnet.c */
  254. extern int deblog, debses, tn_deb;
  255. #ifdef MIT_CURRENT
  256. extern krb5_context k5_context;
  257. #endif /* MIT_CURRENT */
  258. #endif /* CRYPT_DLL */
  259.  
  260. #ifdef LIBDES
  261. #ifndef UNIX
  262. #define des_new_random_key            des_random_key
  263. #define des_set_random_generator_seed des_random_seed
  264. #endif /* UNIX */
  265. #define des_fixup_key_parity          des_set_odd_parity
  266. #else /* LIBDES */
  267. #ifdef UNIX
  268. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  269. #endif /* UNIX */
  270. #ifdef OS2
  271. #define des_new_random_key            ck_des_new_random_key
  272. #define des_set_random_generator_seed ck_des_set_random_generator_seed
  273. #define des_key_sched                 ck_des_key_sched
  274. #define des_ecb_encrypt               ck_des_ecb_encrypt
  275. #define des_string_to_key             ck_des_string_to_key
  276. #define des_fixup_key_parity          ck_des_fixup_key_parity
  277. #endif /* OS2 */
  278. #endif /* LIBDES */
  279.  
  280. #ifdef CK_DES
  281. /* This code comes from Eric Young's libdes package and is not part   */
  282. /* of the standard MIT DES library that is part of Kerberos. However, */
  283. /* it is extremely useful.  So we add it here.                        */
  284.  
  285.  
  286. /* Weak and semi week keys as take from
  287.  * %A D.W. Davies
  288.  * %A W.L. Price
  289.  * %T Security for Computer Networks
  290.  * %I John Wiley & Sons
  291.  * %D 1984
  292.  * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  293.  * (and actual cblock values).
  294.  */
  295. #define NUM_WEAK_KEY    16
  296. static Block weak_keys[NUM_WEAK_KEY]={
  297.         /* weak keys */
  298.         {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  299.         {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  300.         {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  301.         {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  302.         /* semi-weak keys */
  303.         {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  304.         {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  305.         {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  306.         {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  307.         {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  308.         {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  309.         {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  310.         {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  311.         {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  312.         {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  313.         {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  314.         {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  315.  
  316. int
  317. ck_des_is_weak_key(key)
  318. Block key;
  319. {
  320.     int i;
  321.  
  322.     for (i=0; i<NUM_WEAK_KEY; i++) {
  323.         /* Added == 0 to comparision, I obviously don't run
  324.         * this section very often :-(, thanks to
  325.         * engineering@MorningStar.Com for the fix
  326.         * eay 93/06/29
  327.         * Another problem, I was comparing only the first 4
  328.         * bytes, 97/03/18 */
  329.         if (memcmp(weak_keys[i],key,sizeof(Block)) == 0)
  330.             return(1);
  331.     }
  332.     return(0);
  333. }
  334.  
  335. #ifdef UNIX
  336. #ifdef LIBDES
  337. /* These functions are not part of Eric Young's DES library */
  338. /* _unix_time_gmt_unixsec                                  */
  339. /* _des_set_random_generator_seed                          */
  340. /* _des_fixup_key_parity   (added in 0.9.5)                */
  341. /* _des_new_random_key                                     */
  342. #include <sys/time.h>
  343.  
  344. unsigned long
  345. unix_time_gmt_unixsec (usecptr)
  346.     unsigned long  *usecptr;
  347. {
  348.     struct timeval  now;
  349.  
  350.     (void) gettimeofday (&now, (struct timezone *)0);
  351.     if (usecptr)
  352.         *usecptr = now.tv_usec;
  353.     return now.tv_sec;
  354. }
  355.  
  356. void
  357. des_set_random_generator_seed(Block B)
  358. {
  359.     des_random_seed(B);
  360.     return;
  361. }
  362.  
  363. #ifdef COMMENT
  364. /* added to openssl in 0.9.5 */
  365. void
  366. des_fixup_key_parity(Block B)
  367. {
  368.     des_set_odd_parity(B);
  369.     return;
  370. }
  371. #endif /* COMMENT */
  372. int
  373. des_new_random_key(Block B)
  374. {
  375.     int rc=0;
  376.     rc = des_random_key(B);
  377.     return(rc);
  378. }
  379.  
  380. #endif /* LIBDES */
  381. #endif /* UNIX */
  382. #endif /* CK_DES */
  383.  
  384. /*
  385.  * Copyright (c) 1991, 1993
  386.  *      The Regents of the University of California.  All rights reserved.
  387.  *
  388.  * Redistribution and use in source and binary forms, with or without
  389.  * modification, are permitted provided that the following conditions
  390.  * are met:
  391.  * 1. Redistributions of source code must retain the above copyright
  392.  *    notice, this list of conditions and the following disclaimer.
  393.  * 2. Redistributions in binary form must reproduce the above copyright
  394.  *    notice, this list of conditions and the following disclaimer in the
  395.  *    documentation and/or other materials provided with the distribution.
  396.  * 3. All advertising materials mentioning features or use of this software
  397.  *    must display the following acknowledgement:
  398.  *      This product includes software developed by the University of
  399.  *      California, Berkeley and its contributors.
  400.  * 4. Neither the name of the University nor the names of its contributors
  401.  *    may be used to endorse or promote products derived from this software
  402.  *    without specific prior written permission.
  403.  *
  404.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  405.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  406.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  407.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  408.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  409.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  410.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  411.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  412.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  413.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  414.  * SUCH DAMAGE.
  415.  */
  416.  
  417. /* based on @(#)encrypt.c       8.1 (Berkeley) 6/4/93 */
  418.  
  419. /*
  420.  * Copyright (C) 1990 by the Massachusetts Institute of Technology
  421.  *
  422.  * Export of this software from the United States of America may
  423.  * require a specific license from the United States Government.
  424.  * It is the responsibility of any person or organization contemplating
  425.  * export to obtain such a license before exporting.
  426.  *
  427.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  428.  * distribute this software and its documentation for any purpose and
  429.  * without fee is hereby granted, provided that the above copyright
  430.  * notice appear in all copies and that both that copyright notice and
  431.  * this permission notice appear in supporting documentation, and that
  432.  * the name of M.I.T. not be used in advertising or publicity pertaining
  433.  * to distribution of the software without specific, written prior
  434.  * permission.  M.I.T. makes no representations about the suitability of
  435.  * this software for any purpose.  It is provided "as is" without express
  436.  * or implied warranty.
  437.  */
  438.  
  439. #include <stdio.h>
  440.  
  441. /*
  442.  * These function pointers point to the current routines
  443.  * for encrypting and decrypting data.
  444.  */
  445. static VOID     (*encrypt_output) P((unsigned char *, int));
  446. static int      (*decrypt_input) P((int));
  447.  
  448. #ifdef DEBUG
  449. static int encrypt_debug_mode = 1;
  450. static int encrypt_verbose = 1;
  451. #else
  452. static int encrypt_verbose = 1;
  453. static int encrypt_debug_mode = 0;
  454. #endif
  455.  
  456. static char dbgbuf [16384];
  457.  
  458. static int decrypt_mode = 0;
  459. static int encrypt_mode = 0;
  460. static int autoencrypt = 1;
  461. static int autodecrypt = 1;
  462. static int havesessionkey = 0;
  463.  
  464. static kstream EncryptKSGlobalHack = NULL;
  465. static int     EncryptType = ENCTYPE_ANY;
  466.  
  467. #define typemask(x)     ((x) > 0 ? 1 << ((x)-1) : 0)
  468.  
  469. static long i_support_encrypt =
  470.         typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
  471. static long i_support_decrypt =
  472.         typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
  473. static long i_wont_support_encrypt = 0;
  474. static long i_wont_support_decrypt = 0;
  475. #define I_SUPPORT_ENCRYPT       (i_support_encrypt & ~i_wont_support_encrypt)
  476. #define I_SUPPORT_DECRYPT       (i_support_decrypt & ~i_wont_support_decrypt)
  477.  
  478. static long remote_supports_encrypt = 0;
  479. static long remote_supports_decrypt = 0;
  480.  
  481. /* Make sure that this list is in order of algorithm strength     */
  482. /* as it determines the search order for selecting specific       */
  483. /* encryption choices.  All CFB modes must come before OFB modes. */
  484. static Encryptions encryptions[] = {
  485. #ifdef DES_ENCRYPTION
  486.     { "DES3_CFB64",
  487.           ENCTYPE_DES3_CFB64,
  488.           des3_cfb64_encrypt,
  489.           des3_cfb64_decrypt,
  490.           des3_cfb64_init,
  491.           des3_cfb64_start,
  492.           des3_cfb64_is,
  493.           des3_cfb64_reply,
  494.           des3_cfb64_session,
  495.           des3_cfb64_keyid,
  496.           NULL },
  497. #endif /* DES_ENCRYPTION */
  498. #ifdef CAST_ENCRYPTION
  499. #ifndef CAST_EXPORT_ENCRYPTION
  500.     { "CAST128_CFB64",  ENCTYPE_CAST128_CFB64,
  501.           cast_cfb64_encrypt,
  502.           cast_cfb64_decrypt,
  503.           cast_cfb64_init,
  504.           cast_cfb64_start,
  505.           cast_cfb64_is,
  506.           cast_cfb64_reply,
  507.           cast_cfb64_session,
  508.           cast_cfb64_keyid,
  509.           NULL },
  510. #endif
  511. #endif
  512. #ifdef DES_ENCRYPTION
  513.     { "DES_CFB64",
  514.           ENCTYPE_DES_CFB64,
  515.           cfb64_encrypt,
  516.           cfb64_decrypt,
  517.           cfb64_init,
  518.           cfb64_start,
  519.           cfb64_is,
  520.           cfb64_reply,
  521.           cfb64_session,
  522.           cfb64_keyid,
  523.           NULL },
  524. #endif  /* DES_ENCRYPTION */
  525. #if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
  526.     { "CAST5_40_CFB64", ENCTYPE_CAST5_40_CFB64,
  527.           castexp_cfb64_encrypt,
  528.           castexp_cfb64_decrypt,
  529.           castexp_cfb64_init,
  530.           castexp_cfb64_start,
  531.           castexp_cfb64_is,
  532.           castexp_cfb64_reply,
  533.           castexp_cfb64_session,
  534.           castexp_cfb64_keyid,
  535.           NULL },
  536. #endif /* CAST_ENCRYPTION */
  537. #ifdef DES_ENCRYPTION
  538.     { "DES3_OFB64",
  539.           ENCTYPE_DES3_OFB64,
  540.           des3_ofb64_encrypt,
  541.           des3_ofb64_decrypt,
  542.           des3_ofb64_init,
  543.           des3_ofb64_start,
  544.           des3_ofb64_is,
  545.           des3_ofb64_reply,
  546.           des3_ofb64_session,
  547.           des3_ofb64_keyid,
  548.           NULL },
  549. #endif /* DES_ENCRYPTION */
  550. #ifdef CAST_ENCRYPTION
  551. #ifndef CAST_EXPORT_ENCRYPTION
  552.     { "CAST128_OFB64",  ENCTYPE_CAST128_OFB64,
  553.           cast_ofb64_encrypt,
  554.           cast_ofb64_decrypt,
  555.           cast_ofb64_init,
  556.           cast_ofb64_start,
  557.           cast_ofb64_is,
  558.           cast_ofb64_reply,
  559.           cast_ofb64_session,
  560.           cast_ofb64_keyid,
  561.           NULL },
  562. #endif
  563. #endif
  564. #ifdef DES_ENCRYPTION
  565.     { "DES_OFB64",
  566.           ENCTYPE_DES_OFB64,
  567.           ofb64_encrypt,
  568.           ofb64_decrypt,
  569.           ofb64_init,
  570.           ofb64_start,
  571.           ofb64_is,
  572.           ofb64_reply,
  573.           ofb64_session,
  574.           ofb64_keyid,
  575.           NULL },
  576. #endif  /* DES_ENCRYPTION */
  577. #if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
  578.     { "CAST5_40_OFB64", ENCTYPE_CAST5_40_OFB64,
  579.           castexp_ofb64_encrypt,
  580.           castexp_ofb64_decrypt,
  581.           castexp_ofb64_init,
  582.           castexp_ofb64_start,
  583.           castexp_ofb64_is,
  584.           castexp_ofb64_reply,
  585.           castexp_ofb64_session,
  586.           castexp_ofb64_keyid,
  587.           NULL },
  588. #endif /* CAST_ENCRYPTION */
  589.     { 0,0,0,0,0,0,0,0,0,0,0  }
  590. };
  591.  
  592. int
  593. get_crypt_table( struct keytab ** pTable, int * pN )
  594. {
  595.     int i=0,n=0;
  596.  
  597.     if ( *pTable )
  598.     {
  599.         for ( i=0 ; i < *pN ; i++ )
  600.             free( (*pTable)[i].kwd ) ;
  601.         free ( *pTable )  ;
  602.     }
  603.     *pTable = NULL;
  604.     *pN = 0;
  605.  
  606.     /* How many encryption types do we have? */
  607.     while ( encryptions[n].name )
  608.         n++;
  609.  
  610.     if ( n )
  611.     {
  612.         *pTable = malloc( sizeof(struct keytab) * (n+2) ) ;
  613.         if ( !(*pTable) )
  614.             return(0);
  615.  
  616. #ifdef OS2
  617.         (*pTable)[0].kwd =strdup("automatic");
  618. #else /* OS2 */
  619.         makestr(&tmpstring,"automatic");
  620.         (*pTable)[0].kwd = tmpstring;
  621.         tmpstring = NULL;
  622. #endif /* OS2 */
  623.         (*pTable)[0].kwval = ENCTYPE_ANY;
  624.         (*pTable)[0].flgs = 0;
  625. #ifdef OS2
  626.         (*pTable)[1].kwd =strdup("none");
  627. #else /* OS2 */
  628.         makestr(&tmpstring,"none");
  629.         (*pTable)[1].kwd = tmpstring;
  630.         tmpstring = NULL;
  631. #endif /* OS2 */
  632.         (*pTable)[1].kwval = 999;
  633.         (*pTable)[1].flgs = 0;
  634.         (*pN) = 2;
  635.  
  636.         for ( i=0 ; i < n ; i++ ) {
  637.             char * newstr = NULL, * p;
  638.             int newval = encryptions[i].type;
  639.             int j = 0, len = 0;
  640.  
  641. #ifdef OS2
  642.             newstr = strdup(encryptions[i].name);
  643.             strlwr(newstr);
  644. #else /* OS2 */
  645.             makestr(&tmpstring,encryptions[i].name);
  646.             newstr = tmpstring;
  647.             tmpstring = NULL;
  648.             for (p = newstr; *p; p++) if (isupper(*p)) *p = tolower(*p);
  649. #endif /* OS2 */
  650.  
  651.             for (j = 0; j < (*pN); j++) {
  652.                 int tempval = 0;
  653.                 char * tempstr = NULL;
  654.  
  655.                 if ( strcmp( (*pTable)[j].kwd, newstr ) > 0 )
  656.                 {
  657.                     tempval = (*pTable)[j].kwval;
  658.                     tempstr = (*pTable)[j].kwd;
  659.                     (*pTable)[j].kwd = newstr ;
  660.                     (*pTable)[j].kwval = newval;
  661.                     newval = tempval;
  662.                     newstr = tempstr;
  663.                     (*pTable)[j].flgs = 0;
  664.                 }
  665.             }
  666.             (*pTable)[*pN].kwd = newstr ;
  667.             (*pTable)[*pN].kwval = newval;
  668.             (*pTable)[*pN].flgs = 0 ;
  669.             (*pN)++ ;
  670.         }
  671.     } else {
  672.         *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  673.         if ( !(*pTable) )
  674.             return(0);
  675.  
  676. #ifdef OS2
  677.         (*pTable)[0].kwd =strdup("automatic");
  678. #else /* OS2 */
  679.         makestr(&tmpstring,"automatic");
  680.         (*pTable)[0].kwd = tmpstring;
  681.         tmpstring = NULL;
  682. #endif /* OS2 */
  683.         (*pTable)[0].kwval = ENCTYPE_ANY;
  684.         (*pTable)[0].flgs = 0;
  685. #ifdef OS2
  686.         (*pTable)[1].kwd =strdup("none");
  687. #else /* OS2 */
  688.         makestr(&tmpstring,"none");
  689.         (*pTable)[1].kwd = tmpstring;
  690.         tmpstring = NULL;
  691. #endif /* OS2 */
  692.         (*pTable)[1].kwval = 999;
  693.         (*pTable)[1].flgs = 0;
  694.         (*pN) = 2;
  695.     }
  696.     return(*pN);
  697. }
  698.  
  699. static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPTION,
  700.                                       ENCRYPT_SUPPORT };
  701. static unsigned char str_suplen = 0;
  702. static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPTION };
  703. static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPTION, 0, IAC, SE };
  704.  
  705. _PROTOTYP(int  encrypt_request_end, (VOID));
  706. _PROTOTYP(int  encrypt_request_start, (VOID));
  707. _PROTOTYP(int  encrypt_enc_keyid, (unsigned char *, int));
  708. _PROTOTYP(int  encrypt_dec_keyid, (unsigned char *, int));
  709. _PROTOTYP(int  encrypt_support, (unsigned char *, int));
  710. _PROTOTYP(int  encrypt_start, (unsigned char *, int));
  711. _PROTOTYP(int  encrypt_end, (VOID));
  712.  
  713. _PROTOTYP(int encrypt_ks_stream,(struct kstream_data_block *, /* output */
  714.                                    struct kstream_data_block *)); /* input */
  715.  
  716. _PROTOTYP(int decrypt_ks_stream,(struct kstream_data_block *, /* output */
  717.                                    struct kstream_data_block *)); /* input */
  718.  
  719. int
  720. #ifdef CK_ANSIC
  721. encrypt_ks_stream(struct kstream_data_block *i,
  722.                   struct kstream_data_block *o)
  723. #else
  724. encrypt_ks_stream(i,o)
  725.     struct kstream_data_block *i; struct kstream_data_block *o;
  726. #endif
  727. {
  728.     /*
  729.     * this is really quite bogus, since it does an in-place encryption...
  730.     */
  731.     if (encrypt_output) {
  732.         encrypt_output(i->ptr, i->length);
  733.         return 1;
  734.     }
  735.     return 0;
  736. }
  737.  
  738.  
  739. int
  740. #ifdef CK_ANSIC
  741. decrypt_ks_stream(struct kstream_data_block *i,
  742.                   struct kstream_data_block *o)
  743. #else
  744. decrypt_ks_stream(i,o)
  745.     struct kstream_data_block *i; struct kstream_data_block *o;
  746. #endif
  747. {
  748.     unsigned int len;
  749.   /*
  750.    * this is really quite bogus, since it does an in-place decryption...
  751.    */
  752.     if (decrypt_input) {
  753.         for (len = 0 ; len < i->length ; len++)
  754.             ((unsigned char *)i->ptr)[len]
  755.                 = decrypt_input(((unsigned char *)i->ptr)[len]);
  756.         return 1;
  757.     }
  758.     return 0;
  759. }
  760.  
  761. int
  762. #ifdef CK_ANSIC
  763. decrypt_ks_hack(unsigned char *buf, int cnt)
  764. #else
  765. decrypt_ks_hack(buf,cnt) unsigned char *buf; int cnt;
  766. #endif
  767. {
  768.     int len;
  769.   /*
  770.    * this is really quite bogus, since it does an in-place decryption...
  771.    */
  772.     for (len = 0 ; len < cnt ; len++)
  773.         buf[len] = decrypt_input(buf[len]);
  774.  
  775. #ifdef DEBUG
  776.     hexdump("decrypt ks hack", buf, cnt);
  777. #endif
  778.     return 1;
  779. }
  780.  
  781.  
  782. /*
  783.  * parsedat[0] == the suboption we might be negotiating,
  784.  */
  785. int
  786. #ifdef CK_ANSIC
  787. encrypt_parse(unsigned char *parsedat, int end_sub)
  788. #else
  789. encrypt_parse(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  790. #endif
  791. {
  792.     int rc = 0;
  793.  
  794.     switch(parsedat[1]) {
  795.     case ENCRYPT_START:
  796.         rc = encrypt_start(parsedat + 2, end_sub - 2);
  797.         break;
  798.     case ENCRYPT_END:
  799.         rc = encrypt_end();
  800.         break;
  801.     case ENCRYPT_SUPPORT:
  802.         rc = encrypt_support(parsedat + 2, end_sub - 2);
  803.         break;
  804.     case ENCRYPT_REQSTART:
  805.         rc = encrypt_request_start();
  806.         break;
  807.     case ENCRYPT_REQEND:
  808.         /*
  809.         * We can always send an REQEND so that we cannot
  810.         * get stuck encrypting.  We should only get this
  811.         * if we have been able to get in the correct mode
  812.         * anyhow.
  813.         */
  814.         rc = encrypt_request_end();
  815.         break;
  816.     case ENCRYPT_IS:
  817.         rc = encrypt_is(parsedat + 2, end_sub - 2);
  818.         break;
  819.     case ENCRYPT_REPLY:
  820.         rc = encrypt_reply(parsedat + 2, end_sub - 2);
  821.         break;
  822.     case ENCRYPT_ENC_KEYID:
  823.         rc = encrypt_enc_keyid(parsedat + 2, end_sub - 2);
  824.         break;
  825.     case ENCRYPT_DEC_KEYID:
  826.         rc = encrypt_dec_keyid(parsedat + 2, end_sub - 2);
  827.         break;
  828.     default:
  829.         rc = -1;
  830.         break;
  831.     }
  832.     return(rc);
  833. }
  834.  
  835. /* XXX */
  836. Encryptions *
  837. #ifdef CK_ANSIC
  838. findencryption(int type)
  839. #else
  840. findencryption(type) int type;
  841. #endif
  842. {
  843.     Encryptions *ep = encryptions;
  844.  
  845.     if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
  846.         return(0);
  847.     while (ep->type && ep->type != type)
  848.         ++ep;
  849.     return(ep->type ? ep : 0);
  850. }
  851.  
  852. Encryptions *
  853. #ifdef CK_ANSIC
  854. finddecryption(int type)
  855. #else
  856. finddecryption(type) int type;
  857. #endif
  858. {
  859.     Encryptions *ep = encryptions;
  860.  
  861.     if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
  862.         return(0);
  863.     while (ep->type && ep->type != type)
  864.         ++ep;
  865.     return(ep->type ? ep : 0);
  866. }
  867.  
  868. #define MAXKEYLEN 64
  869.  
  870. static struct key_info {
  871.     unsigned char keyid[MAXKEYLEN];
  872.     int keylen;
  873.     int dir;
  874.     int *modep;
  875.     Encryptions *(*getcrypt)();
  876. } ki[2] = {
  877.     { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
  878.     { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
  879. };
  880.  
  881. VOID
  882. #ifdef CK_ANSIC
  883. encrypt_init(kstream iks, int type)
  884. #else
  885. encrypt_init(iks, type) kstream iks; int type;
  886. #endif
  887. {
  888.     Encryptions *ep = encryptions;
  889.  
  890.     i_support_encrypt = i_support_decrypt = 0;
  891.     remote_supports_encrypt = remote_supports_decrypt = 0;
  892.     i_wont_support_encrypt = i_wont_support_decrypt = 0;
  893.     encrypt_mode = 0;
  894.     decrypt_mode = 0;
  895.     encrypt_output = NULL;
  896.     decrypt_input = NULL;
  897.     ki[0].keylen = 0;
  898.     memset(ki[0].keyid,0,MAXKEYLEN);
  899.     ki[1].keylen = 0;
  900.     memset(ki[1].keyid,0,MAXKEYLEN);
  901.     havesessionkey = 0;
  902.     autoencrypt = 1;
  903.     autodecrypt = 1;
  904.  
  905.     EncryptKSGlobalHack = iks;
  906.     EncryptType = type;
  907.  
  908.     str_send[0] = IAC;
  909.     str_send[1] = SB;
  910.     str_send[2] = TELOPT_ENCRYPTION;
  911.     str_send[3] = ENCRYPT_SUPPORT;
  912.     str_suplen = 4;
  913.  
  914.     while (ep->type) {
  915.         if ( EncryptType == ENCTYPE_ANY ||
  916.              EncryptType == ep->type ) {
  917. #ifdef DEBUG
  918.             if (encrypt_debug_mode) {
  919.                 sprintf(dbgbuf, ">>>I will support %s\n",
  920.                          ENCTYPE_NAME(ep->type));       /* safe */
  921.                 debug(F110,"encrypt_init",dbgbuf,0);
  922.             }
  923. #endif
  924.             i_support_encrypt |= typemask(ep->type);
  925.             i_support_decrypt |= typemask(ep->type);
  926.             if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
  927.                 if ((str_send[str_suplen++] = ep->type) == IAC)
  928.                     str_send[str_suplen++] = IAC;
  929.         }
  930.         if (ep->init)
  931.             (*ep->init)(0);
  932.         ++ep;
  933.     }
  934.     str_send[str_suplen++] = IAC;
  935.     str_send[str_suplen++] = SE;
  936. }
  937.  
  938. VOID
  939. #ifdef CK_ANSIC
  940. encrypt_send_support(VOID)
  941. #else
  942. encrypt_send_support()
  943. #endif
  944. {
  945.     Encryptions *ep = encryptions;
  946.  
  947. #ifdef CK_SSL
  948.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  949.         return;
  950. #endif /* CK_SSL */
  951.  
  952.     str_send[0] = IAC;
  953.     str_send[1] = SB;
  954.     str_send[2] = TELOPT_ENCRYPTION;
  955.     str_send[3] = ENCRYPT_SUPPORT;
  956.     str_suplen = 4;
  957.  
  958.     while (ep->type) {
  959.         if ( EncryptType == ENCTYPE_ANY ||
  960.              EncryptType == ep->type ) {
  961. #ifdef DEBUG
  962.             if (encrypt_debug_mode) {
  963.                 sprintf(dbgbuf, ">>>I will support %s\n",
  964.                          ENCTYPE_NAME(ep->type));               /* safe */
  965.                 debug(F110,"encrypt_send_support",dbgbuf,0);
  966.             }
  967. #endif
  968.             if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
  969.                 if ((str_send[str_suplen++] = ep->type) == IAC)
  970.                     str_send[str_suplen++] = IAC;
  971.         }
  972.         ++ep;
  973.     }
  974.     str_send[str_suplen++] = IAC;
  975.     str_send[str_suplen++] = SE;
  976.  
  977.     /*
  978.     * If the user has requested that decryption start
  979.     * immediatly, then send a "REQUEST START" before
  980.     * we negotiate the type.
  981.     */
  982.     if (autodecrypt)
  983.         encrypt_send_request_start();
  984.  
  985.     if (deblog || tn_deb || debses) {
  986.         int i;
  987.         sprintf(tn_msg,"TELNET SENT SB %s SUPPORT ",
  988.                  TELOPT(TELOPT_ENCRYPTION));                    /* safe */
  989.         for ( i=4;i<str_suplen-2;i++ ) {
  990.             if ( str_send[i] == IAC ) {
  991.                 ckstrncat(tn_msg,"IAC ",TN_MSG_LEN);
  992.                 i++;
  993.             }
  994.             ckstrncat(tn_msg,ENCTYPE_NAME(str_send[i]),TN_MSG_LEN);
  995.             ckstrncat(tn_msg," ",TN_MSG_LEN);
  996.         }
  997.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  998.         debug(F100,tn_msg,"",0);
  999.         if (tn_deb || debses) tn_debug(tn_msg);
  1000.     }
  1001.     ttol(str_send, str_suplen);
  1002.  
  1003.     str_suplen = 0;
  1004. }
  1005.  
  1006. /*
  1007.  * Called when ENCRYPT SUPPORT is received.
  1008.  */
  1009. int
  1010. #ifdef CK_ANSIC
  1011. encrypt_support(unsigned char *_typelist, int _cnt)
  1012. #else
  1013. encrypt_support(_typelist, _cnt) unsigned char * _typelist; int _cnt;
  1014. #endif
  1015. {
  1016.     register int type, use_type = 0;
  1017.     unsigned char * typelist = _typelist;
  1018.     int cnt = _cnt;
  1019.     Encryptions *ep;
  1020.  
  1021.     debug(F111,"encrypt_support","cnt",cnt);
  1022.  
  1023.   /*
  1024.    * Forget anything the other side has previously told us.
  1025.    */
  1026.     remote_supports_decrypt = 0;
  1027.  
  1028.     while (cnt-- > 0) {
  1029.         type = *typelist++;
  1030.         if ( EncryptType == ENCTYPE_ANY ||
  1031.              EncryptType == type ) {
  1032. #ifdef DEBUG
  1033.             if (encrypt_debug_mode) {
  1034.                 sprintf(dbgbuf, ">>>Remote supports %s (%d)\n",
  1035.                          ENCTYPE_NAME(type), type);             /* safe */
  1036.                 debug(F110,"encrypt_support",dbgbuf,0);
  1037.             }
  1038. #endif
  1039.             if ((type < ENCTYPE_CNT) &&
  1040.                  (I_SUPPORT_ENCRYPT & typemask(type))) {
  1041.                 remote_supports_decrypt |= typemask(type);
  1042.                 if (use_type == 0)
  1043.                     use_type = type;
  1044.             }
  1045.         }
  1046.     }
  1047.     if (use_type) {
  1048.         ep = findencryption(use_type);
  1049.         if (!ep) {
  1050.             debug(F111,"encrypt_support","findencryption == NULL",use_type);
  1051.             return(-1);
  1052.         }
  1053.         type = ep->start ? (*ep->start)(DIR_ENCRYPT, 0) : 0;
  1054. #ifdef DEBUG
  1055.         if (encrypt_debug_mode) {
  1056.             sprintf(dbgbuf, ">>>(*ep->start)() %s returned %d (%s)\n",
  1057.                      ENCTYPE_NAME(use_type), type, 
  1058.                      ENCRYPT_NAME(type));                       /* safe */
  1059.             debug(F110,"encrypt_support",dbgbuf,0);
  1060.         }
  1061. #endif
  1062.         if (type < 0) {
  1063.             debug(F111,"encrypt_support","type < 0",type);
  1064.             return(-1);
  1065.         }
  1066.         encrypt_mode = use_type;
  1067.         if (type == 0)
  1068.             encrypt_start_output(use_type);
  1069.         debug(F111,"encrypt_support","success",type);
  1070.         return(0);
  1071.     }
  1072.     debug(F111,"encrypt_support","failed",use_type);
  1073.     return(-1);
  1074. }
  1075.  
  1076. int
  1077. #ifdef CK_ANSIC
  1078. encrypt_is(unsigned char *data, int cnt)
  1079. #else
  1080. encrypt_is(data, cnt) unsigned char *data; int cnt;
  1081. #endif /* CK_ANSIC */
  1082. {
  1083.     Encryptions *ep;
  1084.     register int type, ret;
  1085.  
  1086.     if (--cnt < 0)
  1087.         return(-1);
  1088.     type = *data++;
  1089.     if (type < ENCTYPE_CNT)
  1090.         remote_supports_encrypt |= typemask(type);
  1091.     if (!(ep = finddecryption(type))) {
  1092. #ifdef DEBUG
  1093.         if (encrypt_debug_mode) {
  1094.             sprintf(dbgbuf, ">>>encrypt_is:  "
  1095.                      "Can't find type %s (%d) for initial negotiation\n",
  1096.                      ENCTYPE_NAME_OK(type)
  1097.                      ? ENCTYPE_NAME(type) : "(unknown)",
  1098.                      type);                                     /* safe */
  1099.             debug(F110,"encrypt_is",dbgbuf,0);
  1100.         }
  1101. #endif
  1102.         return(-1);
  1103.     }
  1104.     if (!ep->is) {
  1105. #ifdef DEBUG
  1106.         if (encrypt_debug_mode) {
  1107.             sprintf(dbgbuf, ">>>encrypt_is:  "
  1108.                      "No initial negotiation needed for type %s (%d)\n",
  1109.                      ENCTYPE_NAME_OK(type)
  1110.                      ? ENCTYPE_NAME(type) : "(unknown)",
  1111.                      type);                                     /* safe */
  1112.             debug(F110,"encrypt_is",dbgbuf,0);
  1113.         }
  1114. #endif
  1115.         ret = 0;
  1116.     } else {
  1117.         ret = (*ep->is)(data, cnt);
  1118. #ifdef DEBUG
  1119.         if (encrypt_debug_mode) {
  1120.             sprintf(dbgbuf, "encrypt_is:  "
  1121.                      "(*ep->is)(%x, %d) returned %s(%d)\n", data, cnt,
  1122.                      (ret < 0) ? "FAIL " :
  1123.                      (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret); /* safe */
  1124.             debug(F110,"encrypt_is",dbgbuf,0);
  1125.         }
  1126. #endif
  1127.     }
  1128.     if (ret < 0) {
  1129.         autodecrypt = 0;
  1130.         return(-1);
  1131.     } else {
  1132.         decrypt_mode = type;
  1133.         if (ret == 0 && autodecrypt) {
  1134.             encrypt_send_request_start();
  1135.         }
  1136.     }
  1137.     return(0);
  1138. }
  1139.  
  1140. int
  1141. #ifdef CK_ANSIC
  1142. encrypt_reply(unsigned char *data, int cnt)
  1143. #else
  1144. encrypt_reply(data, cnt) unsigned char *data; int cnt;
  1145. #endif
  1146. {
  1147.     Encryptions *ep;
  1148.     register int ret, type;
  1149.  
  1150.     if (--cnt < 0)
  1151.         return(-1);
  1152.     type = *data++;
  1153.     if (!(ep = findencryption(type))) {
  1154. #ifdef DEBUG
  1155.         if (encrypt_debug_mode) {
  1156.             sprintf(dbgbuf,
  1157.                     ">>>Can't find type %s (%d) for initial negotiation\n",
  1158.                      ENCTYPE_NAME_OK(type)
  1159.                      ? ENCTYPE_NAME(type) : "(unknown)",
  1160.                      type);                                     /* safe */
  1161.             debug(F110,"encrypt_reply",dbgbuf,0);
  1162.         }
  1163. #endif
  1164.         return(-1);
  1165.     }
  1166.     if (!ep->reply) {
  1167. #ifdef DEBUG
  1168.         if (encrypt_debug_mode) {
  1169.       sprintf(dbgbuf, ">>>No initial negotiation needed for type %s (%d)\n",
  1170.                ENCTYPE_NAME_OK(type)
  1171.                ? ENCTYPE_NAME(type) : "(unknown)",
  1172.                type);                                           /* safe */
  1173.             debug(F110,"encrypt_reply",dbgbuf,0);
  1174.         }
  1175. #endif
  1176.         ret = 0;
  1177.     } else {
  1178.         ret = (*ep->reply)(data, cnt);
  1179. #ifdef DEBUG
  1180.         if (encrypt_debug_mode) {
  1181.             sprintf(dbgbuf, "(*ep->reply)(%x, %d) returned %s(%d)\n",
  1182.                      data, cnt,
  1183.                      (ret < 0) ? "FAIL " :
  1184.                      (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret); /* safe */
  1185.             debug(F110,"encrypt_reply",dbgbuf,0);
  1186.         }
  1187. #endif
  1188.     }
  1189. #ifdef DEBUG
  1190.     if (encrypt_debug_mode) {
  1191.         sprintf(dbgbuf, ">>>encrypt_reply returned %d\n", ret); /* safe */
  1192.         debug(F110,"encrypt_reply",dbgbuf,0);
  1193.     }
  1194. #endif
  1195.     if (ret < 0) {
  1196.         autoencrypt = 0;
  1197.         return(-1);
  1198.     } else {
  1199.         encrypt_mode = type;
  1200.         if (ret == 0 && autoencrypt)
  1201.             encrypt_start_output(type);
  1202.     }
  1203.     return(0);
  1204. }
  1205.  
  1206. /*
  1207.  * Called when a ENCRYPT START command is received.
  1208.  */
  1209. int
  1210. #ifdef CK_ANSIC
  1211. encrypt_start(unsigned char *data, int cnt)
  1212. #else
  1213. encrypt_start(data, cnt) unsigned char *data; int cnt;
  1214. #endif
  1215. {
  1216.     Encryptions *ep;
  1217.  
  1218.     if (!decrypt_mode) {
  1219.         /*
  1220.         * Something is wrong.  We should not get a START
  1221.         * command without having already picked our
  1222.         * decryption scheme.  Send a REQUEST-END to
  1223.         * attempt to clear the channel...
  1224.         */
  1225.         encrypt_send_request_end();
  1226.         printf("Kerberos authentication error!\n%s\n",
  1227.                 "Warning, Cannot decrypt input stream!!!");
  1228.         return(-1);
  1229.     }
  1230.  
  1231.     if (ep = finddecryption(decrypt_mode)) {
  1232.         if ( decrypt_input != ep->input ) {
  1233.             decrypt_input = ep->input;
  1234.             EncryptKSGlobalHack->decrypt = decrypt_ks_stream;
  1235.             EncryptKSGlobalHack->decrypt_type = ep->type;
  1236.  
  1237.             if (encrypt_verbose) {
  1238.                 sprintf(dbgbuf, "Input is now decrypted with type %s",
  1239.                          ENCTYPE_NAME(decrypt_mode));           /* safe */
  1240.                 debug(F110,"encrypt_start",dbgbuf,0);
  1241.                 printf("%s\n",dbgbuf);
  1242.             }
  1243. #ifdef DEBUG
  1244.             if (encrypt_debug_mode) {
  1245.                 sprintf(dbgbuf, ">>>Start to decrypt input with type %s",
  1246.                          ENCTYPE_NAME(decrypt_mode));           /* safe */
  1247.                 debug(F110,"ck_crp",dbgbuf,0);
  1248.             }
  1249. #endif
  1250.         }
  1251.     } else {
  1252.         char buf[1024];
  1253.         sprintf(buf, "Warning, Cannot decrypt type %s (%d)!!!",
  1254.                   ENCTYPE_NAME_OK(decrypt_mode)
  1255.                   ? ENCTYPE_NAME(decrypt_mode) : "(unknown)",
  1256.                   decrypt_mode);                                /* safe */
  1257.         printf("Kerberos authentication error!\n%s\n",buf);
  1258.         encrypt_send_request_end();
  1259.         return(-1);
  1260.     }
  1261.     return(0);
  1262. }
  1263.  
  1264. int
  1265. #ifdef CK_ANSIC
  1266. encrypt_session_key(Session_Key *key, int server)
  1267. #else
  1268. encrypt_session_key(key, server) Session_Key *key; int server;
  1269. #endif
  1270. {
  1271.     Encryptions *ep = encryptions;
  1272.  
  1273.     if (havesessionkey)
  1274.         return(0);
  1275.  
  1276.     havesessionkey = 1;
  1277.  
  1278.     while (ep->type) {
  1279.         debug(F111,"encrypt_session_key",ep->name,ep->type);
  1280.         if (ep->session) {
  1281.             if ((*ep->session)(key, server) < 0) {
  1282.                 i_wont_support_encrypt |= typemask(ep->type);
  1283.                 i_wont_support_decrypt |= typemask(ep->type);
  1284.             }
  1285.         }
  1286.         ++ep;
  1287.     }
  1288.     debug(F111,"encrypt_session_key (done)",ep->name,ep->type);
  1289.     return(0);
  1290. }
  1291.  
  1292. /*
  1293.  * Called when ENCRYPT END is received.
  1294.  */
  1295. int
  1296. #ifdef CK_ANSIC
  1297. encrypt_end(VOID)
  1298. #else
  1299. encrypt_end()
  1300. #endif
  1301. {
  1302.     decrypt_input = NULL;
  1303.     EncryptKSGlobalHack->decrypt = NULL;
  1304.     EncryptKSGlobalHack->decrypt_type = ENCTYPE_ANY;
  1305. #ifdef DEBUG
  1306.     if (encrypt_debug_mode) {
  1307.         sprintf(dbgbuf, ">>>Input is back to clear text");      /* safe */
  1308.         debug(F110,"encrypt_end",dbgbuf,0);
  1309.     }
  1310. #endif
  1311.     if (encrypt_verbose) {
  1312.         sprintf(dbgbuf, "Input is now clear text");             /* safe */
  1313.         debug(F110,"encrypt_end",dbgbuf,0);
  1314.         printf("%s\n",dbgbuf);
  1315.     }
  1316.     return(0);
  1317. }
  1318.  
  1319. /*
  1320.  * Called when ENCRYPT REQUEST-END is received.
  1321.  */
  1322. int
  1323. #ifdef CK_ANSIC
  1324. encrypt_request_end(VOID)
  1325. #else
  1326. encrypt_request_end()
  1327. #endif
  1328. {
  1329.     encrypt_send_end();
  1330.     return(0);
  1331. }
  1332.  
  1333. /*
  1334.  * Called when ENCRYPT REQUEST-START is received.  If we receive
  1335.  * this before a type is picked, then that indicates that the
  1336.  * other side wants us to start encrypting data as soon as we
  1337.  * can.
  1338.  */
  1339. int
  1340. #ifdef CK_ANSIC
  1341. encrypt_request_start(VOID)
  1342. #else
  1343. encrypt_request_start()
  1344. #endif
  1345. {
  1346.     if (encrypt_mode != 0)
  1347.         encrypt_start_output(encrypt_mode);
  1348.     return(0);
  1349. }
  1350.  
  1351. static unsigned char str_keyid[(MAXKEYLEN*2)+5] = {
  1352.     IAC, SB, TELOPT_ENCRYPTION
  1353. };
  1354. _PROTOTYP(int encrypt_keyid,(struct key_info *,unsigned char *,int));
  1355.  
  1356. int
  1357. #ifdef CK_ANSIC
  1358. encrypt_enc_keyid(unsigned char *keyid, int len)
  1359. #else
  1360. encrypt_enc_keyid(keyid, len) unsigned char *keyid; int len;
  1361. #endif
  1362. {
  1363.     return(encrypt_keyid(&ki[1], keyid, len));
  1364. }
  1365.  
  1366. int
  1367. #ifdef CK_ANSIC
  1368. encrypt_dec_keyid(unsigned char *keyid, int len)
  1369. #else
  1370. encrypt_dec_keyid(keyid, len) unsigned char *keyid; int len;
  1371. #endif /* CK_ANSIC */
  1372. {
  1373.     return(encrypt_keyid(&ki[0], keyid, len));
  1374. }
  1375.  
  1376. int
  1377. #ifdef CK_ANSIC
  1378. encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
  1379. #else
  1380. encrypt_keyid(kp, keyid, len)
  1381.     struct key_info *kp; unsigned char *keyid; int len;
  1382. #endif
  1383. {
  1384.     Encryptions *ep;
  1385.     int dir = kp->dir;
  1386.     register int ret = 0;
  1387.  
  1388.     if (!(ep = (*kp->getcrypt)(*kp->modep))) {
  1389.         if (len == 0)
  1390.             return(-1);
  1391.         kp->keylen = 0;
  1392.     } else if (len == 0 || len > MAXKEYLEN) {
  1393.         /*
  1394.         * Empty option or Key too long, indicates a failure.
  1395.         */
  1396.         if (kp->keylen == 0)
  1397.             return(-1);
  1398.         kp->keylen = 0;
  1399.         if (ep->keyid)
  1400.             (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1401.  
  1402.     } else if ((len != kp->keylen) || (memcmp(keyid, kp->keyid, len) != 0)) {
  1403.         /*
  1404.         * Length or contents are different
  1405.         */
  1406.         kp->keylen = len;
  1407.         memcpy(kp->keyid, keyid, len);          /* length < MAXKEYLEN */
  1408.         if (ep->keyid)
  1409.             (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1410.     } else {
  1411.         if (ep->keyid)
  1412.             ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1413.         if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
  1414.             encrypt_start_output(*kp->modep);
  1415.         return(0);
  1416.     }
  1417.  
  1418.     encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
  1419.     return(0);
  1420. }
  1421.  
  1422. int
  1423. #ifdef CK_ANSIC
  1424. encrypt_send_keyid(int dir, unsigned char *keyid, int keylen, int saveit)
  1425. #else
  1426. encrypt_send_keyid(dir, keyid, keylen, saveit)
  1427.      int dir; unsigned char *keyid; int keylen; int saveit;
  1428. #endif
  1429. {
  1430.     unsigned char *strp;
  1431.  
  1432. #ifdef CK_SSL
  1433.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1434.         return(0);
  1435. #endif /* CK_SSL */
  1436.  
  1437.     str_keyid[3] = (dir == DIR_ENCRYPT)
  1438.         ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
  1439.     if (saveit && keylen <= MAXKEYLEN) {
  1440.         struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
  1441.         memcpy(kp->keyid, keyid, keylen);
  1442.         kp->keylen = keylen;
  1443.     }
  1444.  
  1445.     for (strp = &str_keyid[4]; keylen > 0; --keylen) {
  1446.         if ((*strp++ = *keyid++) == IAC)
  1447.             *strp++ = IAC;
  1448.     }
  1449.     *strp++ = IAC;
  1450.     *strp++ = SE;
  1451.  
  1452.     if (deblog || tn_deb || debses) {
  1453.         int i;
  1454.         sprintf(tn_msg,"TELNET SENT SB %s %s ",
  1455.                  TELOPT(TELOPT_ENCRYPTION),
  1456.                  (dir == DIR_ENCRYPT) ? "ENC-KEYID" : "DEC-KEYID"); /* safe */
  1457.         tn_hex(tn_msg,TN_MSG_LEN,&str_keyid[4],strp-str_keyid-2-4);
  1458.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1459.         debug(F100,tn_msg,"",0);
  1460.         if (tn_deb || debses) tn_debug(tn_msg);
  1461.     }
  1462.     ttol(str_keyid, strp - str_keyid);
  1463.     return(0);
  1464. }
  1465.  
  1466. VOID
  1467. #ifdef CK_ANSIC
  1468. encrypt_auto(int on)
  1469. #else
  1470. encrypt_auto(on) int on;
  1471. #endif
  1472. {
  1473.     if (on < 0)
  1474.         autoencrypt ^= 1;
  1475.     else
  1476.         autoencrypt = on ? 1 : 0;
  1477. }
  1478.  
  1479. VOID
  1480. #ifdef CK_ANSIC
  1481. decrypt_auto(int on)
  1482. #else
  1483. decrypt_auto(on) int on;
  1484. #endif
  1485. {
  1486.     if (on < 0)
  1487.         autodecrypt ^= 1;
  1488.     else
  1489.         autodecrypt = on ? 1 : 0;
  1490. }
  1491.  
  1492. VOID
  1493. #ifdef CK_ANSIC
  1494. encrypt_start_output(int type)
  1495. #else
  1496. encrypt_start_output(type) int type;
  1497. #endif
  1498. {
  1499.     Encryptions *ep;
  1500.     register unsigned char *p;
  1501.     register int i;
  1502.  
  1503. #ifdef CK_SSL
  1504.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1505.         return;
  1506. #endif /* CK_SSL */
  1507.  
  1508.     if (!(ep = findencryption(type))) {
  1509. #ifdef DEBUG
  1510.         if (encrypt_debug_mode) {
  1511.             sprintf(dbgbuf, ">>>Can't encrypt with type %s (%d)\n",
  1512.                      ENCTYPE_NAME_OK(type)
  1513.                      ? ENCTYPE_NAME(type) : "(unknown)",
  1514.                      type);                                     /* safe */
  1515.             debug(F110,"encrypt_start_output",dbgbuf,0);
  1516.         }
  1517. #endif
  1518.         return;
  1519.     }
  1520.     if (ep->start) {
  1521.         i = (*ep->start)(DIR_ENCRYPT, 0);
  1522. #ifdef DEBUG
  1523.         if (encrypt_debug_mode) {
  1524.             sprintf(dbgbuf, ">>>Encrypt start: %s (%d) %s\n",
  1525.                      (i < 0) ? "failed" :
  1526.                      "initial negotiation in progress",
  1527.                      i, ENCTYPE_NAME(type));                    /* safe */
  1528.             debug(F110,"encrypt_start_output",dbgbuf,0);
  1529.         }
  1530. #endif
  1531.         if (i)
  1532.             return;
  1533.     }
  1534.  
  1535.     if ( encrypt_output != ep->output ) {
  1536.         p = str_start;
  1537.         *p++ = IAC;
  1538.         *p++ = SB;
  1539.         *p++ = TELOPT_ENCRYPTION;
  1540.         *p++ = ENCRYPT_START;
  1541.         for (i = 0; i < ki[0].keylen; ++i) {
  1542.             if (( *p++ = ki[0].keyid[i]) == IAC)
  1543.                 *p++ = IAC;
  1544.         }
  1545.         *p++ = IAC;
  1546.         *p++ = SE;
  1547.  
  1548.         if (deblog || tn_deb || debses) {
  1549.             int i;
  1550.             sprintf(tn_msg,"TELNET SENT SB %s START ",
  1551.                      TELOPT(TELOPT_ENCRYPTION));                /* safe */
  1552.             tn_hex(tn_msg,TN_MSG_LEN,&str_start[4],p-str_start-2-4);
  1553.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1554.             debug(F100,tn_msg,"",0);
  1555.             if (tn_deb || debses) tn_debug(tn_msg);
  1556.         }
  1557.         ttol(str_start, p - str_start);
  1558.  
  1559.   /*
  1560.    * If we are already encrypting in some mode, then
  1561.    * encrypt the ring (which includes our request) in
  1562.    * the old mode, mark it all as "clear text" and then
  1563.    * switch to the new mode.
  1564.    */
  1565.         encrypt_output = ep->output;
  1566.         EncryptKSGlobalHack->encrypt = encrypt_ks_stream;
  1567.         EncryptKSGlobalHack->encrypt_type = type;
  1568.         encrypt_mode = type;
  1569. #ifdef DEBUG
  1570.         if (encrypt_debug_mode) {
  1571.             sprintf(dbgbuf, ">>>Started to encrypt output with type %s",
  1572.                      ENCTYPE_NAME(type));                       /* safe */
  1573.             debug(F110,"encrypt_start_output",dbgbuf,0);
  1574.         }
  1575. #endif
  1576.         if (encrypt_verbose) {
  1577.             sprintf(dbgbuf, "Output is now encrypted with type %s",
  1578.                      ENCTYPE_NAME(type));                       /* safe */
  1579.             debug(F110,"encrypt_start_output",dbgbuf,0);
  1580.             printf("%s\n",dbgbuf);
  1581.         }
  1582.     }
  1583. }
  1584.  
  1585. VOID
  1586. #ifdef CK_ANSIC
  1587. encrypt_send_end(VOID)
  1588. #else
  1589. encrypt_send_end()
  1590. #endif
  1591. {
  1592. #ifdef CK_SSL
  1593.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1594.         return;
  1595. #endif /* CK_SSL */
  1596.  
  1597.     if (!encrypt_output)
  1598.         return;
  1599.  
  1600.     str_end[0] = IAC;
  1601.     str_end[1] = SB;
  1602.     str_end[2] = TELOPT_ENCRYPTION;
  1603.     str_end[3] = ENCRYPT_END;
  1604.     str_end[4] = IAC;
  1605.     str_end[5] = SE;
  1606.  
  1607.     if (deblog || tn_deb || debses) {
  1608.         int i;
  1609.         sprintf(tn_msg,"TELNET SENT SB %s END IAC SE",
  1610.                  TELOPT(TELOPT_ENCRYPTION));                    /* safe */
  1611.         debug(F100,tn_msg,"",0);
  1612.         if (tn_deb || debses) tn_debug(tn_msg);
  1613.     }
  1614.     ttol(str_end, sizeof(str_end));
  1615.  
  1616.     encrypt_output = 0;
  1617.     EncryptKSGlobalHack->encrypt = NULL;
  1618.     EncryptKSGlobalHack->encrypt_type = ENCTYPE_ANY;
  1619. #ifdef DEBUG
  1620.     if (encrypt_debug_mode) {
  1621.         sprintf(dbgbuf, ">>>Output is back to clear text");     /* safe */
  1622.         debug(F110,"encrypt_send_end",dbgbuf,0);
  1623.     }
  1624. #endif
  1625.     if (encrypt_verbose) {
  1626.         sprintf(dbgbuf, "Output is now clear text");            /* safe */
  1627.         debug(F110,"encrypt_send_end",dbgbuf,0);
  1628.         printf("%s\n",dbgbuf);
  1629.     }
  1630. }
  1631.  
  1632. VOID
  1633. #ifdef CK_ANSIC
  1634. encrypt_send_request_start(VOID)
  1635. #else
  1636. encrypt_send_request_start()
  1637. #endif
  1638. {
  1639.     register unsigned char *p;
  1640.     register int i;
  1641.  
  1642. #ifdef CK_SSL
  1643.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1644.         return;
  1645. #endif /* CK_SSL */
  1646.  
  1647.     p = str_start;
  1648.     *p++ = IAC;
  1649.     *p++ = SB;
  1650.     *p++ = TELOPT_ENCRYPTION;
  1651.     *p++ = ENCRYPT_REQSTART;
  1652.     for (i = 0; i < ki[1].keylen; ++i) {
  1653.         if (( *p++ = ki[1].keyid[i]) == IAC)
  1654.             *p++ = IAC;
  1655.     }
  1656.     *p++ = IAC;
  1657.     *p++ = SE;
  1658.  
  1659.     if (deblog || tn_deb || debses) {
  1660.         int i;
  1661.         sprintf(tn_msg,"TELNET SENT SB %s REQUEST-START ",
  1662.                  TELOPT(TELOPT_ENCRYPTION));                    /* safe */
  1663.         tn_hex(tn_msg,TN_MSG_LEN,&str_start[4],p-str_start-2-4);
  1664.         ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1665.         debug(F100,tn_msg,"",0);
  1666.         if (tn_deb || debses) tn_debug(tn_msg);
  1667.     }
  1668.     ttol(str_start, p - str_start);
  1669.  
  1670.     if (encrypt_debug_mode) {
  1671.         sprintf(dbgbuf, ">>>Request input to be encrypted\n");  /* safe */
  1672.         debug(F110,"encrypt_send_request_start",dbgbuf,0);
  1673.     }
  1674. }
  1675.  
  1676. VOID
  1677. #ifdef CK_ANSIC
  1678. encrypt_send_request_end(VOID)
  1679. #else
  1680. encrypt_send_request_end()
  1681. #endif
  1682. {
  1683. #ifdef CK_SSL
  1684.     if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1685.         return;
  1686. #endif /* CK_SSL */
  1687.  
  1688.     str_end[0] = IAC;
  1689.     str_end[1] = SB;
  1690.     str_end[2] = TELOPT_ENCRYPTION;
  1691.     str_end[3] = ENCRYPT_REQEND;
  1692.     str_end[4] = IAC;
  1693.     str_end[5] = SE;
  1694.  
  1695.     if (deblog || tn_deb || debses) {
  1696.         int i;
  1697.         sprintf(tn_msg,"TELNET SENT SB %s REQEND IAC SE",
  1698.                  TELOPT(TELOPT_ENCRYPTION));                    /* safe */
  1699.         debug(F100,tn_msg,"",0);
  1700.         if (tn_deb || debses) tn_debug(tn_msg);
  1701.     }
  1702.     ttol(str_end, sizeof(str_end));
  1703.  
  1704.     if (encrypt_debug_mode) {
  1705.         sprintf(dbgbuf, ">>>Request input to be clear text\n"); /* safe */
  1706.         debug(F110,"encrypt_send_request_end",dbgbuf,0);
  1707.     }
  1708. }
  1709.  
  1710. int
  1711. #ifdef CK_ANSIC
  1712. encrypt_is_encrypting(VOID)
  1713. #else
  1714. encrypt_is_encrypting()
  1715. #endif
  1716. {
  1717.     if (encrypt_output)
  1718.         return 1;
  1719.     return 0;
  1720. }
  1721.  
  1722. int
  1723. #ifdef CK_ANSIC
  1724. encrypt_is_decrypting(VOID)
  1725. #else
  1726. encrypt_is_decrypting()
  1727. #endif
  1728. {
  1729.     if (decrypt_input)
  1730.         return 1;
  1731.     return 0;
  1732. }
  1733.  
  1734. #ifdef DEBUG
  1735. void
  1736. encrypt_debug(mode)
  1737.      int mode;
  1738. {
  1739.     encrypt_debug_mode = mode;
  1740. }
  1741. #endif
  1742.  
  1743. #ifdef CK_DES
  1744. /*-
  1745.  * Copyright (c) 1991, 1993
  1746.  *      The Regents of the University of California.  All rights reserved.
  1747.  *
  1748.  * Redistribution and use in source and binary forms, with or without
  1749.  * modification, are permitted provided that the following conditions
  1750.  * are met:
  1751.  * 1. Redistributions of source code must retain the above copyright
  1752.  *    notice, this list of conditions and the following disclaimer.
  1753.  * 2. Redistributions in binary form must reproduce the above copyright
  1754.  *    notice, this list of conditions and the following disclaimer in the
  1755.  *    documentation and/or other materials provided with the distribution.
  1756.  * 3. All advertising materials mentioning features or use of this software
  1757.  *    must display the following acknowledgement:
  1758.  *      This product includes software developed by the University of
  1759.  *      California, Berkeley and its contributors.
  1760.  * 4. Neither the name of the University nor the names of its contributors
  1761.  *    may be used to endorse or promote products derived from this software
  1762.  *    without specific prior written permission.
  1763.  *
  1764.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1765.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1766.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1767.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1768.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1769.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1770.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1771.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1772.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1773.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1774.  * SUCH DAMAGE.
  1775.  */
  1776.  
  1777. /* based on @(#)enc_des.c       8.1 (Berkeley) 6/4/93 */
  1778.  
  1779. #define CFB     0
  1780. #define OFB     1
  1781.  
  1782. #define NO_SEND_IV      1
  1783. #define NO_RECV_IV      2
  1784. #define NO_KEYID        4
  1785. #define IN_PROGRESS     (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
  1786. #define SUCCESS         0
  1787. #define xFAILED         -1
  1788.  
  1789. Schedule test_sched;
  1790.  
  1791. struct des_stinfo {
  1792.     Block               str_output;
  1793.     Block               str_feed;
  1794.     Block               str_iv;
  1795.     Block               str_ikey;
  1796. #ifdef MIT_CURRENT
  1797.     unsigned char       str_keybytes[8];
  1798.     krb5_keyblock       str_key;
  1799. #else /* MIT_CURRENT */
  1800.     Schedule            str_sched;
  1801.     int                 str_index;
  1802. #endif /* MIT_CURRENT */
  1803.     int                 str_flagshift;
  1804. };
  1805.  
  1806. struct des_fb {
  1807. #ifndef MIT_CURRENT
  1808.     Block krbdes_key;
  1809.     Schedule krbdes_sched;
  1810. #endif /* MIT_CURRENT */
  1811.     Block temp_feed;
  1812.     unsigned char fb_feed[64];
  1813.     int need_start;
  1814.     int state[2];
  1815.     int keyid[2];
  1816.     int once;
  1817. #ifdef MIT_CURRENT
  1818.     int validkey;
  1819. #endif /* MIT_CURRENT */
  1820.     struct des_stinfo  streams[2];
  1821. };
  1822. static struct des_fb des_fb[2];
  1823.  
  1824. struct des3_stinfo {
  1825.     Block               str_output;
  1826.     Block               str_feed;
  1827.     Block               str_iv;
  1828.     Block               str_ikey[3];
  1829.     Schedule            str_sched[3];
  1830.     int                 str_index;
  1831.     int                 str_flagshift;
  1832. };
  1833.  
  1834. struct des3_fb {
  1835. #ifndef MIT_CURRENT
  1836.     Block krbdes_key[3];
  1837.     Schedule krbdes_sched[3];
  1838. #endif /* MIT_CURRENT */
  1839.     Block temp_feed;
  1840.     unsigned char fb_feed[64];
  1841.     int need_start;
  1842.     int state[2];
  1843.     int keyid[2];
  1844.     int once;
  1845. #ifdef MIT_CURRENT
  1846.     int validkey;
  1847. #endif /* MIT_CURRENT */
  1848.     struct des3_stinfo streams[2];
  1849. };
  1850. static struct des3_fb des3_fb[2];
  1851.  
  1852. struct keyidlist {
  1853.     char        *keyid;
  1854.     int keyidlen;
  1855.     char        *key;
  1856.     int keylen;
  1857.     int flags;
  1858. } keyidlist [] = {
  1859.     { "\0", 1, 0, 0, 0 },               /* default key of zero */
  1860.     { 0, 0, 0, 0, 0 }
  1861. };
  1862.  
  1863. #define KEYFLAG_MASK    03
  1864.  
  1865. #define KEYFLAG_NOINIT  00
  1866. #define KEYFLAG_INIT    01
  1867. #define KEYFLAG_OK      02
  1868. #define KEYFLAG_BAD     03
  1869.  
  1870. #define KEYFLAG_SHIFT   2
  1871.  
  1872. #define SHIFT_VAL(a,b)  (KEYFLAG_SHIFT*((a)+((b)*2)))
  1873.  
  1874. #define FB64_IV         1
  1875. #define FB64_IV_OK      2
  1876. #define FB64_IV_BAD     3
  1877. #define FB64_CHALLENGE  4
  1878. #define FB64_RESPONSE   5
  1879.  
  1880. void fb64_stream_iv P((Block, struct des_stinfo *));
  1881. void fb64_init P((struct des_fb *));
  1882. static int fb64_start P((struct des_fb *, int, int));
  1883. int fb64_is P((unsigned char *, int, struct des_fb *));
  1884. int fb64_reply P((unsigned char *, int, struct des_fb *));
  1885. static int fb64_session P((Session_Key *, int, struct des_fb *));
  1886. void fb64_stream_key P((Block, struct des_stinfo *));
  1887. int fb64_keyid P((int, unsigned char *, int *, struct des_fb *));
  1888.  
  1889. #ifdef MIT_CURRENT
  1890. static void
  1891. #ifdef CK_ANSIC
  1892. ecb_encrypt(struct des_stinfo *stp, Block in, Block out)
  1893. #else /* CKANSIC */
  1894. ecb_encrypt(stp, in, out)
  1895.     struct des_stinfo *stp;
  1896.     Block in;
  1897.     Block out;
  1898. #endif /* CK_ANSIC */
  1899. {
  1900.     krb5_error_code code;
  1901.     krb5_data din;
  1902.     krb5_enc_data dout;
  1903.  
  1904.     din.length = 8;
  1905.     din.data = in;
  1906.  
  1907.     dout.ciphertext.length = 8;
  1908.     dout.ciphertext.data = out;
  1909.     dout.enctype = ENCTYPE_UNKNOWN;
  1910.  
  1911. #ifdef CRYPT_DLL
  1912.     code = krb5_c_encrypt(*p_k5_context, &stp->str_key, 0, 0,
  1913.                            &din, &dout);
  1914. #else /* CRYPT_DLL */
  1915.     code = krb5_c_encrypt(k5_context, &stp->str_key, 0, 0,
  1916.                            &din, &dout);
  1917. #endif /* CRYPT_DLL */
  1918.     /* XXX I'm not sure what to do if this fails */
  1919.     if (code)
  1920.         com_err("libtelnet", code, "encrypting stream data");
  1921. }
  1922. #endif /* MIT_CURRENT */
  1923.  
  1924. void
  1925. cfb64_init(server)
  1926.     int server;
  1927. {
  1928.     fb64_init(&des_fb[CFB]);
  1929.     des_fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
  1930.     des_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
  1931.     des_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
  1932. }
  1933.  
  1934. void
  1935. ofb64_init(server)
  1936.     int server;
  1937. {
  1938.     fb64_init(&des_fb[OFB]);
  1939.     des_fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
  1940.     des_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
  1941.     des_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
  1942. }
  1943.  
  1944. void
  1945. fb64_init(fbp)
  1946.     register struct des_fb *fbp;
  1947. {
  1948.     memset((void *)fbp, 0, sizeof(*fbp));
  1949.     fbp->state[0] = fbp->state[1] = xFAILED;
  1950.     fbp->fb_feed[0] = IAC;
  1951.     fbp->fb_feed[1] = SB;
  1952.     fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  1953.     fbp->fb_feed[3] = ENCRYPT_IS;
  1954. }
  1955.  
  1956. /*
  1957.  * Returns:
  1958.  *      -1: some error.  Negotiation is done, encryption not ready.
  1959.  *       0: Successful, initial negotiation all done.
  1960.  *       1: successful, negotiation not done yet.
  1961.  *       2: Not yet.  Other things (like getting the key from
  1962.  *          Kerberos) have to happen before we can continue.
  1963.  */
  1964. int
  1965. cfb64_start(dir, server)
  1966.     int dir;
  1967.     int server;
  1968. {
  1969.     return(fb64_start(&des_fb[CFB], dir, server));
  1970. }
  1971. int
  1972. ofb64_start(dir, server)
  1973.     int dir;
  1974.     int server;
  1975. {
  1976.     return(fb64_start(&des_fb[OFB], dir, server));
  1977. }
  1978.  
  1979. static int
  1980. fb64_start(fbp, dir, server)
  1981.     struct des_fb *fbp;
  1982.     int dir;
  1983.     int server;
  1984. {
  1985.     int x;
  1986.     unsigned char *p;
  1987.     register int state;
  1988.  
  1989.     switch (dir) {
  1990.     case DIR_DECRYPT:
  1991.         /*
  1992.         * This is simply a request to have the other side
  1993.         * start output (our input).  He will negotiate an
  1994.         * IV so we need not look for it.
  1995.         */
  1996.         state = fbp->state[dir-1];
  1997.         if (state == xFAILED)
  1998.             state = IN_PROGRESS;
  1999.         break;
  2000.  
  2001.     case DIR_ENCRYPT:
  2002.         state = fbp->state[dir-1];
  2003.         if (state == xFAILED)
  2004.             state = IN_PROGRESS;
  2005.         else if ((state & NO_SEND_IV) == 0)
  2006.             break;
  2007.  
  2008. #ifdef MIT_CURRENT
  2009.         if (!fbp->validkey) {
  2010.             fbp->need_start = 1;
  2011.             break;
  2012.         }
  2013. #else /* MIT_CURRENT */
  2014.         if (!VALIDKEY(fbp->krbdes_key)) {
  2015.             fbp->need_start = 1;
  2016.             break;
  2017.         }
  2018. #endif /* MIT_CURRENT */
  2019.         state &= ~NO_SEND_IV;
  2020.         state |= NO_RECV_IV;
  2021.         /*
  2022.         * Create a random feed and send it over.
  2023.         */
  2024. #ifdef MIT_CURRENT
  2025.         {
  2026.             krb5_data d;
  2027.             krb5_error_code code;
  2028.  
  2029.             d.data = fbp->temp_feed;
  2030.             d.length = sizeof(fbp->temp_feed);
  2031.  
  2032. #ifdef CRYPT_DLL
  2033.             if (code = krb5_c_random_make_octets(*p_k5_context,&d))
  2034.                 return(xFAILED);
  2035. #else /* CRYPT_DLL */
  2036.             if (code = krb5_c_random_make_octets(k5_context,&d))
  2037.                 return(xFAILED);
  2038. #endif /* CRYPT_DLL */
  2039.         }
  2040.  
  2041. #else /* MIT_CURRENT */
  2042.         des_new_random_key(fbp->temp_feed);
  2043.         des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2044.                          fbp->krbdes_sched, 1);
  2045. #endif /* MIT_CURRENT */
  2046.         p = fbp->fb_feed + 3;
  2047.         *p++ = ENCRYPT_IS;
  2048.         p++;
  2049.         *p++ = FB64_IV;
  2050.         for (x = 0; x < sizeof(Block); ++x) {
  2051.             if (( *p++ = fbp->temp_feed[x]) == IAC)
  2052.                 *p++ = IAC;
  2053.         }
  2054.         *p++ = IAC;
  2055.         *p++ = SE;
  2056.  
  2057.         if (deblog || tn_deb || debses) {
  2058.             int i;
  2059.             sprintf(tn_msg,
  2060.                      "TELNET SENT SB %s IS %s FB64_IV ",
  2061.                      TELOPT(fbp->fb_feed[2]),
  2062.                      enctype_names[fbp->fb_feed[4]]);                   /* safe */
  2063.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2064.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2065.             debug(F100,tn_msg,"",0);
  2066.             if (tn_deb || debses) tn_debug(tn_msg);
  2067.         }
  2068.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2069.         break;
  2070.     default:
  2071.         return(xFAILED);
  2072.     }
  2073.     return(fbp->state[dir-1] = state);
  2074. }
  2075.  
  2076. /*
  2077.  * Returns:
  2078.  *      -1: some error.  Negotiation is done, encryption not ready.
  2079.  *       0: Successful, initial negotiation all done.
  2080.  *       1: successful, negotiation not done yet.
  2081.  */
  2082. int
  2083. cfb64_is(data, cnt)
  2084.     unsigned char *data;
  2085.     int cnt;
  2086. {
  2087.     return(fb64_is(data, cnt, &des_fb[CFB]));
  2088. }
  2089.  
  2090. int
  2091. ofb64_is(data, cnt)
  2092.     unsigned char *data;
  2093.     int cnt;
  2094. {
  2095.     return(fb64_is(data, cnt, &des_fb[OFB]));
  2096. }
  2097.  
  2098. int
  2099. fb64_is(data, cnt, fbp)
  2100.     unsigned char *data;
  2101.     int cnt;
  2102.     struct des_fb *fbp;
  2103. {
  2104.     unsigned char *p;
  2105.     register int state = fbp->state[DIR_DECRYPT-1];
  2106.  
  2107.     if (cnt-- < 1)
  2108.         goto failure;
  2109.  
  2110. #ifdef CK_SSL
  2111.     if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2112. #endif /* CK_SSL */
  2113.     switch (*data++) {
  2114.     case FB64_IV:
  2115.         if (cnt != sizeof(Block)) {
  2116. #ifdef DEBUG
  2117.             if (encrypt_debug_mode)
  2118.                 printf("CFB64: initial vector failed on size\r\n");
  2119. #endif
  2120.             state = xFAILED;
  2121.             goto failure;
  2122.         }
  2123.  
  2124. #ifdef DEBUG
  2125.         if (encrypt_debug_mode) {
  2126.             printf("CFB64: initial vector received\r\n");
  2127.             printf("Initializing Decrypt stream\r\n");
  2128.         }
  2129. #endif
  2130.         fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  2131.  
  2132.         p = fbp->fb_feed + 3;
  2133.         *p++ = ENCRYPT_REPLY;
  2134.         p++;
  2135.         *p++ = FB64_IV_OK;
  2136.         *p++ = IAC;
  2137.         *p++ = SE;
  2138.  
  2139.         if (deblog || tn_deb || debses) {
  2140.             int i;
  2141.             sprintf(tn_msg,
  2142.                      "TELNET SENT SB %s REPLY %s FB64_IV_OK ",
  2143.                      TELOPT(fbp->fb_feed[2]),
  2144.                      enctype_names[fbp->fb_feed[4]]);           /* safe */
  2145.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2146.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2147.             debug(F100,tn_msg,"",0);
  2148.             if (tn_deb || debses) tn_debug(tn_msg);
  2149.         }
  2150.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2151.         state = IN_PROGRESS;
  2152.         break;
  2153.  
  2154.     default:
  2155. #if 0
  2156.         if (encrypt_debug_mode) {
  2157.             printf("Unknown option type: %d\r\n", *(data-1));
  2158.             printf("\r\n");
  2159.         }
  2160. #endif
  2161.         /* FALL THROUGH */
  2162.       failure:
  2163.         /*
  2164.         * We failed.  Send an FB64_IV_BAD option
  2165.         * to the other side so it will know that
  2166.         * things failed.
  2167.         */
  2168.         p = fbp->fb_feed + 3;
  2169.         *p++ = ENCRYPT_REPLY;
  2170.         p++;
  2171.         *p++ = FB64_IV_BAD;
  2172.         *p++ = IAC;
  2173.         *p++ = SE;
  2174.  
  2175.         if (deblog || tn_deb || debses) {
  2176.             int i;
  2177.             sprintf(tn_msg,
  2178.                      "TELNET SENT SB %s REPLY %s FB64_IV_BAD ",
  2179.                      TELOPT(fbp->fb_feed[2]),
  2180.                      enctype_names[fbp->fb_feed[4]]);           /* safe */
  2181.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2182.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2183.             debug(F100,tn_msg,"",0);
  2184.             if (tn_deb || debses) tn_debug(tn_msg);
  2185.         }
  2186.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2187.         break;
  2188.     }
  2189.     return(fbp->state[DIR_DECRYPT-1] = state);
  2190. }
  2191.  
  2192. /*
  2193.  * Returns:
  2194.  *      -1: some error.  Negotiation is done, encryption not ready.
  2195.  *       0: Successful, initial negotiation all done.
  2196.  *       1: successful, negotiation not done yet.
  2197.  */
  2198. int
  2199. cfb64_reply(data, cnt)
  2200.     unsigned char *data;
  2201.     int cnt;
  2202. {
  2203.     return(fb64_reply(data, cnt, &des_fb[CFB]));
  2204. }
  2205. int
  2206. ofb64_reply(data, cnt)
  2207.     unsigned char *data;
  2208.     int cnt;
  2209. {
  2210.     return(fb64_reply(data, cnt, &des_fb[OFB]));
  2211. }
  2212.  
  2213.  
  2214. int
  2215. fb64_reply(data, cnt, fbp)
  2216.     unsigned char *data;
  2217.     int cnt;
  2218.     struct des_fb *fbp;
  2219. {
  2220.     register int state = fbp->state[DIR_ENCRYPT-1];
  2221.  
  2222.     if (cnt-- < 1)
  2223.         goto failure;
  2224.  
  2225.     switch (*data++) {
  2226.     case FB64_IV_OK:
  2227.         fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2228.         if (state == xFAILED)
  2229.             state = IN_PROGRESS;
  2230.         state &= ~NO_RECV_IV;
  2231.         encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  2232.         break;
  2233.  
  2234.     case FB64_IV_BAD:
  2235.         memset(fbp->temp_feed, 0, sizeof(Block));
  2236.         fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2237.         state = xFAILED;
  2238.         break;
  2239.  
  2240.     default:
  2241. #if 0
  2242.         if (encrypt_debug_mode) {
  2243.             printf("Unknown option type: %d\r\n", data[-1]);
  2244.             printf("\r\n");
  2245.         }
  2246. #endif
  2247.         /* FALL THROUGH */
  2248.       failure:
  2249.         state = xFAILED;
  2250.         break;
  2251.     }
  2252.     return(fbp->state[DIR_ENCRYPT-1] = state);
  2253. }
  2254.  
  2255. int
  2256. cfb64_session(key, server)
  2257.     Session_Key *key;
  2258.     int server;
  2259. {
  2260.     return(fb64_session(key, server, &des_fb[CFB]));
  2261. }
  2262.  
  2263. int
  2264. ofb64_session(key, server)
  2265.     Session_Key *key;
  2266.     int server;
  2267. {
  2268.     return(fb64_session(key, server, &des_fb[OFB]));
  2269. }
  2270.  
  2271. static int
  2272. fb64_session(key, server, fbp)
  2273.     Session_Key *key;
  2274.     int server;
  2275.     struct des_fb *fbp;
  2276. {
  2277.     int rc=0;
  2278.     int use2keys;
  2279.     struct des_stinfo * s_stream;
  2280.     struct des_stinfo * c_stream;
  2281.  
  2282.     if(server) {
  2283.         s_stream = &fbp->streams[DIR_ENCRYPT-1];
  2284.         c_stream = &fbp->streams[DIR_DECRYPT-1];
  2285.     }
  2286.     else {
  2287.         s_stream = &fbp->streams[DIR_DECRYPT-1];
  2288.         c_stream = &fbp->streams[DIR_ENCRYPT-1];
  2289.     }
  2290.  
  2291.     if (!key || key->length < sizeof(Block)) {
  2292.         CHAR buf[80];
  2293.         sprintf(buf,"Can't set DES session key (%d < %d)",
  2294.                 key ? key->length : 0, sizeof(Block));          /* safe */
  2295. #ifdef DEBUG
  2296.         if (encrypt_debug_mode)
  2297.             printf("%s\r\n",buf);
  2298. #endif
  2299.         debug(F110,"fb64_session",buf,0);
  2300.         return(-1);
  2301.     }
  2302.     use2keys = (key->type == SK_DES ||
  2303.                  key->length < 2 * sizeof(Block)) ? 0 : 1;
  2304. #ifdef MIT_CURRENT
  2305.     if(use2keys) {
  2306.         memcpy((void *) fbp->keybytes,
  2307.                  (void *) (key->data + sizeof(Block)), sizeof(Block));
  2308.         des_fixup_key_parity(fbp->);
  2309.         fb64_stream_key(fbp->krbdes_key, s_stream);
  2310.     }
  2311.  
  2312.     memcpy((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
  2313.     if (key->type != SK_DES)
  2314.         des_fixup_key_parity(fbp->krbdes_key);
  2315.  
  2316.     if(!use2keys)
  2317.         fb64_stream_key(fbp->krbdes_key, s_stream);
  2318.     fb64_stream_key(fbp->krbdes_key, c_stream);
  2319.     fbp->validkey = 1;
  2320.  
  2321.     fb64_stream_key(key->data, &fbp->streams[DIR_ENCRYPT-1]);
  2322.     fb64_stream_key(key->data, &fbp->streams[DIR_DECRYPT-1]);
  2323. #else /* MIT_CURRENT */
  2324.     if(use2keys) {
  2325.         memcpy((void *) fbp->krbdes_key,
  2326.                  (void *) (key->data + sizeof(Block)), sizeof(Block));
  2327.         des_fixup_key_parity(fbp->krbdes_key);
  2328.         fb64_stream_key(fbp->krbdes_key, s_stream);
  2329.     }
  2330.  
  2331.     memcpy((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
  2332.     if (key->type != SK_DES)
  2333.         des_fixup_key_parity(fbp->krbdes_key);
  2334.  
  2335.     if(!use2keys)
  2336.         fb64_stream_key(fbp->krbdes_key, s_stream);
  2337.     fb64_stream_key(fbp->krbdes_key, c_stream);
  2338.  
  2339.     if (fbp->once == 0) {
  2340.         des_set_random_generator_seed(fbp->krbdes_key);
  2341.         fbp->once = 1;
  2342.     }
  2343.  
  2344.     memset(fbp->krbdes_sched,0,sizeof(Schedule));
  2345.     hexdump("fb64_session_key",fbp->krbdes_key,8);
  2346.  
  2347.     rc = des_key_sched(fbp->krbdes_key, fbp->krbdes_sched);
  2348.     if ( rc == -1 ) {
  2349.         printf("?Invalid DES key specified for encryption\n");
  2350.         debug(F110,"fb64_session_key",
  2351.                "invalid DES Key specified for encryption",0);
  2352.     } else if ( rc == -2 ) {
  2353.         printf("?Weak DES key specified for encryption\n");
  2354.         debug(F110,"fb64_session_key",
  2355.                "weak DES Key specified for encryption",0);
  2356.     } else if ( rc != 0 ) {
  2357.         printf("?Key Schedule not created by encryption\n");
  2358.         debug(F110,"fb64_session_key",
  2359.                "Key Schedule not created by encryption",0);
  2360.     }
  2361.  
  2362.     hexdump("fb64_session_key schedule",fbp->krbdes_sched,8*16);
  2363. #endif /* MIT_CURRENT */
  2364.     /*
  2365.     * Now look to see if krbdes_start() was was waiting for
  2366.     * the key to show up.  If so, go ahead an call it now
  2367.     * that we have the key.
  2368.     */
  2369.     if (fbp->need_start) {
  2370.         fbp->need_start = 0;
  2371.         fb64_start(fbp, DIR_ENCRYPT, server);
  2372.     }
  2373.     return(0);
  2374. }
  2375.  
  2376. /*
  2377.  * We only accept a keyid of 0.  If we get a keyid of
  2378.  * 0, then mark the state as SUCCESS.
  2379.  */
  2380. int
  2381. cfb64_keyid(dir, kp, lenp)
  2382.     int dir, *lenp;
  2383.     unsigned char *kp;
  2384. {
  2385.     return(fb64_keyid(dir, kp, lenp, &des_fb[CFB]));
  2386. }
  2387.  
  2388. int
  2389. ofb64_keyid(dir, kp, lenp)
  2390.     int dir, *lenp;
  2391.     unsigned char *kp;
  2392. {
  2393.     return(fb64_keyid(dir, kp, lenp, &des_fb[OFB]));
  2394. }
  2395.  
  2396. int
  2397. fb64_keyid(dir, kp, lenp, fbp)
  2398.     int dir, *lenp;
  2399.     unsigned char *kp;
  2400.     struct des_fb *fbp;
  2401. {
  2402.     register int state = fbp->state[dir-1];
  2403.  
  2404.     if (*lenp != 1 || (*kp != '\0')) {
  2405.         *lenp = 0;
  2406.         return(state);
  2407.     }
  2408.  
  2409.     if (state == xFAILED)
  2410.         state = IN_PROGRESS;
  2411.  
  2412.     state &= ~NO_KEYID;
  2413.  
  2414.     return(fbp->state[dir-1] = state);
  2415. }
  2416.  
  2417. #if 0
  2418. void
  2419. fb64_printsub(data, cnt, buf, buflen, type)
  2420.     unsigned char *data, *buf, *type;
  2421.     int cnt, buflen;
  2422. {
  2423.     char lbuf[64];
  2424.     register int i;
  2425.     char *cp;
  2426.  
  2427.     buf[buflen-1] = '\0';               /* make sure it's NULL terminated */
  2428.     buflen -= 1;
  2429.  
  2430.     switch(data[2]) {
  2431.     case FB64_IV:
  2432.         sprintf(lbuf, "%s_IV", type);
  2433.         cp = lbuf;
  2434.         goto common;
  2435.  
  2436.     case FB64_IV_OK:
  2437.         sprintf(lbuf, "%s_IV_OK", type);
  2438.         cp = lbuf;
  2439.         goto common;
  2440.  
  2441.     case FB64_IV_BAD:
  2442.         sprintf(lbuf, "%s_IV_BAD", type);
  2443.         cp = lbuf;
  2444.         goto common;
  2445.  
  2446.     case FB64_CHALLENGE:
  2447.         sprintf(lbuf, "%s_CHALLENGE", type);
  2448.         cp = lbuf;
  2449.         goto common;
  2450.  
  2451.     case FB64_RESPONSE:
  2452.         sprintf(lbuf, "%s_RESPONSE", type);
  2453.         cp = lbuf;
  2454.         goto common;
  2455.  
  2456.     default:
  2457.         sprintf(lbuf, " %d (unknown)", data[2]);
  2458.         cp = lbuf;
  2459.       common:
  2460.         for (; (buflen > 0) && (*buf = *cp++); buf++)
  2461.             buflen--;
  2462.         for (i = 3; i < cnt; i++) {
  2463.             sprintf(lbuf, " %d", data[i]);
  2464.             for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  2465.                 buflen--;
  2466.         }
  2467.         break;
  2468.     }
  2469. }
  2470.  
  2471. void
  2472. cfb64_printsub(data, cnt, buf, buflen)
  2473.     unsigned char *data, *buf;
  2474.     int cnt, buflen;
  2475. {
  2476.     fb64_printsub(data, cnt, buf, buflen, "CFB64");
  2477. }
  2478.  
  2479. void
  2480. ofb64_printsub(data, cnt, buf, buflen)
  2481.     unsigned char *data, *buf;
  2482.     int cnt, buflen;
  2483. {
  2484.     fb64_printsub(data, cnt, buf, buflen, "OFB64");
  2485. }
  2486. #endif
  2487.  
  2488. void
  2489. fb64_stream_iv(seed, stp)
  2490.     Block seed;
  2491.     register struct des_stinfo *stp;
  2492. {
  2493.     int rc=0;
  2494.  
  2495.     memcpy(stp->str_iv,     seed, sizeof(Block));
  2496.     memcpy(stp->str_output, seed, sizeof(Block));
  2497.  
  2498.     memset(stp->str_sched,0,sizeof(Schedule));
  2499.  
  2500.     hexdump("fb64_stream_iv",stp->str_ikey,8);
  2501.  
  2502. #ifndef MIT_CURRENT
  2503.     rc = des_key_sched(stp->str_ikey, stp->str_sched);
  2504.     if ( rc == -1 ) {
  2505.         printf("?Invalid DES key specified for encryption\r\n");
  2506.         debug(F110,"fb64_stream_iv",
  2507.                "invalid DES Key specified for encryption",0);
  2508.     } else if ( rc == -2 ) {
  2509.         printf("?Weak DES key specified for encryption\r\n");
  2510.         debug(F110,"fb64_stream_iv",
  2511.                "weak DES Key specified for encryption",0);
  2512.     } else if ( rc != 0 ) {
  2513.         printf("?Key Schedule not created by encryption\r\n");
  2514.         debug(F110,"fb64_stream_iv",
  2515.                "Key Schedule not created by encryption",0);
  2516.     }
  2517.     hexdump("fb64_stream_iv schedule",stp->str_sched,8*16);
  2518. #endif /* MIT_CURRENT */
  2519.  
  2520.     stp->str_index = sizeof(Block);
  2521. }
  2522.  
  2523. void
  2524. fb64_stream_key(key, stp)
  2525.     Block key;
  2526.     register struct des_stinfo *stp;
  2527. {
  2528.     int rc = 0;
  2529.  
  2530. #ifdef MIT_CURRENT
  2531.     memcpy(stp->str_keybytes, key, sizeof(Block));
  2532.     stp->str_key.length = 8;
  2533.     stp->str_key.contents = stp->str_keybytes;
  2534.     /* the original version of this code uses des ecb mode, but
  2535.     it only ever does one block at a time.  cbc with a zero iv
  2536.     is identical */
  2537.     stp->str_key.enctype = ENCTYPE_DES_CBC_RAW;
  2538. #else /* MIT_CURRENT */
  2539.     memcpy(stp->str_ikey, key, sizeof(Block));
  2540.  
  2541.     memset(stp->str_sched,0,sizeof(Schedule));
  2542.  
  2543.     hexdump("fb64_stream_key",key,8);
  2544.  
  2545.     rc = des_key_sched(key, stp->str_sched);
  2546.     if ( rc == -1 ) {
  2547.         printf("?Invalid DES key specified for encryption\r\n");
  2548.         debug(F110,"fb64_stream_iv",
  2549.                "invalid DES Key specified for encryption",0);
  2550.     } else if ( rc == -2 ) {
  2551.         printf("?Weak DES key specified for encryption\r\n");
  2552.         debug(F110,"fb64_stream_iv",
  2553.                "weak DES Key specified for encryption",0);
  2554.     } else if ( rc != 0 ) {
  2555.         printf("?Key Schedule not created by encryption\r\n");
  2556.         debug(F110,"fb64_stream_iv",
  2557.                "Key Schedule not created by encryption",0);
  2558.     }
  2559.     hexdump("fb64_stream_key schedule",stp->str_sched,8*16);
  2560. #endif /* MIT_CURRENT */
  2561.  
  2562.     memcpy(stp->str_output, stp->str_iv, sizeof(Block));
  2563.  
  2564.     stp->str_index = sizeof(Block);
  2565. }
  2566.  
  2567. /*
  2568.  * DES 64 bit Cipher Feedback
  2569.  *
  2570.  *     key --->+-----+
  2571.  *          +->| DES |--+
  2572.  *          |  +-----+  |
  2573.  *          |           v
  2574.  *  INPUT --(--------->(+)+---> DATA
  2575.  *          |             |
  2576.  *          +-------------+
  2577.  *
  2578.  *
  2579.  * Given:
  2580.  *      iV: Initial vector, 64 bits (8 bytes) long.
  2581.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  2582.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  2583.  *
  2584.  *      V0 = DES(iV, key)
  2585.  *      On = Dn ^ Vn
  2586.  *      V(n+1) = DES(On, key)
  2587.  */
  2588.  
  2589. void
  2590. cfb64_encrypt(s, c)
  2591.     register unsigned char *s;
  2592.     int c;
  2593. {
  2594.     register struct des_stinfo *stp = &des_fb[CFB].streams[DIR_ENCRYPT-1];
  2595.     register int index;
  2596.  
  2597.     index = stp->str_index;
  2598.     while (c-- > 0) {
  2599.         if (index == sizeof(Block)) {
  2600.             Block b;
  2601. #ifdef MIT_CURRENT
  2602.             ecb_encrypt(stp, stp->str_output, b);
  2603. #else /* MIT_CURRENT */
  2604.             des_ecb_encrypt(stp->str_output, b, stp->str_sched, 1);
  2605. #endif /* MIT_CURRENT */
  2606.             memcpy(stp->str_feed,b,sizeof(Block));
  2607.             index = 0;
  2608.         }
  2609.  
  2610.         /* On encryption, we store (feed ^ data) which is cypher */
  2611.         *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  2612.         s++;
  2613.         index++;
  2614.     }
  2615.     stp->str_index = index;
  2616. }
  2617.  
  2618. int
  2619. cfb64_decrypt(data)
  2620.     int data;
  2621. {
  2622.     register struct des_stinfo *stp = &des_fb[CFB].streams[DIR_DECRYPT-1];
  2623.     int index;
  2624.  
  2625.     if (data == -1) {
  2626.         /*
  2627.         * Back up one byte.  It is assumed that we will
  2628.         * never back up more than one byte.  If we do, this
  2629.         * may or may not work.
  2630.         */
  2631.         if (stp->str_index)
  2632.             --stp->str_index;
  2633.         return(0);
  2634.     }
  2635.  
  2636.     index = stp->str_index++;
  2637.     if (index == sizeof(Block)) {
  2638.         Block b;
  2639. #ifdef MIT_CURRENT
  2640.         ecb_encrypt(stp, stp->str_output, b);
  2641. #else /* MIT_CURRENT */
  2642.         des_ecb_encrypt(stp->str_output, b, stp->str_sched, 1);
  2643. #endif /* MIT_CURRENT */
  2644.         memcpy(stp->str_feed, b, sizeof(Block));
  2645.         stp->str_index = 1;     /* Next time will be 1 */
  2646.         index = 0;              /* But now use 0 */
  2647.     }
  2648.  
  2649.     /* On decryption we store (data) which is cypher. */
  2650.     stp->str_output[index] = data;
  2651.     return(data ^ stp->str_feed[index]);
  2652. }
  2653.  
  2654. /*
  2655.  * DES 64 bit Output Feedback
  2656.  *
  2657.  * key --->+-----+
  2658.  *      +->| DES |--+
  2659.  *      |  +-----+  |
  2660.  *      +-----------+
  2661.  *                  v
  2662.  *  INPUT -------->(+) ----> DATA
  2663.  *
  2664.  * Given:
  2665.  *      iV: Initial vector, 64 bits (8 bytes) long.
  2666.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  2667.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  2668.  *
  2669.  *      V0 = DES(iV, key)
  2670.  *      V(n+1) = DES(Vn, key)
  2671.  *      On = Dn ^ Vn
  2672.  */
  2673. void
  2674. ofb64_encrypt(s, c)
  2675.     register unsigned char *s;
  2676.     int c;
  2677. {
  2678.     register struct des_stinfo *stp = &des_fb[OFB].streams[DIR_ENCRYPT-1];
  2679.     register int index;
  2680.  
  2681.     index = stp->str_index;
  2682.     while (c-- > 0) {
  2683.         if (index == sizeof(Block)) {
  2684.             Block b;
  2685. #ifdef MIT_CURRENT
  2686.             ecb_encrypt(stp, stp->str_feed, b);
  2687. #else /* MIT_CURRENT */
  2688.             des_ecb_encrypt(stp->str_feed, b, stp->str_sched, 1);
  2689. #endif /* MIT_CURRENT */
  2690.             memcpy(stp->str_feed,b,sizeof(Block));
  2691.             index = 0;
  2692.         }
  2693.         *s++ ^= stp->str_feed[index];
  2694.         index++;
  2695.     }
  2696.     stp->str_index = index;
  2697. }
  2698.  
  2699. int
  2700. ofb64_decrypt(data)
  2701.     int data;
  2702. {
  2703.     register struct des_stinfo *stp = &des_fb[OFB].streams[DIR_DECRYPT-1];
  2704.     int index;
  2705.  
  2706.     if (data == -1) {
  2707.         /*
  2708.         * Back up one byte.  It is assumed that we will
  2709.         * never back up more than one byte.  If we do, this
  2710.         * may or may not work.
  2711.         */
  2712.         if (stp->str_index)
  2713.             --stp->str_index;
  2714.         return(0);
  2715.     }
  2716.  
  2717.     index = stp->str_index++;
  2718.     if (index == sizeof(Block)) {
  2719.         Block b;
  2720. #ifdef MIT_CURRENT
  2721.         ecb_encrypt(stp, stp->str_feed, b);
  2722. #else /* MIT_CURRENT */
  2723.         des_ecb_encrypt(stp->str_feed, b, stp->str_sched, 1);
  2724. #endif /* MIT_CURRENT */
  2725.         memcpy(stp->str_feed, b, sizeof(Block));
  2726.         stp->str_index = 1;     /* Next time will be 1 */
  2727.         index = 0;              /* But now use 0 */
  2728.     }
  2729.  
  2730.     return(data ^ stp->str_feed[index]);
  2731. }
  2732.  
  2733.  
  2734. void des3_fb64_stream_iv P((Block, struct des3_stinfo *));
  2735. void des3_fb64_init P((struct des3_fb *));
  2736. static int des3_fb64_start P((struct des3_fb *, int, int));
  2737. int des3_fb64_is P((unsigned char *, int, struct des3_fb *));
  2738. int des3_fb64_reply P((unsigned char *, int, struct des3_fb *));
  2739. static int des3_fb64_session P((Session_Key *, int, struct des3_fb *));
  2740. void des3_fb64_stream_key P((Block *, struct des3_stinfo *));
  2741. int des3_fb64_keyid P((int, unsigned char *, int *, struct des3_fb *));
  2742.  
  2743. void
  2744. des3_cfb64_init(server)
  2745.     int server;
  2746. {
  2747.     des3_fb64_init(&des3_fb[CFB]);
  2748.     des3_fb[CFB].fb_feed[4] = ENCTYPE_DES3_CFB64;
  2749.     des3_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
  2750.     des3_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
  2751. }
  2752.  
  2753. void
  2754. des3_ofb64_init(server)
  2755.     int server;
  2756. {
  2757.     des3_fb64_init(&des3_fb[OFB]);
  2758.     des3_fb[OFB].fb_feed[4] = ENCTYPE_DES3_OFB64;
  2759.     des3_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
  2760.     des3_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
  2761. }
  2762.  
  2763. void
  2764. des3_fb64_init(fbp)
  2765.     register struct des3_fb *fbp;
  2766. {
  2767.     memset((void *)fbp, 0, sizeof(*fbp));
  2768.     fbp->state[0] = fbp->state[1] = xFAILED;
  2769.     fbp->fb_feed[0] = IAC;
  2770.     fbp->fb_feed[1] = SB;
  2771.     fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  2772.     fbp->fb_feed[3] = ENCRYPT_IS;
  2773. }
  2774.  
  2775. /*
  2776.  * Returns:
  2777.  *      -1: some error.  Negotiation is done, encryption not ready.
  2778.  *       0: Successful, initial negotiation all done.
  2779.  *       1: successful, negotiation not done yet.
  2780.  *       2: Not yet.  Other things (like getting the key from
  2781.  *          Kerberos) have to happen before we can continue.
  2782.  */
  2783. int
  2784. des3_cfb64_start(dir, server)
  2785.     int dir;
  2786.     int server;
  2787. {
  2788.     return(des3_fb64_start(&des3_fb[CFB], dir, server));
  2789. }
  2790. int
  2791. des3_ofb64_start(dir, server)
  2792.     int dir;
  2793.     int server;
  2794. {
  2795.     return(des3_fb64_start(&des3_fb[OFB], dir, server));
  2796. }
  2797.  
  2798. static int
  2799. des3_fb64_start(fbp, dir, server)
  2800.     struct des3_fb *fbp;
  2801.     int dir;
  2802.     int server;
  2803. {
  2804.     int x;
  2805.     unsigned char *p;
  2806.     register int state;
  2807.  
  2808.     switch (dir) {
  2809.     case DIR_DECRYPT:
  2810.         /*
  2811.         * This is simply a request to have the other side
  2812.         * start output (our input).  He will negotiate an
  2813.         * IV so we need not look for it.
  2814.         */
  2815.         state = fbp->state[dir-1];
  2816.         if (state == xFAILED)
  2817.             state = IN_PROGRESS;
  2818.         break;
  2819.  
  2820.     case DIR_ENCRYPT:
  2821.         state = fbp->state[dir-1];
  2822.         if (state == xFAILED)
  2823.             state = IN_PROGRESS;
  2824.         else if ((state & NO_SEND_IV) == 0)
  2825.             break;
  2826.  
  2827.         if (!VALIDKEY(fbp->krbdes_key[0]) ||
  2828.             !VALIDKEY(fbp->krbdes_key[1]) ||
  2829.             !VALIDKEY(fbp->krbdes_key[2]) ) {
  2830.             fbp->need_start = 1;
  2831.             break;
  2832.         }
  2833.         state &= ~NO_SEND_IV;
  2834.         state |= NO_RECV_IV;
  2835.         /*
  2836.         * Create a random feed and send it over.
  2837.         */
  2838.         des_new_random_key(fbp->temp_feed);
  2839. #ifdef LIBDES
  2840.         des_ecb3_encrypt(fbp->temp_feed, fbp->temp_feed,
  2841.                          fbp->krbdes_sched[0],
  2842.                          fbp->krbdes_sched[1],
  2843.                          fbp->krbdes_sched[2],
  2844.                          1);
  2845. #else /* LIBDES */
  2846.         des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2847.                          fbp->krbdes_sched[0], 1);
  2848.         des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2849.                          fbp->krbdes_sched[1], 0);
  2850.         des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2851.                          fbp->krbdes_sched[2], 1);
  2852. #endif /* LIBDES */
  2853.  
  2854.         p = fbp->fb_feed + 3;
  2855.         *p++ = ENCRYPT_IS;
  2856.         p++;
  2857.         *p++ = FB64_IV;
  2858.         for (x = 0; x < sizeof(Block); ++x) {
  2859.             if (( *p++ = fbp->temp_feed[x]) == IAC)
  2860.                 *p++ = IAC;
  2861.         }
  2862.         *p++ = IAC;
  2863.         *p++ = SE;
  2864.  
  2865.         if (deblog || tn_deb || debses) {
  2866.             int i;
  2867.             sprintf(tn_msg,
  2868.                      "TELNET SENT SB %s IS %s FB64_IV ",
  2869.                      TELOPT(fbp->fb_feed[2]),
  2870.                      enctype_names[fbp->fb_feed[4]]);           /* safe */
  2871.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2872.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2873.             debug(F100,tn_msg,"",0);
  2874.             if (tn_deb || debses) tn_debug(tn_msg);
  2875.         }
  2876.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2877.         break;
  2878.     default:
  2879.         return(xFAILED);
  2880.     }
  2881.     return(fbp->state[dir-1] = state);
  2882. }
  2883.  
  2884. /*
  2885.  * Returns:
  2886.  *      -1: some error.  Negotiation is done, encryption not ready.
  2887.  *       0: Successful, initial negotiation all done.
  2888.  *       1: successful, negotiation not done yet.
  2889.  */
  2890. int
  2891. des3_cfb64_is(data, cnt)
  2892.     unsigned char *data;
  2893.     int cnt;
  2894. {
  2895.     return(des3_fb64_is(data, cnt, &des3_fb[CFB]));
  2896. }
  2897.  
  2898. int
  2899. des3_ofb64_is(data, cnt)
  2900.     unsigned char *data;
  2901.     int cnt;
  2902. {
  2903.     return(des3_fb64_is(data, cnt, &des3_fb[OFB]));
  2904. }
  2905.  
  2906. int
  2907. des3_fb64_is(data, cnt, fbp)
  2908.     unsigned char *data;
  2909.     int cnt;
  2910.     struct des3_fb *fbp;
  2911. {
  2912.     unsigned char *p;
  2913.     register int state = fbp->state[DIR_DECRYPT-1];
  2914.  
  2915.     if (cnt-- < 1)
  2916.         goto failure;
  2917.  
  2918. #ifdef CK_SSL
  2919.     if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2920. #endif /* CK_SSL */
  2921.     switch (*data++) {
  2922.     case FB64_IV:
  2923.         if (cnt != sizeof(Block)) {
  2924. #ifdef DEBUG
  2925.             if (encrypt_debug_mode)
  2926.                 printf("DES3_FB64: initial vector failed on size\r\n");
  2927. #endif
  2928.             state = xFAILED;
  2929.             goto failure;
  2930.         }
  2931.  
  2932. #ifdef DEBUG
  2933.         if (encrypt_debug_mode) {
  2934.             printf("DES3_FB64: initial vector received\r\n");
  2935.             printf("Initializing Decrypt stream\r\n");
  2936.         }
  2937. #endif
  2938.         des3_fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  2939.  
  2940.         p = fbp->fb_feed + 3;
  2941.         *p++ = ENCRYPT_REPLY;
  2942.         p++;
  2943.         *p++ = FB64_IV_OK;
  2944.         *p++ = IAC;
  2945.         *p++ = SE;
  2946.  
  2947.         if (deblog || tn_deb || debses) {
  2948.             int i;
  2949.             sprintf(tn_msg,
  2950.                      "TELNET SENT SB %s REPLY %s FB64_IV_OK ",
  2951.                      TELOPT(fbp->fb_feed[2]),
  2952.                      enctype_names[fbp->fb_feed[4]]);           /* safe */
  2953.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2954.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2955.             debug(F100,tn_msg,"",0);
  2956.             if (tn_deb || debses) tn_debug(tn_msg);
  2957.         }
  2958.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2959.         state = IN_PROGRESS;
  2960.         break;
  2961.  
  2962.     default:
  2963. #if 0
  2964.         if (encrypt_debug_mode) {
  2965.             printf("Unknown option type: %d\r\n", *(data-1));
  2966.             printf("\r\n");
  2967.         }
  2968. #endif
  2969.         /* FALL THROUGH */
  2970.       failure:
  2971.         /*
  2972.         * We failed.  Send an FB64_IV_BAD option
  2973.         * to the other side so it will know that
  2974.         * things failed.
  2975.         */
  2976.         p = fbp->fb_feed + 3;
  2977.         *p++ = ENCRYPT_REPLY;
  2978.         p++;
  2979.         *p++ = FB64_IV_BAD;
  2980.         *p++ = IAC;
  2981.         *p++ = SE;
  2982.  
  2983.         if (deblog || tn_deb || debses) {
  2984.             int i;
  2985.             sprintf(tn_msg,
  2986.                      "TELNET SENT SB %s REPLY %s FB64_IV_BAD ",
  2987.                      TELOPT(fbp->fb_feed[2]),
  2988.                      enctype_names[fbp->fb_feed[4]]);           /* safe */
  2989.             tn_hex(tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],(p-fbp->fb_feed)-2-6);
  2990.             ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2991.             debug(F100,tn_msg,"",0);
  2992.             if (tn_deb || debses) tn_debug(tn_msg);
  2993.         }
  2994.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  2995.         break;
  2996.     }
  2997.     return(fbp->state[DIR_DECRYPT-1] = state);
  2998. }
  2999.  
  3000. /*
  3001.  * Returns:
  3002.  *      -1: some error.  Negotiation is done, encryption not ready.
  3003.  *       0: Successful, initial negotiation all done.
  3004.  *       1: successful, negotiation not done yet.
  3005.  */
  3006. int
  3007. des3_cfb64_reply(data, cnt)
  3008.     unsigned char *data;
  3009.     int cnt;
  3010. {
  3011.     return(des3_fb64_reply(data, cnt, &des3_fb[CFB]));
  3012. }
  3013. int
  3014. des3_ofb64_reply(data, cnt)
  3015.     unsigned char *data;
  3016.     int cnt;
  3017. {
  3018.     return(des3_fb64_reply(data, cnt, &des3_fb[OFB]));
  3019. }
  3020.  
  3021.  
  3022. int
  3023. des3_fb64_reply(data, cnt, fbp)
  3024.     unsigned char *data;
  3025.     int cnt;
  3026.     struct des3_fb *fbp;
  3027. {
  3028.     register int state = fbp->state[DIR_ENCRYPT-1];
  3029.  
  3030.     if (cnt-- < 1)
  3031.         goto failure;
  3032.  
  3033.     switch (*data++) {
  3034.     case FB64_IV_OK:
  3035.         des3_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  3036.         if (state == xFAILED)
  3037.             state = IN_PROGRESS;
  3038.         state &= ~NO_RECV_IV;
  3039.         encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  3040.         break;
  3041.  
  3042.     case FB64_IV_BAD:
  3043.         memset(fbp->temp_feed, 0, sizeof(Block));
  3044.         des3_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  3045.         state = xFAILED;
  3046.         break;
  3047.  
  3048.     default:
  3049. #if 0
  3050.         if (encrypt_debug_mode) {
  3051.             printf("Unknown option type: %d\r\n", data[-1]);
  3052.             printf("\r\n");
  3053.         }
  3054. #endif
  3055.         /* FALL THROUGH */
  3056.       failure:
  3057.         state = xFAILED;
  3058.         break;
  3059.     }
  3060.     return(fbp->state[DIR_ENCRYPT-1] = state);
  3061. }
  3062.  
  3063. int
  3064. des3_cfb64_session(key, server)
  3065.     Session_Key *key;
  3066.     int server;
  3067. {
  3068.     return(des3_fb64_session(key, server, &des3_fb[CFB]));
  3069. }
  3070.  
  3071. int
  3072. des3_ofb64_session(key, server)
  3073.     Session_Key *key;
  3074.     int server;
  3075. {
  3076.     return(des3_fb64_session(key, server, &des3_fb[OFB]));
  3077. }
  3078.  
  3079. static int
  3080. des3_fb64_session(key, server, fbp)
  3081.     Session_Key *key;
  3082.     int server;
  3083.     struct des3_fb *fbp;
  3084. {
  3085.     int rc=0,i=0;
  3086.     int keys2use=0;
  3087.     struct des3_stinfo * s_stream;
  3088.     struct des3_stinfo * c_stream;
  3089.  
  3090.     if(server) {
  3091.         s_stream = &fbp->streams[DIR_ENCRYPT-1];
  3092.         c_stream = &fbp->streams[DIR_DECRYPT-1];
  3093.     }
  3094.     else {
  3095.         s_stream = &fbp->streams[DIR_DECRYPT-1];
  3096.         c_stream = &fbp->streams[DIR_ENCRYPT-1];
  3097.     }
  3098.  
  3099.     keys2use = key->length / sizeof(Block);
  3100.     if (!key || (key->type == SK_DES) || (keys2use < 2)) {
  3101.         CHAR buf[80];
  3102.         sprintf(buf,"Can't set 3DES session key (%d < %d)",
  3103.                 key ? key->length : 0, 2 * sizeof(Block));      /* safe */
  3104. #ifdef DEBUG
  3105.         if (encrypt_debug_mode)
  3106.             printf("%s\r\n",buf);
  3107. #endif
  3108.         debug(F110,"des3_fb64_session",buf,0);
  3109.         return(-1);
  3110.     }
  3111.  
  3112.     debug(F111,"des3_fb64_session","keys2use",keys2use);
  3113.     /* Compute the first set of keys / key order */
  3114.     switch ( keys2use ) {
  3115.     case 2:
  3116.         memcpy((void *)fbp->krbdes_key[0], (void *)key->data, sizeof(Block));
  3117.         memcpy((void *) fbp->krbdes_key[1],(void *)(key->data + sizeof(Block)),
  3118.                  sizeof(Block));
  3119.         memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  3120.         break;
  3121.     case 3:
  3122.     default:
  3123.         memcpy((void *)fbp->krbdes_key[0], (void *)key->data, sizeof(Block));
  3124.         memcpy((void *) fbp->krbdes_key[1],(void *)(key->data + sizeof(Block)),
  3125.                  sizeof(Block));
  3126.         memcpy((void *) fbp->krbdes_key[2],
  3127.                  (void *) (key->data + 2*sizeof(Block)), sizeof(Block));
  3128.         break;
  3129.     }
  3130.     hexdump("des3_session_key key->data",key->data,sizeof(Block));
  3131.     hexdump("des3_session_key fbp->krbdes_key[0]",
  3132.             fbp->krbdes_key[0],
  3133.             sizeof(Block)
  3134.             );
  3135.     if (fbp->once == 0) {
  3136.         des_set_random_generator_seed(fbp->krbdes_key[0]);
  3137.         fbp->once = 1;
  3138.     }
  3139.  
  3140.     for ( i=0;i<3;i++ )
  3141.         des_fixup_key_parity(fbp->krbdes_key[i]);
  3142.     des3_fb64_stream_key(fbp->krbdes_key, s_stream);
  3143.  
  3144.  
  3145.     /* Compute the second set of keys / key order */
  3146.     switch ( keys2use ) {
  3147.     case 2:
  3148.         memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3149.                  sizeof(Block));
  3150.         memcpy((void *)fbp->krbdes_key[1], (void *)key->data, sizeof(Block));
  3151.         memcpy((void *) fbp->krbdes_key[2],(void *)(key->data + sizeof(Block)),
  3152.                  sizeof(Block));
  3153.         break;
  3154.     case 3:
  3155.         memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3156.                  sizeof(Block));
  3157.         memcpy((void *) fbp->krbdes_key[1],
  3158.                  (void *) (key->data + 2*sizeof(Block)), sizeof(Block));
  3159.         memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  3160.         break;
  3161.     case 4:
  3162.         memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3163.                  sizeof(Block));
  3164.         memcpy((void *) fbp->krbdes_key[1],
  3165.                  (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3166.         memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  3167.         break;
  3168.     case 5:
  3169.         memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3170.                  sizeof(Block));
  3171.         memcpy((void *) fbp->krbdes_key[1],
  3172.                  (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3173.         memcpy((void *)fbp->krbdes_key[2],
  3174.                  (void *)(key->data + 4*sizeof(Block)), sizeof(Block));
  3175.         break;
  3176.     case 6:
  3177.         memcpy((void *) fbp->krbdes_key[0],
  3178.                  (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3179.         memcpy((void *)fbp->krbdes_key[1],
  3180.                  (void *)(key->data + 4*sizeof(Block)), sizeof(Block));
  3181.         memcpy((void *) fbp->krbdes_key[2],
  3182.                  (void *) (key->data + 5 *sizeof(Block)),  sizeof(Block));
  3183.         break;
  3184.     }
  3185.  
  3186.     for ( i=0;i<3;i++ )
  3187.         des_fixup_key_parity(fbp->krbdes_key[i]);
  3188.     des3_fb64_stream_key(fbp->krbdes_key, c_stream);
  3189.  
  3190.     /* now use the second set of keys to build the default Key Schedule */
  3191.     /* which is used for generating the IV.                             */
  3192.     for ( i=0;i<3;i++ ) {
  3193.         memset(fbp->krbdes_sched[i],0,sizeof(Schedule));
  3194.  
  3195.         rc = des_key_sched(fbp->krbdes_key[i], fbp->krbdes_sched[i]);
  3196.         if ( rc == -1 ) {
  3197.             printf("?Invalid DES key specified for encryption [DES3,%s]\r\n",
  3198.                     server?"server":"client");
  3199.             debug(F110,"des3_fb64_stream_iv",
  3200.                    "invalid DES Key specified for encryption",0);
  3201.         } else if ( rc == -2 ) {
  3202.             printf("?Weak DES key specified for encryption\r\n");
  3203.             debug(F110,"des3_fb64_stream_iv",
  3204.                    "weak DES Key specified for encryption",0);
  3205.         } else if ( rc != 0 ) {
  3206.             printf("?Key Schedule not created by encryption\r\n");
  3207.             debug(F110,"des3_fb64_stream_iv",
  3208.                    "Key Schedule not created by encryption",0);
  3209.         }
  3210.         hexdump("des3_fb64_session_key schedule",fbp->krbdes_sched[i],8*16);
  3211.     }
  3212.     /*
  3213.     * Now look to see if krbdes_start() was was waiting for
  3214.     * the key to show up.  If so, go ahead an call it now
  3215.     * that we have the key.
  3216.     */
  3217.     if (fbp->need_start) {
  3218.         fbp->need_start = 0;
  3219.         des3_fb64_start(fbp, DIR_ENCRYPT, server);
  3220.     }
  3221.     return(0);
  3222. }
  3223.  
  3224. /*
  3225.  * We only accept a keyid of 0.  If we get a keyid of
  3226.  * 0, then mark the state as SUCCESS.
  3227.  */
  3228. int
  3229. des3_cfb64_keyid(dir, kp, lenp)
  3230.     int dir, *lenp;
  3231.     unsigned char *kp;
  3232. {
  3233.     return(des3_fb64_keyid(dir, kp, lenp, &des3_fb[CFB]));
  3234. }
  3235.  
  3236. int
  3237. des3_ofb64_keyid(dir, kp, lenp)
  3238.     int dir, *lenp;
  3239.     unsigned char *kp;
  3240. {
  3241.     return(des3_fb64_keyid(dir, kp, lenp, &des3_fb[OFB]));
  3242. }
  3243.  
  3244. int
  3245. des3_fb64_keyid(dir, kp, lenp, fbp)
  3246.     int dir, *lenp;
  3247.     unsigned char *kp;
  3248.     struct des3_fb *fbp;
  3249. {
  3250.     register int state = fbp->state[dir-1];
  3251.  
  3252.     if (*lenp != 1 || (*kp != '\0')) {
  3253.         *lenp = 0;
  3254.         return(state);
  3255.     }
  3256.  
  3257.     if (state == xFAILED)
  3258.         state = IN_PROGRESS;
  3259.  
  3260.     state &= ~NO_KEYID;
  3261.  
  3262.     return(fbp->state[dir-1] = state);
  3263. }
  3264.  
  3265. #if 0
  3266. void
  3267. des3_fb64_printsub(data, cnt, buf, buflen, type)
  3268.     unsigned char *data, *buf, *type;
  3269.     int cnt, buflen;
  3270. {
  3271.     char lbuf[64];
  3272.     register int i;
  3273.     char *cp;
  3274.  
  3275.     buf[buflen-1] = '\0';               /* make sure it's NULL terminated */
  3276.     buflen -= 1;
  3277.  
  3278.     switch(data[2]) {
  3279.     case FB64_IV:
  3280.         sprintf(lbuf, "%s_IV", type);
  3281.         cp = lbuf;
  3282.         goto common;
  3283.  
  3284.     case FB64_IV_OK:
  3285.         sprintf(lbuf, "%s_IV_OK", type);
  3286.         cp = lbuf;
  3287.         goto common;
  3288.  
  3289.     case FB64_IV_BAD:
  3290.         sprintf(lbuf, "%s_IV_BAD", type);
  3291.         cp = lbuf;
  3292.         goto common;
  3293.  
  3294.     case FB64_CHALLENGE:
  3295.         sprintf(lbuf, "%s_CHALLENGE", type);
  3296.         cp = lbuf;
  3297.         goto common;
  3298.  
  3299.     case FB64_RESPONSE:
  3300.         sprintf(lbuf, "%s_RESPONSE", type);
  3301.         cp = lbuf;
  3302.         goto common;
  3303.  
  3304.     default:
  3305.         sprintf(lbuf, " %d (unknown)", data[2]);
  3306.         cp = lbuf;
  3307.       common:
  3308.         for (; (buflen > 0) && (*buf = *cp++); buf++)
  3309.             buflen--;
  3310.         for (i = 3; i < cnt; i++) {
  3311.             sprintf(lbuf, " %d", data[i]);
  3312.             for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  3313.                 buflen--;
  3314.         }
  3315.         break;
  3316.     }
  3317. }
  3318.  
  3319. void
  3320. des3_cfb64_printsub(data, cnt, buf, buflen)
  3321.     unsigned char *data, *buf;
  3322.     int cnt, buflen;
  3323. {
  3324.     des3_fb64_printsub(data, cnt, buf, buflen, "CFB64");
  3325. }
  3326.  
  3327. void
  3328. des3_ofb64_printsub(data, cnt, buf, buflen)
  3329.     unsigned char *data, *buf;
  3330.     int cnt, buflen;
  3331. {
  3332.     des3_fb64_printsub(data, cnt, buf, buflen, "OFB64");
  3333. }
  3334. #endif
  3335.  
  3336. void
  3337. des3_fb64_stream_iv(seed, stp)
  3338.     Block seed;
  3339.     register struct des3_stinfo *stp;
  3340. {
  3341.     int rc=0, i = 0;;
  3342.  
  3343.     memcpy(stp->str_iv,     seed, sizeof(Block));
  3344.     memcpy(stp->str_output, seed, sizeof(Block));
  3345.     for ( i=0;i<3;i++ ) {
  3346.         memset(stp->str_sched[i],0,sizeof(Schedule));
  3347.  
  3348.         hexdump("des3_fb64_stream_iv",stp->str_ikey[i],8);
  3349.  
  3350.         rc = des_key_sched(stp->str_ikey[i], stp->str_sched[i]);
  3351.         if ( rc == -1 ) {
  3352.             printf("?Invalid DES key specified for encryption [DES3 iv]\r\n");
  3353.             debug(F110,"des3_fb64_stream_iv",
  3354.                    "invalid DES Key specified for encryption",0);
  3355.         } else if ( rc == -2 ) {
  3356.             printf("?Weak DES key specified for encryption\r\n");
  3357.             debug(F110,"des3_fb64_stream_iv",
  3358.                    "weak DES Key specified for encryption",0);
  3359.         } else if ( rc != 0 ) {
  3360.             printf("?Key Schedule not created by encryption\r\n");
  3361.             debug(F110,"des3_fb64_stream_iv",
  3362.                    "Key Schedule not created by encryption",0);
  3363.         }
  3364.         hexdump("des3_fb64_stream_iv schedule",stp->str_sched[i],8*16);
  3365.     }
  3366.     stp->str_index = sizeof(Block);
  3367. }
  3368.  
  3369. void
  3370. des3_fb64_stream_key(key, stp)
  3371.     Block * key;
  3372.     register struct des3_stinfo *stp;
  3373. {
  3374.     int rc = 0, i = 0;
  3375.  
  3376.     for ( i=0;i<3;i++ ) {
  3377.         memcpy(stp->str_ikey[i], key[i], sizeof(Block));
  3378.  
  3379.         memset(stp->str_sched[i],0,sizeof(Schedule));
  3380.  
  3381.         hexdump("des3_fb64_stream_key",key[i],8);
  3382.  
  3383.         rc = des_key_sched(key[i], stp->str_sched[i]);
  3384.         if ( rc == -1 ) {
  3385.             printf("?Invalid DES key specified for encryption [DES3 key]\r\n");
  3386.             debug(F110,"des3_fb64_stream_iv",
  3387.                    "invalid DES Key specified for encryption",0);
  3388.         } else if ( rc == -2 ) {
  3389.             printf("?Weak DES key specified for encryption\r\n");
  3390.             debug(F110,"des3_fb64_stream_iv",
  3391.                    "weak DES Key specified for encryption",0);
  3392.         } else if ( rc != 0 ) {
  3393.             printf("?Key Schedule not created by encryption\r\n");
  3394.             debug(F110,"des3_fb64_stream_iv",
  3395.                    "Key Schedule not created by encryption",0);
  3396.         }
  3397.         hexdump("des3_fb64_stream_key schedule",stp->str_sched[i],8*16);
  3398.     }
  3399.  
  3400.     memcpy(stp->str_output, stp->str_iv, sizeof(Block));
  3401.     stp->str_index = sizeof(Block);
  3402. }
  3403.  
  3404. /*
  3405.  * DES3 64 bit Cipher Feedback
  3406.  *
  3407.  *                key1       key2       key3
  3408.  *                 |          |          |
  3409.  *                 v          v          v
  3410.  *             +-------+  +-------+  +-------+
  3411.  *          +->| DES-e |->| DES-d |->| DES-e |-- +
  3412.  *          |  +-------+  +-------+  +-------+   |
  3413.  *          |                                    v
  3414.  *  INPUT --(-------------------------------->(+)+---> DATA
  3415.  *          |                                    |
  3416.  *          +------------------------------------+
  3417.  *
  3418.  *
  3419.  * Given:
  3420.  *      iV: Initial vector, 64 bits (8 bytes) long.
  3421.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  3422.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  3423.  *
  3424.  *      V0 = DES-e(DES-d(DES-e(iV, key1),key2),key3)
  3425.  *      On = Dn ^ Vn
  3426.  *      V(n+1) = DES-e(DES-d(DES-e(On, key1),key2),key3)
  3427.  */
  3428.  
  3429. void
  3430. des3_cfb64_encrypt(s, c)
  3431.     register unsigned char *s;
  3432.     int c;
  3433. {
  3434.     register struct des3_stinfo *stp = &des3_fb[CFB].streams[DIR_ENCRYPT-1];
  3435.     register int index;
  3436.  
  3437.     index = stp->str_index;
  3438.     while (c-- > 0) {
  3439.         if (index == sizeof(Block)) {
  3440.             Block b;
  3441. #ifdef LIBDES
  3442.             des_ecb3_encrypt(stp->str_output, b, stp->str_sched[0],
  3443.                               stp->str_sched[1], stp->str_sched[2], 1);
  3444. #else /* LIBDES */
  3445.             des_ecb_encrypt(stp->str_output, b,
  3446.                              stp->str_sched[0], 1);
  3447.             des_ecb_encrypt(stp->str_output, b,
  3448.                              stp->str_sched[1], 0);
  3449.             des_ecb_encrypt(stp->str_output, b,
  3450.                              stp->str_sched[2], 1);
  3451. #endif /* LIBDES */
  3452.             memcpy(stp->str_feed,b,sizeof(Block));
  3453.             index = 0;
  3454.         }
  3455.  
  3456.         /* On encryption, we store (feed ^ data) which is cypher */
  3457.         *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  3458.         s++;
  3459.         index++;
  3460.     }
  3461.     stp->str_index = index;
  3462. }
  3463.  
  3464. int
  3465. des3_cfb64_decrypt(data)
  3466.     int data;
  3467. {
  3468.     register struct des3_stinfo *stp = &des3_fb[CFB].streams[DIR_DECRYPT-1];
  3469.     int index;
  3470.  
  3471.     if (data == -1) {
  3472.         /*
  3473.         * Back up one byte.  It is assumed that we will
  3474.         * never back up more than one byte.  If we do, this
  3475.         * may or may not work.
  3476.         */
  3477.         if (stp->str_index)
  3478.             --stp->str_index;
  3479.         return(0);
  3480.     }
  3481.  
  3482.     index = stp->str_index++;
  3483.     if (index == sizeof(Block)) {
  3484.         Block b;
  3485. #ifdef LIBDES
  3486.         des_ecb3_encrypt(stp->str_output, b, stp->str_sched[0],
  3487.                           stp->str_sched[1], stp->str_sched[2], 1);
  3488. #else /* LIBDES */
  3489.             des_ecb_encrypt(stp->str_output, b,
  3490.                              stp->str_sched[0], 1);
  3491.             des_ecb_encrypt(stp->str_output, b,
  3492.                              stp->str_sched[1], 0);
  3493.             des_ecb_encrypt(stp->str_output, b,
  3494.                              stp->str_sched[2], 1);
  3495. #endif /* LIBDES */
  3496.         memcpy(stp->str_feed, b, sizeof(Block));
  3497.         stp->str_index = 1;     /* Next time will be 1 */
  3498.         index = 0;              /* But now use 0 */
  3499.     }
  3500.  
  3501.     /* On decryption we store (data) which is cypher. */
  3502.     stp->str_output[index] = data;
  3503.     return(data ^ stp->str_feed[index]);
  3504. }
  3505.  
  3506. /*
  3507.  * DES3 64 bit Output Feedback
  3508.  *
  3509.  *
  3510.  *                key1       key2       key3
  3511.  *                 |          |          |
  3512.  *                 v          v          v
  3513.  *             +-------+  +-------+  +-------+
  3514.  *          +->| DES-e |->| DES-d |->| DES-e |-- +
  3515.  *          |  +-------+  +-------+  +-------+   |
  3516.  *          +------------------------------------+
  3517.  *                                               v
  3518.  *  INPUT ------------------------------------->(+) ----> DATA
  3519.  *
  3520.  * Given:
  3521.  *      iV: Initial vector, 64 bits (8 bytes) long.
  3522.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  3523.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  3524.  *
  3525.  *      V0 = DES-e(DES-d(DES-e(iV, key1),key2),key3)
  3526.  *      V(n+1) = DES-e(DES-d(DES-e(Vn, key1),key2),key3)
  3527.  *      On = Dn ^ Vn
  3528.  */
  3529. void
  3530. des3_ofb64_encrypt(s, c)
  3531.     register unsigned char *s;
  3532.     int c;
  3533. {
  3534.     register struct des3_stinfo *stp = &des3_fb[OFB].streams[DIR_ENCRYPT-1];
  3535.     register int index;
  3536.  
  3537.     index = stp->str_index;
  3538.     while (c-- > 0) {
  3539.         if (index == sizeof(Block)) {
  3540.             Block b;
  3541. #ifdef LIBDES
  3542.             des_ecb3_encrypt(stp->str_feed, b, stp->str_sched[0],
  3543.                              stp->str_sched[1], stp->str_sched[2], 1);
  3544. #else /* LIBDES */
  3545.             des_ecb_encrypt(stp->str_output, b,
  3546.                              stp->str_sched[0], 1);
  3547.             des_ecb_encrypt(stp->str_output, b,
  3548.                              stp->str_sched[1], 0);
  3549.             des_ecb_encrypt(stp->str_output, b,
  3550.                              stp->str_sched[2], 1);
  3551. #endif /* LIBDES */
  3552.             memcpy(stp->str_feed,b,sizeof(Block));
  3553.             index = 0;
  3554.         }
  3555.         *s++ ^= stp->str_feed[index];
  3556.         index++;
  3557.     }
  3558.     stp->str_index = index;
  3559. }
  3560.  
  3561. int
  3562. des3_ofb64_decrypt(data)
  3563.     int data;
  3564. {
  3565.     register struct des3_stinfo *stp = &des3_fb[OFB].streams[DIR_DECRYPT-1];
  3566.     int index;
  3567.  
  3568.     if (data == -1) {
  3569.         /*
  3570.         * Back up one byte.  It is assumed that we will
  3571.         * never back up more than one byte.  If we do, this
  3572.         * may or may not work.
  3573.         */
  3574.         if (stp->str_index)
  3575.             --stp->str_index;
  3576.         return(0);
  3577.     }
  3578.  
  3579.     index = stp->str_index++;
  3580.     if (index == sizeof(Block)) {
  3581.         Block b;
  3582. #ifdef LIBDES
  3583.         des_ecb3_encrypt(stp->str_feed, b, stp->str_sched[0],
  3584.                           stp->str_sched[1], stp->str_sched[2], 1);
  3585. #else /* LIBDES */
  3586.             des_ecb_encrypt(stp->str_output, b,
  3587.                              stp->str_sched[0], 1);
  3588.             des_ecb_encrypt(stp->str_output, b,
  3589.                              stp->str_sched[1], 0);
  3590.             des_ecb_encrypt(stp->str_output, b,
  3591.                              stp->str_sched[2], 1);
  3592. #endif /* LIBDES */
  3593.         memcpy(stp->str_feed, b, sizeof(Block));
  3594.         stp->str_index = 1;     /* Next time will be 1 */
  3595.         index = 0;              /* But now use 0 */
  3596.     }
  3597.  
  3598.     return(data ^ stp->str_feed[index]);
  3599. }
  3600. #endif /* CK_DES */
  3601.  
  3602. #ifdef CK_CAST
  3603. /*-
  3604.  * Copyright (c) 1991, 1993
  3605.  *      The Regents of the University of California.  All rights reserved.
  3606.  *
  3607.  * Redistribution and use in source and binary forms, with or without
  3608.  * modification, are permitted provided that the following conditions
  3609.  * are met:
  3610.  * 1. Redistributions of source code must retain the above copyright
  3611.  *    notice, this list of conditions and the following disclaimer.
  3612.  * 2. Redistributions in binary form must reproduce the above copyright
  3613.  *    notice, this list of conditions and the following disclaimer in the
  3614.  *    documentation and/or other materials provided with the distribution.
  3615.  * 3. All advertising materials mentioning features or use of this software
  3616.  *    must display the following acknowledgement:
  3617.  *      This product includes software developed by the University of
  3618.  *      California, Berkeley and its contributors.
  3619.  * 4. Neither the name of the University nor the names of its contributors
  3620.  *    may be used to endorse or promote products derived from this software
  3621.  *    without specific prior written permission.
  3622.  *
  3623.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  3624.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3625.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3626.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  3627.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  3628.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  3629.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  3630.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  3631.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  3632.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  3633.  * SUCH DAMAGE.
  3634.  */
  3635.  
  3636. /*
  3637.  * Copyright (c) 1997 Stanford University
  3638.  *
  3639.  * Permission to use, copy, modify, distribute, and sell this software and
  3640.  * its documentation for any purpose is hereby granted without fee, provided
  3641.  * that the above copyright notices and this permission notice appear in
  3642.  * all copies of the software and related documentation.
  3643.  *
  3644.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  3645.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  3646.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  3647.  *
  3648.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  3649.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  3650.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  3651.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  3652.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  3653.  */
  3654.  
  3655. #include <stdio.h>
  3656. #ifdef  __STDC__
  3657. #include <stdlib.h>
  3658. #endif
  3659.  
  3660. /*
  3661.  * cast.h
  3662.  * Author: Tom Wu
  3663.  *
  3664.  * Type and function declarations for CAST.
  3665.  */
  3666.  
  3667. #ifndef _CAST_H_
  3668. #define _CAST_H_
  3669.  
  3670. #ifndef P
  3671. #ifdef __STDC__
  3672. #define P(x) x
  3673. #else
  3674. #define P(x) ()
  3675. #endif /* __STDC__ */
  3676. #endif /* P */
  3677.  
  3678. #ifndef LITTLE_ENDIAN
  3679. #ifndef BIG_ENDIAN
  3680. #ifndef WORDS_BIGENDIAN
  3681. #define LITTLE_ENDIAN 1
  3682. #endif /* WORDS_BIGENDIAN */
  3683. #endif /* BIG_ENDIAN */
  3684. #endif /* LITTLE_ENDIAN */
  3685.  
  3686. typedef unsigned int uint32;    /* Must be 32 bits */
  3687. typedef uint32 * uint32p;
  3688. typedef unsigned char uint8;
  3689. typedef uint8 * uint8p;
  3690.  
  3691. typedef struct {
  3692.   struct CastSubkeyPair {
  3693.     uint32 Km;
  3694.     uint32 Kr;
  3695.   } K[16];
  3696.   int ksize;
  3697. } CastKeySched;
  3698.  
  3699. /*
  3700.  * cast*_key_sched(schedule, key)
  3701.  *
  3702.  * Initializes the CAST key schedule "schedule" according to the given key.
  3703.  * The different setup routines accept different length keys:
  3704.  *
  3705.  *   ck_cast5_40_key_sched: 40-bit/5-byte (12 round) keys
  3706.  *   ck_cast5_64_key_sched: 64-bit/8-byte (12 round) keys
  3707.  *   ck_cast5_80_key_sched: 80-bit/10-byte (12 round) keys
  3708.  *   ck_cast128_key_sched: 128-bit/16-byte (16 round) keys
  3709.  */
  3710.  
  3711. extern void ck_cast5_40_key_sched P((CastKeySched *, uint8 *));
  3712. extern void ck_cast5_64_key_sched P((CastKeySched *, uint8 *));
  3713. extern void ck_cast5_80_key_sched P((CastKeySched *, uint8 *));
  3714. extern void  ck_cast128_key_sched P((CastKeySched *, uint8 *));
  3715.  
  3716. /*
  3717.  * ck_cast_ecb_encrypt(output, input, schedule, mode)
  3718.  * ck_cast_ecb_crypt(data, schedule, mode)
  3719.  *
  3720.  * Encrypts the 64-bit "input" according to the CAST key schedule
  3721.  * "schedule" and places the result in "output".  If "mode" is 0,
  3722.  * ck_cast_ecb_encrypt will encrypt, otherwise it will decrypt.
  3723.  * "Output" and "input" can point to the same memory, in which case
  3724.  * en/decryption will be performed in place.
  3725.  *
  3726.  * ck_cast_ecb_crypt accepts input in the form of an array of two
  3727.  * 32-bit words and performs encryption/decryption in place.
  3728.  */
  3729.  
  3730. extern void ck_cast_ecb_encrypt P((uint8 *, uint8 *, CastKeySched *, int));
  3731. extern void ck_cast_ecb_crypt P((uint32 *, CastKeySched *, int));
  3732.  
  3733. #endif /* CAST_H */
  3734.  
  3735. extern encrypt_debug_mode;
  3736.  
  3737. #define CFB_40  0
  3738. #define OFB_40  1
  3739. #ifdef CAST_EXPORT_ENCRYPTION
  3740. #define FB_CNT  2
  3741. #else
  3742. #define CFB_128 2
  3743. #define OFB_128 3
  3744. #define FB_CNT  4
  3745. #endif
  3746.  
  3747. #define NO_SEND_IV      1
  3748. #define NO_RECV_IV      2
  3749. #define NO_KEYID        4
  3750. #define IN_PROGRESS     (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
  3751. #define SUCCESS         0
  3752. #define cFAILED         -1
  3753.  
  3754.  
  3755. struct cast_fb {
  3756.         Block temp_feed;
  3757.         unsigned char fb_feed[64];
  3758.         int key_isset;
  3759.         int need_start;
  3760.         int state[2];
  3761.         struct cast_stinfo {
  3762.                 Block           str_output;
  3763.                 Block           str_feed;
  3764.                 Block           str_iv;
  3765.                 CastKeySched    str_sched;
  3766.                 int             str_index;
  3767.         } streams[2];
  3768. };
  3769.  
  3770. static struct cast_fb cast_fb[FB_CNT];
  3771.  
  3772. #define FB64_IV         1
  3773. #define FB64_IV_OK      2
  3774. #define FB64_IV_BAD     3
  3775.  
  3776.  
  3777. static void cast_fb64_stream_iv P((Block, struct cast_stinfo *));
  3778. static void cast_fb64_init P((struct cast_fb *));
  3779. static int cast_fb64_start P((struct cast_fb *, int, int));
  3780. static int cast_fb64_is P((unsigned char *, int, struct cast_fb *));
  3781. static int cast_fb64_reply P((unsigned char *, int, struct cast_fb *));
  3782. static int cast_fb64_session P((Session_Key *, int, struct cast_fb *, int));
  3783. static void cast_fb64_stream_key P((Block, struct cast_stinfo *, int));
  3784. static int cast_fb64_keyid P((int, unsigned char *, int *, struct cast_fb *));
  3785. static void _cast_cfb64_encrypt P((unsigned char *,int, struct cast_stinfo *));
  3786. static int _cast_cfb64_decrypt P((int, struct cast_stinfo *));
  3787. static void _cast_ofb64_encrypt P((unsigned char *,int, struct cast_stinfo *));
  3788. static int _cast_ofb64_decrypt P((int, struct cast_stinfo *));
  3789.  
  3790. #ifndef CAST_EXPORT_ENCRYPTION
  3791. void
  3792. cast_cfb64_init(server)
  3793.         int server;
  3794. {
  3795.         cast_fb64_init(&cast_fb[CFB_128]);
  3796.         cast_fb[CFB_128].fb_feed[4] = ENCTYPE_CAST128_CFB64;
  3797. }
  3798.  
  3799. void
  3800. cast_ofb64_init(server)
  3801.         int server;
  3802. {
  3803.         cast_fb64_init(&cast_fb[OFB_128]);
  3804.         cast_fb[OFB_128].fb_feed[4] = ENCTYPE_CAST128_OFB64;
  3805. }
  3806. #endif
  3807.  
  3808. void
  3809. castexp_cfb64_init(server)
  3810.         int server;
  3811. {
  3812.         cast_fb64_init(&cast_fb[CFB_40]);
  3813.         cast_fb[CFB_40].fb_feed[4] = ENCTYPE_CAST5_40_CFB64;
  3814. }
  3815.  
  3816. void
  3817. castexp_ofb64_init(server)
  3818.         int server;
  3819. {
  3820.         cast_fb64_init(&cast_fb[OFB_40]);
  3821.         cast_fb[OFB_40].fb_feed[4] = ENCTYPE_CAST5_40_OFB64;
  3822. }
  3823.  
  3824. static void
  3825. cast_fb64_init(fbp)
  3826.     register struct cast_fb *fbp;
  3827. {
  3828.     memset((void *)fbp, 0, sizeof(*fbp));
  3829.     fbp->key_isset = 0;
  3830.     fbp->state[0] = fbp->state[1] = cFAILED;
  3831.     fbp->fb_feed[0] = IAC;
  3832.     fbp->fb_feed[1] = SB;
  3833.     fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  3834.     fbp->fb_feed[3] = ENCRYPT_IS;
  3835. }
  3836.  
  3837. /*
  3838.  * Returns:
  3839.  *      -1: some error.  Negotiation is done, encryption not ready.
  3840.  *       0: Successful, initial negotiation all done.
  3841.  *       1: successful, negotiation not done yet.
  3842.  *       2: Not yet.  Other things (like getting the key from
  3843.  *          Kerberos) have to happen before we can continue.
  3844.  */
  3845. #ifndef CAST_EXPORT_ENCRYPTION
  3846. int
  3847. cast_cfb64_start(dir, server)
  3848.     int dir;
  3849.     int server;
  3850. {
  3851.     return(cast_fb64_start(&cast_fb[CFB_128], dir, server));
  3852. }
  3853.  
  3854. int
  3855. cast_ofb64_start(dir, server)
  3856.     int dir;
  3857.     int server;
  3858. {
  3859.     return(cast_fb64_start(&cast_fb[OFB_128], dir, server));
  3860. }
  3861. #endif
  3862.  
  3863. int
  3864. castexp_cfb64_start(dir, server)
  3865.     int dir;
  3866.     int server;
  3867. {
  3868.     return(cast_fb64_start(&cast_fb[CFB_40], dir, server));
  3869. }
  3870.  
  3871. int
  3872. castexp_ofb64_start(dir, server)
  3873.     int dir;
  3874.     int server;
  3875. {
  3876.     return(cast_fb64_start(&cast_fb[OFB_40], dir, server));
  3877. }
  3878.  
  3879. static int
  3880. cast_fb64_start(fbp, dir, server)
  3881.     struct cast_fb *fbp;
  3882.     int dir;
  3883.     int server;
  3884. {
  3885.     Block b;
  3886.     int x;
  3887.     unsigned char *p;
  3888.     register int state;
  3889.  
  3890.     switch (dir) {
  3891.     case DIR_DECRYPT:
  3892.         /*
  3893.          * This is simply a request to have the other side
  3894.          * start output (our input).  He will negotiate an
  3895.          * IV so we need not look for it.
  3896.          */
  3897.         state = fbp->state[dir-1];
  3898.         if (state == cFAILED)
  3899.             state = IN_PROGRESS;
  3900.         break;
  3901.  
  3902.     case DIR_ENCRYPT:
  3903.         state = fbp->state[dir-1];
  3904.         if (state == cFAILED)
  3905.             state = IN_PROGRESS;
  3906.         else if ((state & NO_SEND_IV) == 0)
  3907.             break;
  3908.  
  3909.         if (!fbp->key_isset) {
  3910.             fbp->need_start = 1;
  3911.             break;
  3912.         }
  3913.         state &= ~NO_SEND_IV;
  3914.         state |= NO_RECV_IV;
  3915. #ifdef DEBUG
  3916.         if (encrypt_debug_mode)
  3917.             printf("Creating new feed\r\n");
  3918. #endif
  3919.         /*
  3920.          * Create a random feed and send it over.
  3921.          */
  3922.         ck_cast_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  3923.                           &fbp->streams[dir-1].str_sched, 0);
  3924.  
  3925.         p = fbp->fb_feed + 3;
  3926.         *p++ = ENCRYPT_IS;
  3927.         p++;
  3928.         *p++ = FB64_IV;
  3929.         for (x = 0; x < sizeof(Block); ++x) {
  3930.             if ((*p++ = fbp->temp_feed[x]) == IAC)
  3931.                 *p++ = IAC;
  3932.         }
  3933.         *p++ = IAC;
  3934.         *p++ = SE;
  3935.  
  3936.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  3937.         break;
  3938.     default:
  3939.         return(cFAILED);
  3940.     }
  3941.     return(fbp->state[dir-1] = state);
  3942. }
  3943.  
  3944. /*
  3945.  * Returns:
  3946.  *      -1: some error.  Negotiation is done, encryption not ready.
  3947.  *       0: Successful, initial negotiation all done.
  3948.  *       1: successful, negotiation not done yet.
  3949.  */
  3950. #ifndef CAST_EXPORT_ENCRYPTION
  3951. int
  3952. cast_cfb64_is(data, cnt)
  3953.     unsigned char *data;
  3954.     int cnt;
  3955. {
  3956.     return(cast_fb64_is(data, cnt, &cast_fb[CFB_128]));
  3957. }
  3958.  
  3959. int
  3960. cast_ofb64_is(data, cnt)
  3961.     unsigned char *data;
  3962.     int cnt;
  3963. {
  3964.     return(cast_fb64_is(data, cnt, &cast_fb[OFB_128]));
  3965. }
  3966. #endif
  3967.  
  3968. int
  3969. castexp_cfb64_is(data, cnt)
  3970.     unsigned char *data;
  3971.     int cnt;
  3972. {
  3973.     return(cast_fb64_is(data, cnt, &cast_fb[CFB_40]));
  3974. }
  3975.  
  3976. int
  3977. castexp_ofb64_is(data, cnt)
  3978.     unsigned char *data;
  3979.     int cnt;
  3980. {
  3981.     return(cast_fb64_is(data, cnt, &cast_fb[OFB_40]));
  3982. }
  3983.  
  3984. static int
  3985. cast_fb64_is(data, cnt, fbp)
  3986.     unsigned char *data;
  3987.     int cnt;
  3988.     struct cast_fb *fbp;
  3989. {
  3990.     int x;
  3991.     unsigned char *p;
  3992.     Block b;
  3993.     register int state = fbp->state[DIR_DECRYPT-1];
  3994.  
  3995.     if (cnt-- < 1)
  3996.         goto failure;
  3997.  
  3998. #ifdef CK_SSL
  3999.     if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  4000. #endif /* CK_SSL */
  4001.     switch (*data++) {
  4002.     case FB64_IV:
  4003.         if (cnt != sizeof(Block)) {
  4004. #ifdef DEBUG
  4005.             if (encrypt_debug_mode)
  4006.                 printf("FB64: initial vector failed on size\r\n");
  4007. #endif
  4008.             state = cFAILED;
  4009.             goto failure;
  4010.         }
  4011. #ifdef DEBUG
  4012.         if (encrypt_debug_mode)
  4013.             printf("FB64: initial vector received\r\n");
  4014.  
  4015.         if (encrypt_debug_mode)
  4016.             printf("Initializing Decrypt stream\r\n");
  4017. #endif
  4018.         cast_fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  4019.  
  4020.         p = fbp->fb_feed + 3;
  4021.         *p++ = ENCRYPT_REPLY;
  4022.         p++;
  4023.         *p++ = FB64_IV_OK;
  4024.         *p++ = IAC;
  4025.         *p++ = SE;
  4026.  
  4027.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  4028.         state = IN_PROGRESS;
  4029.         break;
  4030.  
  4031.     default:
  4032.         /* unknown option type */
  4033.         /* FALL THROUGH */
  4034.       failure:
  4035.         /*
  4036.         * We failed.  Send an FB64_IV_BAD option
  4037.         * to the other side so it will know that
  4038.         * things failed.
  4039.         */
  4040.         p = fbp->fb_feed + 3;
  4041.         *p++ = ENCRYPT_REPLY;
  4042.         p++;
  4043.         *p++ = FB64_IV_BAD;
  4044.         *p++ = IAC;
  4045.         *p++ = SE;
  4046.  
  4047.         ttol(fbp->fb_feed, p - fbp->fb_feed);
  4048.         break;
  4049.     }
  4050.     return(fbp->state[DIR_DECRYPT-1] = state);
  4051. }
  4052.  
  4053. /*
  4054.  * Returns:
  4055.  *      -1: some error.  Negotiation is done, encryption not ready.
  4056.  *       0: Successful, initial negotiation all done.
  4057.  *       1: successful, negotiation not done yet.
  4058.  */
  4059. #ifndef CAST_EXPORT_ENCRYPTION
  4060. int
  4061. cast_cfb64_reply(data, cnt)
  4062.     unsigned char *data;
  4063.     int cnt;
  4064. {
  4065.     return(cast_fb64_reply(data, cnt, &cast_fb[CFB_128]));
  4066. }
  4067.  
  4068. int
  4069. cast_ofb64_reply(data, cnt)
  4070.     unsigned char *data;
  4071.     int cnt;
  4072. {
  4073.     return(cast_fb64_reply(data, cnt, &cast_fb[OFB_128]));
  4074. }
  4075. #endif
  4076.  
  4077. int
  4078. castexp_cfb64_reply(data, cnt)
  4079.     unsigned char *data;
  4080.     int cnt;
  4081. {
  4082.     return(cast_fb64_reply(data, cnt, &cast_fb[CFB_40]));
  4083. }
  4084.  
  4085. int
  4086. castexp_ofb64_reply(data, cnt)
  4087.     unsigned char *data;
  4088.     int cnt;
  4089. {
  4090.     return(cast_fb64_reply(data, cnt, &cast_fb[OFB_40]));
  4091. }
  4092.  
  4093. static int
  4094. cast_fb64_reply(data, cnt, fbp)
  4095.     unsigned char *data;
  4096.     int cnt;
  4097.     struct cast_fb *fbp;
  4098. {
  4099.     int x;
  4100.     unsigned char *p;
  4101.     Block b;
  4102.     register int state = fbp->state[DIR_ENCRYPT-1];
  4103.  
  4104.     if (cnt-- < 1)
  4105.         goto failure;
  4106.  
  4107.     switch (*data++) {
  4108.     case FB64_IV_OK:
  4109.         cast_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  4110.         if (state == cFAILED)
  4111.             state = IN_PROGRESS;
  4112.         state &= ~NO_RECV_IV;
  4113.         encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  4114.         break;
  4115.  
  4116.     case FB64_IV_BAD:
  4117.         memset(fbp->temp_feed, 0, sizeof(Block));
  4118.         cast_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  4119.         state = cFAILED;
  4120.         break;
  4121.  
  4122.     default:
  4123. #if 0
  4124.         if (encrypt_debug_mode) {
  4125.             printf("Unknown option type: %d\r\n", data[-1]);
  4126.             printd(data, cnt);
  4127.             printf("\r\n");
  4128.         }
  4129. #endif
  4130.         /* FALL THROUGH */
  4131.       failure:
  4132.         state = cFAILED;
  4133.         break;
  4134.     }
  4135.     return(fbp->state[DIR_ENCRYPT-1] = state);
  4136. }
  4137.  
  4138. #ifndef CAST_EXPORT_ENCRYPTION
  4139. int
  4140. cast_cfb64_session(key, server)
  4141.     Session_Key *key;
  4142.     int server;
  4143. {
  4144.     return(cast_fb64_session(key, server, &cast_fb[CFB_128], 1));
  4145. }
  4146.  
  4147. int
  4148. cast_ofb64_session(key, server)
  4149.     Session_Key *key;
  4150.     int server;
  4151. {
  4152.     return(cast_fb64_session(key, server, &cast_fb[OFB_128], 1));
  4153. }
  4154. #endif
  4155.  
  4156. int
  4157. castexp_cfb64_session(key, server)
  4158.     Session_Key *key;
  4159.     int server;
  4160. {
  4161.     return(cast_fb64_session(key, server, &cast_fb[CFB_40], 0));
  4162. }
  4163.  
  4164. int
  4165. castexp_ofb64_session(key, server)
  4166.     Session_Key *key;
  4167.     int server;
  4168. {
  4169.     return(cast_fb64_session(key, server, &cast_fb[OFB_40], 0));
  4170. }
  4171.  
  4172. #define CAST128_KEYLEN  16      /* 128 bits */
  4173. #define CAST5_40_KEYLEN  5      /*  40 bits */
  4174.  
  4175. static int
  4176. cast_fb64_session(key, server, fbp, fs)
  4177.     Session_Key *key;
  4178.     int server;
  4179.     struct cast_fb *fbp;
  4180.     int fs;
  4181. {
  4182.     int klen;
  4183.     unsigned char * kptr;
  4184.  
  4185.     if(fs)
  4186.         klen = CAST128_KEYLEN;
  4187.     else
  4188.         klen = CAST5_40_KEYLEN;
  4189.  
  4190.     if (!key || key->length < klen) {
  4191.         CHAR buf[80];
  4192.         sprintf(buf,"Can't set CAST session key (%d < %d)",
  4193.                 key ? key->length : 0, klen);                   /* safe */
  4194. #ifdef DEBUG
  4195.         if (encrypt_debug_mode)
  4196.             printf("%s\r\n",buf);
  4197. #endif
  4198.         debug(F110,"cast_fb64_session",buf,0);
  4199.         return(cFAILED);
  4200.     }
  4201.     if(key->length < 2 * klen)
  4202.         kptr = key->data;
  4203.     else
  4204.         kptr = key->data + klen;
  4205.  
  4206.     if(server) {
  4207.         cast_fb64_stream_key(kptr, &fbp->streams[DIR_ENCRYPT-1], fs);
  4208.         cast_fb64_stream_key(key->data, &fbp->streams[DIR_DECRYPT-1], fs);
  4209.     }
  4210.     else {
  4211.         cast_fb64_stream_key(kptr, &fbp->streams[DIR_DECRYPT-1], fs);
  4212.         cast_fb64_stream_key(key->data, &fbp->streams[DIR_ENCRYPT-1], fs);
  4213.     }
  4214.  
  4215.     /* Stuff leftovers into the feed */
  4216.     if(key->length >= 2 * klen + sizeof(Block))
  4217.         memcpy(fbp->temp_feed, key->data + 2 * klen, sizeof(Block));
  4218.     else {
  4219. #ifdef COMMENT
  4220.         /* This is a better way of erasing the password */
  4221.         /* but we do not want to link in libsrp         */
  4222.         t_random(fbp->temp_feed, sizeof(Block));
  4223. #else
  4224.         memset(fbp->temp_feed, 0, sizeof(Block));
  4225. #endif
  4226.     }
  4227.  
  4228.     fbp->key_isset = 1;
  4229.     /*
  4230.     * Now look to see if cast_fb64_start() was was waiting for
  4231.     * the key to show up.  If so, go ahead an call it now
  4232.     * that we have the key.
  4233.     */
  4234.     if (fbp->need_start) {
  4235.         fbp->need_start = 0;
  4236.         cast_fb64_start(fbp, DIR_ENCRYPT, server);
  4237.     }
  4238.     return(0);
  4239. }
  4240.  
  4241. /*
  4242.  * We only accept a keyid of 0.  If we get a keyid of
  4243.  * 0, then mark the state as SUCCESS.
  4244.  */
  4245. #ifndef CAST_EXPORT_ENCRYPTION
  4246. int
  4247. cast_cfb64_keyid(dir, kp, lenp)
  4248.     int dir, *lenp;
  4249.     unsigned char *kp;
  4250. {
  4251.     return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[CFB_128]));
  4252. }
  4253.  
  4254. int
  4255. cast_ofb64_keyid(dir, kp, lenp)
  4256.     int dir, *lenp;
  4257.     unsigned char *kp;
  4258. {
  4259.     return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[OFB_128]));
  4260. }
  4261. #endif
  4262.  
  4263. int
  4264. castexp_cfb64_keyid(dir, kp, lenp)
  4265.     int dir, *lenp;
  4266.     unsigned char *kp;
  4267. {
  4268.     return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[CFB_40]));
  4269. }
  4270.  
  4271. int
  4272. castexp_ofb64_keyid(dir, kp, lenp)
  4273.     int dir, *lenp;
  4274.     unsigned char *kp;
  4275. {
  4276.     return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[OFB_40]));
  4277. }
  4278.  
  4279. static int
  4280. cast_fb64_keyid(dir, kp, lenp, fbp)
  4281.     int dir, *lenp;
  4282.     unsigned char *kp;
  4283.     struct cast_fb *fbp;
  4284. {
  4285.     register int state = fbp->state[dir-1];
  4286.  
  4287.     if (*lenp != 1 || (*kp != '\0')) {
  4288.         *lenp = 0;
  4289.         return(state);
  4290.     }
  4291.  
  4292.     if (state == cFAILED)
  4293.         state = IN_PROGRESS;
  4294.  
  4295.     state &= ~NO_KEYID;
  4296.  
  4297.     return(fbp->state[dir-1] = state);
  4298. }
  4299.  
  4300. static void
  4301. cast_fb64_printsub(data, cnt, buf, buflen, type)
  4302.     unsigned char *data, *buf, *type;
  4303.     int cnt, buflen;
  4304. {
  4305.     char lbuf[64];
  4306.     register int i;
  4307.     char *cp;
  4308.  
  4309.     buf[buflen-1] = '\0';               /* make sure it's NULL terminated */
  4310.     buflen -= 1;
  4311.  
  4312.     switch(data[2]) {
  4313.     case FB64_IV:
  4314.         sprintf(lbuf, "%s_IV", type);
  4315.         cp = lbuf;
  4316.         goto common;
  4317.  
  4318.     case FB64_IV_OK:
  4319.         sprintf(lbuf, "%s_IV_OK", type);
  4320.         cp = lbuf;
  4321.         goto common;
  4322.  
  4323.     case FB64_IV_BAD:
  4324.         sprintf(lbuf, "%s_IV_BAD", type);
  4325.         cp = lbuf;
  4326.         goto common;
  4327.  
  4328.     default:
  4329.         sprintf(lbuf, " %d (unknown)", data[2]);
  4330.         cp = lbuf;
  4331.       common:
  4332.         for (; (buflen > 0) && (*buf = *cp++); buf++)
  4333.             buflen--;
  4334.         for (i = 3; i < cnt; i++) {
  4335.             sprintf(lbuf, " %d", data[i]);
  4336.             for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  4337.                 buflen--;
  4338.         }
  4339.         break;
  4340.     }
  4341. }
  4342.  
  4343. void
  4344. cast_cfb64_printsub(data, cnt, buf, buflen)
  4345.     unsigned char *data, *buf;
  4346.     int cnt, buflen;
  4347. {
  4348.     cast_fb64_printsub(data, cnt, buf, buflen, "CFB64");
  4349. }
  4350.  
  4351. void
  4352. cast_ofb64_printsub(data, cnt, buf, buflen)
  4353.     unsigned char *data, *buf;
  4354.     int cnt, buflen;
  4355. {
  4356.     cast_fb64_printsub(data, cnt, buf, buflen, "OFB64");
  4357. }
  4358.  
  4359. static void
  4360. cast_fb64_stream_iv(seed, stp)
  4361.     Block seed;
  4362.     register struct cast_stinfo *stp;
  4363. {
  4364.     memcpy((void *)stp->str_iv, (void *)seed, sizeof(Block));
  4365.     memcpy((void *)stp->str_output, (void *)seed, sizeof(Block));
  4366.  
  4367.     stp->str_index = sizeof(Block);
  4368. }
  4369.  
  4370. static void
  4371. cast_fb64_stream_key(key, stp, fs)
  4372.     unsigned char * key;
  4373.     register struct cast_stinfo *stp;
  4374.     int fs;
  4375. {
  4376. #ifndef CAST_EXPORT_ENCRYPTION
  4377.     if(fs)
  4378.         ck_cast128_key_sched(&stp->str_sched, key);
  4379.     else
  4380. #endif
  4381.         ck_cast5_40_key_sched(&stp->str_sched, key);
  4382.  
  4383.     memcpy((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
  4384.  
  4385.     stp->str_index = sizeof(Block);
  4386. }
  4387.  
  4388. /*
  4389.  * CAST 64 bit Cipher Feedback
  4390.  *
  4391.  *     key --->+------+
  4392.  *          +->| CAST |--+
  4393.  *          |  +------+  |
  4394.  *          |            v
  4395.  *  INPUT --(---------->(+)+---> DATA
  4396.  *          |              |
  4397.  *          +--------------+
  4398.  *
  4399.  *
  4400.  * Given:
  4401.  *      iV: Initial vector, 64 bits (8 bytes) long.
  4402.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  4403.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  4404.  *
  4405.  *      V0 = CAST(iV, key)
  4406.  *      On = Dn ^ Vn
  4407.  *      V(n+1) = CAST(On, key)
  4408.  */
  4409. #ifndef CAST_EXPORT_ENCRYPTION
  4410. void
  4411. cast_cfb64_encrypt(s, c)
  4412.         register unsigned char *s;
  4413.         int c;
  4414. {
  4415.     _cast_cfb64_encrypt(s, c, &cast_fb[CFB_128].streams[DIR_ENCRYPT-1]);
  4416. }
  4417. #endif
  4418.  
  4419. void
  4420. castexp_cfb64_encrypt(s, c)
  4421.     register unsigned char *s;
  4422.     int c;
  4423. {
  4424.     _cast_cfb64_encrypt(s, c, &cast_fb[CFB_40].streams[DIR_ENCRYPT-1]);
  4425. }
  4426.  
  4427. static void
  4428. _cast_cfb64_encrypt(s, c, stp)
  4429.     register unsigned char *s;
  4430.     int c;
  4431.     register struct cast_stinfo *stp;
  4432. {
  4433.     register int index;
  4434.  
  4435.     index = stp->str_index;
  4436.     while (c-- > 0) {
  4437.         if (index == sizeof(Block)) {
  4438.             Block b;
  4439.             ck_cast_ecb_encrypt(b, stp->str_output, &stp->str_sched, 0);
  4440.             memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4441.             index = 0;
  4442.         }
  4443.  
  4444.         /* On encryption, we store (feed ^ data) which is cypher */
  4445.         *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  4446.         s++;
  4447.         index++;
  4448.     }
  4449.     stp->str_index = index;
  4450. }
  4451.  
  4452. #ifndef CAST_EXPORT_ENCRYPTION
  4453. int
  4454. cast_cfb64_decrypt(data)
  4455.     int data;
  4456. {
  4457.     return _cast_cfb64_decrypt(data, &cast_fb[CFB_128].streams[DIR_DECRYPT-1]);
  4458. }
  4459. #endif
  4460.  
  4461. int
  4462. castexp_cfb64_decrypt(data)
  4463.     int data;
  4464. {
  4465.     return _cast_cfb64_decrypt(data, &cast_fb[CFB_40].streams[DIR_DECRYPT-1]);
  4466. }
  4467.  
  4468. static int
  4469. _cast_cfb64_decrypt(data, stp)
  4470.     int data;
  4471.     register struct cast_stinfo *stp;
  4472. {
  4473.     int index;
  4474.  
  4475.     if (data == -1) {
  4476.         /*
  4477.         * Back up one byte.  It is assumed that we will
  4478.         * never back up more than one byte.  If we do, this
  4479.         * may or may not work.
  4480.         */
  4481.         if (stp->str_index)
  4482.             --stp->str_index;
  4483.         return(0);
  4484.     }
  4485.  
  4486.     index = stp->str_index++;
  4487.     if (index == sizeof(Block)) {
  4488.         Block b;
  4489.         ck_cast_ecb_encrypt(b, stp->str_output, &stp->str_sched, 0);
  4490.         memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4491.         stp->str_index = 1;     /* Next time will be 1 */
  4492.         index = 0;              /* But now use 0 */
  4493.     }
  4494.  
  4495.     /* On decryption we store (data) which is cypher. */
  4496.     stp->str_output[index] = data;
  4497.     return(data ^ stp->str_feed[index]);
  4498. }
  4499.  
  4500. /*
  4501.  * CAST 64 bit Output Feedback
  4502.  *
  4503.  * key --->+------+
  4504.  *      +->| CAST |--+
  4505.  *      |  +------+  |
  4506.  *      +------------+
  4507.  *                   v
  4508.  *  INPUT --------->(+) ----> DATA
  4509.  *
  4510.  * Given:
  4511.  *      iV: Initial vector, 64 bits (8 bytes) long.
  4512.  *      Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  4513.  *      On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  4514.  *
  4515.  *      V0 = CAST(iV, key)
  4516.  *      V(n+1) = CAST(Vn, key)
  4517.  *      On = Dn ^ Vn
  4518.  */
  4519. #ifndef CAST_EXPORT_ENCRYPTION
  4520. void
  4521. cast_ofb64_encrypt(s, c)
  4522.     register unsigned char *s;
  4523.     int c;
  4524. {
  4525.     _cast_ofb64_encrypt(s, c, &cast_fb[OFB_128].streams[DIR_ENCRYPT-1]);
  4526. }
  4527. #endif
  4528.  
  4529. void
  4530. castexp_ofb64_encrypt(s, c)
  4531.     register unsigned char *s;
  4532.     int c;
  4533. {
  4534.   _cast_ofb64_encrypt(s, c, &cast_fb[OFB_40].streams[DIR_ENCRYPT-1]);
  4535. }
  4536.  
  4537. static void
  4538. _cast_ofb64_encrypt(s, c, stp)
  4539.     register unsigned char *s;
  4540.     int c;
  4541.     register struct cast_stinfo *stp;
  4542. {
  4543.     register int index;
  4544.  
  4545.     index = stp->str_index;
  4546.     while (c-- > 0) {
  4547.         if (index == sizeof(Block)) {
  4548.             Block b;
  4549.             ck_cast_ecb_encrypt(b, stp->str_feed, &stp->str_sched, 0);
  4550.             memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4551.             index = 0;
  4552.         }
  4553.         *s++ ^= stp->str_feed[index];
  4554.         index++;
  4555.     }
  4556.     stp->str_index = index;
  4557. }
  4558.  
  4559. #ifndef CAST_EXPORT_ENCRYPTION
  4560. int
  4561. cast_ofb64_decrypt(data)
  4562.     int data;
  4563. {
  4564.     return _cast_ofb64_decrypt(data, &cast_fb[OFB_128].streams[DIR_DECRYPT-1]);
  4565. }
  4566. #endif
  4567.  
  4568. int
  4569. castexp_ofb64_decrypt(data)
  4570.     int data;
  4571. {
  4572.     return _cast_ofb64_decrypt(data, &cast_fb[OFB_40].streams[DIR_DECRYPT-1]);
  4573. }
  4574.  
  4575. static int
  4576. _cast_ofb64_decrypt(data, stp)
  4577.     int data;
  4578.     register struct cast_stinfo *stp;
  4579. {
  4580.     int index;
  4581.  
  4582.     if (data == -1) {
  4583.         /*
  4584.         * Back up one byte.  It is assumed that we will
  4585.         * never back up more than one byte.  If we do, this
  4586.         * may or may not work.
  4587.         */
  4588.         if (stp->str_index)
  4589.             --stp->str_index;
  4590.         return(0);
  4591.     }
  4592.  
  4593.     index = stp->str_index++;
  4594.     if (index == sizeof(Block)) {
  4595.         Block b;
  4596.         ck_cast_ecb_encrypt(b, stp->str_feed, &stp->str_sched, 0);
  4597.         memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4598.         stp->str_index = 1;     /* Next time will be 1 */
  4599.         index = 0;              /* But now use 0 */
  4600.     }
  4601.  
  4602.     return(data ^ stp->str_feed[index]);
  4603. }
  4604.  
  4605. /*
  4606.  * Copyright (c) 1997 Stanford University
  4607.  *
  4608.  * Permission to use, copy, modify, distribute, and sell this software and
  4609.  * its documentation for any purpose is hereby granted without fee, provided
  4610.  * that the above copyright notices and this permission notice appear in
  4611.  * all copies of the software and related documentation.
  4612.  *
  4613.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  4614.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  4615.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  4616.  *
  4617.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  4618.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  4619.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  4620.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  4621.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  4622.  */
  4623.  
  4624. /*
  4625.  * cast.c
  4626.  * Author: Tom Wu
  4627.  *
  4628.  * An implementation of the CAST-128 encryption algorithm, as
  4629.  * specified in RFC 2144.
  4630.  */
  4631.  
  4632. /* The first four S-boxes are for encryption/decryption */
  4633.  
  4634. static uint32 S1[] = {
  4635.   0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9c004dd3,
  4636.   0x6003e540, 0xcf9fc949, 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675,
  4637.   0x6e63a0e0, 0x15c361d2, 0xc2e7661d, 0x22d4ff8e, 0x28683b6f, 0xc07fd059,
  4638.   0xff2379c8, 0x775f50e2, 0x43c340d3, 0xdf2f8656, 0x887ca41a, 0xa2d2bd2d,
  4639.   0xa1c9e0d6, 0x346c4819, 0x61b76d87, 0x22540f2f, 0x2abe32e1, 0xaa54166b,
  4640.   0x22568e3a, 0xa2d341d0, 0x66db40c8, 0xa784392f, 0x004dff2f, 0x2db9d2de,
  4641.   0x97943fac, 0x4a97c1d8, 0x527644b7, 0xb5f437a7, 0xb82cbaef, 0xd751d159,
  4642.   0x6ff7f0ed, 0x5a097a1f, 0x827b68d0, 0x90ecf52e, 0x22b0c054, 0xbc8e5935,
  4643.   0x4b6d2f7f, 0x50bb64a2, 0xd2664910, 0xbee5812d, 0xb7332290, 0xe93b159f,
  4644.   0xb48ee411, 0x4bff345d, 0xfd45c240, 0xad31973f, 0xc4f6d02e, 0x55fc8165,
  4645.   0xd5b1caad, 0xa1ac2dae, 0xa2d4b76d, 0xc19b0c50, 0x882240f2, 0x0c6e4f38,
  4646.   0xa4e4bfd7, 0x4f5ba272, 0x564c1d2f, 0xc59c5319, 0xb949e354, 0xb04669fe,
  4647.   0xb1b6ab8a, 0xc71358dd, 0x6385c545, 0x110f935d, 0x57538ad5, 0x6a390493,
  4648.   0xe63d37e0, 0x2a54f6b3, 0x3a787d5f, 0x6276a0b5, 0x19a6fcdf, 0x7a42206a,
  4649.   0x29f9d4d5, 0xf61b1891, 0xbb72275e, 0xaa508167, 0x38901091, 0xc6b505eb,
  4650.   0x84c7cb8c, 0x2ad75a0f, 0x874a1427, 0xa2d1936b, 0x2ad286af, 0xaa56d291,
  4651.   0xd7894360, 0x425c750d, 0x93b39e26, 0x187184c9, 0x6c00b32d, 0x73e2bb14,
  4652.   0xa0bebc3c, 0x54623779, 0x64459eab, 0x3f328b82, 0x7718cf82, 0x59a2cea6,
  4653.   0x04ee002e, 0x89fe78e6, 0x3fab0950, 0x325ff6c2, 0x81383f05, 0x6963c5c8,
  4654.   0x76cb5ad6, 0xd49974c9, 0xca180dcf, 0x380782d5, 0xc7fa5cf6, 0x8ac31511,
  4655.   0x35e79e13, 0x47da91d0, 0xf40f9086, 0xa7e2419e, 0x31366241, 0x051ef495,
  4656.   0xaa573b04, 0x4a805d8d, 0x548300d0, 0x00322a3c, 0xbf64cddf, 0xba57a68e,
  4657.   0x75c6372b, 0x50afd341, 0xa7c13275, 0x915a0bf5, 0x6b54bfab, 0x2b0b1426,
  4658.   0xab4cc9d7, 0x449ccd82, 0xf7fbf265, 0xab85c5f3, 0x1b55db94, 0xaad4e324,
  4659.   0xcfa4bd3f, 0x2deaa3e2, 0x9e204d02, 0xc8bd25ac, 0xeadf55b3, 0xd5bd9e98,
  4660.   0xe31231b2, 0x2ad5ad6c, 0x954329de, 0xadbe4528, 0xd8710f69, 0xaa51c90f,
  4661.   0xaa786bf6, 0x22513f1e, 0xaa51a79b, 0x2ad344cc,0x7b5a41f0, 0xd37cfbad,
  4662.   0x1b069505, 0x41ece491, 0xb4c332e6, 0x032268d4, 0xc9600acc, 0xce387e6d,
  4663.   0xbf6bb16c, 0x6a70fb78, 0x0d03d9c9, 0xd4df39de, 0xe01063da, 0x4736f464,
  4664.   0x5ad328d8, 0xb347cc96, 0x75bb0fc3, 0x98511bfb, 0x4ffbcc35, 0xb58bcf6a,
  4665.   0xe11f0abc, 0xbfc5fe4a, 0xa70aec10, 0xac39570a,0x3f04442f, 0x6188b153,
  4666.   0xe0397a2e, 0x5727cb79, 0x9ceb418f, 0x1cacd68d, 0x2ad37c96, 0x0175cb9d,
  4667.   0xc69dff09, 0xc75b65f0, 0xd9db40d8, 0xec0e7779, 0x4744ead4, 0xb11c3274,
  4668.   0xdd24cb9e, 0x7e1c54bd, 0xf01144f9, 0xd2240eb1, 0x9675b3fd, 0xa3ac3755,
  4669.   0xd47c27af, 0x51c85f4d, 0x56907596, 0xa5bb15e6,0x580304f0, 0xca042cf1,
  4670.   0x011a37ea, 0x8dbfaadb, 0x35ba3e4a, 0x3526ffa0, 0xc37b4d09, 0xbc306ed9,
  4671.   0x98a52666, 0x5648f725, 0xff5e569d, 0x0ced63d0, 0x7c63b2cf, 0x700b45e1,
  4672.   0xd5ea50f1, 0x85a92872, 0xaf1fbda7, 0xd4234870, 0xa7870bf3, 0x2d3b4d79,
  4673.   0x42e04198, 0x0cd0ede7, 0x26470db8, 0xf881814c,0x474d6ad7, 0x7c0c5e5c,
  4674.   0xd1231959, 0x381b7298, 0xf5d2f4db, 0xab838653, 0x6e2f1e23, 0x83719c9e,
  4675.   0xbd91e046, 0x9a56456e, 0xdc39200c, 0x20c8c571, 0x962bda1c, 0xe1e696ff,
  4676.   0xb141ab08, 0x7cca89b9, 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d,
  4677.   0x427b169c, 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf
  4678. };
  4679.  
  4680. static uint32 S2[] = {
  4681.   0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 0xeec5207a,
  4682.   0x55889c94, 0x72fc0651, 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba,
  4683.   0x99c430ef, 0x5f0c0794, 0x18dcdb7d, 0xa1d6eff3, 0xa0b52f7b, 0x59e83605,
  4684.   0xee15b094, 0xe9ffd909, 0xdc440086, 0xef944459, 0xba83ccb3, 0xe0c3cdfb,
  4685.   0xd1da4181, 0x3b092ab1, 0xf997f1c1, 0xa5e6cf7b, 0x01420ddb, 0xe4e7ef5b,
  4686.   0x25a1ff41, 0xe180f806, 0x1fc41080, 0x179bee7a, 0xd37ac6a9, 0xfe5830a4,
  4687.   0x98de8b7f, 0x77e83f4e, 0x79929269, 0x24fa9f7b, 0xe113c85b, 0xacc40083,
  4688.   0xd7503525, 0xf7ea615f, 0x62143154, 0x0d554b63, 0x5d681121, 0xc866c359,
  4689.   0x3d63cf73, 0xcee234c0, 0xd4d87e87, 0x5c672b21, 0x071f6181, 0x39f7627f,
  4690.   0x361e3084, 0xe4eb573b, 0x602f64a4, 0xd63acd9c, 0x1bbc4635, 0x9e81032d,
  4691.   0x2701f50c, 0x99847ab4, 0xa0e3df79, 0xba6cf38c, 0x10843094, 0x2537a95e,
  4692.   0xf46f6ffe, 0xa1ff3b1f, 0x208cfb6a, 0x8f458c74, 0xd9e0a227, 0x4ec73a34,
  4693.   0xfc884f69, 0x3e4de8df, 0xef0e0088, 0x3559648d, 0x8a45388c, 0x1d804366,
  4694.   0x721d9bfd, 0xa58684bb, 0xe8256333, 0x844e8212, 0x128d8098, 0xfed33fb4,
  4695.   0xce280ae1, 0x27e19ba5, 0xd5a6c252, 0xe49754bd, 0xc5d655dd, 0xeb667064,
  4696.   0x77840b4d, 0xa1b6a801, 0x84db26a9, 0xe0b56714, 0x21f043b7, 0xe5d05860,
  4697.   0x54f03084, 0x066ff472, 0xa31aa153, 0xdadc4755, 0xb5625dbf, 0x68561be6,
  4698.   0x83ca6b94, 0x2d6ed23b, 0xeccf01db, 0xa6d3d0ba, 0xb6803d5c, 0xaf77a709,
  4699.   0x33b4a34c, 0x397bc8d6, 0x5ee22b95, 0x5f0e5304, 0x81ed6f61, 0x20e74364,
  4700.   0xb45e1378, 0xde18639b, 0x881ca122, 0xb96726d1, 0x8049a7e8, 0x22b7da7b,
  4701.   0x5e552d25, 0x5272d237, 0x79d2951c, 0xc60d894c, 0x488cb402, 0x1ba4fe5b,
  4702.   0xa4b09f6b, 0x1ca815cf, 0xa20c3005, 0x8871df63, 0xb9de2fcb, 0x0cc6c9e9,
  4703.   0x0beeff53, 0xe3214517, 0xb4542835, 0x9f63293c, 0xee41e729, 0x6e1d2d7c,
  4704.   0x50045286, 0x1e6685f3, 0xf33401c6, 0x30a22c95, 0x31a70850, 0x60930f13,
  4705.   0x73f98417, 0xa1269859, 0xec645c44, 0x52c877a9, 0xcdff33a6, 0xa02b1741,
  4706.   0x7cbad9a2, 0x2180036f, 0x50d99c08, 0xcb3f4861, 0xc26bd765, 0x64a3f6ab,
  4707.   0x80342676, 0x25a75e7b, 0xe4e6d1fc, 0x20c710e6, 0xcdf0b680, 0x17844d3b,
  4708.   0x31eef84d, 0x7e0824e4, 0x2ccb49eb, 0x846a3bae, 0x8ff77888, 0xee5d60f6,
  4709.   0x7af75673, 0x2fdd5cdb, 0xa11631c1, 0x30f66f43, 0xb3faec54, 0x157fd7fa,
  4710.   0xef8579cc, 0xd152de58, 0xdb2ffd5e, 0x8f32ce19, 0x306af97a, 0x02f03ef8,
  4711.   0x99319ad5, 0xc242fa0f, 0xa7e3ebb0, 0xc68e4906, 0xb8da230c, 0x80823028,
  4712.   0xdcdef3c8, 0xd35fb171, 0x088a1bc8, 0xbec0c560, 0x61a3c9e8, 0xbca8f54d,
  4713.   0xc72feffa, 0x22822e99, 0x82c570b4, 0xd8d94e89, 0x8b1c34bc, 0x301e16e6,
  4714.   0x273be979, 0xb0ffeaa6, 0x61d9b8c6, 0x00b24869, 0xb7ffce3f, 0x08dc283b,
  4715.   0x43daf65a, 0xf7e19798, 0x7619b72f, 0x8f1c9ba4, 0xdc8637a0, 0x16a7d3b1,
  4716.   0x9fc393b7, 0xa7136eeb, 0xc6bcc63e, 0x1a513742, 0xef6828bc, 0x520365d6,
  4717.   0x2d6a77ab, 0x3527ed4b, 0x821fd216, 0x095c6e2e, 0xdb92f2fb, 0x5eea29cb,
  4718.   0x145892f5, 0x91584f7f, 0x5483697b, 0x2667a8cc, 0x85196048, 0x8c4bacea,
  4719.   0x833860d4, 0x0d23e0f9, 0x6c387e8a, 0x0ae6d249, 0xb284600c, 0xd835731d,
  4720.   0xdcb1c647, 0xac4c56ea, 0x3ebd81b3, 0x230eabb0, 0x6438bc87, 0xf0b5b1fa,
  4721.   0x8f5ea2b3, 0xfc184642, 0x0a036b7a, 0x4fb089bd, 0x649da589, 0xa345415e,
  4722.   0x5c038323, 0x3e5d3bb9, 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef,
  4723.   0x7160a539, 0x73bfbe70, 0x83877605, 0x4523ecf1
  4724. };
  4725.  
  4726. static uint32 S3[] = {
  4727.   0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 0x369fe44b,
  4728.   0x8c1fc644, 0xaececa90, 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae,
  4729.   0x920e8806, 0xf0ad0548, 0xe13c8d83, 0x927010d5, 0x11107d9f, 0x07647db9,
  4730.   0xb2e3e4d4, 0x3d4f285e, 0xb9afa820, 0xfade82e0, 0xa067268b, 0x8272792e,
  4731.   0x553fb2c0, 0x489ae22b, 0xd4ef9794, 0x125e3fbc, 0x21fffcee, 0x825b1bfd,
  4732.   0x9255c5ed, 0x1257a240, 0x4e1a8302, 0xbae07fff, 0x528246e7, 0x8e57140e,
  4733.   0x3373f7bf, 0x8c9f8188, 0xa6fc4ee8, 0xc982b5a5, 0xa8c01db7, 0x579fc264,
  4734.   0x67094f31, 0xf2bd3f5f, 0x40fff7c1, 0x1fb78dfc, 0x8e6bd2c1, 0x437be59b,
  4735.   0x99b03dbf, 0xb5dbc64b, 0x638dc0e6, 0x55819d99, 0xa197c81c, 0x4a012d6e,
  4736.   0xc5884a28, 0xccc36f71, 0xb843c213, 0x6c0743f1, 0x8309893c, 0x0feddd5f,
  4737.   0x2f7fe850, 0xd7c07f7e, 0x02507fbf, 0x5afb9a04, 0xa747d2d0, 0x1651192e,
  4738.   0xaf70bf3e, 0x58c31380, 0x5f98302e, 0x727cc3c4, 0x0a0fb402, 0x0f7fef82,
  4739.   0x8c96fdad, 0x5d2c2aae, 0x8ee99a49, 0x50da88b8, 0x8427f4a0, 0x1eac5790,
  4740.   0x796fb449, 0x8252dc15, 0xefbd7d9b, 0xa672597d, 0xada840d8, 0x45f54504,
  4741.   0xfa5d7403, 0xe83ec305, 0x4f91751a, 0x925669c2, 0x23efe941, 0xa903f12e,
  4742.   0x60270df2, 0x0276e4b6, 0x94fd6574, 0x927985b2, 0x8276dbcb, 0x02778176,
  4743.   0xf8af918d, 0x4e48f79e, 0x8f616ddf, 0xe29d840e, 0x842f7d83, 0x340ce5c8,
  4744.   0x96bbb682, 0x93b4b148, 0xef303cab, 0x984faf28, 0x779faf9b, 0x92dc560d,
  4745.   0x224d1e20, 0x8437aa88, 0x7d29dc96, 0x2756d3dc, 0x8b907cee, 0xb51fd240,
  4746.   0xe7c07ce3, 0xe566b4a1, 0xc3e9615e, 0x3cf8209d, 0x6094d1e3, 0xcd9ca341,
  4747.   0x5c76460e, 0x00ea983b, 0xd4d67881, 0xfd47572c, 0xf76cedd9, 0xbda8229c,
  4748.   0x127dadaa, 0x438a074e, 0x1f97c090, 0x081bdb8a, 0x93a07ebe, 0xb938ca15,
  4749.   0x97b03cff, 0x3dc2c0f8, 0x8d1ab2ec, 0x64380e51, 0x68cc7bfb, 0xd90f2788,
  4750.   0x12490181, 0x5de5ffd4, 0xdd7ef86a, 0x76a2e214, 0xb9a40368, 0x925d958f,
  4751.   0x4b39fffa, 0xba39aee9, 0xa4ffd30b, 0xfaf7933b, 0x6d498623, 0x193cbcfa,
  4752.   0x27627545, 0x825cf47a, 0x61bd8ba0, 0xd11e42d1, 0xcead04f4, 0x127ea392,
  4753.   0x10428db7, 0x8272a972, 0x9270c4a8, 0x127de50b, 0x285ba1c8, 0x3c62f44f,
  4754.   0x35c0eaa5, 0xe805d231, 0x428929fb, 0xb4fcdf82, 0x4fb66a53, 0x0e7dc15b,
  4755.   0x1f081fab, 0x108618ae, 0xfcfd086d, 0xf9ff2889, 0x694bcc11, 0x236a5cae,
  4756.   0x12deca4d, 0x2c3f8cc5, 0xd2d02dfe, 0xf8ef5896, 0xe4cf52da, 0x95155b67,
  4757.   0x494a488c, 0xb9b6a80c, 0x5c8f82bc, 0x89d36b45, 0x3a609437, 0xec00c9a9,
  4758.   0x44715253, 0x0a874b49, 0xd773bc40, 0x7c34671c, 0x02717ef6, 0x4feb5536,
  4759.   0xa2d02fff, 0xd2bf60c4, 0xd43f03c0, 0x50b4ef6d, 0x07478cd1, 0x006e1888,
  4760.   0xa2e53f55, 0xb9e6d4bc, 0xa2048016, 0x97573833, 0xd7207d67, 0xde0f8f3d,
  4761.   0x72f87b33, 0xabcc4f33, 0x7688c55d, 0x7b00a6b0, 0x947b0001, 0x570075d2,
  4762.   0xf9bb88f8, 0x8942019e, 0x4264a5ff, 0x856302e0, 0x72dbd92b, 0xee971b69,
  4763.   0x6ea22fde, 0x5f08ae2b, 0xaf7a616d, 0xe5c98767, 0xcf1febd2, 0x61efc8c2,
  4764.   0xf1ac2571, 0xcc8239c2, 0x67214cb8, 0xb1e583d1, 0xb7dc3e62, 0x7f10bdce,
  4765.   0xf90a5c38, 0x0ff0443d, 0x606e6dc6, 0x60543a49, 0x5727c148, 0x2be98a1d,
  4766.   0x8ab41738, 0x20e1be24, 0xaf96da0f, 0x68458425, 0x99833be5, 0x600d457d,
  4767.   0x282f9350, 0x8334b362, 0xd91d1120, 0x2b6d8da0, 0x642b1e31, 0x9c305a00,
  4768.   0x52bce688, 0x1b03588a, 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5,
  4769.   0xdfef4636, 0xa133c501, 0xe9d3531c, 0xee353783
  4770. };
  4771.  
  4772. static uint32 S4[] = {
  4773.   0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 0x64ad8c57,
  4774.   0x85510443, 0xfa020ed1, 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120,
  4775.   0xfd059d43, 0x6497b7b1, 0xf3641f63, 0x241e4adf, 0x28147f5f, 0x4fa2b8cd,
  4776.   0xc9430040, 0x0cc32220, 0xfdd30b30, 0xc0a5374f, 0x1d2d00d9, 0x24147b15,
  4777.   0xee4d111a, 0x0fca5167, 0x71ff904c, 0x2d195ffe, 0x1a05645f, 0x0c13fefe,
  4778.   0x081b08ca, 0x05170121, 0x80530100, 0xe83e5efe, 0xac9af4f8, 0x7fe72701,
  4779.   0xd2b8ee5f, 0x06df4261, 0xbb9e9b8a, 0x7293ea25, 0xce84ffdf, 0xf5718801,
  4780.   0x3dd64b04, 0xa26f263b, 0x7ed48400, 0x547eebe6, 0x446d4ca0, 0x6cf3d6f5,
  4781.   0x2649abdf, 0xaea0c7f5, 0x36338cc1, 0x503f7e93, 0xd3772061, 0x11b638e1,
  4782.   0x72500e03, 0xf80eb2bb, 0xabe0502e, 0xec8d77de, 0x57971e81, 0xe14f6746,
  4783.   0xc9335400, 0x6920318f, 0x081dbb99, 0xffc304a5, 0x4d351805, 0x7f3d5ce3,
  4784.   0xa6c866c6, 0x5d5bcca9, 0xdaec6fea, 0x9f926f91, 0x9f46222f, 0x3991467d,
  4785.   0xa5bf6d8e, 0x1143c44f, 0x43958302, 0xd0214eeb, 0x022083b8, 0x3fb6180c,
  4786.   0x18f8931e, 0x281658e6, 0x26486e3e, 0x8bd78a70, 0x7477e4c1, 0xb506e07c,
  4787.   0xf32d0a25, 0x79098b02, 0xe4eabb81, 0x28123b23, 0x69dead38, 0x1574ca16,
  4788.   0xdf871b62, 0x211c40b7, 0xa51a9ef9, 0x0014377b, 0x041e8ac8, 0x09114003,
  4789.   0xbd59e4d2, 0xe3d156d5, 0x4fe876d5, 0x2f91a340, 0x557be8de, 0x00eae4a7,
  4790.   0x0ce5c2ec, 0x4db4bba6, 0xe756bdff, 0xdd3369ac, 0xec17b035, 0x06572327,
  4791.   0x99afc8b0, 0x56c8c391, 0x6b65811c, 0x5e146119, 0x6e85cb75, 0xbe07c002,
  4792.   0xc2325577, 0x893ff4ec, 0x5bbfc92d, 0xd0ec3b25, 0xb7801ab7, 0x8d6d3b24,
  4793.   0x20c763ef, 0xc366a5fc, 0x9c382880, 0x0ace3205, 0xaac9548a, 0xeca1d7c7,
  4794.   0x041afa32, 0x1d16625a, 0x6701902c, 0x9b757a54, 0x31d477f7, 0x9126b031,
  4795.   0x36cc6fdb, 0xc70b8b46, 0xd9e66a48, 0x56e55a79, 0x026a4ceb, 0x52437eff,
  4796.   0x2f8f76b4, 0x0df980a5, 0x8674cde3, 0xedda04eb, 0x17a9be04, 0x2c18f4df,
  4797.   0xb7747f9d, 0xab2af7b4, 0xefc34d20, 0x2e096b7c, 0x1741a254, 0xe5b6a035,
  4798.   0x213d42f6, 0x2c1c7c26, 0x61c2f50f, 0x6552daf9, 0xd2c231f8, 0x25130f69,
  4799.   0xd8167fa2, 0x0418f2c8, 0x001a96a6, 0x0d1526ab, 0x63315c21, 0x5e0a72ec,
  4800.   0x49bafefd, 0x187908d9, 0x8d0dbd86, 0x311170a7, 0x3e9b640c, 0xcc3e10d7,
  4801.   0xd5cad3b6, 0x0caec388, 0xf73001e1, 0x6c728aff, 0x71eae2a1, 0x1f9af36e,
  4802.   0xcfcbd12f, 0xc1de8417, 0xac07be6b, 0xcb44a1d8, 0x8b9b0f56, 0x013988c3,
  4803.   0xb1c52fca, 0xb4be31cd, 0xd8782806, 0x12a3a4e2, 0x6f7de532, 0x58fd7eb6,
  4804.   0xd01ee900, 0x24adffc2, 0xf4990fc5, 0x9711aac5, 0x001d7b95, 0x82e5e7d2,
  4805.   0x109873f6, 0x00613096, 0xc32d9521, 0xada121ff, 0x29908415, 0x7fbb977f,
  4806.   0xaf9eb3db, 0x29c9ed2a, 0x5ce2a465, 0xa730f32c, 0xd0aa3fe8, 0x8a5cc091,
  4807.   0xd49e2ce7, 0x0ce454a9, 0xd60acd86, 0x015f1919, 0x77079103, 0xdea03af6,
  4808.   0x78a8565e, 0xdee356df, 0x21f05cbe, 0x8b75e387, 0xb3c50651, 0xb8a5c3ef,
  4809.   0xd8eeb6d2, 0xe523be77, 0xc2154529, 0x2f69efdf, 0xafe67afb, 0xf470c4b2,
  4810.   0xf3e0eb5b, 0xd6cc9876, 0x39e4460c, 0x1fda8538, 0x1987832f, 0xca007367,
  4811.   0xa99144f8, 0x296b299e, 0x492fc295, 0x9266beab, 0xb5676e69, 0x9bd3ddda,
  4812.   0xdf7e052f, 0xdb25701c, 0x1b5e51ee, 0xf65324e6, 0x6afce36c, 0x0316cc04,
  4813.   0x8644213e, 0xb7dc59d0, 0x7965291f, 0xccd6fd43, 0x41823979, 0x932bcdf6,
  4814.   0xb657c34d, 0x4edfd282, 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e,
  4815.   0x13ecf0b0, 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2
  4816. };
  4817.  
  4818. /* Encrypt/decrypt one 64-bit block of data */
  4819.  
  4820. void
  4821. ck_cast_ecb_encrypt(out, in, sched, mode)
  4822.     uint8p out;
  4823.     uint8p in;
  4824.     CastKeySched * sched;
  4825.     int mode;           /* zero means encrypt */
  4826. {
  4827.     uint32 t[2];
  4828.  
  4829. #ifdef LITTLE_ENDIAN
  4830.     t[0] = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
  4831.     t[1] = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | in[7];
  4832. #else
  4833.     t[0] = *(uint32p) in;
  4834.     t[1] = *(uint32p) (in + 4);
  4835. #endif
  4836.  
  4837.     ck_cast_ecb_crypt(t, sched, mode);
  4838.  
  4839. #ifdef LITTLE_ENDIAN
  4840.     out[0] = (t[0] >> 24) & 0xff;
  4841.     out[1] = (t[0] >> 16) & 0xff;
  4842.     out[2] = (t[0] >> 8) & 0xff;
  4843.     out[3] = t[0] & 0xff;
  4844.     out[4] = (t[1] >> 24) & 0xff;
  4845.     out[5] = (t[1] >> 16) & 0xff;
  4846.     out[6] = (t[1] >> 8) & 0xff;
  4847.     out[7] = t[1] & 0xff;
  4848. #else
  4849.     *(uint32p) out = t[0];
  4850.     *(uint32p) (out + 4) = t[1];
  4851. #endif
  4852. }
  4853.  
  4854. void
  4855. ck_cast_ecb_crypt(data, sched, mode)
  4856.     uint32p data;
  4857.     CastKeySched * sched;
  4858.     int mode;
  4859. {
  4860.     register uint32 L, R, temp;
  4861.     register struct CastSubkeyPair * kp;
  4862.     register uint8p Ia, Ib, Ic, Id;
  4863.     uint32 I;
  4864.  
  4865. #ifdef LITTLE_ENDIAN
  4866.     Id = (uint8p) &I;
  4867.     Ic = Id + 1;
  4868.     Ib = Ic + 1;
  4869.     Ia = Ib + 1;
  4870. #else
  4871.     Ia = (uint8p) &I;
  4872.     Ib = Ia + 1;
  4873.     Ic = Ib + 1;
  4874.     Id = Ic + 1;
  4875. #endif
  4876.  
  4877.     L = data[0];
  4878.     R = data[1];
  4879.  
  4880. #define type0(left,right)       \
  4881.       temp = kp->Km + right;\
  4882.       I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4883.       left ^= ((S1[*Ia] ^ S2[*Ib]) - S3[*Ic]) + S4[*Id];
  4884.  
  4885. #define type1(left,right)       \
  4886.       temp = kp->Km ^ right;\
  4887.       I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4888.       left ^= ((S1[*Ia] - S2[*Ib]) + S3[*Ic]) ^ S4[*Id];
  4889.  
  4890. #define type2(left,right)       \
  4891.       temp = kp->Km - right;\
  4892.       I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4893.       left ^= ((S1[*Ia] + S2[*Ib]) ^ S3[*Ic]) - S4[*Id];
  4894.  
  4895.     if(mode) {
  4896. #ifndef CAST_EXPORT_ENCRYPTION
  4897.         if(sched->ksize > 10) {
  4898.             kp = &sched->K[15];
  4899.             type0(L, R); --kp;
  4900.             type2(R, L); --kp;
  4901.             type1(L, R); --kp;
  4902.             type0(R, L); --kp;
  4903.         }
  4904.         else
  4905. #endif
  4906.         kp = &sched->K[11];
  4907.         type2(L, R); --kp;
  4908.         type1(R, L); --kp;
  4909.         type0(L, R); --kp;
  4910.         type2(R, L); --kp;
  4911.         type1(L, R); --kp;
  4912.         type0(R, L); --kp;
  4913.         type2(L, R); --kp;
  4914.         type1(R, L); --kp;
  4915.         type0(L, R); --kp;
  4916.         type2(R, L); --kp;
  4917.         type1(L, R); --kp;
  4918.         type0(R, L);
  4919.     }
  4920.     else {
  4921.         kp = &sched->K[0];
  4922.         type0(L, R); ++kp;
  4923.         type1(R, L); ++kp;
  4924.         type2(L, R); ++kp;
  4925.         type0(R, L); ++kp;
  4926.         type1(L, R); ++kp;
  4927.         type2(R, L); ++kp;
  4928.         type0(L, R); ++kp;
  4929.         type1(R, L); ++kp;
  4930.         type2(L, R); ++kp;
  4931.         type0(R, L); ++kp;
  4932.         type1(L, R); ++kp;
  4933.         type2(R, L); ++kp;
  4934. #ifndef CAST_EXPORT_ENCRYPTION
  4935.         if(sched->ksize > 10) {
  4936.             type0(L, R); ++kp;
  4937.             type1(R, L); ++kp;
  4938.             type2(L, R); ++kp;
  4939.             type0(R, L);
  4940.         }
  4941. #endif
  4942.     }
  4943.  
  4944.     data[0] = R;
  4945.     data[1] = L;
  4946. }
  4947.  
  4948. /* The last four S-boxes are for key schedule setup */
  4949.  
  4950. static uint32 S5[] = {
  4951.   0x7ec90c04, 0x2c6e74b9, 0x9b0e66df, 0xa6337911, 0xb86a7fff, 0x1dd358f5,
  4952.   0x44dd9d44, 0x1731167f, 0x08fbf1fa, 0xe7f511cc, 0xd2051b00, 0x735aba00,
  4953.   0x2ab722d8, 0x386381cb, 0xacf6243a, 0x69befd7a, 0xe6a2e77f, 0xf0c720cd,
  4954.   0xc4494816, 0xccf5c180, 0x38851640, 0x15b0a848, 0xe68b18cb, 0x4caadeff,
  4955.   0x5f480a01, 0x0412b2aa, 0x259814fc, 0x41d0efe2, 0x4e40b48d, 0x248eb6fb,
  4956.   0x8dba1cfe, 0x41a99b02, 0x1a550a04, 0xba8f65cb, 0x7251f4e7, 0x95a51725,
  4957.   0xc106ecd7, 0x97a5980a, 0xc539b9aa, 0x4d79fe6a, 0xf2f3f763, 0x68af8040,
  4958.   0xed0c9e56, 0x11b4958b, 0xe1eb5a88, 0x8709e6b0, 0xd7e07156, 0x4e29fea7,
  4959.   0x6366e52d, 0x02d1c000, 0xc4ac8e05, 0x9377f571, 0x0c05372a, 0x578535f2,
  4960.   0x2261be02, 0xd642a0c9, 0xdf13a280, 0x74b55bd2, 0x682199c0, 0xd421e5ec,
  4961.   0x53fb3ce8, 0xc8adedb3, 0x28a87fc9, 0x3d959981, 0x5c1ff900, 0xfe38d399,
  4962.   0x0c4eff0b, 0x062407ea, 0xaa2f4fb1, 0x4fb96976, 0x90c79505, 0xb0a8a774,
  4963.   0xef55a1ff, 0xe59ca2c2, 0xa6b62d27, 0xe66a4263, 0xdf65001f, 0x0ec50966,
  4964.   0xdfdd55bc, 0x29de0655, 0x911e739a, 0x17af8975, 0x32c7911c, 0x89f89468,
  4965.   0x0d01e980, 0x524755f4, 0x03b63cc9, 0x0cc844b2, 0xbcf3f0aa, 0x87ac36e9,
  4966.   0xe53a7426, 0x01b3d82b, 0x1a9e7449, 0x64ee2d7e, 0xcddbb1da, 0x01c94910,
  4967.   0xb868bf80, 0x0d26f3fd, 0x9342ede7, 0x04a5c284, 0x636737b6, 0x50f5b616,
  4968.   0xf24766e3, 0x8eca36c1, 0x136e05db, 0xfef18391, 0xfb887a37, 0xd6e7f7d4,
  4969.   0xc7fb7dc9, 0x3063fcdf, 0xb6f589de, 0xec2941da, 0x26e46695, 0xb7566419,
  4970.   0xf654efc5, 0xd08d58b7, 0x48925401, 0xc1bacb7f, 0xe5ff550f, 0xb6083049,
  4971.   0x5bb5d0e8, 0x87d72e5a, 0xab6a6ee1, 0x223a66ce, 0xc62bf3cd, 0x9e0885f9,
  4972.   0x68cb3e47, 0x086c010f, 0xa21de820, 0xd18b69de, 0xf3f65777, 0xfa02c3f6,
  4973.   0x407edac3, 0xcbb3d550, 0x1793084d, 0xb0d70eba, 0x0ab378d5, 0xd951fb0c,
  4974.   0xded7da56, 0x4124bbe4, 0x94ca0b56, 0x0f5755d1, 0xe0e1e56e, 0x6184b5be,
  4975.   0x580a249f, 0x94f74bc0, 0xe327888e, 0x9f7b5561, 0xc3dc0280, 0x05687715,
  4976.   0x646c6bd7, 0x44904db3, 0x66b4f0a3, 0xc0f1648a, 0x697ed5af, 0x49e92ff6,
  4977.   0x309e374f, 0x2cb6356a, 0x85808573, 0x4991f840, 0x76f0ae02, 0x083be84d,
  4978.   0x28421c9a, 0x44489406, 0x736e4cb8, 0xc1092910, 0x8bc95fc6, 0x7d869cf4,
  4979.   0x134f616f, 0x2e77118d, 0xb31b2be1, 0xaa90b472, 0x3ca5d717, 0x7d161bba,
  4980.   0x9cad9010, 0xaf462ba2, 0x9fe459d2, 0x45d34559, 0xd9f2da13, 0xdbc65487,
  4981.   0xf3e4f94e, 0x176d486f, 0x097c13ea, 0x631da5c7, 0x445f7382, 0x175683f4,
  4982.   0xcdc66a97, 0x70be0288, 0xb3cdcf72, 0x6e5dd2f3, 0x20936079, 0x459b80a5,
  4983.   0xbe60e2db, 0xa9c23101, 0xeba5315c, 0x224e42f2, 0x1c5c1572, 0xf6721b2c,
  4984.   0x1ad2fff3, 0x8c25404e, 0x324ed72f, 0x4067b7fd, 0x0523138e, 0x5ca3bc78,
  4985.   0xdc0fd66e, 0x75922283, 0x784d6b17, 0x58ebb16e, 0x44094f85, 0x3f481d87,
  4986.   0xfcfeae7b, 0x77b5ff76, 0x8c2302bf, 0xaaf47556, 0x5f46b02a, 0x2b092801,
  4987.   0x3d38f5f7, 0x0ca81f36, 0x52af4a8a, 0x66d5e7c0, 0xdf3b0874, 0x95055110,
  4988.   0x1b5ad7a8, 0xf61ed5ad, 0x6cf6e479, 0x20758184, 0xd0cefa65, 0x88f7be58,
  4989.   0x4a046826, 0x0ff6f8f3, 0xa09c7f70, 0x5346aba0, 0x5ce96c28, 0xe176eda3,
  4990.   0x6bac307f, 0x376829d2, 0x85360fa9, 0x17e3fe2a, 0x24b79767, 0xf5a96b20,
  4991.   0xd6cd2595, 0x68ff1ebf, 0x7555442c, 0xf19f06be, 0xf9e0659a, 0xeeb9491d,
  4992.   0x34010718, 0xbb30cab8, 0xe822fe15, 0x88570983, 0x750e6249, 0xda627e55,
  4993.   0x5e76ffa8, 0xb1534546, 0x6d47de08, 0xefe9e7d4
  4994. };
  4995.  
  4996. static uint32 S6[] = {
  4997.   0xf6fa8f9d, 0x2cac6ce1, 0x4ca34867, 0xe2337f7c, 0x95db08e7, 0x016843b4,
  4998.   0xeced5cbc, 0x325553ac, 0xbf9f0960, 0xdfa1e2ed, 0x83f0579d, 0x63ed86b9,
  4999.   0x1ab6a6b8, 0xde5ebe39, 0xf38ff732, 0x8989b138, 0x33f14961, 0xc01937bd,
  5000.   0xf506c6da, 0xe4625e7e, 0xa308ea99, 0x4e23e33c, 0x79cbd7cc, 0x48a14367,
  5001.   0xa3149619, 0xfec94bd5, 0xa114174a, 0xeaa01866, 0xa084db2d, 0x09a8486f,
  5002.   0xa888614a, 0x2900af98, 0x01665991, 0xe1992863, 0xc8f30c60, 0x2e78ef3c,
  5003.   0xd0d51932, 0xcf0fec14, 0xf7ca07d2, 0xd0a82072, 0xfd41197e, 0x9305a6b0,
  5004.   0xe86be3da, 0x74bed3cd, 0x372da53c, 0x4c7f4448, 0xdab5d440, 0x6dba0ec3,
  5005.   0x083919a7, 0x9fbaeed9, 0x49dbcfb0, 0x4e670c53, 0x5c3d9c01, 0x64bdb941,
  5006.   0x2c0e636a, 0xba7dd9cd, 0xea6f7388, 0xe70bc762, 0x35f29adb, 0x5c4cdd8d,
  5007.   0xf0d48d8c, 0xb88153e2, 0x08a19866, 0x1ae2eac8, 0x284caf89, 0xaa928223,
  5008.   0x9334be53, 0x3b3a21bf, 0x16434be3, 0x9aea3906, 0xefe8c36e, 0xf890cdd9,
  5009.   0x80226dae, 0xc340a4a3, 0xdf7e9c09, 0xa694a807, 0x5b7c5ecc, 0x221db3a6,
  5010.   0x9a69a02f, 0x68818a54, 0xceb2296f, 0x53c0843a, 0xfe893655, 0x25bfe68a,
  5011.   0xb4628abc, 0xcf222ebf, 0x25ac6f48, 0xa9a99387, 0x53bddb65, 0xe76ffbe7,
  5012.   0xe967fd78, 0x0ba93563, 0x8e342bc1, 0xe8a11be9, 0x4980740d, 0xc8087dfc,
  5013.   0x8de4bf99, 0xa11101a0, 0x7fd37975, 0xda5a26c0, 0xe81f994f, 0x9528cd89,
  5014.   0xfd339fed, 0xb87834bf, 0x5f04456d, 0x22258698, 0xc9c4c83b, 0x2dc156be,
  5015.   0x4f628daa, 0x57f55ec5, 0xe2220abe, 0xd2916ebf, 0x4ec75b95, 0x24f2c3c0,
  5016.   0x42d15d99, 0xcd0d7fa0, 0x7b6e27ff, 0xa8dc8af0, 0x7345c106, 0xf41e232f,
  5017.   0x35162386, 0xe6ea8926, 0x3333b094, 0x157ec6f2, 0x372b74af, 0x692573e4,
  5018.   0xe9a9d848, 0xf3160289, 0x3a62ef1d, 0xa787e238, 0xf3a5f676, 0x74364853,
  5019.   0x20951063, 0x4576698d, 0xb6fad407, 0x592af950, 0x36f73523, 0x4cfb6e87,
  5020.   0x7da4cec0, 0x6c152daa, 0xcb0396a8, 0xc50dfe5d, 0xfcd707ab, 0x0921c42f,
  5021.   0x89dff0bb, 0x5fe2be78, 0x448f4f33, 0x754613c9, 0x2b05d08d, 0x48b9d585,
  5022.   0xdc049441, 0xc8098f9b, 0x7dede786, 0xc39a3373, 0x42410005, 0x6a091751,
  5023.   0x0ef3c8a6, 0x890072d6, 0x28207682, 0xa9a9f7be, 0xbf32679d, 0xd45b5b75,
  5024.   0xb353fd00, 0xcbb0e358, 0x830f220a, 0x1f8fb214, 0xd372cf08, 0xcc3c4a13,
  5025.   0x8cf63166, 0x061c87be, 0x88c98f88, 0x6062e397, 0x47cf8e7a, 0xb6c85283,
  5026.   0x3cc2acfb, 0x3fc06976, 0x4e8f0252, 0x64d8314d, 0xda3870e3, 0x1e665459,
  5027.   0xc10908f0, 0x513021a5, 0x6c5b68b7, 0x822f8aa0, 0x3007cd3e, 0x74719eef,
  5028.   0xdc872681, 0x073340d4, 0x7e432fd9, 0x0c5ec241, 0x8809286c, 0xf592d891,
  5029.   0x08a930f6, 0x957ef305, 0xb7fbffbd, 0xc266e96f, 0x6fe4ac98, 0xb173ecc0,
  5030.   0xbc60b42a, 0x953498da, 0xfba1ae12, 0x2d4bd736, 0x0f25faab, 0xa4f3fceb,
  5031.   0xe2969123, 0x257f0c3d, 0x9348af49, 0x361400bc, 0xe8816f4a, 0x3814f200,
  5032.   0xa3f94043, 0x9c7a54c2, 0xbc704f57, 0xda41e7f9, 0xc25ad33a, 0x54f4a084,
  5033.   0xb17f5505, 0x59357cbe, 0xedbd15c8, 0x7f97c5ab, 0xba5ac7b5, 0xb6f6deaf,
  5034.   0x3a479c3a, 0x5302da25, 0x653d7e6a, 0x54268d49, 0x51a477ea, 0x5017d55b,
  5035.   0xd7d25d88, 0x44136c76, 0x0404a8c8, 0xb8e5a121, 0xb81a928a, 0x60ed5869,
  5036.   0x97c55b96, 0xeaec991b, 0x29935913, 0x01fdb7f1, 0x088e8dfa, 0x9ab6f6f5,
  5037.   0x3b4cbf9f, 0x4a5de3ab, 0xe6051d35, 0xa0e1d855, 0xd36b4cf1, 0xf544edeb,
  5038.   0xb0e93524, 0xbebb8fbd, 0xa2d762cf, 0x49c92f54, 0x38b5f331, 0x7128a454,
  5039.   0x48392905, 0xa65b1db8, 0x851c97bd, 0xd675cf2f
  5040. };
  5041.  
  5042. static uint32 S7[] = {
  5043.   0x85e04019, 0x332bf567, 0x662dbfff, 0xcfc65693, 0x2a8d7f6f, 0xab9bc912,
  5044.   0xde6008a1, 0x2028da1f, 0x0227bce7, 0x4d642916, 0x18fac300, 0x50f18b82,
  5045.   0x2cb2cb11, 0xb232e75c, 0x4b3695f2, 0xb28707de, 0xa05fbcf6, 0xcd4181e9,
  5046.   0xe150210c, 0xe24ef1bd, 0xb168c381, 0xfde4e789, 0x5c79b0d8, 0x1e8bfd43,
  5047.   0x4d495001, 0x38be4341, 0x913cee1d, 0x92a79c3f, 0x089766be, 0xbaeeadf4,
  5048.   0x1286becf, 0xb6eacb19, 0x2660c200, 0x7565bde4, 0x64241f7a, 0x8248dca9,
  5049.   0xc3b3ad66, 0x28136086, 0x0bd8dfa8, 0x356d1cf2, 0x107789be, 0xb3b2e9ce,
  5050.   0x0502aa8f, 0x0bc0351e, 0x166bf52a, 0xeb12ff82, 0xe3486911, 0xd34d7516,
  5051.   0x4e7b3aff, 0x5f43671b, 0x9cf6e037, 0x4981ac83, 0x334266ce, 0x8c9341b7,
  5052.   0xd0d854c0, 0xcb3a6c88, 0x47bc2829, 0x4725ba37, 0xa66ad22b, 0x7ad61f1e,
  5053.   0x0c5cbafa, 0x4437f107, 0xb6e79962, 0x42d2d816, 0x0a961288, 0xe1a5c06e,
  5054.   0x13749e67, 0x72fc081a, 0xb1d139f7, 0xf9583745, 0xcf19df58, 0xbec3f756,
  5055.   0xc06eba30, 0x07211b24, 0x45c28829, 0xc95e317f, 0xbc8ec511, 0x38bc46e9,
  5056.   0xc6e6fa14, 0xbae8584a, 0xad4ebc46, 0x468f508b, 0x7829435f, 0xf124183b,
  5057.   0x821dba9f, 0xaff60ff4, 0xea2c4e6d, 0x16e39264, 0x92544a8b, 0x009b4fc3,
  5058.   0xaba68ced, 0x9ac96f78, 0x06a5b79a, 0xb2856e6e, 0x1aec3ca9, 0xbe838688,
  5059.   0x0e0804e9, 0x55f1be56, 0xe7e5363b, 0xb3a1f25d, 0xf7debb85, 0x61fe033c,
  5060.   0x16746233, 0x3c034c28, 0xda6d0c74, 0x79aac56c, 0x3ce4e1ad, 0x51f0c802,
  5061.   0x98f8f35a, 0x1626a49f, 0xeed82b29, 0x1d382fe3, 0x0c4fb99a, 0xbb325778,
  5062.   0x3ec6d97b, 0x6e77a6a9, 0xcb658b5c, 0xd45230c7, 0x2bd1408b, 0x60c03eb7,
  5063.   0xb9068d78, 0xa33754f4, 0xf430c87d, 0xc8a71302, 0xb96d8c32, 0xebd4e7be,
  5064.   0xbe8b9d2d, 0x7979fb06, 0xe7225308, 0x8b75cf77, 0x11ef8da4, 0xe083c858,
  5065.   0x8d6b786f, 0x5a6317a6, 0xfa5cf7a0, 0x5dda0033, 0xf28ebfb0, 0xf5b9c310,
  5066.   0xa0eac280, 0x08b9767a, 0xa3d9d2b0, 0x79d34217, 0x021a718d, 0x9ac6336a,
  5067.   0x2711fd60, 0x438050e3, 0x069908a8, 0x3d7fedc4, 0x826d2bef, 0x4eeb8476,
  5068.   0x488dcf25, 0x36c9d566, 0x28e74e41, 0xc2610aca, 0x3d49a9cf, 0xbae3b9df,
  5069.   0xb65f8de6, 0x92aeaf64, 0x3ac7d5e6, 0x9ea80509, 0xf22b017d, 0xa4173f70,
  5070.   0xdd1e16c3, 0x15e0d7f9, 0x50b1b887, 0x2b9f4fd5, 0x625aba82, 0x6a017962,
  5071.   0x2ec01b9c, 0x15488aa9, 0xd716e740, 0x40055a2c, 0x93d29a22, 0xe32dbf9a,
  5072.   0x058745b9, 0x3453dc1e, 0xd699296e, 0x496cff6f, 0x1c9f4986, 0xdfe2ed07,
  5073.   0xb87242d1, 0x19de7eae, 0x053e561a, 0x15ad6f8c, 0x66626c1c, 0x7154c24c,
  5074.   0xea082b2a, 0x93eb2939, 0x17dcb0f0, 0x58d4f2ae, 0x9ea294fb, 0x52cf564c,
  5075.   0x9883fe66, 0x2ec40581, 0x763953c3, 0x01d6692e, 0xd3a0c108, 0xa1e7160e,
  5076.   0xe4f2dfa6, 0x693ed285, 0x74904698, 0x4c2b0edd, 0x4f757656, 0x5d393378,
  5077.   0xa132234f, 0x3d321c5d, 0xc3f5e194, 0x4b269301, 0xc79f022f, 0x3c997e7e,
  5078.   0x5e4f9504, 0x3ffafbbd, 0x76f7ad0e, 0x296693f4, 0x3d1fce6f, 0xc61e45be,
  5079.   0xd3b5ab34, 0xf72bf9b7, 0x1b0434c0, 0x4e72b567, 0x5592a33d, 0xb5229301,
  5080.   0xcfd2a87f, 0x60aeb767, 0x1814386b, 0x30bcc33d, 0x38a0c07d, 0xfd1606f2,
  5081.   0xc363519b, 0x589dd390, 0x5479f8e6, 0x1cb8d647, 0x97fd61a9, 0xea7759f4,
  5082.   0x2d57539d, 0x569a58cf, 0xe84e63ad, 0x462e1b78, 0x6580f87e, 0xf3817914,
  5083.   0x91da55f4, 0x40a230f3, 0xd1988f35, 0xb6e318d2, 0x3ffa50bc, 0x3d40f021,
  5084.   0xc3c0bdae, 0x4958c24c, 0x518f36b2, 0x84b1d370, 0x0fedce83, 0x878ddada,
  5085.   0xf2a279c7, 0x94e01be8, 0x90716f4b, 0x954b8aa3
  5086. };
  5087.  
  5088. static uint32 S8[] = {
  5089.   0xe216300d, 0xbbddfffc, 0xa7ebdabd, 0x35648095, 0x7789f8b7, 0xe6c1121b,
  5090.   0x0e241600, 0x052ce8b5, 0x11a9cfb0, 0xe5952f11, 0xece7990a, 0x9386d174,
  5091.   0x2a42931c, 0x76e38111, 0xb12def3a, 0x37ddddfc, 0xde9adeb1, 0x0a0cc32c,
  5092.   0xbe197029, 0x84a00940, 0xbb243a0f, 0xb4d137cf, 0xb44e79f0, 0x049eedfd,
  5093.   0x0b15a15d, 0x480d3168, 0x8bbbde5a, 0x669ded42, 0xc7ece831, 0x3f8f95e7,
  5094.   0x72df191b, 0x7580330d, 0x94074251, 0x5c7dcdfa, 0xabbe6d63, 0xaa402164,
  5095.   0xb301d40a, 0x02e7d1ca, 0x53571dae, 0x7a3182a2, 0x12a8ddec, 0xfdaa335d,
  5096.   0x176f43e8, 0x71fb46d4, 0x38129022, 0xce949ad4, 0xb84769ad, 0x965bd862,
  5097.   0x82f3d055, 0x66fb9767, 0x15b80b4e, 0x1d5b47a0, 0x4cfde06f, 0xc28ec4b8,
  5098.   0x57e8726e, 0x647a78fc, 0x99865d44, 0x608bd593, 0x6c200e03, 0x39dc5ff6,
  5099.   0x5d0b00a3, 0xae63aff2, 0x7e8bd632, 0x70108c0c, 0xbbd35049, 0x2998df04,
  5100.   0x980cf42a, 0x9b6df491, 0x9e7edd53, 0x06918548, 0x58cb7e07, 0x3b74ef2e,
  5101.   0x522fffb1, 0xd24708cc, 0x1c7e27cd, 0xa4eb215b, 0x3cf1d2e2, 0x19b47a38,
  5102.   0x424f7618, 0x35856039, 0x9d17dee7, 0x27eb35e6, 0xc9aff67b, 0x36baf5b8,
  5103.   0x09c467cd, 0xc18910b1, 0xe11dbf7b, 0x06cd1af8, 0x7170c608, 0x2d5e3354,
  5104.   0xd4de495a, 0x64c6d006, 0xbcc0c62c, 0x3dd00db3, 0x708f8f34, 0x77d51b42,
  5105.   0x264f620f, 0x24b8d2bf, 0x15c1b79e, 0x46a52564, 0xf8d7e54e, 0x3e378160,
  5106.   0x7895cda5, 0x859c15a5, 0xe6459788, 0xc37bc75f, 0xdb07ba0c, 0x0676a3ab,
  5107.   0x7f229b1e, 0x31842e7b, 0x24259fd7, 0xf8bef472, 0x835ffcb8, 0x6df4c1f2,
  5108.   0x96f5b195, 0xfd0af0fc, 0xb0fe134c, 0xe2506d3d, 0x4f9b12ea, 0xf215f225,
  5109.   0xa223736f, 0x9fb4c428, 0x25d04979, 0x34c713f8, 0xc4618187, 0xea7a6e98,
  5110.   0x7cd16efc, 0x1436876c, 0xf1544107, 0xbedeee14, 0x56e9af27, 0xa04aa441,
  5111.   0x3cf7c899, 0x92ecbae6, 0xdd67016d, 0x151682eb, 0xa842eedf, 0xfdba60b4,
  5112.   0xf1907b75, 0x20e3030f, 0x24d8c29e, 0xe139673b, 0xefa63fb8, 0x71873054,
  5113.   0xb6f2cf3b, 0x9f326442, 0xcb15a4cc, 0xb01a4504, 0xf1e47d8d, 0x844a1be5,
  5114.   0xbae7dfdc, 0x42cbda70, 0xcd7dae0a, 0x57e85b7a, 0xd53f5af6, 0x20cf4d8c,
  5115.   0xcea4d428, 0x79d130a4, 0x3486ebfb, 0x33d3cddc, 0x77853b53, 0x37effcb5,
  5116.   0xc5068778, 0xe580b3e6, 0x4e68b8f4, 0xc5c8b37e, 0x0d809ea2, 0x398feb7c,
  5117.   0x132a4f94, 0x43b7950e, 0x2fee7d1c, 0x223613bd, 0xdd06caa2, 0x37df932b,
  5118.   0xc4248289, 0xacf3ebc3, 0x5715f6b7, 0xef3478dd, 0xf267616f, 0xc148cbe4,
  5119.   0x9052815e, 0x5e410fab, 0xb48a2465, 0x2eda7fa4, 0xe87b40e4, 0xe98ea084,
  5120.   0x5889e9e1, 0xefd390fc, 0xdd07d35b, 0xdb485694, 0x38d7e5b2, 0x57720101,
  5121.   0x730edebc, 0x5b643113, 0x94917e4f, 0x503c2fba, 0x646f1282, 0x7523d24a,
  5122.   0xe0779695, 0xf9c17a8f, 0x7a5b2121, 0xd187b896, 0x29263a4d, 0xba510cdf,
  5123.   0x81f47c9f, 0xad1163ed, 0xea7b5965, 0x1a00726e, 0x11403092, 0x00da6d77,
  5124.   0x4a0cdd61, 0xad1f4603, 0x605bdfb0, 0x9eedc364, 0x22ebe6a8, 0xcee7d28a,
  5125.   0xa0e736a0, 0x5564a6b9, 0x10853209, 0xc7eb8f37, 0x2de705ca, 0x8951570f,
  5126.   0xdf09822b, 0xbd691a6c, 0xaa12e4f2, 0x87451c0f, 0xe0f6a27a, 0x3ada4819,
  5127.   0x4cf1764f, 0x0d771c2b, 0x67cdb156, 0x350d8384, 0x5938fa0f, 0x42399ef3,
  5128.   0x36997b07, 0x0e84093d, 0x4aa93e61, 0x8360d87b, 0x1fa98b0c, 0x1149382c,
  5129.   0xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1,
  5130.   0xa466bb1e, 0xf8da0a82, 0x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d,
  5131.   0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e
  5132. };
  5133.  
  5134. /* Initialize a key schedule from a 128-bit key */
  5135.  
  5136. static void
  5137. cast_key_sched(sched, key)
  5138.      CastKeySched * sched;
  5139.      uint8p key;
  5140. {
  5141.     uint8p x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xA, xB, xC, xD, xE, xF;
  5142.     uint8p z0, z1, z2, z3, z4, z5, z6, z7, z8, z9, zA, zB, zC, zD, zE, zF;
  5143.     uint32 X03, X47, X8B, XCF, Z03, Z47, Z8B, ZCF;
  5144.  
  5145. #ifdef LITTLE_ENDIAN
  5146.     x3 = (uint8p) &X03;  x2 = x3 + 1;  x1 = x2 + 1;  x0 = x1 + 1;
  5147.     x7 = (uint8p) &X47;  x6 = x7 + 1;  x5 = x6 + 1;  x4 = x5 + 1;
  5148.     xB = (uint8p) &X8B;  xA = xB + 1;  x9 = xA + 1;  x8 = x9 + 1;
  5149.     xF = (uint8p) &XCF;  xE = xF + 1;  xD = xE + 1;  xC = xD + 1;
  5150.     z3 = (uint8p) &Z03;  z2 = z3 + 1;  z1 = z2 + 1;  z0 = z1 + 1;
  5151.     z7 = (uint8p) &Z47;  z6 = z7 + 1;  z5 = z6 + 1;  z4 = z5 + 1;
  5152.     zB = (uint8p) &Z8B;  zA = zB + 1;  z9 = zA + 1;  z8 = z9 + 1;
  5153.     zF = (uint8p) &ZCF;  zE = zF + 1;  zD = zE + 1;  zC = zD + 1;
  5154. #else
  5155.     x0 = (uint8p) &X03;  x1 = x0 + 1;  x2 = x1 + 1;  x3 = x2 + 1;
  5156.     x4 = (uint8p) &X47;  x5 = x4 + 1;  x6 = x5 + 1;  x7 = x6 + 1;
  5157.     x8 = (uint8p) &X8B;  x9 = x8 + 1;  xA = x9 + 1;  xB = xA + 1;
  5158.     xC = (uint8p) &XCF;  xD = xC + 1;  xE = xD + 1;  xF = xE + 1;
  5159.     z0 = (uint8p) &Z03;  z1 = z0 + 1;  z2 = z1 + 1;  z3 = z2 + 1;
  5160.     z4 = (uint8p) &Z47;  z5 = z4 + 1;  z6 = z5 + 1;  z7 = z6 + 1;
  5161.     z8 = (uint8p) &Z8B;  z9 = z8 + 1;  zA = z9 + 1;  zB = zA + 1;
  5162.     zC = (uint8p) &ZCF;  zD = zC + 1;  zE = zD + 1;  zF = zE + 1;
  5163. #endif
  5164.  
  5165. #ifdef LITTLE_ENDIAN
  5166.     *x0 = key[0];
  5167.     *x1 = key[1];
  5168.     *x2 = key[2];
  5169.     *x3 = key[3];
  5170.     *x4 = key[4];
  5171.     *x5 = key[5];
  5172.     *x6 = key[6];
  5173.     *x7 = key[7];
  5174.     *x8 = key[8];
  5175.     *x9 = key[9];
  5176.     *xA = key[10];
  5177.     *xB = key[11];
  5178.     *xC = key[12];
  5179.     *xD = key[13];
  5180.     *xE = key[14];
  5181.     *xF = key[15];
  5182. #else
  5183.     X03 = *(uint32p) key;
  5184.     X47 = *(uint32p) (key + 4);
  5185.     X8B = *(uint32p) (key + 8);
  5186.     XCF = *(uint32p) (key + 12);
  5187. #endif
  5188.  
  5189.   /* First half of key schedule */
  5190.  
  5191.     Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  5192.     Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  5193.     Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  5194.     ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  5195.  
  5196.     sched->K[0].Km = S5[*z8] ^ S6[*z9] ^ S7[*z7] ^ S8[*z6] ^ S5[*z2];
  5197.     sched->K[1].Km = S5[*zA] ^ S6[*zB] ^ S7[*z5] ^ S8[*z4] ^ S6[*z6];
  5198.     sched->K[2].Km = S5[*zC] ^ S6[*zD] ^ S7[*z3] ^ S8[*z2] ^ S7[*z9];
  5199.     sched->K[3].Km = S5[*zE] ^ S6[*zF] ^ S7[*z1] ^ S8[*z0] ^ S8[*zC];
  5200.  
  5201.     X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  5202.     X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  5203.     X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  5204.     XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  5205.  
  5206.     sched->K[4].Km = S5[*x3] ^ S6[*x2] ^ S7[*xC] ^ S8[*xD] ^ S5[*x8];
  5207.     sched->K[5].Km = S5[*x1] ^ S6[*x0] ^ S7[*xE] ^ S8[*xF] ^ S6[*xD];
  5208.     sched->K[6].Km = S5[*x7] ^ S6[*x6] ^ S7[*x8] ^ S8[*x9] ^ S7[*x3];
  5209.     sched->K[7].Km = S5[*x5] ^ S6[*x4] ^ S7[*xA] ^ S8[*xB] ^ S8[*x7];
  5210.  
  5211.     Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  5212.     Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  5213.     Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  5214.     ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  5215.  
  5216.     sched->K[8].Km = S5[*z3] ^ S6[*z2] ^ S7[*zC] ^ S8[*zD] ^ S5[*z9];
  5217.     sched->K[9].Km = S5[*z1] ^ S6[*z0] ^ S7[*zE] ^ S8[*zF] ^ S6[*zC];
  5218.     sched->K[10].Km = S5[*z7] ^ S6[*z6] ^ S7[*z8] ^ S8[*z9] ^ S7[*z2];
  5219.     sched->K[11].Km = S5[*z5] ^ S6[*z4] ^ S7[*zA] ^ S8[*zB] ^ S8[*z6];
  5220.  
  5221.     X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  5222.     X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  5223.     X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  5224.     XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  5225.  
  5226.     sched->K[12].Km = S5[*x8] ^ S6[*x9] ^ S7[*x7] ^ S8[*x6] ^ S5[*x3];
  5227.     sched->K[13].Km = S5[*xA] ^ S6[*xB] ^ S7[*x5] ^ S8[*x4] ^ S6[*x7];
  5228.     sched->K[14].Km = S5[*xC] ^ S6[*xD] ^ S7[*x3] ^ S8[*x2] ^ S7[*x8];
  5229.     sched->K[15].Km = S5[*xE] ^ S6[*xF] ^ S7[*x1] ^ S8[*x0] ^ S8[*xD];
  5230.  
  5231.   /* Second half of key schedule - just like first half */
  5232.  
  5233.     Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  5234.     Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  5235.     Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  5236.     ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  5237.  
  5238.     sched->K[0].Kr = (S5[*z8] ^ S6[*z9] ^ S7[*z7] ^ S8[*z6] ^ S5[*z2]) & 0x1f;
  5239.     sched->K[1].Kr = (S5[*zA] ^ S6[*zB] ^ S7[*z5] ^ S8[*z4] ^ S6[*z6]) & 0x1f;
  5240.     sched->K[2].Kr = (S5[*zC] ^ S6[*zD] ^ S7[*z3] ^ S8[*z2] ^ S7[*z9]) & 0x1f;
  5241.     sched->K[3].Kr = (S5[*zE] ^ S6[*zF] ^ S7[*z1] ^ S8[*z0] ^ S8[*zC]) & 0x1f;
  5242.  
  5243.     X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  5244.     X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  5245.     X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  5246.     XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  5247.  
  5248.     sched->K[4].Kr = (S5[*x3] ^ S6[*x2] ^ S7[*xC] ^ S8[*xD] ^ S5[*x8]) & 0x1f;
  5249.     sched->K[5].Kr = (S5[*x1] ^ S6[*x0] ^ S7[*xE] ^ S8[*xF] ^ S6[*xD]) & 0x1f;
  5250.     sched->K[6].Kr = (S5[*x7] ^ S6[*x6] ^ S7[*x8] ^ S8[*x9] ^ S7[*x3]) & 0x1f;
  5251.     sched->K[7].Kr = (S5[*x5] ^ S6[*x4] ^ S7[*xA] ^ S8[*xB] ^ S8[*x7]) & 0x1f;
  5252.  
  5253.     Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  5254.     Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  5255.     Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  5256.     ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  5257.  
  5258.     sched->K[8].Kr = (S5[*z3] ^ S6[*z2] ^ S7[*zC] ^ S8[*zD] ^ S5[*z9]) & 0x1f;
  5259.     sched->K[9].Kr = (S5[*z1] ^ S6[*z0] ^ S7[*zE] ^ S8[*zF] ^ S6[*zC]) & 0x1f;
  5260.     sched->K[10].Kr = (S5[*z7] ^ S6[*z6] ^ S7[*z8] ^ S8[*z9] ^ S7[*z2]) & 0x1f;
  5261.     sched->K[11].Kr = (S5[*z5] ^ S6[*z4] ^ S7[*zA] ^ S8[*zB] ^ S8[*z6]) & 0x1f;
  5262.  
  5263.     X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  5264.     X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  5265.     X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  5266.     XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  5267.  
  5268.     sched->K[12].Kr = (S5[*x8] ^ S6[*x9] ^ S7[*x7] ^ S8[*x6] ^ S5[*x3]) & 0x1f;
  5269.     sched->K[13].Kr = (S5[*xA] ^ S6[*xB] ^ S7[*x5] ^ S8[*x4] ^ S6[*x7]) & 0x1f;
  5270.     sched->K[14].Kr = (S5[*xC] ^ S6[*xD] ^ S7[*x3] ^ S8[*x2] ^ S7[*x8]) & 0x1f;
  5271.     sched->K[15].Kr = (S5[*xE] ^ S6[*xF] ^ S7[*x1] ^ S8[*x0] ^ S8[*xD]) & 0x1f;
  5272. }
  5273.  
  5274. /* Initialize with a full-strength 128-bit key */
  5275.  
  5276. #ifndef CAST_EXPORT_ENCRYPTION
  5277. void
  5278. ck_cast128_key_sched(sched, key)
  5279.     CastKeySched * sched;
  5280.     uint8 * key;
  5281. {
  5282.     sched->ksize = 16;
  5283.     cast_key_sched(sched, key);
  5284. }
  5285. #endif
  5286.  
  5287. /* Handle reduced-keysize variants */
  5288.  
  5289. static void
  5290. cast5_key_sched(sched, key, sz)
  5291.     CastKeySched * sched;
  5292.     uint8 * key;
  5293.     int sz;
  5294. {
  5295.     uint8 buf[16];
  5296.  
  5297.     sched->ksize = sz;
  5298.     memset(buf, 0, sizeof(buf));
  5299.     memcpy(buf, key, sz);
  5300.     cast_key_sched(sched, buf);
  5301. }
  5302.  
  5303. /* 40, 64, and 80-bit keys - all use 12 rounds */
  5304.  
  5305. void
  5306. ck_cast5_40_key_sched(sched, key)
  5307.     CastKeySched * sched;
  5308.     uint8 * key;
  5309. {
  5310.     cast5_key_sched(sched, key, 5);
  5311. }
  5312.  
  5313. #ifndef CAST_EXPORT_ENCRYPTION
  5314. void
  5315. ck_cast5_64_key_sched(sched, key)
  5316.      CastKeySched * sched;
  5317.      uint8 * key;
  5318. {
  5319.     cast5_key_sched(sched, key, 8);
  5320. }
  5321.  
  5322. void
  5323. ck_cast5_80_key_sched(sched, key)
  5324.      CastKeySched * sched;
  5325.      uint8 * key;
  5326. {
  5327.     cast5_key_sched(sched, key, 10);
  5328. }
  5329. #endif /* CAST_EXPORT_ENCRYPTION */
  5330. #endif /* CK_CAST */
  5331.  
  5332. #ifdef CRYPT_DLL
  5333. static char *
  5334. ck_crypt_dll_version()
  5335. {
  5336.     return(ckcrpv);
  5337. }
  5338.  
  5339. int
  5340. crypt_dll_init( struct _crypt_dll_init * init )
  5341. {
  5342. #ifdef LIBDES
  5343.     extern int des_check_key;
  5344.     extern void libdes_dll_init(struct _crypt_dll_init *);
  5345.     des_check_key = 1;
  5346. #endif /* LIBDES */
  5347.  
  5348.     if ( init->version >= 1 ) {
  5349.         p_ttol = init->p_ttol;
  5350.         p_dodebug = init->p_dodebug;
  5351.         p_dohexdump = init->p_dohexdump;
  5352.         p_tn_debug = init->p_tn_debug;
  5353.         p_vscrnprintf = init->p_vscrnprintf;
  5354.         if ( init->version == 1 )
  5355.             return(1);
  5356.     }
  5357.     if ( init->version >= 2 ) {
  5358.         /* This is a k5_context but we don't want to include krb5.h */
  5359.         p_k5_context = (void *) init->p_k5_context;
  5360.         if ( init->version == 2 )
  5361.             return(1);
  5362.     }
  5363.     if ( init->version >= 3 ) {
  5364.         init->p_install_funcs("encrypt_parse",encrypt_parse);
  5365.         init->p_install_funcs("encrypt_init",encrypt_init);
  5366.         init->p_install_funcs("encrypt_session_key",encrypt_session_key);
  5367.         init->p_install_funcs("encrypt_send_request_start",
  5368.                               encrypt_send_request_start
  5369.                               );
  5370.         init->p_install_funcs("encrypt_request_start",encrypt_request_start);
  5371.         init->p_install_funcs("encrypt_send_request_end",
  5372.                               encrypt_send_request_end
  5373.                               );
  5374.         init->p_install_funcs("encrypt_request_end",encrypt_request_end);
  5375.         init->p_install_funcs("encrypt_send_end",encrypt_send_end);
  5376.         init->p_install_funcs("encrypt_send_support",encrypt_send_support);
  5377.         init->p_install_funcs("encrypt_is_encrypting",encrypt_is_encrypting);
  5378.         init->p_install_funcs("encrypt_is_decrypting",encrypt_is_decrypting);
  5379.         init->p_install_funcs("get_crypt_table",get_crypt_table);
  5380.         init->p_install_funcs("des_is_weak_key",ck_des_is_weak_key);
  5381.         libdes_dll_init(init);
  5382.         if (init->version == 3)
  5383.       return(1);
  5384.         init->p_install_funcs("crypt_dll_version",ck_crypt_dll_version);
  5385.         if (init->version == 4)
  5386.       return(1);
  5387.         init->version = 4;
  5388.         return(1);
  5389.     }
  5390.     return(0);
  5391. }
  5392. #endif /* CRYPT_DLL */
  5393. #endif /* CK_ENCRYPTION */
  5394.