2sha1.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * SHA-1 implementation largely based on libmincrypt in the the Android
  6. * Open Source Project (platorm/system/core.git/libmincrypt/sha.c
  7. */
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2sha.h"
  11. /*
  12. * Some machines lack byteswap.h and endian.h. These have to use the
  13. * slower code, even if they're little-endian.
  14. */
  15. #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
  16. /*
  17. * This version is about 28% faster than the generic version below,
  18. * but assumes little-endianness.
  19. */
  20. static uint32_t ror27(uint32_t val)
  21. {
  22. return (val >> 27) | (val << 5);
  23. }
  24. static uint32_t ror2(uint32_t val)
  25. {
  26. return (val >> 2) | (val << 30);
  27. }
  28. static uint32_t ror31(uint32_t val)
  29. {
  30. return (val >> 31) | (val << 1);
  31. }
  32. static void sha1_transform(struct vb2_sha1_context *ctx)
  33. {
  34. /* Note that this array uses 80*4=320 bytes of stack */
  35. uint32_t W[80];
  36. register uint32_t A, B, C, D, E;
  37. int t;
  38. A = ctx->state[0];
  39. B = ctx->state[1];
  40. C = ctx->state[2];
  41. D = ctx->state[3];
  42. E = ctx->state[4];
  43. #define SHA_F1(A,B,C,D,E,t) \
  44. E += ror27(A) + \
  45. (W[t] = bswap_32(ctx->buf.w[t])) + \
  46. (D^(B&(C^D))) + 0x5A827999; \
  47. B = ror2(B);
  48. for (t = 0; t < 15; t += 5) {
  49. SHA_F1(A,B,C,D,E,t + 0);
  50. SHA_F1(E,A,B,C,D,t + 1);
  51. SHA_F1(D,E,A,B,C,t + 2);
  52. SHA_F1(C,D,E,A,B,t + 3);
  53. SHA_F1(B,C,D,E,A,t + 4);
  54. }
  55. SHA_F1(A,B,C,D,E,t + 0); /* 16th one, t == 15 */
  56. #undef SHA_F1
  57. #define SHA_F1(A,B,C,D,E,t) \
  58. E += ror27(A) + \
  59. (W[t] = ror31(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16])) + \
  60. (D^(B&(C^D))) + 0x5A827999; \
  61. B = ror2(B);
  62. SHA_F1(E,A,B,C,D,t + 1);
  63. SHA_F1(D,E,A,B,C,t + 2);
  64. SHA_F1(C,D,E,A,B,t + 3);
  65. SHA_F1(B,C,D,E,A,t + 4);
  66. #undef SHA_F1
  67. #define SHA_F2(A,B,C,D,E,t) \
  68. E += ror27(A) + \
  69. (W[t] = ror31(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16])) + \
  70. (B^C^D) + 0x6ED9EBA1; \
  71. B = ror2(B);
  72. for (t = 20; t < 40; t += 5) {
  73. SHA_F2(A,B,C,D,E,t + 0);
  74. SHA_F2(E,A,B,C,D,t + 1);
  75. SHA_F2(D,E,A,B,C,t + 2);
  76. SHA_F2(C,D,E,A,B,t + 3);
  77. SHA_F2(B,C,D,E,A,t + 4);
  78. }
  79. #undef SHA_F2
  80. #define SHA_F3(A,B,C,D,E,t) \
  81. E += ror27(A) + \
  82. (W[t] = ror31(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16])) + \
  83. ((B&C)|(D&(B|C))) + 0x8F1BBCDC; \
  84. B = ror2(B);
  85. for (; t < 60; t += 5) {
  86. SHA_F3(A,B,C,D,E,t + 0);
  87. SHA_F3(E,A,B,C,D,t + 1);
  88. SHA_F3(D,E,A,B,C,t + 2);
  89. SHA_F3(C,D,E,A,B,t + 3);
  90. SHA_F3(B,C,D,E,A,t + 4);
  91. }
  92. #undef SHA_F3
  93. #define SHA_F4(A,B,C,D,E,t) \
  94. E += ror27(A) + \
  95. (W[t] = ror31(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16])) + \
  96. (B^C^D) + 0xCA62C1D6; \
  97. B = ror2(B);
  98. for (; t < 80; t += 5) {
  99. SHA_F4(A,B,C,D,E,t + 0);
  100. SHA_F4(E,A,B,C,D,t + 1);
  101. SHA_F4(D,E,A,B,C,t + 2);
  102. SHA_F4(C,D,E,A,B,t + 3);
  103. SHA_F4(B,C,D,E,A,t + 4);
  104. }
  105. #undef SHA_F4
  106. ctx->state[0] += A;
  107. ctx->state[1] += B;
  108. ctx->state[2] += C;
  109. ctx->state[3] += D;
  110. ctx->state[4] += E;
  111. }
  112. void vb2_sha1_update(struct vb2_sha1_context *ctx,
  113. const uint8_t *data,
  114. uint32_t size)
  115. {
  116. int i = ctx->count % sizeof(ctx->buf);
  117. const uint8_t *p = (const uint8_t*)data;
  118. ctx->count += size;
  119. while (size > sizeof(ctx->buf) - i) {
  120. memcpy(&ctx->buf.b[i], p, sizeof(ctx->buf) - i);
  121. size -= sizeof(ctx->buf) - i;
  122. p += sizeof(ctx->buf) - i;
  123. sha1_transform(ctx);
  124. i = 0;
  125. }
  126. while (size--) {
  127. ctx->buf.b[i++] = *p++;
  128. if (i == sizeof(ctx->buf)) {
  129. sha1_transform(ctx);
  130. i = 0;
  131. }
  132. }
  133. }
  134. uint8_t *vb2_sha1_finalize(struct vb2_sha1_context *ctx)
  135. {
  136. uint32_t cnt = ctx->count * 8;
  137. int i;
  138. vb2_sha1_update(ctx, (uint8_t*)"\x80", 1);
  139. while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
  140. vb2_sha1_update(ctx, (uint8_t*)"\0", 1);
  141. }
  142. for (i = 0; i < 8; ++i) {
  143. uint8_t tmp = cnt >> ((7 - i) * 8);
  144. vb2_sha1_update(ctx, &tmp, 1);
  145. }
  146. for (i = 0; i < 5; i++) {
  147. ctx->buf.w[i] = bswap_32(ctx->state[i]);
  148. }
  149. return ctx->buf.b;
  150. }
  151. #else /* #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN) */
  152. #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
  153. static void sha1_transform(struct vb2_sha1_context *ctx)
  154. {
  155. /* Note that this array uses 80*4=320 bytes of stack */
  156. uint32_t W[80];
  157. uint32_t A, B, C, D, E;
  158. uint8_t *p = ctx->buf;
  159. int t;
  160. for(t = 0; t < 16; ++t) {
  161. uint32_t tmp = *p++ << 24;
  162. tmp |= *p++ << 16;
  163. tmp |= *p++ << 8;
  164. tmp |= *p++;
  165. W[t] = tmp;
  166. }
  167. for(; t < 80; t++) {
  168. W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  169. }
  170. A = ctx->state[0];
  171. B = ctx->state[1];
  172. C = ctx->state[2];
  173. D = ctx->state[3];
  174. E = ctx->state[4];
  175. for(t = 0; t < 80; t++) {
  176. uint32_t tmp = rol(5,A) + E + W[t];
  177. if (t < 20)
  178. tmp += (D^(B&(C^D))) + 0x5A827999;
  179. else if ( t < 40)
  180. tmp += (B^C^D) + 0x6ED9EBA1;
  181. else if ( t < 60)
  182. tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
  183. else
  184. tmp += (B^C^D) + 0xCA62C1D6;
  185. E = D;
  186. D = C;
  187. C = rol(30,B);
  188. B = A;
  189. A = tmp;
  190. }
  191. ctx->state[0] += A;
  192. ctx->state[1] += B;
  193. ctx->state[2] += C;
  194. ctx->state[3] += D;
  195. ctx->state[4] += E;
  196. }
  197. void vb2_sha1_update(struct vb2_sha1_context *ctx,
  198. const uint8_t *data,
  199. uint32_t size)
  200. {
  201. int i = (int)(ctx->count % sizeof(ctx->buf));
  202. const uint8_t* p = (const uint8_t*) data;
  203. ctx->count += size;
  204. while (size--) {
  205. ctx->buf[i++] = *p++;
  206. if (i == sizeof(ctx->buf)) {
  207. sha1_transform(ctx);
  208. i = 0;
  209. }
  210. }
  211. }
  212. void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest)
  213. {
  214. uint32_t cnt = ctx->count << 3;
  215. int i;
  216. vb2_sha1_update(ctx, (uint8_t*)"\x80", 1);
  217. while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
  218. vb2_sha1_update(ctx, (uint8_t*)"\0", 1);
  219. }
  220. for (i = 0; i < 8; ++i) {
  221. uint8_t tmp = (uint8_t)((uint64_t)cnt >> ((7 - i) * 8));
  222. vb2_sha1_update(ctx, &tmp, 1);
  223. }
  224. for (i = 0; i < 5; i++) {
  225. uint32_t tmp = ctx->state[i];
  226. *digest++ = (uint8_t)(tmp >> 24);
  227. *digest++ = (uint8_t)(tmp >> 16);
  228. *digest++ = (uint8_t)(tmp >> 8);
  229. *digest++ = (uint8_t)(tmp >> 0);
  230. }
  231. }
  232. #endif /* endianness */
  233. void vb2_sha1_init(struct vb2_sha1_context *ctx)
  234. {
  235. ctx->state[0] = 0x67452301;
  236. ctx->state[1] = 0xefcdab89;
  237. ctx->state[2] = 0x98badcfe;
  238. ctx->state[3] = 0x10325476;
  239. ctx->state[4] = 0xc3d2e1f0;
  240. ctx->count = 0;
  241. }