sha1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* sha1.c - an implementation of Secure Hash Algorithm 1 (SHA1)
  2. * based on RFC 3174.
  3. *
  4. * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  16. */
  17. #include <string.h>
  18. #include "byte_order.h"
  19. #include "sha1.h"
  20. /**
  21. * Initialize context before calculaing hash.
  22. *
  23. * @param ctx context to initialize
  24. */
  25. void rhash_sha1_init(sha1_ctx *ctx)
  26. {
  27. ctx->length = 0;
  28. /* initialize algorithm state */
  29. ctx->hash[0] = 0x67452301;
  30. ctx->hash[1] = 0xefcdab89;
  31. ctx->hash[2] = 0x98badcfe;
  32. ctx->hash[3] = 0x10325476;
  33. ctx->hash[4] = 0xc3d2e1f0;
  34. }
  35. /**
  36. * The core transformation. Process a 512-bit block.
  37. * The function has been taken from RFC 3174 with little changes.
  38. *
  39. * @param hash algorithm state
  40. * @param block the message block to process
  41. */
  42. static void rhash_sha1_process_block(unsigned* hash, const unsigned* block)
  43. {
  44. int t; /* Loop counter */
  45. uint32_t temp; /* Temporary word value */
  46. uint32_t W[80]; /* Word sequence */
  47. uint32_t A, B, C, D, E; /* Word buffers */
  48. /* initialize the first 16 words in the array W */
  49. for (t = 0; t < 16; t++) {
  50. /* note: it is much faster to apply be2me here, then using be32_copy */
  51. W[t] = be2me_32(block[t]);
  52. }
  53. /* initialize the rest */
  54. for (t = 16; t < 80; t++) {
  55. W[t] = ROTL32(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
  56. }
  57. A = hash[0];
  58. B = hash[1];
  59. C = hash[2];
  60. D = hash[3];
  61. E = hash[4];
  62. for (t = 0; t < 20; t++) {
  63. /* the following is faster than ((B & C) | ((~B) & D)) */
  64. temp = ROTL32(A, 5) + (((C ^ D) & B) ^ D)
  65. + E + W[t] + 0x5A827999;
  66. E = D;
  67. D = C;
  68. C = ROTL32(B, 30);
  69. B = A;
  70. A = temp;
  71. }
  72. for (t = 20; t < 40; t++) {
  73. temp = ROTL32(A, 5) + (B ^ C ^ D) + E + W[t] + 0x6ED9EBA1;
  74. E = D;
  75. D = C;
  76. C = ROTL32(B, 30);
  77. B = A;
  78. A = temp;
  79. }
  80. for (t = 40; t < 60; t++) {
  81. temp = ROTL32(A, 5) + ((B & C) | (B & D) | (C & D))
  82. + E + W[t] + 0x8F1BBCDC;
  83. E = D;
  84. D = C;
  85. C = ROTL32(B, 30);
  86. B = A;
  87. A = temp;
  88. }
  89. for (t = 60; t < 80; t++) {
  90. temp = ROTL32(A, 5) + (B ^ C ^ D) + E + W[t] + 0xCA62C1D6;
  91. E = D;
  92. D = C;
  93. C = ROTL32(B, 30);
  94. B = A;
  95. A = temp;
  96. }
  97. hash[0] += A;
  98. hash[1] += B;
  99. hash[2] += C;
  100. hash[3] += D;
  101. hash[4] += E;
  102. }
  103. /**
  104. * Calculate message hash.
  105. * Can be called repeatedly with chunks of the message to be hashed.
  106. *
  107. * @param ctx the algorithm context containing current hashing state
  108. * @param msg message chunk
  109. * @param size length of the message chunk
  110. */
  111. void rhash_sha1_update(sha1_ctx *ctx, const unsigned char* msg, size_t size)
  112. {
  113. unsigned index = (unsigned)ctx->length & 63;
  114. ctx->length += size;
  115. /* fill partial block */
  116. if (index) {
  117. unsigned left = sha1_block_size - index;
  118. memcpy(ctx->message + index, msg, (size < left ? size : left));
  119. if (size < left) return;
  120. /* process partial block */
  121. rhash_sha1_process_block(ctx->hash, (unsigned*)ctx->message);
  122. msg += left;
  123. size -= left;
  124. }
  125. while (size >= sha1_block_size) {
  126. unsigned* aligned_message_block;
  127. if (IS_ALIGNED_32(msg)) {
  128. /* the most common case is processing of an already aligned message
  129. without copying it */
  130. aligned_message_block = (unsigned*)msg;
  131. } else {
  132. memcpy(ctx->message, msg, sha1_block_size);
  133. aligned_message_block = (unsigned*)ctx->message;
  134. }
  135. rhash_sha1_process_block(ctx->hash, aligned_message_block);
  136. msg += sha1_block_size;
  137. size -= sha1_block_size;
  138. }
  139. if (size) {
  140. /* save leftovers */
  141. memcpy(ctx->message, msg, size);
  142. }
  143. }
  144. /**
  145. * Store calculated hash into the given array.
  146. *
  147. * @param ctx the algorithm context containing current hashing state
  148. * @param result calculated hash in binary form
  149. */
  150. void rhash_sha1_final(sha1_ctx *ctx, unsigned char* result)
  151. {
  152. unsigned index = (unsigned)ctx->length & 63;
  153. unsigned* msg32 = (unsigned*)ctx->message;
  154. /* pad message and run for last block */
  155. ctx->message[index++] = 0x80;
  156. while ((index & 3) != 0) {
  157. ctx->message[index++] = 0;
  158. }
  159. index >>= 2;
  160. /* if no room left in the message to store 64-bit message length */
  161. if (index > 14) {
  162. /* then fill the rest with zeros and process it */
  163. while (index < 16) {
  164. msg32[index++] = 0;
  165. }
  166. rhash_sha1_process_block(ctx->hash, msg32);
  167. index = 0;
  168. }
  169. while (index < 14) {
  170. msg32[index++] = 0;
  171. }
  172. msg32[14] = be2me_32( (unsigned)(ctx->length >> 29) );
  173. msg32[15] = be2me_32( (unsigned)(ctx->length << 3) );
  174. rhash_sha1_process_block(ctx->hash, msg32);
  175. if (result) be32_copy(result, 0, &ctx->hash, sha1_hash_size);
  176. }