2
0

apr_sha1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * The exported function:
  18. *
  19. * apr_sha1_base64(const char *clear, int len, char *out);
  20. *
  21. * provides a means to SHA1 crypt/encode a plaintext password in
  22. * a way which makes password files compatible with those commonly
  23. * used in netscape web and ldap installations. It was put together
  24. * by Clinton Wong <clintdw@netcom.com>, who also notes that:
  25. *
  26. * Note: SHA1 support is useful for migration purposes, but is less
  27. * secure than Apache's password format, since Apache's (MD5)
  28. * password format uses a random eight character salt to generate
  29. * one of many possible hashes for the same password. Netscape
  30. * uses plain SHA1 without a salt, so the same password
  31. * will always generate the same hash, making it easier
  32. * to break since the search space is smaller.
  33. *
  34. * See also the documentation in support/SHA1 as to hints on how to
  35. * migrate an existing netscape installation and other supplied utitlites.
  36. *
  37. * This software also makes use of the following component:
  38. *
  39. * NIST Secure Hash Algorithm
  40. * heavily modified by Uwe Hollerbach uh@alumni.caltech edu
  41. * from Peter C. Gutmann's implementation as found in
  42. * Applied Cryptography by Bruce Schneier
  43. * This code is hereby placed in the public domain
  44. */
  45. #include "apr_sha1.h"
  46. #include "apr_base64.h"
  47. #include "apr_strings.h"
  48. #include "apr_lib.h"
  49. #if APR_CHARSET_EBCDIC
  50. #include "apr_xlate.h"
  51. #endif /*APR_CHARSET_EBCDIC*/
  52. #include <string.h>
  53. /* a bit faster & bigger, if defined */
  54. #define UNROLL_LOOPS
  55. /* NIST's proposed modification to SHA, 7/11/94 */
  56. #define USE_MODIFIED_SHA
  57. /* SHA f()-functions */
  58. #define f1(x,y,z) ((x & y) | (~x & z))
  59. #define f2(x,y,z) (x ^ y ^ z)
  60. #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
  61. #define f4(x,y,z) (x ^ y ^ z)
  62. /* SHA constants */
  63. #define CONST1 0x5a827999L
  64. #define CONST2 0x6ed9eba1L
  65. #define CONST3 0x8f1bbcdcL
  66. #define CONST4 0xca62c1d6L
  67. /* 32-bit rotate */
  68. #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
  69. #define FUNC(n,i) \
  70. temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
  71. E = D; D = C; C = ROT32(B,30); B = A; A = temp
  72. #define SHA_BLOCKSIZE 64
  73. #if APR_CHARSET_EBCDIC
  74. static apr_xlate_t *ebcdic2ascii_xlate;
  75. APU_DECLARE(apr_status_t) apr_SHA1InitEBCDIC(apr_xlate_t *x)
  76. {
  77. apr_status_t rv;
  78. int onoff;
  79. /* Only single-byte conversion is supported.
  80. */
  81. rv = apr_xlate_sb_get(x, &onoff);
  82. if (rv) {
  83. return rv;
  84. }
  85. if (!onoff) { /* If conversion is not single-byte-only */
  86. return APR_EINVAL;
  87. }
  88. ebcdic2ascii_xlate = x;
  89. return APR_SUCCESS;
  90. }
  91. #endif
  92. /* do SHA transformation */
  93. static void sha_transform(apr_sha1_ctx_t *sha_info)
  94. {
  95. int i;
  96. apr_uint32_t temp, A, B, C, D, E, W[80];
  97. for (i = 0; i < 16; ++i) {
  98. W[i] = sha_info->data[i];
  99. }
  100. for (i = 16; i < 80; ++i) {
  101. W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
  102. #ifdef USE_MODIFIED_SHA
  103. W[i] = ROT32(W[i], 1);
  104. #endif /* USE_MODIFIED_SHA */
  105. }
  106. A = sha_info->digest[0];
  107. B = sha_info->digest[1];
  108. C = sha_info->digest[2];
  109. D = sha_info->digest[3];
  110. E = sha_info->digest[4];
  111. #ifdef UNROLL_LOOPS
  112. FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
  113. FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
  114. FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
  115. FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
  116. FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
  117. FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
  118. FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
  119. FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
  120. FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
  121. FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
  122. FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
  123. FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
  124. FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
  125. FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
  126. FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
  127. FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
  128. #else /* !UNROLL_LOOPS */
  129. for (i = 0; i < 20; ++i) {
  130. FUNC(1,i);
  131. }
  132. for (i = 20; i < 40; ++i) {
  133. FUNC(2,i);
  134. }
  135. for (i = 40; i < 60; ++i) {
  136. FUNC(3,i);
  137. }
  138. for (i = 60; i < 80; ++i) {
  139. FUNC(4,i);
  140. }
  141. #endif /* !UNROLL_LOOPS */
  142. sha_info->digest[0] += A;
  143. sha_info->digest[1] += B;
  144. sha_info->digest[2] += C;
  145. sha_info->digest[3] += D;
  146. sha_info->digest[4] += E;
  147. }
  148. union endianTest {
  149. long Long;
  150. char Char[sizeof(long)];
  151. };
  152. static char isLittleEndian(void)
  153. {
  154. static union endianTest u;
  155. u.Long = 1;
  156. return (u.Char[0] == 1);
  157. }
  158. /* change endianness of data */
  159. /* count is the number of bytes to do an endian flip */
  160. static void maybe_byte_reverse(apr_uint32_t *buffer, int count)
  161. {
  162. int i;
  163. apr_byte_t ct[4], *cp;
  164. if (isLittleEndian()) { /* do the swap only if it is little endian */
  165. count /= sizeof(apr_uint32_t);
  166. cp = (apr_byte_t *) buffer;
  167. for (i = 0; i < count; ++i) {
  168. ct[0] = cp[0];
  169. ct[1] = cp[1];
  170. ct[2] = cp[2];
  171. ct[3] = cp[3];
  172. cp[0] = ct[3];
  173. cp[1] = ct[2];
  174. cp[2] = ct[1];
  175. cp[3] = ct[0];
  176. cp += sizeof(apr_uint32_t);
  177. }
  178. }
  179. }
  180. /* initialize the SHA digest */
  181. APU_DECLARE(void) apr_sha1_init(apr_sha1_ctx_t *sha_info)
  182. {
  183. sha_info->digest[0] = 0x67452301L;
  184. sha_info->digest[1] = 0xefcdab89L;
  185. sha_info->digest[2] = 0x98badcfeL;
  186. sha_info->digest[3] = 0x10325476L;
  187. sha_info->digest[4] = 0xc3d2e1f0L;
  188. sha_info->count_lo = 0L;
  189. sha_info->count_hi = 0L;
  190. sha_info->local = 0;
  191. }
  192. /* update the SHA digest */
  193. APU_DECLARE(void) apr_sha1_update_binary(apr_sha1_ctx_t *sha_info,
  194. const unsigned char *buffer,
  195. unsigned int count)
  196. {
  197. unsigned int i;
  198. if ((sha_info->count_lo + ((apr_uint32_t) count << 3)) < sha_info->count_lo) {
  199. ++sha_info->count_hi;
  200. }
  201. sha_info->count_lo += (apr_uint32_t) count << 3;
  202. sha_info->count_hi += (apr_uint32_t) count >> 29;
  203. if (sha_info->local) {
  204. i = SHA_BLOCKSIZE - sha_info->local;
  205. if (i > count) {
  206. i = count;
  207. }
  208. memcpy(((apr_byte_t *) sha_info->data) + sha_info->local, buffer, i);
  209. count -= i;
  210. buffer += i;
  211. sha_info->local += i;
  212. if (sha_info->local == SHA_BLOCKSIZE) {
  213. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  214. sha_transform(sha_info);
  215. }
  216. else {
  217. return;
  218. }
  219. }
  220. while (count >= SHA_BLOCKSIZE) {
  221. memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
  222. buffer += SHA_BLOCKSIZE;
  223. count -= SHA_BLOCKSIZE;
  224. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  225. sha_transform(sha_info);
  226. }
  227. memcpy(sha_info->data, buffer, count);
  228. sha_info->local = count;
  229. }
  230. APU_DECLARE(void) apr_sha1_update(apr_sha1_ctx_t *sha_info, const char *buf,
  231. unsigned int count)
  232. {
  233. #if APR_CHARSET_EBCDIC
  234. int i;
  235. const apr_byte_t *buffer = (const apr_byte_t *) buf;
  236. apr_size_t inbytes_left, outbytes_left;
  237. if ((sha_info->count_lo + ((apr_uint32_t) count << 3)) < sha_info->count_lo) {
  238. ++sha_info->count_hi;
  239. }
  240. sha_info->count_lo += (apr_uint32_t) count << 3;
  241. sha_info->count_hi += (apr_uint32_t) count >> 29;
  242. /* Is there a remainder of the previous Update operation? */
  243. if (sha_info->local) {
  244. i = SHA_BLOCKSIZE - sha_info->local;
  245. if (i > count) {
  246. i = count;
  247. }
  248. inbytes_left = outbytes_left = i;
  249. apr_xlate_conv_buffer(ebcdic2ascii_xlate, buffer, &inbytes_left,
  250. ((apr_byte_t *) sha_info->data) + sha_info->local,
  251. &outbytes_left);
  252. count -= i;
  253. buffer += i;
  254. sha_info->local += i;
  255. if (sha_info->local == SHA_BLOCKSIZE) {
  256. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  257. sha_transform(sha_info);
  258. }
  259. else {
  260. return;
  261. }
  262. }
  263. while (count >= SHA_BLOCKSIZE) {
  264. inbytes_left = outbytes_left = SHA_BLOCKSIZE;
  265. apr_xlate_conv_buffer(ebcdic2ascii_xlate, buffer, &inbytes_left,
  266. (apr_byte_t *) sha_info->data, &outbytes_left);
  267. buffer += SHA_BLOCKSIZE;
  268. count -= SHA_BLOCKSIZE;
  269. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  270. sha_transform(sha_info);
  271. }
  272. inbytes_left = outbytes_left = count;
  273. apr_xlate_conv_buffer(ebcdic2ascii_xlate, buffer, &inbytes_left,
  274. (apr_byte_t *) sha_info->data, &outbytes_left);
  275. sha_info->local = count;
  276. #else
  277. apr_sha1_update_binary(sha_info, (const unsigned char *) buf, count);
  278. #endif
  279. }
  280. /* finish computing the SHA digest */
  281. APU_DECLARE(void) apr_sha1_final(unsigned char digest[APR_SHA1_DIGESTSIZE],
  282. apr_sha1_ctx_t *sha_info)
  283. {
  284. int count, i, j;
  285. apr_uint32_t lo_bit_count, hi_bit_count, k;
  286. lo_bit_count = sha_info->count_lo;
  287. hi_bit_count = sha_info->count_hi;
  288. count = (int) ((lo_bit_count >> 3) & 0x3f);
  289. ((apr_byte_t *) sha_info->data)[count++] = 0x80;
  290. if (count > SHA_BLOCKSIZE - 8) {
  291. memset(((apr_byte_t *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
  292. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  293. sha_transform(sha_info);
  294. memset((apr_byte_t *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
  295. }
  296. else {
  297. memset(((apr_byte_t *) sha_info->data) + count, 0,
  298. SHA_BLOCKSIZE - 8 - count);
  299. }
  300. maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  301. sha_info->data[14] = hi_bit_count;
  302. sha_info->data[15] = lo_bit_count;
  303. sha_transform(sha_info);
  304. for (i = 0, j = 0; j < APR_SHA1_DIGESTSIZE; i++) {
  305. k = sha_info->digest[i];
  306. digest[j++] = (unsigned char) ((k >> 24) & 0xff);
  307. digest[j++] = (unsigned char) ((k >> 16) & 0xff);
  308. digest[j++] = (unsigned char) ((k >> 8) & 0xff);
  309. digest[j++] = (unsigned char) (k & 0xff);
  310. }
  311. }
  312. APU_DECLARE(void) apr_sha1_base64(const char *clear, int len, char *out)
  313. {
  314. int l;
  315. apr_sha1_ctx_t context;
  316. apr_byte_t digest[APR_SHA1_DIGESTSIZE];
  317. if (strncmp(clear, APR_SHA1PW_ID, APR_SHA1PW_IDLEN) == 0) {
  318. clear += APR_SHA1PW_IDLEN;
  319. }
  320. apr_sha1_init(&context);
  321. apr_sha1_update(&context, clear, len);
  322. apr_sha1_final(digest, &context);
  323. /* private marker. */
  324. apr_cpystrn(out, APR_SHA1PW_ID, APR_SHA1PW_IDLEN + 1);
  325. /* SHA1 hash is always 20 chars */
  326. l = apr_base64_encode_binary(out + APR_SHA1PW_IDLEN, digest, sizeof(digest));
  327. out[l + APR_SHA1PW_IDLEN] = '\0';
  328. /*
  329. * output of base64 encoded SHA1 is always 28 chars + APR_SHA1PW_IDLEN
  330. */
  331. }