Advertisement
AnthonyCagliano

Untitled

Mar 27th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. ; aes_gcm helper
  2. ; aes_gf2_mul(uint8_t *out, uint8_t *op1, uint8_t *op2)
  3. ; multiplication is add then double, then a polynomial reduction
  4. _aes_gf2_mul:
  5. ld hl, -16
  6. call ti._frameset
  7. lea de, ix - 16 ; stack mem?
  8. ld hl, (ix + 9) ; op1 (save a copy)
  9. ld bc, 16
  10. ldir ; ix - 32 = tmp = op1
  11.  
  12. ; zero out output
  13. ld de, (ix + 6) ; op 1
  14. xor a
  15. ld (de), a
  16. inc de
  17. ld hl, (ix + 6)
  18. ld bc, 15
  19. ldir
  20.  
  21. ld hl, (ix + 12) ; op2 = for bit in bits
  22. ld c, 16
  23. .loop_op2:
  24. ld a, (hl)
  25. push hl
  26. ld b, 8
  27. .loop_bits_in_byte:
  28. rra
  29. push af
  30. sbc a,a
  31. push bc
  32. ld c,a
  33.  
  34. ; add op1 (res) + tmp
  35. ld hl, (ix + 6) ; hl = (dest)
  36. lea de, ix - 16 ; de = tmp (src)
  37. ld b, 16
  38. .loop_add:
  39. ld a, (de)
  40. and a, c
  41. xor a, (hl)
  42. ld (hl), a
  43. inc hl
  44. inc de
  45. djnz .loop_add
  46.  
  47. ; now double tmp
  48. lea hl, ix - 16 ; tmp in hl
  49. ld b, 16
  50. or a ; reset carry
  51. .loop_mul2:
  52. rl (hl)
  53. inc hl
  54. djnz .loop_mul2
  55.  
  56. ; now xor with polynomial if tmp degree too high
  57. ; method below is constant-time
  58.  
  59. ld a, 10000011b
  60. xor a, (ix - 1)
  61. ld (ix - 1), a
  62.  
  63.  
  64. .no_xor_poly:
  65. pop bc
  66. pop af
  67. djnz .loop_bits_in_byte
  68. pop hl
  69. inc hl
  70. dec c
  71. jr nz, .loop_op2
  72. ld sp, ix
  73. pop ix
  74. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement