fcrypt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DES low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. /* NOCW */
  15. #include <stdio.h>
  16. #ifdef _OSD_POSIX
  17. #ifndef CHARSET_EBCDIC
  18. #define CHARSET_EBCDIC 1
  19. #endif
  20. #endif
  21. #ifdef CHARSET_EBCDIC
  22. #include <openssl/ebcdic.h>
  23. #endif
  24. #include <openssl/crypto.h>
  25. #include "des_local.h"
  26. /*
  27. * Added more values to handle illegal salt values the way normal crypt()
  28. * implementations do.
  29. */
  30. static const unsigned char con_salt[128] = {
  31. 0xD2,
  32. 0xD3,
  33. 0xD4,
  34. 0xD5,
  35. 0xD6,
  36. 0xD7,
  37. 0xD8,
  38. 0xD9,
  39. 0xDA,
  40. 0xDB,
  41. 0xDC,
  42. 0xDD,
  43. 0xDE,
  44. 0xDF,
  45. 0xE0,
  46. 0xE1,
  47. 0xE2,
  48. 0xE3,
  49. 0xE4,
  50. 0xE5,
  51. 0xE6,
  52. 0xE7,
  53. 0xE8,
  54. 0xE9,
  55. 0xEA,
  56. 0xEB,
  57. 0xEC,
  58. 0xED,
  59. 0xEE,
  60. 0xEF,
  61. 0xF0,
  62. 0xF1,
  63. 0xF2,
  64. 0xF3,
  65. 0xF4,
  66. 0xF5,
  67. 0xF6,
  68. 0xF7,
  69. 0xF8,
  70. 0xF9,
  71. 0xFA,
  72. 0xFB,
  73. 0xFC,
  74. 0xFD,
  75. 0xFE,
  76. 0xFF,
  77. 0x00,
  78. 0x01,
  79. 0x02,
  80. 0x03,
  81. 0x04,
  82. 0x05,
  83. 0x06,
  84. 0x07,
  85. 0x08,
  86. 0x09,
  87. 0x0A,
  88. 0x0B,
  89. 0x05,
  90. 0x06,
  91. 0x07,
  92. 0x08,
  93. 0x09,
  94. 0x0A,
  95. 0x0B,
  96. 0x0C,
  97. 0x0D,
  98. 0x0E,
  99. 0x0F,
  100. 0x10,
  101. 0x11,
  102. 0x12,
  103. 0x13,
  104. 0x14,
  105. 0x15,
  106. 0x16,
  107. 0x17,
  108. 0x18,
  109. 0x19,
  110. 0x1A,
  111. 0x1B,
  112. 0x1C,
  113. 0x1D,
  114. 0x1E,
  115. 0x1F,
  116. 0x20,
  117. 0x21,
  118. 0x22,
  119. 0x23,
  120. 0x24,
  121. 0x25,
  122. 0x20,
  123. 0x21,
  124. 0x22,
  125. 0x23,
  126. 0x24,
  127. 0x25,
  128. 0x26,
  129. 0x27,
  130. 0x28,
  131. 0x29,
  132. 0x2A,
  133. 0x2B,
  134. 0x2C,
  135. 0x2D,
  136. 0x2E,
  137. 0x2F,
  138. 0x30,
  139. 0x31,
  140. 0x32,
  141. 0x33,
  142. 0x34,
  143. 0x35,
  144. 0x36,
  145. 0x37,
  146. 0x38,
  147. 0x39,
  148. 0x3A,
  149. 0x3B,
  150. 0x3C,
  151. 0x3D,
  152. 0x3E,
  153. 0x3F,
  154. 0x40,
  155. 0x41,
  156. 0x42,
  157. 0x43,
  158. 0x44,
  159. };
  160. static const unsigned char cov_2char[64] = {
  161. 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  162. 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
  163. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
  164. 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
  165. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
  166. 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  167. 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
  168. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
  169. };
  170. char *DES_crypt(const char *buf, const char *salt)
  171. {
  172. static char buff[14];
  173. #ifndef CHARSET_EBCDIC
  174. return DES_fcrypt(buf, salt, buff);
  175. #else
  176. char e_salt[2 + 1];
  177. char e_buf[32 + 1]; /* replace 32 by 8 ? */
  178. char *ret;
  179. if (salt[0] == '\0' || salt[1] == '\0')
  180. return NULL;
  181. /* Copy salt, convert to ASCII. */
  182. e_salt[0] = salt[0];
  183. e_salt[1] = salt[1];
  184. e_salt[2] = '\0';
  185. ebcdic2ascii(e_salt, e_salt, sizeof(e_salt));
  186. /* Convert password to ASCII. */
  187. OPENSSL_strlcpy(e_buf, buf, sizeof(e_buf));
  188. ebcdic2ascii(e_buf, e_buf, sizeof(e_buf));
  189. /* Encrypt it (from/to ASCII); if it worked, convert back. */
  190. ret = DES_fcrypt(e_buf, e_salt, buff);
  191. if (ret != NULL)
  192. ascii2ebcdic(ret, ret, strlen(ret));
  193. return ret;
  194. #endif
  195. }
  196. char *DES_fcrypt(const char *buf, const char *salt, char *ret)
  197. {
  198. unsigned int i, j, x, y;
  199. DES_LONG Eswap0, Eswap1;
  200. DES_LONG out[2], ll;
  201. DES_cblock key;
  202. DES_key_schedule ks;
  203. unsigned char bb[9];
  204. unsigned char *b = bb;
  205. unsigned char c, u;
  206. x = ret[0] = salt[0];
  207. if (x == 0 || x >= sizeof(con_salt))
  208. return NULL;
  209. Eswap0 = con_salt[x] << 2;
  210. x = ret[1] = salt[1];
  211. if (x == 0 || x >= sizeof(con_salt))
  212. return NULL;
  213. Eswap1 = con_salt[x] << 6;
  214. /*
  215. * EAY r=strlen(buf); r=(r+7)/8;
  216. */
  217. for (i = 0; i < 8; i++) {
  218. c = *(buf++);
  219. if (!c)
  220. break;
  221. key[i] = (c << 1);
  222. }
  223. for (; i < 8; i++)
  224. key[i] = 0;
  225. DES_set_key_unchecked(&key, &ks);
  226. fcrypt_body(&(out[0]), &ks, Eswap0, Eswap1);
  227. ll = out[0];
  228. l2c(ll, b);
  229. ll = out[1];
  230. l2c(ll, b);
  231. y = 0;
  232. u = 0x80;
  233. bb[8] = 0;
  234. for (i = 2; i < 13; i++) {
  235. c = 0;
  236. for (j = 0; j < 6; j++) {
  237. c <<= 1;
  238. if (bb[y] & u)
  239. c |= 1;
  240. u >>= 1;
  241. if (!u) {
  242. y++;
  243. u = 0x80;
  244. }
  245. }
  246. ret[i] = cov_2char[c];
  247. }
  248. ret[13] = '\0';
  249. return ret;
  250. }