sha2_128.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * sha2 128-bit
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <libsec.h>
  7. static void encode64(uchar*, u64int*, ulong);
  8. static DigestState* sha2_128(uchar *, ulong, uchar *, SHA2_256state *, int);
  9. extern void _sha2block128(uchar*, ulong, u64int*);
  10. /*
  11. * for sha2_384 and sha2_512, len must be multiple of 128 for all but
  12. * the last call. There must be room in the input buffer to pad.
  13. *
  14. * Note: sha2_384 calls sha2_512block as sha2_384; it just uses a different
  15. * initial seed to produce a truncated 384b hash result. otherwise
  16. * it's the same as sha2_512.
  17. */
  18. SHA2_384state*
  19. sha2_384(uchar *p, ulong len, uchar *digest, SHA2_384state *s)
  20. {
  21. if(s == nil) {
  22. s = mallocz(sizeof(*s), 1);
  23. if(s == nil)
  24. return nil;
  25. s->malloced = 1;
  26. }
  27. if(s->seeded == 0){
  28. /*
  29. * seed the state with the first 64 bits of the fractional
  30. * parts of the square roots of the 9th thru 16th primes.
  31. */
  32. s->bstate[0] = 0xcbbb9d5dc1059ed8LL;
  33. s->bstate[1] = 0x629a292a367cd507LL;
  34. s->bstate[2] = 0x9159015a3070dd17LL;
  35. s->bstate[3] = 0x152fecd8f70e5939LL;
  36. s->bstate[4] = 0x67332667ffc00b31LL;
  37. s->bstate[5] = 0x8eb44a8768581511LL;
  38. s->bstate[6] = 0xdb0c2e0d64f98fa7LL;
  39. s->bstate[7] = 0x47b5481dbefa4fa4LL;
  40. s->seeded = 1;
  41. }
  42. return sha2_128(p, len, digest, s, SHA2_384dlen);
  43. }
  44. SHA2_512state*
  45. sha2_512(uchar *p, ulong len, uchar *digest, SHA2_512state *s)
  46. {
  47. if(s == nil) {
  48. s = mallocz(sizeof(*s), 1);
  49. if(s == nil)
  50. return nil;
  51. s->malloced = 1;
  52. }
  53. if(s->seeded == 0){
  54. /*
  55. * seed the state with the first 64 bits of the fractional
  56. * parts of the square roots of the first 8 primes 2..19).
  57. */
  58. s->bstate[0] = 0x6a09e667f3bcc908LL;
  59. s->bstate[1] = 0xbb67ae8584caa73bLL;
  60. s->bstate[2] = 0x3c6ef372fe94f82bLL;
  61. s->bstate[3] = 0xa54ff53a5f1d36f1LL;
  62. s->bstate[4] = 0x510e527fade682d1LL;
  63. s->bstate[5] = 0x9b05688c2b3e6c1fLL;
  64. s->bstate[6] = 0x1f83d9abfb41bd6bLL;
  65. s->bstate[7] = 0x5be0cd19137e2179LL;
  66. s->seeded = 1;
  67. }
  68. return sha2_128(p, len, digest, s, SHA2_512dlen);
  69. }
  70. /* common 128 byte block padding and count code for SHA2_384 and SHA2_512 */
  71. static DigestState*
  72. sha2_128(uchar *p, ulong len, uchar *digest, SHA2_512state *s, int dlen)
  73. {
  74. int i;
  75. u64int x[16];
  76. uchar buf[256];
  77. uchar *e;
  78. /* fill out the partial 128 byte block from previous calls */
  79. if(s->blen){
  80. i = 128 - s->blen;
  81. if(len < i)
  82. i = len;
  83. memmove(s->buf + s->blen, p, i);
  84. len -= i;
  85. s->blen += i;
  86. p += i;
  87. if(s->blen == 128){
  88. _sha2block128(s->buf, s->blen, s->bstate);
  89. s->len += s->blen;
  90. s->blen = 0;
  91. }
  92. }
  93. /* do 128 byte blocks */
  94. i = len & ~(128-1);
  95. if(i){
  96. _sha2block128(p, i, s->bstate);
  97. s->len += i;
  98. len -= i;
  99. p += i;
  100. }
  101. /* save the left overs if not last call */
  102. if(digest == 0){
  103. if(len){
  104. memmove(s->buf, p, len);
  105. s->blen += len;
  106. }
  107. return s;
  108. }
  109. /*
  110. * this is the last time through, pad what's left with 0x80,
  111. * 0's, and the input count to create a multiple of 128 bytes.
  112. */
  113. if(s->blen){
  114. p = s->buf;
  115. len = s->blen;
  116. } else {
  117. memmove(buf, p, len);
  118. p = buf;
  119. }
  120. s->len += len;
  121. e = p + len;
  122. if(len < 112)
  123. i = 112 - len;
  124. else
  125. i = 240 - len;
  126. memset(e, 0, i);
  127. *e = 0x80;
  128. len += i;
  129. /* append the count */
  130. x[0] = 0; /* assume 32b length, i.e. < 4GB */
  131. x[1] = s->len<<3;
  132. encode64(p+len, x, 16);
  133. /* digest the last part */
  134. _sha2block128(p, len+16, s->bstate);
  135. s->len += len+16;
  136. /* return result and free state */
  137. encode64(digest, s->bstate, dlen);
  138. if(s->malloced == 1)
  139. free(s);
  140. return nil;
  141. }
  142. /*
  143. * Encodes input (ulong long) into output (uchar).
  144. * Assumes len is a multiple of 8.
  145. */
  146. static void
  147. encode64(uchar *output, u64int *input, ulong len)
  148. {
  149. u64int x;
  150. uchar *e;
  151. for(e = output + len; output < e;) {
  152. x = *input++;
  153. *output++ = x >> 56;
  154. *output++ = x >> 48;
  155. *output++ = x >> 40;
  156. *output++ = x >> 32;
  157. *output++ = x >> 24;
  158. *output++ = x >> 16;
  159. *output++ = x >> 8;
  160. *output++ = x;
  161. }
  162. }
  163. DigestState*
  164. hmac_sha2_384(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
  165. DigestState *s)
  166. {
  167. return hmac_x(p, len, key, klen, digest, s, sha2_384, SHA2_384dlen);
  168. }
  169. DigestState*
  170. hmac_sha2_512(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
  171. DigestState *s)
  172. {
  173. return hmac_x(p, len, key, klen, digest, s, sha2_512, SHA2_512dlen);
  174. }