sha256.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;*
  2. ;* sha256.inc
  3. ;* https://gitlab.com/bztsrc/sha256asm
  4. ;*
  5. ;* Copyright (C) 2018 bzt (bztsrc@gitlab)
  6. ;*
  7. ;* Permission is hereby granted, free of charge, to any person
  8. ;* obtaining a copy of this software and associated documentation
  9. ;* files (the "Software"), to deal in the Software without
  10. ;* restriction, including without limitation the rights to use, copy,
  11. ;* modify, merge, publish, distribute, sublicense, and/or sell copies
  12. ;* of the Software, and to permit persons to whom the Software is
  13. ;* furnished to do so, subject to the following conditions:
  14. ;*
  15. ;* The above copyright notice and this permission notice shall be
  16. ;* included in all copies or substantial portions of the Software.
  17. ;*
  18. ;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. ;* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. ;* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. ;* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. ;* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. ;* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. ;* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. ;* DEALINGS IN THE SOFTWARE.
  26. ;*
  27. ;* @brief Very small SHA-256 implementation for protected mode
  28. ;*** interface macros ***
  29. ;**
  30. ;* Initialize the SHA256_CTX
  31. ;* void sha256_init(void)
  32. ;*
  33. macro sha256_init
  34. {
  35. call sha_init
  36. }
  37. ;**
  38. ;* Add a buffer to SHA256_CTX
  39. ;* void sha256_update(unsigned char *buffer, size_t len)
  40. ;*
  41. macro sha256_update buf, len
  42. {
  43. if ~ buf eq esi
  44. mov esi, buf
  45. end if
  46. if ~ len eq ecx
  47. mov ecx, len
  48. end if
  49. call sha_upd
  50. }
  51. ;**
  52. ;* Return the SHA-256 checksum
  53. ;* void sha256_final(unsigned char *chk[32])
  54. ;*
  55. macro sha256_final buf
  56. {
  57. if ~ buf eq edi
  58. mov edi, buf
  59. end if
  60. call sha_final
  61. }
  62. ;*** implementation, text segment ***
  63. ; No input. Clobbers EAX
  64. sha_init: xor eax, eax
  65. mov dword [sha_l], eax
  66. mov dword [sha_b], eax
  67. mov dword [sha_b+4], eax
  68. mov dword [sha_s ], 06a09e667h
  69. mov dword [sha_s+ 4], 0bb67ae85h
  70. mov dword [sha_s+ 8], 03c6ef372h
  71. mov dword [sha_s+12], 0a54ff53ah
  72. mov dword [sha_s+16], 0510e527fh
  73. mov dword [sha_s+20], 09b05688ch
  74. mov dword [sha_s+24], 01f83d9abh
  75. mov dword [sha_s+28], 05be0cd19h
  76. ret
  77. ; IN: ESI = buffer, ECX = length. Clobbers EAX, EDI.
  78. sha_upd: mov edi, dword [sha_l]
  79. add edi, sha_d
  80. ; if(len>0)
  81. or ecx, ecx
  82. jz .end
  83. ; for(;len--;d++) {
  84. ; ctx->d[ctx->l++]=*d;
  85. .next: movsb
  86. inc byte [sha_l]
  87. ; if(ctx->l==64) {
  88. cmp byte [sha_l], 64
  89. jne @f
  90. ; sha256_t(ctx);
  91. call sha_final.sha_t
  92. ; SHA_ADD(ctx->b[0],ctx->b[1],512);
  93. add dword [sha_b], 512
  94. adc dword [sha_b+4], 0
  95. ; ctx->l=0;
  96. mov byte [sha_l], 0
  97. ; edi=sha_d
  98. sub edi, 64
  99. ; }
  100. @@: dec ecx
  101. jnz .next
  102. .end: ret
  103. ; IN: EDI = output buffer. Clobbers EAX, EBX, ECX.
  104. sha_final: push esi
  105. push edi
  106. mov ebx, edi
  107. ; i=ctx->l; ctx->d[i++]=0x80;
  108. mov edi, dword [sha_l]
  109. mov ecx, edi
  110. add edi, sha_d
  111. mov al, 80h
  112. stosb
  113. inc ecx
  114. xor eax, eax
  115. ; if(ctx->l<56) {while(i<56) ctx->d[i++]=0x00;}
  116. cmp cl, 57
  117. jae @f
  118. neg ecx
  119. add ecx, 56
  120. repnz stosb
  121. jmp .padded
  122. @@: ; else {while(i<64) ctx->d[i++]=0x00;sha256_t(ctx);memset(ctx->d,0,56);}
  123. neg ecx
  124. add ecx, 64
  125. repnz stosb
  126. call .sha_t
  127. mov ecx, 56/4
  128. mov edi, sha_d
  129. repnz stosd
  130. .padded: ; SHA_ADD(ctx->b[0],ctx->b[1],ctx->l*8);
  131. mov eax, dword [sha_l]
  132. shl eax, 3
  133. add dword [sha_b], eax
  134. adc dword [sha_b+4], 0
  135. ; ctx->d[63]=ctx->b[0];ctx->d[62]=ctx->b[0]>>8;ctx->d[61]=ctx->b[0]>>16;ctx->d[60]=ctx->b[0]>>24;
  136. mov eax, dword [sha_b]
  137. bswap eax
  138. mov dword [sha_d+60], eax
  139. ; ctx->d[59]=ctx->b[1];ctx->d[58]=ctx->b[1]>>8;ctx->d[57]=ctx->b[1]>>16;ctx->d[56]=ctx->b[1]>>24;
  140. mov eax, dword [sha_b+4]
  141. bswap eax
  142. mov dword [sha_d+56], eax
  143. ; sha256_t(ctx);
  144. call .sha_t
  145. ; for(i=0;i<4;i++) {
  146. ; h[i] =(ctx->s[0]>>(24-i*8)); h[i+4] =(ctx->s[1]>>(24-i*8));
  147. ; h[i+8] =(ctx->s[2]>>(24-i*8)); h[i+12]=(ctx->s[3]>>(24-i*8));
  148. ; h[i+16]=(ctx->s[4]>>(24-i*8)); h[i+20]=(ctx->s[5]>>(24-i*8));
  149. ; h[i+24]=(ctx->s[6]>>(24-i*8)); h[i+28]=(ctx->s[7]>>(24-i*8));
  150. ; }
  151. mov edi, ebx
  152. mov esi, sha_s
  153. mov cl, 8
  154. @@: lodsd
  155. bswap eax
  156. stosd
  157. dec cl
  158. jnz @b
  159. pop edi
  160. pop esi
  161. ret
  162. ; private func, sha transform
  163. .sha_t: push esi
  164. push edi
  165. push edx
  166. push ecx
  167. push ebx
  168. ; for(i=0,j=0;i<16;i++,j+=4) m[i]=(ctx->d[j]<<24)|(ctx->d[j+1]<<16)|(ctx->d[j+2]<<8)|(ctx->d[j+3]);
  169. mov cl, 16
  170. mov edi, _m
  171. mov esi, sha_d
  172. @@: lodsd
  173. bswap eax
  174. stosd
  175. dec cl
  176. jnz @b
  177. ; for(;i<64;i++) m[i]=SHA_SIG1(m[i-2])+m[i-7]+SHA_SIG0(m[i-15])+m[i-16];
  178. mov cl, 48
  179. ; SHA_SIG0[m[i-15]) (SHA_ROTR(x,7)^SHA_ROTR(x,18)^((x)>>3))
  180. @@: mov eax, dword [edi-15*4]
  181. mov ebx, eax
  182. mov edx, eax
  183. ror eax, 7
  184. ror ebx, 18
  185. shr edx, 3
  186. xor eax, ebx
  187. xor eax, edx
  188. ; SHA_SIG1(m[i-2]) (SHA_ROTR(x,17)^SHA_ROTR(x,19)^((x)>>10))
  189. mov ebx, dword [edi-2*4]
  190. mov edx, ebx
  191. ror ebx, 17
  192. ror edx, 19
  193. xor ebx, edx
  194. rol edx, 19
  195. shr edx, 10
  196. xor ebx, edx
  197. add eax, ebx
  198. ; m[i-7]
  199. add eax, dword [edi-7*4]
  200. ; m[i-16]
  201. add eax, dword [edi-16*4]
  202. stosd
  203. dec cl
  204. jnz @b
  205. ; a=ctx->s[0];b=ctx->s[1];c=ctx->s[2];d=ctx->s[3];
  206. ; e=ctx->s[4];f=ctx->s[5];g=ctx->s[6];h=ctx->s[7];
  207. xor ecx, ecx
  208. mov cl, 8
  209. mov esi, sha_s
  210. mov edi, _a
  211. repnz movsd
  212. ; for(i=0;i<64;i++) {
  213. mov esi, _m
  214. @@: ; t1=h+SHA_EP1(e)+SHA_CH(e,f,g)+sha256_k[i]+m[i];
  215. mov eax, dword [_h]
  216. mov dword [t1], eax
  217. ; SHA_EP1(e) (SHA_ROTR(x,6)^SHA_ROTR(x,11)^SHA_ROTR(x,25))
  218. mov eax, dword [_e]
  219. mov ebx, eax
  220. ror eax, 6
  221. ror ebx, 11
  222. xor eax, ebx
  223. ror ebx, 14 ; 25 = 11+14
  224. xor eax, ebx
  225. add dword [t1], eax
  226. ; SHA_CH(e,f,g) (((x)&(y))^(~(x)&(z)))
  227. mov eax, dword [_e]
  228. mov ebx, eax
  229. not ebx
  230. and eax, dword [_f]
  231. and ebx, dword [_g]
  232. xor eax, ebx
  233. add dword [t1], eax
  234. ; sha256_k[i]
  235. mov eax, dword [sha256_k+4*ecx]
  236. add dword [t1], eax
  237. ; m[i]
  238. lodsd
  239. add dword [t1], eax
  240. ; t2=SHA_EP0(a)+SHA_MAJ(a,b,c);
  241. ; SHA_EP0(a) (SHA_ROTR(x,2)^SHA_ROTR(x,13)^SHA_ROTR(x,22))
  242. mov eax, dword [_a]
  243. mov ebx, eax
  244. ror eax, 2
  245. ror ebx, 13
  246. xor eax, ebx
  247. ror ebx, 9 ; 22 = 13+9
  248. xor eax, ebx
  249. mov dword [t2], eax
  250. ; SHA_MAJ(a,b,c) (((x)&(y))^((x)&(z))^((y)&(z)))
  251. mov eax, dword [_a]
  252. mov edx, dword [_c]
  253. mov ebx, eax
  254. and eax, dword [_b]
  255. and ebx, edx
  256. xor eax, ebx
  257. mov ebx, dword [_b]
  258. and ebx, edx
  259. xor eax, ebx
  260. add dword [t2], eax
  261. ; h=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;
  262. mov eax, dword [_g]
  263. mov dword [_h], eax
  264. mov eax, dword [_f]
  265. mov dword [_g], eax
  266. mov eax, dword [_e]
  267. mov dword [_f], eax
  268. mov eax, dword [_d]
  269. add eax, dword [t1]
  270. mov dword [_e], eax
  271. mov eax, dword [_c]
  272. mov dword [_d], eax
  273. mov eax, dword [_b]
  274. mov dword [_c], eax
  275. mov eax, dword [_a]
  276. mov dword [_b], eax
  277. mov eax, dword [t1]
  278. add eax, dword [t2]
  279. mov dword [_a], eax
  280. ; }
  281. inc cl
  282. cmp cl, 64
  283. jne @b
  284. ; ctx->s[0]+=a;ctx->s[1]+=b;ctx->s[2]+=c;ctx->s[3]+=d;
  285. ; ctx->s[4]+=e;ctx->s[5]+=f;ctx->s[6]+=g;ctx->s[7]+=h;
  286. mov cl, 8
  287. mov esi, _a
  288. mov edi, sha_s
  289. @@: lodsd
  290. add dword [edi], eax
  291. add edi, 4
  292. dec cl
  293. jnz @b
  294. pop ebx
  295. pop ecx
  296. pop edx
  297. pop edi
  298. pop esi
  299. xor eax, eax
  300. ret
  301. sha256_k: dd 0428a2f98h, 071374491h, 0b5c0fbcfh, 0e9b5dba5h, 03956c25bh, 059f111f1h, 0923f82a4h, 0ab1c5ed5h
  302. dd 0d807aa98h, 012835b01h, 0243185beh, 0550c7dc3h, 072be5d74h, 080deb1feh, 09bdc06a7h, 0c19bf174h
  303. dd 0e49b69c1h, 0efbe4786h, 00fc19dc6h, 0240ca1cch, 02de92c6fh, 04a7484aah, 05cb0a9dch, 076f988dah
  304. dd 0983e5152h, 0a831c66dh, 0b00327c8h, 0bf597fc7h, 0c6e00bf3h, 0d5a79147h, 006ca6351h, 014292967h
  305. dd 027b70a85h, 02e1b2138h, 04d2c6dfch, 053380d13h, 0650a7354h, 0766a0abbh, 081c2c92eh, 092722c85h
  306. dd 0a2bfe8a1h, 0a81a664bh, 0c24b8b70h, 0c76c51a3h, 0d192e819h, 0d6990624h, 0f40e3585h, 0106aa070h
  307. dd 019a4c116h, 01e376c08h, 02748774ch, 034b0bcb5h, 0391c0cb3h, 04ed8aa4ah, 05b9cca4fh, 0682e6ff3h
  308. dd 0748f82eeh, 078a5636fh, 084c87814h, 08cc70208h, 090befffah, 0a4506cebh, 0bef9a3f7h, 0c67178f2h
  309. ; SHA256_CTX and temporary variables in bss segment
  310. sha_d: db 64 dup ?
  311. sha_l: dd ?
  312. sha_b: dd 2 dup ?
  313. sha_s: dd 8 dup ?
  314. _a: dd ?
  315. _b: dd ?
  316. _c: dd ?
  317. _d: dd ?
  318. _e: dd ?
  319. _f: dd ?
  320. _g: dd ?
  321. _h: dd ?
  322. t1: dd ?
  323. t2: dd ?
  324. _m: dd 64 dup ?