sha.c 15 KB

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