sha2_small_common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* sha2_small_common.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/sha2_small_common.h>
  19. #define LITTLE_ENDIAN
  20. /**
  21. * rotate x right by n positions
  22. */
  23. static
  24. uint32_t rotr32( uint32_t x, uint8_t n){
  25. return ((x>>n) | (x<<(32-n)));
  26. }
  27. static
  28. uint32_t rotl32( uint32_t x, uint8_t n){
  29. return ((x<<n) | (x>>(32-n)));
  30. }
  31. /*************************************************************************/
  32. // #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8))
  33. /*
  34. static
  35. uint32_t change_endian32(uint32_t x){
  36. return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8));
  37. }
  38. */
  39. /* sha256 functions as macros for speed and size, cause they are called only once */
  40. #define CH(x,y,z) (((x)&(y)) ^ ((~(x))&(z)))
  41. #define MAJ(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z)))
  42. #define SIGMA_0(x) (rotr32((x), 2) ^ rotr32((x),13) ^ rotl32((x),10))
  43. #define SIGMA_1(x) (rotr32((x), 6) ^ rotr32((x),11) ^ rotl32((x),7))
  44. #define SIGMA_a(x) (rotr32((x), 7) ^ rotl32((x),14) ^ ((x)>>3))
  45. #define SIGMA_b(x) (rotl32((x),15) ^ rotl32((x),13) ^ ((x)>>10))
  46. const
  47. uint32_t k[]={
  48. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  49. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  50. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  51. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  52. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  53. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  54. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  55. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  56. };
  57. static
  58. void load_endian32_changed(uint8_t* dest, uint8_t* src, uint16_t words){
  59. while(words--){
  60. *dest++ = src[3];
  61. *dest++ = src[2];
  62. *dest++ = src[1];
  63. *dest++ = src[0];
  64. src += 4;
  65. }
  66. }
  67. /**
  68. * block must be, 512, Bit = 64, Byte, long !!!
  69. */
  70. void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* block){
  71. uint32_t w[16], wx;
  72. uint8_t i;
  73. uint32_t a[8],t1,t2;
  74. /* init w */
  75. #if defined LITTLE_ENDIAN
  76. load_endian32_changed((uint8_t*)w, (uint8_t*)block, 16);
  77. #elif defined BIG_ENDIAN
  78. memcpy((void*)w, block, 64);
  79. #endif
  80. /*
  81. for (i=16; i<64; ++i){
  82. w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16];
  83. }
  84. */
  85. /* init working variables */
  86. memcpy((void*)a,(void*)(state->h), 8*4);
  87. /* do the, fun stuff, */
  88. for (i=0; i<64; ++i){
  89. if(i<16){
  90. wx = w[i];
  91. }else{
  92. wx = SIGMA_b(w[14]) + w[9] + SIGMA_a(w[1]) + w[0];
  93. memmove(&(w[0]), &(w[1]), 15*4);
  94. w[15] = wx;
  95. }
  96. t1 = a[7] + SIGMA_1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + wx;
  97. t2 = SIGMA_0(a[0]) + MAJ(a[0],a[1],a[2]);
  98. memmove(&(a[1]), &(a[0]), 7*4); /* a[7]=a[6]; a[6]=a[5]; a[5]=a[4]; a[4]=a[3]; a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; */
  99. a[4] += t1;
  100. a[0] = t1 + t2;
  101. }
  102. /* update, the, state, */
  103. for (i=0; i<8; ++i){
  104. state->h[i] += a[i];
  105. }
  106. state->length += 1;
  107. }
  108. void sha2_small_common_lastBlock(sha2_small_common_ctx_t *state, const void* block, uint16_t length_b){
  109. uint8_t lb[512/8]; /* local block */
  110. uint64_t len;
  111. while(length_b>=512){
  112. sha2_small_common_nextBlock(state, block);
  113. length_b -= 512;
  114. block = (uint8_t*)block+64;
  115. }
  116. len = state->length*512 + length_b;
  117. memset(lb, 0, 64);
  118. memcpy(lb, block, (length_b+7)/8);
  119. /* set the final one bit */
  120. lb[length_b/8] |= 0x80>>(length_b & 0x7);
  121. /* pad with zeros */
  122. if (length_b>=512-64){ /* not enouth space for 64bit length value */
  123. sha2_small_common_nextBlock(state, lb);
  124. memset(lb, 0, 64);
  125. }
  126. /* store the 64bit length value */
  127. #if defined LITTLE_ENDIAN
  128. /* this is now rolled up */
  129. uint8_t i;
  130. i=7;
  131. do{
  132. lb[63-i] = ((uint8_t*)&len)[i];
  133. }while(i--);
  134. #elif defined BIG_ENDIAN
  135. *((uint64_t)&(lb[56])) = len;
  136. #endif
  137. sha2_small_common_nextBlock(state, lb);
  138. }