Advertisement
AnthonyCagliano

Untitled

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