sha1block.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "os.h"
  10. void
  11. _sha1block(uint8_t *p, uint32_t len, uint32_t *s)
  12. {
  13. uint32_t a, b, c, d, e, x;
  14. uint8_t *end;
  15. uint32_t *wp, *wend;
  16. uint32_t w[80];
  17. /* at this point, we have a multiple of 64 bytes */
  18. for(end = p+len; p < end;){
  19. a = s[0];
  20. b = s[1];
  21. c = s[2];
  22. d = s[3];
  23. e = s[4];
  24. wend = w + 15;
  25. for(wp = w; wp < wend; wp += 5){
  26. wp[0] = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  27. e += ((a<<5) | (a>>27)) + wp[0];
  28. e += 0x5a827999 + (((c^d)&b)^d);
  29. b = (b<<30)|(b>>2);
  30. wp[1] = (p[4]<<24) | (p[5]<<16) | (p[6]<<8) | p[7];
  31. d += ((e<<5) | (e>>27)) + wp[1];
  32. d += 0x5a827999 + (((b^c)&a)^c);
  33. a = (a<<30)|(a>>2);
  34. wp[2] = (p[8]<<24) | (p[9]<<16) | (p[10]<<8) | p[11];
  35. c += ((d<<5) | (d>>27)) + wp[2];
  36. c += 0x5a827999 + (((a^b)&e)^b);
  37. e = (e<<30)|(e>>2);
  38. wp[3] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
  39. b += ((c<<5) | (c>>27)) + wp[3];
  40. b += 0x5a827999 + (((e^a)&d)^a);
  41. d = (d<<30)|(d>>2);
  42. wp[4] = (p[16]<<24) | (p[17]<<16) | (p[18]<<8) | p[19];
  43. a += ((b<<5) | (b>>27)) + wp[4];
  44. a += 0x5a827999 + (((d^e)&c)^e);
  45. c = (c<<30)|(c>>2);
  46. p += 20;
  47. }
  48. wp[0] = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  49. e += ((a<<5) | (a>>27)) + wp[0];
  50. e += 0x5a827999 + (((c^d)&b)^d);
  51. b = (b<<30)|(b>>2);
  52. x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
  53. wp[1] = (x<<1) | (x>>31);
  54. d += ((e<<5) | (e>>27)) + wp[1];
  55. d += 0x5a827999 + (((b^c)&a)^c);
  56. a = (a<<30)|(a>>2);
  57. x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
  58. wp[2] = (x<<1) | (x>>31);
  59. c += ((d<<5) | (d>>27)) + wp[2];
  60. c += 0x5a827999 + (((a^b)&e)^b);
  61. e = (e<<30)|(e>>2);
  62. x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
  63. wp[3] = (x<<1) | (x>>31);
  64. b += ((c<<5) | (c>>27)) + wp[3];
  65. b += 0x5a827999 + (((e^a)&d)^a);
  66. d = (d<<30)|(d>>2);
  67. x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
  68. wp[4] = (x<<1) | (x>>31);
  69. a += ((b<<5) | (b>>27)) + wp[4];
  70. a += 0x5a827999 + (((d^e)&c)^e);
  71. c = (c<<30)|(c>>2);
  72. wp += 5;
  73. p += 4;
  74. wend = w + 40;
  75. for(; wp < wend; wp += 5){
  76. x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
  77. wp[0] = (x<<1) | (x>>31);
  78. e += ((a<<5) | (a>>27)) + wp[0];
  79. e += 0x6ed9eba1 + (b^c^d);
  80. b = (b<<30)|(b>>2);
  81. x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
  82. wp[1] = (x<<1) | (x>>31);
  83. d += ((e<<5) | (e>>27)) + wp[1];
  84. d += 0x6ed9eba1 + (a^b^c);
  85. a = (a<<30)|(a>>2);
  86. x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
  87. wp[2] = (x<<1) | (x>>31);
  88. c += ((d<<5) | (d>>27)) + wp[2];
  89. c += 0x6ed9eba1 + (e^a^b);
  90. e = (e<<30)|(e>>2);
  91. x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
  92. wp[3] = (x<<1) | (x>>31);
  93. b += ((c<<5) | (c>>27)) + wp[3];
  94. b += 0x6ed9eba1 + (d^e^a);
  95. d = (d<<30)|(d>>2);
  96. x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
  97. wp[4] = (x<<1) | (x>>31);
  98. a += ((b<<5) | (b>>27)) + wp[4];
  99. a += 0x6ed9eba1 + (c^d^e);
  100. c = (c<<30)|(c>>2);
  101. }
  102. wend = w + 60;
  103. for(; wp < wend; wp += 5){
  104. x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
  105. wp[0] = (x<<1) | (x>>31);
  106. e += ((a<<5) | (a>>27)) + wp[0];
  107. e += 0x8f1bbcdc + ((b&c)|((b|c)&d));
  108. b = (b<<30)|(b>>2);
  109. x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
  110. wp[1] = (x<<1) | (x>>31);
  111. d += ((e<<5) | (e>>27)) + wp[1];
  112. d += 0x8f1bbcdc + ((a&b)|((a|b)&c));
  113. a = (a<<30)|(a>>2);
  114. x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
  115. wp[2] = (x<<1) | (x>>31);
  116. c += ((d<<5) | (d>>27)) + wp[2];
  117. c += 0x8f1bbcdc + ((e&a)|((e|a)&b));
  118. e = (e<<30)|(e>>2);
  119. x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
  120. wp[3] = (x<<1) | (x>>31);
  121. b += ((c<<5) | (c>>27)) + wp[3];
  122. b += 0x8f1bbcdc + ((d&e)|((d|e)&a));
  123. d = (d<<30)|(d>>2);
  124. x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
  125. wp[4] = (x<<1) | (x>>31);
  126. a += ((b<<5) | (b>>27)) + wp[4];
  127. a += 0x8f1bbcdc + ((c&d)|((c|d)&e));
  128. c = (c<<30)|(c>>2);
  129. }
  130. wend = w + 80;
  131. for(; wp < wend; wp += 5){
  132. x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
  133. wp[0] = (x<<1) | (x>>31);
  134. e += ((a<<5) | (a>>27)) + wp[0];
  135. e += 0xca62c1d6 + (b^c^d);
  136. b = (b<<30)|(b>>2);
  137. x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
  138. wp[1] = (x<<1) | (x>>31);
  139. d += ((e<<5) | (e>>27)) + wp[1];
  140. d += 0xca62c1d6 + (a^b^c);
  141. a = (a<<30)|(a>>2);
  142. x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
  143. wp[2] = (x<<1) | (x>>31);
  144. c += ((d<<5) | (d>>27)) + wp[2];
  145. c += 0xca62c1d6 + (e^a^b);
  146. e = (e<<30)|(e>>2);
  147. x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
  148. wp[3] = (x<<1) | (x>>31);
  149. b += ((c<<5) | (c>>27)) + wp[3];
  150. b += 0xca62c1d6 + (d^e^a);
  151. d = (d<<30)|(d>>2);
  152. x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
  153. wp[4] = (x<<1) | (x>>31);
  154. a += ((b<<5) | (b>>27)) + wp[4];
  155. a += 0xca62c1d6 + (c^d^e);
  156. c = (c<<30)|(c>>2);
  157. }
  158. /* save state */
  159. s[0] += a;
  160. s[1] += b;
  161. s[2] += c;
  162. s[3] += d;
  163. s[4] += e;
  164. }
  165. }