Advertisement
AnthonyCagliano

Untitled

Apr 4th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. ; aes_gf2_mul(uint8_t *out, uint8_t *op1, uint8_t *op2);
  2. _aes_gf2_mul_little:
  3. ; Galois-Field GF(2^128) multiplication routine
  4. ; little endian fields expected
  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 b, 0
  23. ld c, 16
  24. .loop_op2:
  25. ld a, (hl)
  26. push hl
  27. ld b, 8
  28. .loop_bits_in_byte:
  29. rra
  30. push af
  31. sbc a,a
  32. push bc
  33. ld c,a
  34.  
  35. ; add op1 (res) + tmp
  36. ld hl, (ix + 6) ; hl = (dest)
  37. lea de, ix - 16 ; de = tmp (src)
  38. ld b, 16
  39. .loop_add:
  40. ld a, (de)
  41. and a, c
  42. xor a, (hl)
  43. ld (hl), a
  44. inc hl
  45. inc de
  46. djnz .loop_add
  47.  
  48. ; now double tmp
  49. lea hl, ix - 16 ; tmp in hl little endian
  50. ld b, 16
  51. or a ; reset carry
  52. .loop_mul2:
  53. rl (hl)
  54. inc hl ; little endian
  55. djnz .loop_mul2
  56.  
  57. ; now xor with polynomial x^128 + x^7 + x^2 + x + 1
  58. ; if bit 128 set, xor least-significant byte with 10000111b
  59.  
  60. sbc a, a
  61. and a, 10000111b
  62. xor a, (ix - 16) ; little endian
  63. ld (ix - 16), a
  64.  
  65. .no_xor_poly:
  66. pop bc
  67. pop af
  68. djnz .loop_bits_in_byte
  69. pop hl
  70. inc hl ; little endian
  71. dec c
  72. jr nz, .loop_op2
  73. ld sp, ix
  74. pop ix
  75. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement