set_key.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* $OpenBSD: set_key.c,v 1.3 2013/11/18 18:49:53 brad Exp $ */
  2. /* lib/des/set_key.c */
  3. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  4. * All rights reserved.
  5. *
  6. * This file is part of an SSL implementation written
  7. * by Eric Young (eay@mincom.oz.au).
  8. * The implementation was written so as to conform with Netscapes SSL
  9. * specification. This library and applications are
  10. * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  11. * as long as the following conditions are aheared to.
  12. *
  13. * Copyright remains Eric Young's, and as such any Copyright notices in
  14. * the code are not to be removed. If this code is used in a product,
  15. * Eric Young should be given attribution as the author of the parts used.
  16. * This can be in the form of a textual message at program startup or
  17. * in documentation (online or textual) provided with the package.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. All advertising materials mentioning features or use of this software
  28. * must display the following acknowledgement:
  29. * This product includes software developed by Eric Young (eay@mincom.oz.au)
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. *
  43. * The licence and distribution terms for any publically available version or
  44. * derivative of this code cannot be changed. i.e. this code cannot simply be
  45. * copied and put under another distribution licence
  46. * [including the GNU Public Licence.]
  47. */
  48. /* set_key.c v 1.4 eay 24/9/91
  49. * 1.4 Speed up by 400% :-)
  50. * 1.3 added register declarations.
  51. * 1.2 unrolled make_key_sched a bit more
  52. * 1.1 added norm_expand_bits
  53. * 1.0 First working version
  54. */
  55. #include "des_locl.h"
  56. #include "podd.h"
  57. #include "sk.h"
  58. #ifdef PROTO
  59. static int check_parity(des_cblock (*key));
  60. #else
  61. static int check_parity();
  62. #endif
  63. int des_check_key=0;
  64. void
  65. des_set_odd_parity(des_cblock (*key))
  66. {
  67. int i;
  68. for (i = 0; i < DES_KEY_SZ; i++)
  69. (*key)[i] = odd_parity[(*key)[i]];
  70. }
  71. static int
  72. check_parity(des_cblock (*key))
  73. {
  74. int i;
  75. for (i = 0; i < DES_KEY_SZ; i++) {
  76. if ((*key)[i] != odd_parity[(*key)[i]])
  77. return(0);
  78. }
  79. return (1);
  80. }
  81. /* Weak and semi week keys as take from
  82. * %A D.W. Davies
  83. * %A W.L. Price
  84. * %T Security for Computer Networks
  85. * %I John Wiley & Sons
  86. * %D 1984
  87. * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  88. * (and actual cblock values).
  89. */
  90. #define NUM_WEAK_KEY 16
  91. static des_cblock weak_keys[NUM_WEAK_KEY]={
  92. /* weak keys */
  93. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  94. {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  95. {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  96. {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  97. /* semi-weak keys */
  98. {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  99. {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  100. {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  101. {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  102. {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  103. {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  104. {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  105. {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  106. {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  107. {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  108. {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  109. {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  110. int
  111. des_is_weak_key(des_cblock (*key))
  112. {
  113. int i;
  114. for (i = 0; i < NUM_WEAK_KEY; i++) {
  115. /* Added == 0 to comparision, I obviously don't run
  116. * this section very often :-(, thanks to
  117. * engineering@MorningStar.Com for the fix
  118. * eay 93/06/29 */
  119. if (bcmp(weak_keys[i], key, sizeof(des_cblock)) == 0)
  120. return (1);
  121. }
  122. return (0);
  123. }
  124. /* NOW DEFINED IN des_local.h
  125. * See ecb_encrypt.c for a pseudo description of these macros.
  126. * #define PERM_OP(a, b, t, n, m) ((t) = ((((a) >> (n))^(b)) & (m)),\
  127. * (b)^=(t),\
  128. * (a) = ((a)^((t) << (n))))
  129. */
  130. #define HPERM_OP(a, t, n, m) ((t) = ((((a) << (16 - (n)))^(a)) & (m)),\
  131. (a) = (a)^(t)^(t >> (16 - (n))))
  132. static int shifts2[16]={0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0};
  133. /* return 0 if key parity is odd (correct),
  134. * return -1 if key parity error,
  135. * return -2 if illegal weak key.
  136. */
  137. int
  138. des_set_key(des_cblock (*key), des_key_schedule schedule)
  139. {
  140. register u_int32_t c, d, t, s;
  141. register unsigned char *in;
  142. register u_int32_t *k;
  143. register int i;
  144. if (des_check_key) {
  145. if (!check_parity(key))
  146. return(-1);
  147. if (des_is_weak_key(key))
  148. return(-2);
  149. }
  150. k = (u_int32_t *) schedule;
  151. in = (unsigned char *) key;
  152. c2l(in, c);
  153. c2l(in, d);
  154. /* do PC1 in 60 simple operations */
  155. /* PERM_OP(d, c, t, 4, 0x0f0f0f0fL);
  156. HPERM_OP(c, t, -2, 0xcccc0000L);
  157. HPERM_OP(c, t, -1, 0xaaaa0000L);
  158. HPERM_OP(c, t, 8, 0x00ff0000L);
  159. HPERM_OP(c, t, -1, 0xaaaa0000L);
  160. HPERM_OP(d, t, -8, 0xff000000L);
  161. HPERM_OP(d, t, 8, 0x00ff0000L);
  162. HPERM_OP(d, t, 2, 0x33330000L);
  163. d = ((d & 0x00aa00aaL) << 7L) | ((d & 0x55005500L) >> 7L) | (d & 0xaa55aa55L);
  164. d = (d >> 8) | ((c & 0xf0000000L) >> 4);
  165. c &= 0x0fffffffL; */
  166. /* I now do it in 47 simple operations :-)
  167. * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  168. * for the inspiration. :-) */
  169. PERM_OP (d, c, t, 4, 0x0f0f0f0fL);
  170. HPERM_OP(c, t, -2, 0xcccc0000L);
  171. HPERM_OP(d, t, -2, 0xcccc0000L);
  172. PERM_OP (d, c, t, 1, 0x55555555L);
  173. PERM_OP (c, d, t, 8, 0x00ff00ffL);
  174. PERM_OP (d, c, t, 1, 0x55555555L);
  175. d = (((d & 0x000000ffL) << 16L) | (d & 0x0000ff00L) |
  176. ((d & 0x00ff0000L) >> 16L) | ((c & 0xf0000000L) >> 4L));
  177. c &= 0x0fffffffL;
  178. for (i = 0; i < ITERATIONS; i++) {
  179. if (shifts2[i])
  180. { c = ((c >> 2L) | (c << 26L)); d = ((d >> 2L) | (d << 26L)); }
  181. else
  182. { c = ((c >> 1L) | (c << 27L)); d = ((d >> 1L) | (d << 27L)); }
  183. c &= 0x0fffffffL;
  184. d &= 0x0fffffffL;
  185. /* could be a few less shifts but I am to lazy at this
  186. * point in time to investigate */
  187. s = des_skb[0][ (c ) & 0x3f ]|
  188. des_skb[1][((c >> 6) & 0x03) | ((c >> 7L) & 0x3c)]|
  189. des_skb[2][((c >> 13) & 0x0f) | ((c >> 14L) & 0x30)]|
  190. des_skb[3][((c >> 20) & 0x01) | ((c >> 21L) & 0x06) |
  191. ((c >> 22L) & 0x38)];
  192. t = des_skb[4][ (d ) & 0x3f ]|
  193. des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)]|
  194. des_skb[6][ (d >> 15L) & 0x3f ]|
  195. des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)];
  196. /* table contained 0213 4657 */
  197. *(k++) = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL;
  198. s = ((s >> 16L) | (t & 0xffff0000L));
  199. s = (s << 4L) | (s >> 28L);
  200. *(k++) = s & 0xffffffffL;
  201. }
  202. return (0);
  203. }
  204. int
  205. des_key_sched(des_cblock (*key), des_key_schedule schedule)
  206. {
  207. return (des_set_key(key, schedule));
  208. }