sha.c 15 KB

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