sha1.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Yet another SHA-1 implementation.
  2. *
  3. * Copyright (c) 2003 Red Hat, Inc.
  4. * Written by Nalin Dahyabhai <nalin@redhat.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU Public License, in which case the provisions of the GPL are
  21. * required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /* See http://www.itl.nist.gov/fipspubs/fip180-1.htm for descriptions. */
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <netinet/in.h>
  42. #include <fcntl.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <endian.h>
  47. #include <unistd.h>
  48. #include "sha1.h"
  49. static unsigned char
  50. padding[SHA1_BLOCK_SIZE] = {
  51. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. };
  56. static uint32_t
  57. F(uint32_t b, uint32_t c, uint32_t d)
  58. {
  59. return (b & c) | ((~b) & d);
  60. }
  61. static uint32_t
  62. G(uint32_t b, uint32_t c, uint32_t d)
  63. {
  64. return b ^ c ^ d;
  65. }
  66. static uint32_t
  67. H(uint32_t b, uint32_t c, uint32_t d)
  68. {
  69. return (b & c) | (b & d) | (c & d);
  70. }
  71. static uint32_t
  72. RL(uint32_t n, uint32_t s)
  73. {
  74. return (n << s) | (n >> (32 - s));
  75. }
  76. static uint32_t
  77. sha1_round(uint32_t (*FUNC)(uint32_t, uint32_t, uint32_t),
  78. uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e,
  79. uint32_t i, uint32_t n)
  80. {
  81. return RL(a, 5) + FUNC(b, c, d) + e + i + n;
  82. }
  83. void
  84. sha1_init(struct sha1_context *ctx)
  85. {
  86. memset(ctx, 0, sizeof(*ctx));
  87. ctx->a = 0x67452301;
  88. ctx->b = 0xefcdab89;
  89. ctx->c = 0x98badcfe;
  90. ctx->d = 0x10325476;
  91. ctx->e = 0xc3d2e1f0;
  92. }
  93. static void
  94. sha1_process(struct sha1_context *ctx, uint32_t buffer[SHA1_BLOCK_SIZE / 4])
  95. {
  96. uint32_t a, b, c, d, e, temp;
  97. uint32_t data[80];
  98. int i;
  99. for (i = 0; i < 16; i++) {
  100. data[i] = htonl(buffer[i]);
  101. }
  102. for (i = 16; i < 80; i++) {
  103. data[i] = RL(data[i - 3] ^ data[i - 8] ^ data[i - 14] ^ data[i - 16], 1);
  104. }
  105. a = ctx->a;
  106. b = ctx->b;
  107. c = ctx->c;
  108. d = ctx->d;
  109. e = ctx->e;
  110. for (i = 0; i < 20; i++) {
  111. temp = sha1_round(F, a, b, c, d, e, data[i], 0x5a827999);
  112. e = d; d = c; c = RL(b, 30); b = a; a = temp;
  113. }
  114. for (i = 20; i < 40; i++) {
  115. temp = sha1_round(G, a, b, c, d, e, data[i], 0x6ed9eba1);
  116. e = d; d = c; c = RL(b, 30); b = a; a = temp;
  117. }
  118. for (i = 40; i < 60; i++) {
  119. temp = sha1_round(H, a, b, c, d, e, data[i], 0x8f1bbcdc);
  120. e = d; d = c; c = RL(b, 30); b = a; a = temp;
  121. }
  122. for (i = 60; i < 80; i++) {
  123. temp = sha1_round(G, a, b, c, d, e, data[i], 0xca62c1d6);
  124. e = d; d = c; c = RL(b, 30); b = a; a = temp;
  125. }
  126. ctx->a += a;
  127. ctx->b += b;
  128. ctx->c += c;
  129. ctx->d += d;
  130. ctx->e += e;
  131. memset(buffer, 0, sizeof(buffer[0]) * SHA1_BLOCK_SIZE / 4);
  132. memset(data, 0, sizeof(data));
  133. }
  134. void
  135. sha1_update(struct sha1_context *ctx, const unsigned char *data, size_t length)
  136. {
  137. size_t i = 0, l = length, c, t;
  138. uint32_t count = 0;
  139. /* Process any pending + data blocks. */
  140. while (l + ctx->pending_count >= SHA1_BLOCK_SIZE) {
  141. c = ctx->pending_count;
  142. t = SHA1_BLOCK_SIZE - c;
  143. memcpy(ctx->pending.c + c, &data[i], t);
  144. sha1_process(ctx, ctx->pending.i);
  145. i += t;
  146. l -= t;
  147. ctx->pending_count = 0;
  148. }
  149. /* Save what's left of the data block as a pending data block. */
  150. c = ctx->pending_count;
  151. memcpy(ctx->pending.c + c, &data[i], l);
  152. ctx->pending_count += l;
  153. /* Update the message length. */
  154. ctx->count += length;
  155. /* Update our internal counts. */
  156. if (length != 0) {
  157. count = ctx->counts[0];
  158. ctx->counts[0] += length;
  159. if (count >= ctx->counts[0]) {
  160. ctx->counts[1]++;
  161. }
  162. }
  163. }
  164. size_t
  165. sha1_output(struct sha1_context *ctx, unsigned char *out)
  166. {
  167. struct sha1_context ctx2;
  168. /* Output the sum. */
  169. if (out != NULL) {
  170. uint32_t c;
  171. memcpy(&ctx2, ctx, sizeof(ctx2));
  172. /* Pad this block. */
  173. c = ctx2.pending_count;
  174. memcpy(ctx2.pending.c + c,
  175. padding, SHA1_BLOCK_SIZE - c);
  176. /* Do we need to process two blocks now? */
  177. if (c >= (SHA1_BLOCK_SIZE - (sizeof(uint32_t) * 2))) {
  178. /* Process this block. */
  179. sha1_process(&ctx2, ctx2.pending.i);
  180. /* Set up another block. */
  181. ctx2.pending_count = 0;
  182. memset(ctx2.pending.c, 0, SHA1_BLOCK_SIZE);
  183. ctx2.pending.c[0] =
  184. (c == SHA1_BLOCK_SIZE) ? 0x80 : 0;
  185. }
  186. /* Process the final block. */
  187. ctx2.counts[1] <<= 3;
  188. if (ctx2.counts[0] >> 29) {
  189. ctx2.counts[1] |=
  190. (ctx2.counts[0] >> 29);
  191. }
  192. ctx2.counts[0] <<= 3;
  193. ctx2.counts[0] = htonl(ctx2.counts[0]);
  194. ctx2.counts[1] = htonl(ctx2.counts[1]);
  195. memcpy(ctx2.pending.c + 56,
  196. &ctx2.counts[1], sizeof(uint32_t));
  197. memcpy(ctx2.pending.c + 60,
  198. &ctx2.counts[0], sizeof(uint32_t));
  199. sha1_process(&ctx2, ctx2.pending.i);
  200. /* Output the data. */
  201. out[ 3] = (ctx2.a >> 0) & 0xff;
  202. out[ 2] = (ctx2.a >> 8) & 0xff;
  203. out[ 1] = (ctx2.a >> 16) & 0xff;
  204. out[ 0] = (ctx2.a >> 24) & 0xff;
  205. out[ 7] = (ctx2.b >> 0) & 0xff;
  206. out[ 6] = (ctx2.b >> 8) & 0xff;
  207. out[ 5] = (ctx2.b >> 16) & 0xff;
  208. out[ 4] = (ctx2.b >> 24) & 0xff;
  209. out[11] = (ctx2.c >> 0) & 0xff;
  210. out[10] = (ctx2.c >> 8) & 0xff;
  211. out[ 9] = (ctx2.c >> 16) & 0xff;
  212. out[ 8] = (ctx2.c >> 24) & 0xff;
  213. out[15] = (ctx2.d >> 0) & 0xff;
  214. out[14] = (ctx2.d >> 8) & 0xff;
  215. out[13] = (ctx2.d >> 16) & 0xff;
  216. out[12] = (ctx2.d >> 24) & 0xff;
  217. out[19] = (ctx2.e >> 0) & 0xff;
  218. out[18] = (ctx2.e >> 8) & 0xff;
  219. out[17] = (ctx2.e >> 16) & 0xff;
  220. out[16] = (ctx2.e >> 24) & 0xff;
  221. }
  222. return SHA1_OUTPUT_SIZE;
  223. }