sha.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* NIST Secure Hash Algorithm */
  2. /* heavily modified by Uwe Hollerbach uh@alumni.caltech edu */
  3. /* from Peter C. Gutmann's implementation as found in */ /* Applied Cryptography by Bruce Schneier */
  4. /* NIST's proposed modification to SHA of 7/11/94 may be */
  5. /* activated by defining USE_MODIFIED_SHA */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "sha.h"
  10. #include "cmsis_os.h"
  11. extern const char _binary_input_small_asc_start;
  12. extern const char _binary_input_small_asc_end;
  13. extern const char _binary_input_small_asc_size;
  14. // static BYTE sha_input_string[] = "KurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonneguKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworryKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonneguKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthattsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefutureKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepoweraKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonneguKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97WearsunscreenIfIcouldofferyouonlyonetipforthefuturesunscreenwouldbeitThelongtermbenefitsofsunscreenhavebeenprovedbyscientistswhereastherestofmyadvicehasnobasismorereliablethanmyownmeanderingexperienceIwilldispensethisadvicenowEnjoythepowerandbeautyofyouryouthOhnevermindYouwillnotunderstandthepowerandbeautyofyouryouthuntiltheyvefadedButtrustmein20yearsyoulllookbackatphotosofyourselfandrecallinawayyoucantgraspnowhowmuchpossibilitylaybeforeyouandhowfabulousyoureallylookedYouarenotasfatasyouimagineDontworryaboutthefutureOrworrybutknowthatKurtVonnegutsCommencementAddressatMITLadiesandgentlemenoftheclassof97Wearsunscreen";
  15. #define UNROLL_LOOPS 1
  16. /* SHA f()-functions */
  17. #define f1(x,y,z) ((x & y) | (~x & z))
  18. #define f2(x,y,z) (x ^ y ^ z)
  19. #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
  20. #define f4(x,y,z) (x ^ y ^ z)
  21. /* SHA constants */
  22. #define CONST1 0x5a827999L
  23. #define CONST2 0x6ed9eba1L
  24. #define CONST3 0x8f1bbcdcL
  25. #define CONST4 0xca62c1d6L
  26. /* 32-bit rotate */
  27. #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
  28. #define FUNC(n,i) \
  29. temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
  30. E = D; D = C; C = ROT32(B,30); B = A; A = temp
  31. /* do SHA transformation */
  32. static void sha_transform(SHA_INFO *sha_info)
  33. {
  34. int i;
  35. LONG temp, A, B, C, D, E, W[80];
  36. for (i = 0; i < 16; ++i) {
  37. W[i] = sha_info->data[i];
  38. }
  39. for (i = 16; i < 80; ++i) {
  40. W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
  41. #ifdef USE_MODIFIED_SHA
  42. W[i] = ROT32(W[i], 1);
  43. #endif /* USE_MODIFIED_SHA */
  44. }
  45. A = sha_info->digest[0];
  46. B = sha_info->digest[1];
  47. C = sha_info->digest[2];
  48. D = sha_info->digest[3];
  49. E = sha_info->digest[4];
  50. #ifdef UNROLL_LOOPS
  51. FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
  52. FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
  53. FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
  54. FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
  55. FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
  56. FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
  57. FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
  58. FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
  59. FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
  60. FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
  61. FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
  62. FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
  63. FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
  64. FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
  65. FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
  66. FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
  67. #else /* !UNROLL_LOOPS */
  68. for (i = 0; i < 20; ++i) {
  69. FUNC(1,i);
  70. }
  71. for (i = 20; i < 40; ++i) {
  72. FUNC(2,i);
  73. }
  74. for (i = 40; i < 60; ++i) {
  75. FUNC(3,i);
  76. }
  77. for (i = 60; i < 80; ++i) {
  78. FUNC(4,i);
  79. }
  80. #endif /* !UNROLL_LOOPS */
  81. sha_info->digest[0] += A;
  82. sha_info->digest[1] += B;
  83. sha_info->digest[2] += C;
  84. sha_info->digest[3] += D;
  85. sha_info->digest[4] += E;
  86. }
  87. #ifdef LITTLE_ENDIAN
  88. /* change endianness of data */
  89. static void byte_reverse(LONG *buffer, int count)
  90. {
  91. int i;
  92. BYTE ct[4], *cp;
  93. count /= sizeof(LONG);
  94. cp = (BYTE *) buffer;
  95. for (i = 0; i < count; ++i) {
  96. ct[0] = cp[0];
  97. ct[1] = cp[1];
  98. ct[2] = cp[2];
  99. ct[3] = cp[3];
  100. cp[0] = ct[3];
  101. cp[1] = ct[2];
  102. cp[2] = ct[1];
  103. cp[3] = ct[0];
  104. cp += sizeof(LONG);
  105. }
  106. }
  107. #endif /* LITTLE_ENDIAN */
  108. /* initialize the SHA digest */
  109. void sha_init(SHA_INFO *sha_info)
  110. {
  111. sha_info->digest[0] = 0x67452301L;
  112. sha_info->digest[1] = 0xefcdab89L;
  113. sha_info->digest[2] = 0x98badcfeL;
  114. sha_info->digest[3] = 0x10325476L;
  115. sha_info->digest[4] = 0xc3d2e1f0L;
  116. sha_info->count_lo = 0L;
  117. sha_info->count_hi = 0L;
  118. }
  119. /* update the SHA digest */
  120. void sha_update(SHA_INFO *sha_info, BYTE *buffer, int count)
  121. {
  122. if ((sha_info->count_lo + ((LONG) count << 3)) < sha_info->count_lo) {
  123. ++sha_info->count_hi;
  124. }
  125. sha_info->count_lo += (LONG) count << 3;
  126. sha_info->count_hi += (LONG) count >> 29;
  127. while (count >= SHA_BLOCKSIZE) {
  128. memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
  129. #ifdef LITTLE_ENDIAN
  130. byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  131. #endif /* LITTLE_ENDIAN */
  132. sha_transform(sha_info);
  133. buffer += SHA_BLOCKSIZE;
  134. count -= SHA_BLOCKSIZE;
  135. }
  136. memcpy(sha_info->data, buffer, count);
  137. }
  138. /* finish computing the SHA digest */
  139. void sha_final(SHA_INFO *sha_info)
  140. {
  141. int count;
  142. LONG lo_bit_count, hi_bit_count;
  143. lo_bit_count = sha_info->count_lo;
  144. hi_bit_count = sha_info->count_hi;
  145. count = (int) ((lo_bit_count >> 3) & 0x3f);
  146. ((BYTE *) sha_info->data)[count++] = 0x80;
  147. if (count > 56) {
  148. memset((BYTE *) &sha_info->data + count, 0, 64 - count);
  149. #ifdef LITTLE_ENDIAN
  150. byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  151. #endif /* LITTLE_ENDIAN */
  152. sha_transform(sha_info);
  153. memset(&sha_info->data, 0, 56);
  154. } else {
  155. memset((BYTE *) &sha_info->data + count, 0, 56 - count);
  156. }
  157. #ifdef LITTLE_ENDIAN
  158. byte_reverse(sha_info->data, SHA_BLOCKSIZE);
  159. #endif /* LITTLE_ENDIAN */
  160. sha_info->data[14] = hi_bit_count;
  161. sha_info->data[15] = lo_bit_count;
  162. sha_transform(sha_info);
  163. }
  164. /* compute the SHA digest of a FILE stream */
  165. // #define BLOCK_SIZE 8192
  166. #define BLOCK_SIZE 2048
  167. void sha_stream(SHA_INFO *sha_info)
  168. {
  169. int i;
  170. BYTE data[BLOCK_SIZE];
  171. sha_init(sha_info);
  172. // printf("%d\r\n", &_binary_input_small_asc_start);
  173. // printf("%d\r\n", &_binary_input_small_asc_size);
  174. // printf("%d\r\n", &_binary_input_small_asc_end);
  175. int read = 0;
  176. int input_size = (int)&_binary_input_small_asc_size;
  177. BYTE *input = (BYTE *)&_binary_input_small_asc_start;
  178. BYTE *input_end = (BYTE *)&_binary_input_small_asc_end;
  179. while(read < input_size) {
  180. int read_size = read + BLOCK_SIZE < input_size ? BLOCK_SIZE : input_size - read;
  181. memcpy(data, input, read_size);
  182. read += read_size;
  183. sha_update(sha_info, data, sizeof(data));
  184. }
  185. // memcpy(data, sha_input_string, sizeof(sha_input_string));
  186. // sha_update(sha_info, data, sizeof(sha_input_string));
  187. // sha_update(sha_info, sha_input_string, sizeof(sha_input_string));
  188. /*
  189. while ((i = fread(data, 1, BLOCK_SIZE, fin)) > 0) {
  190. sha_update(sha_info, data, i);
  191. }
  192. */
  193. sha_final(sha_info);
  194. }
  195. /* print a SHA digest */
  196. void sha_print(SHA_INFO *sha_info)
  197. {
  198. portDISABLE_INTERRUPTS();
  199. printf("%08lx %08lx %08lx %08lx %08lx\r\n",
  200. sha_info->digest[0], sha_info->digest[1], sha_info->digest[2],
  201. sha_info->digest[3], sha_info->digest[4]);
  202. portENABLE_INTERRUPTS();
  203. }
  204. void vSha() {
  205. SHA_INFO sha_info;
  206. int IMC_REPEAT = 50;
  207. // IMC_REPEAT = 70;
  208. for(int imc_repeat=0; imc_repeat < IMC_REPEAT; imc_repeat++) {
  209. sha_stream(&sha_info);
  210. } // imc_repeat
  211. sha_print(&sha_info);
  212. }