pppcrypt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * pppcrypt.c - PPP/DES linkage for MS-CHAP and EAP SRP-SHA1
  3. *
  4. * Extracted from chap_ms.c by James Carlson.
  5. *
  6. * Copyright (c) 1995 Eric Rosenquist. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The name(s) of the authors of this software must not be used to
  21. * endorse or promote products derived from this software without
  22. * prior written permission.
  23. *
  24. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  25. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  26. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  27. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  28. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  29. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  30. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31. */
  32. #include <errno.h>
  33. #include "pppd.h"
  34. #include "pppcrypt.h"
  35. static u_char
  36. Get7Bits(input, startBit)
  37. u_char *input;
  38. int startBit;
  39. {
  40. unsigned int word;
  41. word = (unsigned)input[startBit / 8] << 8;
  42. word |= (unsigned)input[startBit / 8 + 1];
  43. word >>= 15 - (startBit % 8 + 7);
  44. return word & 0xFE;
  45. }
  46. static void
  47. MakeKey(key, des_key)
  48. u_char *key; /* IN 56 bit DES key missing parity bits */
  49. u_char *des_key; /* OUT 64 bit DES key with parity bits added */
  50. {
  51. des_key[0] = Get7Bits(key, 0);
  52. des_key[1] = Get7Bits(key, 7);
  53. des_key[2] = Get7Bits(key, 14);
  54. des_key[3] = Get7Bits(key, 21);
  55. des_key[4] = Get7Bits(key, 28);
  56. des_key[5] = Get7Bits(key, 35);
  57. des_key[6] = Get7Bits(key, 42);
  58. des_key[7] = Get7Bits(key, 49);
  59. #ifndef USE_CRYPT
  60. des_set_odd_parity((des_cblock *)des_key);
  61. #endif
  62. }
  63. #ifdef USE_CRYPT
  64. /*
  65. * in == 8-byte string (expanded version of the 56-bit key)
  66. * out == 64-byte string where each byte is either 1 or 0
  67. * Note that the low-order "bit" is always ignored by by setkey()
  68. */
  69. static void
  70. Expand(in, out)
  71. u_char *in;
  72. u_char *out;
  73. {
  74. int j, c;
  75. int i;
  76. for (i = 0; i < 64; in++){
  77. c = *in;
  78. for (j = 7; j >= 0; j--)
  79. *out++ = (c >> j) & 01;
  80. i += 8;
  81. }
  82. }
  83. /* The inverse of Expand
  84. */
  85. static void
  86. Collapse(in, out)
  87. u_char *in;
  88. u_char *out;
  89. {
  90. int j;
  91. int i;
  92. unsigned int c;
  93. for (i = 0; i < 64; i += 8, out++) {
  94. c = 0;
  95. for (j = 7; j >= 0; j--, in++)
  96. c |= *in << j;
  97. *out = c & 0xff;
  98. }
  99. }
  100. bool
  101. DesSetkey(key)
  102. u_char *key;
  103. {
  104. u_char des_key[8];
  105. u_char crypt_key[66];
  106. MakeKey(key, des_key);
  107. Expand(des_key, crypt_key);
  108. errno = 0;
  109. setkey((const char *)crypt_key);
  110. if (errno != 0)
  111. return (0);
  112. return (1);
  113. }
  114. bool
  115. DesEncrypt(clear, cipher)
  116. u_char *clear; /* IN 8 octets */
  117. u_char *cipher; /* OUT 8 octets */
  118. {
  119. u_char des_input[66];
  120. Expand(clear, des_input);
  121. errno = 0;
  122. encrypt((char *)des_input, 0);
  123. if (errno != 0)
  124. return (0);
  125. Collapse(des_input, cipher);
  126. return (1);
  127. }
  128. bool
  129. DesDecrypt(cipher, clear)
  130. u_char *cipher; /* IN 8 octets */
  131. u_char *clear; /* OUT 8 octets */
  132. {
  133. u_char des_input[66];
  134. Expand(cipher, des_input);
  135. errno = 0;
  136. encrypt((char *)des_input, 1);
  137. if (errno != 0)
  138. return (0);
  139. Collapse(des_input, clear);
  140. return (1);
  141. }
  142. #else /* USE_CRYPT */
  143. static des_key_schedule key_schedule;
  144. bool
  145. DesSetkey(key)
  146. u_char *key;
  147. {
  148. des_cblock des_key;
  149. MakeKey(key, des_key);
  150. des_set_key(&des_key, key_schedule);
  151. return (1);
  152. }
  153. bool
  154. DesEncrypt(clear, key, cipher)
  155. u_char *clear; /* IN 8 octets */
  156. u_char *cipher; /* OUT 8 octets */
  157. {
  158. des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher,
  159. key_schedule, 1);
  160. return (1);
  161. }
  162. bool
  163. DesDecrypt(cipher, clear)
  164. u_char *cipher; /* IN 8 octets */
  165. u_char *clear; /* OUT 8 octets */
  166. {
  167. des_ecb_encrypt((des_cblock *)cipher, (des_cblock *)clear,
  168. key_schedule, 0);
  169. return (1);
  170. }
  171. #endif /* USE_CRYPT */