sha1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C)2005-2016 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "osdef.h"
  23. #include "sha1.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. // original code by Steve Reid
  27. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  28. #ifdef TARGET_BIG_ENDIAN
  29. # define blk0(i) block[i]
  30. #else
  31. # define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \
  32. |(rol(block[i],8)&0x00FF00FF))
  33. #endif
  34. #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
  35. ^block[(i+2)&15]^block[i&15],1))
  36. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  37. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  39. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  40. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  41. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  42. static void sha1_transform( unsigned int state[5], unsigned char buffer[64] ) {
  43. unsigned int a, b, c, d, e;
  44. unsigned int block[16];
  45. memcpy(block, buffer, 64);
  46. /* Copy context->state[] to working vars */
  47. a = state[0];
  48. b = state[1];
  49. c = state[2];
  50. d = state[3];
  51. e = state[4];
  52. /* 4 rounds of 20 operations each. Loop unrolled. */
  53. R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  54. R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  55. R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  56. R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  57. R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  58. R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  59. R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  60. R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  61. R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  62. R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  63. R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  64. R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  65. R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  66. R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  67. R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  68. R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  69. R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  70. R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  71. R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  72. R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  73. /* Add the working vars back into context.state[] */
  74. state[0] += a;
  75. state[1] += b;
  76. state[2] += c;
  77. state[3] += d;
  78. state[4] += e;
  79. }
  80. void sha1_init( SHA1_CTX *context ) {
  81. /* SHA1 initialization constants */
  82. context->state[0] = 0x67452301;
  83. context->state[1] = 0xEFCDAB89;
  84. context->state[2] = 0x98BADCFE;
  85. context->state[3] = 0x10325476;
  86. context->state[4] = 0xC3D2E1F0;
  87. context->count[0] = context->count[1] = 0;
  88. }
  89. void sha1_update( SHA1_CTX *context, const unsigned char *data, unsigned int len ) {
  90. unsigned int i, j;
  91. j = (context->count[0] >> 3) & 63;
  92. if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
  93. context->count[1] += (len >> 29);
  94. if ((j + len) > 63) {
  95. memcpy(&context->buffer[j], data, (i = 64-j));
  96. sha1_transform(context->state, context->buffer);
  97. for ( ; i + 63 < len; i += 64 )
  98. sha1_transform(context->state, (unsigned char *)&data[i]);
  99. j = 0;
  100. } else
  101. i = 0;
  102. memcpy(&context->buffer[j], &data[i], len - i);
  103. }
  104. void sha1_final( SHA1_CTX *context, unsigned char digest[SHA1_SIZE] ) {
  105. unsigned int i;
  106. unsigned char finalcount[8];
  107. for (i = 0; i < 8; i++) {
  108. finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  109. >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  110. }
  111. sha1_update(context, (unsigned char *)"\200", 1);
  112. while ((context->count[0] & 504) != 448) {
  113. sha1_update(context, (unsigned char *)"\0", 1);
  114. }
  115. sha1_update(context, finalcount, 8);
  116. for (i = 0; i < SHA1_SIZE; i++) {
  117. digest[i] = (unsigned char)
  118. ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
  119. }
  120. sha1_transform(context->state, context->buffer);
  121. }