Advertisement
aaaaaa123456789

SHA-1 implementation in gbz80

Sep 6th, 2018
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. offset: MACRO
  2. ; I want to keep myself sane
  3. if _NARG > 1
  4. ld a, \2
  5. endc
  6. add a, LOW(\1)
  7. ld LOW(\1), a
  8. jr nc, .no_carry_\@
  9. inc HIGH(\1)
  10. .no_carry_\@
  11. ENDM
  12.  
  13. CalculateSHA1:
  14. ; in: hl: pointer to data, a: length (bytes)
  15. ; out: hl = hl + 64 pointing to hash, all other regs clobbered, initial data buffer clobbered
  16. ; returns carry iif a > 55 (since this would require implementing proper blocking)
  17. ; prereq's: a <= 55, hl points to an 84-byte buffer initialized with the data
  18. cp 55
  19. ccf
  20. ret c
  21. push hl
  22.  
  23. ; pad the block
  24. ld c, a
  25. offset hl
  26. ld a, $80
  27. ld [hli], a
  28. ld a, c
  29. sub 62
  30. ld b, a
  31. xor a
  32. jr .padding_loop_check
  33. .padding_loop
  34. ld [hli], a
  35. .padding_loop_check
  36. inc b
  37. jr nz, .padding_loop
  38. ld a, c
  39. add a, a
  40. add a, a
  41. add a, a
  42. ld b, a
  43. sbc a
  44. and 1
  45. ld [hli], a
  46. ld a, b
  47. ld [hli], a
  48.  
  49. ; initialize the registers
  50. ld b, 20
  51. ld de, .initialization_data
  52. .initialization_loop
  53. ld a, [de]
  54. inc de
  55. ld [hli], a
  56. dec b
  57. jr nz, .initialization_loop
  58. pop hl
  59.  
  60. ; 80 iterations of operations
  61. xor a
  62. .iteration_loop
  63. call .combine_states_and_constant
  64. push hl
  65. push af
  66. and $f
  67. add a, a
  68. add a, a
  69. offset hl
  70. call .add_word
  71. pop af
  72. pop hl
  73. push af
  74. call .update_states
  75. pop af
  76. cp $40
  77. call c, .update_buffer_word
  78. inc a
  79. cp $50
  80. jr c, .iteration_loop
  81.  
  82. ; add the results to the initialization data
  83. offset hl, $40
  84. push hl
  85. xor a
  86. .final_addition_loop
  87. push af
  88. call .load_word
  89. dec hl
  90. dec hl
  91. dec hl
  92. dec hl
  93. pop af
  94. push af
  95. push hl
  96. ld hl, .initialization_data
  97. add a, a
  98. add a, a
  99. offset hl
  100. call .add_word
  101. pop hl
  102. call .store_word
  103. pop af
  104. inc a
  105. cp 5
  106. jr c, .final_addition_loop
  107.  
  108. ; and we're done!
  109. pop hl
  110. and a
  111. ret
  112.  
  113. .load_word
  114. ld a, [hli]
  115. ld b, a
  116. ld a, [hli]
  117. ld c, a
  118. ld a, [hli]
  119. ld d, a
  120. ld a, [hli]
  121. ld e, a
  122. ret
  123.  
  124. .store_word
  125. ld a, b
  126. ld [hli], a
  127. ld a, c
  128. ld [hli], a
  129. ld a, d
  130. ld [hli], a
  131. ld a, e
  132. ld [hli], a
  133. ret
  134.  
  135. .and_word
  136. ld a, [hli]
  137. and b
  138. ld b, a
  139. ld a, [hli]
  140. and c
  141. ld c, a
  142. ld a, [hli]
  143. and d
  144. ld d, a
  145. ld a, [hli]
  146. and e
  147. ld e, a
  148. ret
  149.  
  150. .or_word
  151. ld a, [hli]
  152. or b
  153. ld b, a
  154. ld a, [hli]
  155. or c
  156. ld c, a
  157. ld a, [hli]
  158. or d
  159. ld d, a
  160. ld a, [hli]
  161. or e
  162. ld e, a
  163. ret
  164.  
  165. .rotate_word_left
  166. sla e
  167. rl d
  168. rl c
  169. rl b
  170. ret nc
  171. inc e
  172. ret
  173.  
  174. .rotate_word_right
  175. srl b
  176. rr c
  177. rr d
  178. rr e
  179. ret nc
  180. set 7, b
  181. ret
  182.  
  183. .combine_states_and_constant
  184. ; must preserve a, hl
  185. push af
  186. push hl
  187. ld c, a
  188. offset hl, $44
  189. ld a, c
  190. call .initial_states_combination
  191. pop hl
  192. push hl
  193. offset hl, $50
  194. call .add_word
  195. pop hl
  196. push hl
  197. offset hl, $40
  198. add sp, -4
  199. push hl
  200. ld hl, sp + 2
  201. call .store_word
  202. pop hl
  203. call .load_word
  204. ld l, 5
  205. .rotation_loop
  206. call .rotate_word_left
  207. dec l
  208. jr nz, .rotation_loop
  209. ld hl, sp + 0
  210. call .add_word
  211. add sp, 4
  212. pop hl
  213. pop af
  214. push af
  215. push hl
  216. call .add_iteration_constant
  217. pop hl
  218. pop af
  219. ret
  220.  
  221. .initial_states_combination
  222. cp 20
  223. jr c, .selection_function
  224. cp 40
  225. jr c, .xor_function
  226. cp 60
  227. jr c, .majority_function
  228. .xor_function
  229. call .load_word
  230. call .xor_word
  231. .xor_word
  232. ld a, [hli]
  233. xor b
  234. ld b, a
  235. ld a, [hli]
  236. xor c
  237. ld c, a
  238. ld a, [hli]
  239. xor d
  240. ld d, a
  241. ld a, [hli]
  242. xor e
  243. ld e, a
  244. ret
  245.  
  246. .majority_function
  247. add sp, -4
  248. call .load_word
  249. push hl
  250. push de
  251. push bc
  252. call .and_word
  253. push hl
  254. ld hl, sp + 8
  255. call .store_word
  256. pop hl
  257. pop bc
  258. pop de
  259. call .and_word
  260. ld hl, sp + 2
  261. call .or_word
  262. ld hl, sp + 2 ;faster than push/pop, and same size
  263. call .store_word
  264. pop hl
  265. call .load_word
  266. call .and_word
  267. ld hl, sp + 0
  268. call .or_word
  269. add sp, 4
  270. ret
  271.  
  272. .selection_function
  273. add sp, -4
  274. call .load_word
  275. push de
  276. push bc
  277. call .and_word
  278. push hl
  279. ld hl, sp + 6
  280. call .store_word
  281. pop hl
  282. pop bc
  283. ld a, b
  284. cpl
  285. ld b, a
  286. ld a, c
  287. cpl
  288. ld c, a
  289. pop de
  290. ld a, d
  291. cpl
  292. ld d, a
  293. ld a, e
  294. cpl
  295. ld e, a
  296. call .and_word
  297. ld hl, sp + 0
  298. call .or_word
  299. add sp, 4
  300. ret
  301.  
  302. .add_iteration_constant
  303. ld l, -1
  304. .iteration_constant_loop
  305. inc l
  306. sub 20
  307. jr nc, .iteration_constant_loop
  308. ld a, l
  309. add a, a
  310. add a, a
  311. ld hl, .iteration_constants
  312. offset hl
  313. .add_word
  314. inc hl
  315. inc hl
  316. inc hl
  317. ld a, [hld]
  318. add a, e
  319. ld e, a
  320. ld a, [hld]
  321. adc d
  322. ld d, a
  323. ld a, [hld]
  324. adc c
  325. ld c, a
  326. ld a, [hl]
  327. adc b
  328. ld b, a
  329. ret
  330.  
  331. .update_buffer_word
  332. ; must preserve a, hl
  333. push af
  334. push hl
  335. sub 3
  336. and $f
  337. add a, a
  338. add a, a
  339. offset hl
  340. call .load_word
  341. pop hl
  342. pop af
  343. push af
  344. sub 8
  345. call .xor_word_by_offset
  346. pop af
  347. push af
  348. sub 14
  349. call .xor_word_by_offset
  350. pop af
  351. push af
  352. push hl
  353. and $f
  354. add a, a
  355. add a, a
  356. offset hl
  357. push hl
  358. call .xor_word
  359. call .rotate_word_left
  360. pop hl
  361. call .store_word
  362. pop hl
  363. pop af
  364. ret
  365.  
  366. .xor_word_by_offset
  367. push hl
  368. and $f
  369. add a, a
  370. add a, a
  371. offset hl
  372. call .xor_word
  373. pop hl
  374. ret
  375.  
  376. .update_states
  377. ; must preserve hl
  378. push bc
  379. push de
  380. ld a, $4c
  381. call .shift_word_position
  382. ld a, $48
  383. call .shift_word_position
  384. push hl
  385. offset hl, $44
  386. call .load_word
  387. call .rotate_word_right
  388. call .rotate_word_right
  389. call .store_word
  390. pop hl
  391. ld a, $40
  392. call .shift_word_position
  393. pop de
  394. pop bc
  395. push hl
  396. offset hl, $40
  397. call .store_word
  398. pop hl
  399. ret
  400.  
  401. .shift_word_position
  402. push hl
  403. offset hl
  404. call .load_word
  405. call .store_word
  406. pop hl
  407. ret
  408.  
  409. .initialization_data
  410. db $67, $45, $23, $01
  411. db $ef, $cd, $ab, $89
  412. db $98, $ba, $dc, $fe
  413. db $10, $32, $54, $76
  414. db $c3, $d2, $e1, $f0
  415.  
  416. .iteration_constants
  417. db $5a, $82, $79, $99
  418. db $6e, $d9, $eb, $a1
  419. db $8f, $1b, $bc, $dc
  420. db $ca, $62, $c1, $d6
Advertisement
RAW Paste Data Copied
Advertisement