sha2block64.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * sha2_256 block cipher
  3. *
  4. * Implementation straight from Federal Information Processing Standards
  5. * publication 180-2 (+Change Notice to include SHA-224) August 1, 2002
  6. * note: the following upper and lower case macro names are distinct
  7. * and reflect the functions defined in FIPS pub. 180-2.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #define ROTR(x,n) (((x) >> (n)) | ((x) << (32-(n))))
  12. #define sigma0(x) (ROTR((x),7) ^ ROTR((x),18) ^ ((x) >> 3))
  13. #define sigma1(x) (ROTR((x),17) ^ ROTR((x),19) ^ ((x) >> 10))
  14. #define SIGMA0(x) (ROTR((x),2) ^ ROTR((x),13) ^ ROTR((x),22))
  15. #define SIGMA1(x) (ROTR((x),6) ^ ROTR((x),11) ^ ROTR((x),25))
  16. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  17. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  18. /*
  19. * first 32 bits of the fractional parts of cube roots of
  20. * first 64 primes (2..311).
  21. */
  22. static u32int K256[64] = {
  23. 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
  24. 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
  25. 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
  26. 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
  27. 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
  28. 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
  29. 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
  30. 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
  31. 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
  32. 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
  33. 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
  34. 0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
  35. 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
  36. 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
  37. 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
  38. 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2,
  39. };
  40. void
  41. _sha2block64(uchar *p, ulong len, u32int *s)
  42. {
  43. u32int a, b, c, d, e, f, g, h, t1, t2;
  44. u32int *kp, *wp;
  45. u32int w[64];
  46. uchar *end;
  47. /* at this point, we have a multiple of 64 bytes */
  48. for(end = p+len; p < end;){
  49. a = s[0];
  50. b = s[1];
  51. c = s[2];
  52. d = s[3];
  53. e = s[4];
  54. f = s[5];
  55. g = s[6];
  56. h = s[7];
  57. for(wp = w; wp < &w[16]; wp++, p += 4)
  58. wp[0] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
  59. for(; wp < &w[64]; wp++)
  60. wp[0] = sigma1(wp[-2]) + wp[-7] +
  61. sigma0(wp[-15]) + wp[-16];
  62. for(kp = K256, wp = w; wp < &w[64]; ) {
  63. t1 = h + SIGMA1(e) + Ch(e,f,g) + *kp++ + *wp++;
  64. t2 = SIGMA0(a) + Maj(a,b,c);
  65. h = g;
  66. g = f;
  67. f = e;
  68. e = d + t1;
  69. d = c;
  70. c = b;
  71. b = a;
  72. a = t1 + t2;
  73. }
  74. /* save state */
  75. s[0] += a;
  76. s[1] += b;
  77. s[2] += c;
  78. s[3] += d;
  79. s[4] += e;
  80. s[5] += f;
  81. s[6] += g;
  82. s[7] += h;
  83. }
  84. }