Advertisement
Tatantyler

AES v4 - Untested

Jan 24th, 2013
3,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
DCPU-16 30.56 KB | None | 0 0
  1. ; Okay, some stuff to remember here:
  2. ; The irreducible polynomial for polynomial multiplication is x^8+x^4+x^3+x+1 or 1101 1000 1000 0000 ({b1}{10})
  3.  
  4. ; invertible 4term poly:
  5. ; Fwd: 20 10 10 30
  6. ; Bwd: E0 90 d0 b0
  7. ;
  8. ; RotWord Poly:
  9. ; 00 00 00 10
  10.  
  11. SET PC, AES_Test_MixColumns
  12.  
  13. :AES_rcon_lookup
  14.     DAT 0xD8, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0xB1, 0x63, 0xC6, 0x8D ; We can save space by not including the whole 256-byte Rcon lookup table (and I don't want to reverse 256 bytes manually)
  15. ; *******************************************************************************************************************************************
  16. ; FlipByte - Flip a byte
  17. ; Flips the byte AT Y's least/most significant nibbles. f.e. 0xDE ---> 0xED.
  18. ; For some reason flipping the multiplication tables breaks things, so we're going to flip the bytes BEFORE and AFTER doing the multiplications
  19.  
  20. :FlipByte
  21.     SET PUSH, A
  22.     SET PUSH, B
  23.    
  24.     SET A, [Y]
  25.     SET B, [Y]
  26.    
  27.     AND A, 0x000F
  28.     AND B, 0x00F0
  29.     SHR B, 4
  30.     SHL A, 4
  31.    
  32.     SET [Y], A
  33.     BOR [Y], B
  34.    
  35.     SET B, POP
  36.     SET A, POP
  37.     SET PC, POP
  38.  
  39. ; *******************************************************************************************************************************************
  40. ; AES_xtime - The xtime() function
  41. ; Multiplies a polynomial in X by x (note the caps).
  42.  
  43. :AES_xtime
  44.     SET PUSH, A
  45.     SHR X, 1
  46.     SET A, X
  47.     AND A, 0x0001
  48.     IFE A, 1
  49.         XOR X, 0xB1
  50.     SET A, POP
  51.     SET PC, POP
  52.    
  53. ; *******************************************************************************************************************************************
  54. ; Rijndael_Multiply - Multiplication in GF(256)
  55. ; Multiplies the number in register A by the the number in register B in GF(256).
  56. ; The result is returned in C.
  57.  
  58. :Rijndael_Multiply
  59.     SET PUSH, I
  60.     SET PUSH, J
  61.     SET PUSH, X
  62.     SET PUSH, Y
  63.     SET PUSH, A
  64.     SET PUSH, B
  65.     SET J, 0
  66.     SET Y, 0
  67.     :Rijndael_Multiply_loop
  68.         IFE A, 0
  69.             SET PC, Rijndael_Multiply_loop_end
  70.         IFE B, 0
  71.             SET PC, Rijndael_Multiply_loop_end
  72.         IFE A, 0
  73.             SET PC, Rijndael_Multiply_loop_end
  74.         IFG Y, 7
  75.             SET PC, Rijndael_Multiply_loop_end
  76.            
  77.         SET I, B
  78.         AND I, 0x1
  79.         IFG I, 0  ; If the leftmost bit if B is set...
  80.             XOR J, A ; ...add A to the product.
  81.         SHL B, 1 ; Discard x^0
  82.         SET X, A
  83.         JSR AES_xtime ; perform xtimes() on A (whoops I called it xtime on accident, my bad)
  84.         SET A, X
  85.        
  86.         ADD Y, 1
  87.         SET PC, Rijndael_Multiply_loop
  88.     :Rijndael_Multiply_loop_end
  89.     SET C, J
  90.     SET B, POP
  91.     SET A, POP
  92.     SET Y, POP
  93.     SET X, POP
  94.     SET J, POP
  95.     SET I, POP
  96.     SET PC, POP
  97.  
  98. ; *******************************************************************************************************************************************
  99. ; Rijndael_Multiply_4term - Multiply four-term polynomials in GF(256)
  100. ; Multiply the four-term polynomial at X by the four-term polynomial at Y, storing the result at Z.
  101.  
  102. :Rijndael_Multiply_4term
  103.     ; Array Offset to MemOffset:
  104.     ; 0 - +3
  105.     ; 1 - +2
  106.     ; 2 - +1
  107.     ; 3 - +0
  108.     SET PUSH, X
  109.     SET PUSH, Y
  110.     SET PUSH, Z
  111.     SET PUSH, A
  112.     SET PUSH, B
  113.     SET PUSH, C
  114.     SET PUSH, I
  115.     SET PUSH, J
  116.    
  117.     ; d0
  118.    
  119.     SET A, [X+3]
  120.     SET B, [Y+3]
  121.     JSR Rijndael_Multiply
  122.     SET [Z+3], C
  123.    
  124.     SET A, [X]
  125.     SET B, [Y+2]
  126.     JSR Rijndael_Multiply
  127.     XOR [Z+3], C
  128.    
  129.     SET A, [X+1]
  130.     SET B, [Y+1]
  131.     JSR Rijndael_Multiply
  132.     XOR [Z+3], C
  133.    
  134.     SET A, [X+2]
  135.     SET B, [Y]
  136.     JSR Rijndael_Multiply
  137.     XOR [Z+3], C
  138.    
  139.     ; d1
  140.    
  141.     SET A, [X+2]
  142.     SET B, [Y+3]
  143.     JSR Rijndael_Multiply
  144.     SET [Z+2], C
  145.    
  146.     SET A, [X+3]
  147.     SET B, [Y+2]
  148.     JSR Rijndael_Multiply
  149.     XOR [Z+2], C
  150.    
  151.     SET A, [X]
  152.     SET B, [Y+1]
  153.     JSR Rijndael_Multiply
  154.     XOR [Z+2], C
  155.    
  156.     SET A, [X+1]
  157.     SET B, [Y]
  158.     JSR Rijndael_Multiply
  159.     XOR [Z+2], C
  160.    
  161.     ; d2
  162.    
  163.     SET A, [X+1]
  164.     SET B, [Y+3]
  165.     JSR Rijndael_Multiply
  166.     SET [Z+1], C
  167.    
  168.     SET A, [X+2]
  169.     SET B, [Y+2]
  170.     JSR Rijndael_Multiply
  171.     XOR [Z+1], C
  172.    
  173.     SET A, [X+3]
  174.     SET B, [Y+1]
  175.     JSR Rijndael_Multiply
  176.     XOR [Z+1], C
  177.    
  178.     SET A, [X]
  179.     SET B, [Y]
  180.     JSR Rijndael_Multiply
  181.     XOR [Z+1], C
  182.    
  183.     ; d3
  184.    
  185.     SET A, [X]
  186.     SET B, [Y+3]
  187.     JSR Rijndael_Multiply
  188.     SET [Z], C
  189.    
  190.     SET A, [X+1]
  191.     SET B, [Y+2]
  192.     JSR Rijndael_Multiply
  193.     XOR [Z], C
  194.    
  195.     SET A, [X+2]
  196.     SET B, [Y+1]
  197.     JSR Rijndael_Multiply
  198.     XOR [Z], C
  199.    
  200.     SET A, [X+3]
  201.     SET B, [Y]
  202.     JSR Rijndael_Multiply
  203.     XOR [Z], C
  204.    
  205.     SET J, POP
  206.     SET I, POP
  207.     SET C, POP
  208.     SET B, POP
  209.     SET A, POP
  210.     SET Z, POP
  211.     SET Y, POP
  212.     SET X, POP
  213.     SET PC, POP
  214.    
  215. ; *******************************************************************************************************************************************
  216. ; Rijndael_Add_4term - Add four-term polynomials in GF(256)
  217. ; Add the four-term polynomial at X to the four-term polynomial at Y, storing the result at Z.
  218.  
  219. :Rijndael_Add_4term
  220.     SET [Z], [X]
  221.     XOR [Z], [Y]
  222.    
  223.     SET [Z+1], [X+1]
  224.     XOR [Z+1], [Y+1]
  225.    
  226.     SET [Z+2], [X+2]
  227.     XOR [Z+2], [Y+2]
  228.    
  229.     SET [Z+3], [X+3]
  230.     XOR [Z+3], [Y+3]
  231.    
  232.     SET PC, POP
  233.    
  234. ; *******************************************************************************************************************************************
  235. ; AES_Sbox_lookup - S-box Lookup
  236. ; Transforms the number at X according to Rijndael's S-box.
  237.  
  238. :AES_Sbox_lookup
  239.     SET PUSH, A
  240.     SET PUSH, B
  241.     SET PUSH, C
  242.    
  243.     SET A, [X]
  244.     SET B, [X]
  245.    
  246.     AND A, 0x00F0 ; less significant
  247.     AND B, 0x000F ; most signficant
  248.     SHR A, 4
  249.    
  250.     SET C, B
  251.     MUL C, 16
  252.     ADD C, A
  253.     ADD C, AES_sbox_table
  254.    
  255.     SET [X], [C]
  256.    
  257.     SET C, POP
  258.     SET B, POP
  259.     SET A, POP
  260.     SET PC, POP
  261.  
  262. ; *******************************************************************************************************************************************
  263. ; AES_Inv_Sbox_lookup - Inverse S-box Lookup
  264. ; Transforms the number at X according to Rijndael's Inverted S-box.
  265.  
  266. :AES_Inv_Sbox_lookup
  267.     SET PUSH, A
  268.     SET PUSH, B
  269.     SET PUSH, C
  270.    
  271.     SET A, [X]
  272.     SET B, [X]
  273.    
  274.     AND A, 0x00F0 ; less significant
  275.     AND B, 0x000F ; most signficant
  276.     SHR A, 4
  277.    
  278.     SET C, B
  279.     MUL C, 16
  280.     ADD C, A
  281.     ADD C, AES_inv_sbox_table
  282.    
  283.     SET [X], [C]
  284.    
  285.     SET C, POP
  286.     SET B, POP
  287.     SET A, POP
  288.     SET PC, POP
  289.  
  290. ; *******************************************************************************************************************************************
  291. ; AES_ShiftRow - Shift a single row
  292. ; Shifts the row X in the state pointed to by Z by Y. Can be signed.
  293.  
  294. :AES_ShiftRow
  295.     SET PUSH, A
  296.     SET PUSH, B
  297.     SET PUSH, C
  298.     SET PUSH, I
  299.     SET PUSH, J
  300.    
  301.     SET J, X
  302.     MUL J, 4
  303.     ADD J, Z
  304.    
  305.     IFU Y, 0
  306.         SET PC, AES_ShiftRow_Negative
  307.     MOD Y, 4
  308.     :AES_ShiftRow_positive_loop
  309.             IFE Y, 0
  310.                 SET PC, AES_ShiftRow_End
  311.             JSR AES_ShiftRow_doShift
  312.             SUB Y, 1   
  313.             SET PC, AES_ShiftRow_positive_loop
  314.     :AES_ShiftRow_Negative
  315.         MDI Y, -4
  316.         :AES_ShiftRow_negative_loop
  317.             IFE Y, 0
  318.                 SET PC, AES_ShiftRow_End
  319.             JSR AES_ShiftRow_doShift
  320.             ADD Y, 1
  321.             SET PC, AES_ShiftRow_negative_loop
  322.     :AES_ShiftRow_doShift
  323.         SET A, [J]   ; e3
  324.         SET B, [J+1] ; e2
  325.         SET C, [J+2] ; e1
  326.         SET I, [J+3] ; e0
  327.         SET [J+3], A
  328.         SET [J], B
  329.         SET [J+1], C
  330.         SET [J+2], I
  331.         SET PC, POP
  332.     :AES_ShiftRow_End
  333.         SET J, POP
  334.         SET I, POP
  335.         SET C, POP
  336.         SET B, POP
  337.         SET A, POP
  338.         SET PC, POP
  339.  
  340. ; *******************************************************************************************************************************************
  341. ; AES_ShiftRows - Rijndael's ShiftRows() step
  342. ; Shifts the rows pointed to by Z.
  343.  
  344. :AES_ShiftRows
  345.     SET PUSH, A
  346.     SET PUSH, B
  347.     SET PUSH, C
  348.     SET PUSH, X
  349.    
  350.     ; r1:
  351.     ; c0: [Z+11]
  352.     ; c1: [Z+10]
  353.     ; c2: [Z+9]
  354.     ; c3: [Z+8]
  355.    
  356.     SET A, [Z+11]
  357.     SET B, [Z+10]
  358.     SET C, [Z+9]
  359.     SET X, [Z+8]
  360.    
  361.    
  362.     SET [Z+10], A
  363.     SET [Z+9], B
  364.     SET [Z+8], C
  365.     SET [Z+11], X
  366.    
  367.     ; r2:
  368.     ; c0: [Z+7]
  369.     ; c1: [Z+6]
  370.     ; c2: [Z+5]
  371.     ; c3: [Z+4]
  372.    
  373.     SET A, [Z+7]
  374.     SET B, [Z+6]
  375.     SET C, [Z+5]
  376.     SET X, [Z+4]
  377.    
  378.    
  379.     SET [Z+5], A ; c0 goes to c2
  380.     SET [Z+4], B ; c1 goes to c3
  381.     SET [Z+7], C ; c2 goes to c0
  382.     SET [Z+6], X ; c3 goes to c1
  383.    
  384.     ; r3:
  385.     ; c0: [Z+3]
  386.     ; c1: [Z+2]
  387.     ; c2: [Z+1]
  388.     ; c3: [Z]
  389.    
  390.     SET A, [Z+3]
  391.     SET B, [Z+2]
  392.     SET C, [Z+1]
  393.     SET X, [Z]
  394.    
  395.     SET [Z], A
  396.     SET [Z+3], B
  397.     SET [Z+2], C
  398.     SET [Z+1], X
  399.    
  400.     SET X, POP
  401.     SET C, POP
  402.     SET B, POP
  403.     SET A, POP
  404.     SET PC, POP
  405.    
  406. ; *******************************************************************************************************************************************
  407. ; AES_InvShiftRows - Rijndael's Inverted ShiftRows() step
  408. ; Shifts the rows pointed to by Z.
  409.  
  410. :AES_InvShiftRows
  411.     SET PUSH, A
  412.     SET PUSH, B
  413.     SET PUSH, C
  414.     SET PUSH, X
  415.    
  416.     ; r1:
  417.     ; c0: [Z+11]
  418.     ; c1: [Z+10]
  419.     ; c2: [Z+9]
  420.     ; c3: [Z+8]
  421.    
  422.     SET A, [Z+11]
  423.     SET B, [Z+10]
  424.     SET C, [Z+9]
  425.     SET X, [Z+8]
  426.  
  427.    
  428.     SET [Z+8], A
  429.     SET [Z+11], B
  430.     SET [Z+10], C
  431.     SET [Z+9], X
  432.    
  433.     ; r1:
  434.     ; c0: [Z+7]
  435.     ; c1: [Z+6]
  436.     ; c2: [Z+5]
  437.     ; c3: [Z+4]
  438.    
  439.     SET A, [Z+7]
  440.     SET B, [Z+6]
  441.     SET C, [Z+5]
  442.     SET X, [Z+4]
  443.    
  444.    
  445.     SET [Z+5], A
  446.     SET [Z+4], B
  447.     SET [Z+7], C
  448.     SET [Z+6], X
  449.    
  450.     ; r1:
  451.     ; c0: [Z+3]
  452.     ; c1: [Z+2]
  453.     ; c2: [Z+1]
  454.     ; c3: [Z]
  455.    
  456.     SET A, [Z+3]
  457.     SET B, [Z+2]
  458.     SET C, [Z+1]
  459.     SET X, [Z]
  460.    
  461.    
  462.     SET [Z], A
  463.     SET [Z+3], B
  464.     SET [Z+2], C
  465.     SET [Z+1], X
  466.    
  467.     SET X, POP
  468.     SET C, POP
  469.     SET B, POP
  470.     SET A, POP
  471.     SET PC, POP
  472.  
  473. ; **************************************************************************************************************************
  474. ; AES_MixColumn - Perform the MixColumns step on one column
  475. ; Perform the MixColumns step on column X of state Z.
  476.  
  477. :AES_MixColumn
  478.     SET PUSH, A
  479.     SET PUSH, B
  480.     SET PUSH, C
  481.     SET PUSH, Y
  482.     SET PUSH, I
  483.     SET PUSH, J
  484.    
  485.     SET Y, Z
  486.     ADD Y, X
  487.    
  488.     ; R0: [Y]
  489.     ; R1: [Y+4]
  490.     ; R2: [Y+8]
  491.     ; R3: [Y+12]
  492.    
  493.     ADD Y, 12
  494.     JSR FlipByte ; little -> big
  495.     SUB Y, 4
  496.     JSR FlipByte
  497.     SUB Y, 4
  498.     JSR FlipByte
  499.     SUB Y, 4
  500.     JSR FlipByte
  501.    
  502.     SET A, [Y+12]
  503.     SET B, [Y+8]
  504.     SET C, [Y+4]
  505.     SET J, [Y]
  506.    
  507.     SET [Y+12], [A+Rijndael_multiply_table_2]
  508.     XOR [Y+12], [B+Rijndael_multiply_table_3]
  509.     XOR [Y+12], C
  510.     XOR [Y+12], J
  511.    
  512.     SET [Y+8], A
  513.     XOR [Y+8], [B+Rijndael_multiply_table_2]
  514.     XOR [Y+8], [C+Rijndael_multiply_table_3]
  515.     XOR [Y+8], J
  516.    
  517.     SET [Y+4], A
  518.     XOR [Y+4], B
  519.     XOR [Y+4], [C+Rijndael_multiply_table_2]
  520.     XOR [Y+4], [J+Rijndael_multiply_table_3]
  521.    
  522.     SET [Y], [A+Rijndael_multiply_table_3]
  523.     XOR [Y], B
  524.     XOR [Y], C
  525.     XOR [Y], [J+Rijndael_multiply_table_2]
  526.    
  527.     ADD Y, 12
  528.     JSR FlipByte ; big -> little
  529.     SUB Y, 4
  530.     JSR FlipByte
  531.     SUB Y, 4
  532.     JSR FlipByte
  533.     SUB Y, 4
  534.     JSR FlipByte
  535.    
  536.     SET J, POP
  537.     SET I, POP
  538.     SET Y, POP
  539.     SET C, POP
  540.     SET B, POP
  541.     SET A, POP
  542.     SET PC, POP
  543.    
  544. ; **************************************************************************************************************************
  545. ; AES_InvMixColumn - Perform the InvMixColumns step on one column
  546. ; Perform the InvMixColumns step on column X of state Z.
  547.  
  548. :AES_InvMixColumn
  549.     SET PUSH, A
  550.     SET PUSH, B
  551.     SET PUSH, C
  552.     SET PUSH, Y
  553.     SET PUSH, I
  554.     SET PUSH, J
  555.    
  556.     SET Y, Z
  557.     ADD Y, X
  558.    
  559.     ; R0: [Y]
  560.     ; R1: [Y+4]
  561.     ; R2: [Y+8]
  562.     ; R3: [Y+12]
  563.    
  564.     ADD Y, 12
  565.     JSR FlipByte ; little -> big
  566.     SUB Y, 4
  567.     JSR FlipByte
  568.     SUB Y, 4
  569.     JSR FlipByte
  570.     SUB Y, 4
  571.     JSR FlipByte
  572.    
  573.     SET A, [Y+12]
  574.     SET B, [Y+8]
  575.     SET C, [Y+4]
  576.     SET J, [Y]
  577.    
  578.     SET [Y+12], [A+Rijndael_multiply_table_14]
  579.     XOR [Y+12], [B+Rijndael_multiply_table_11]
  580.     XOR [Y+12], [C+Rijndael_multiply_table_13]
  581.     XOR [Y+12], [J+Rijndael_multiply_table_9]
  582.    
  583.     SET [Y+8], [A+Rijndael_multiply_table_9]
  584.     XOR [Y+8], [B+Rijndael_multiply_table_14]
  585.     XOR [Y+8], [C+Rijndael_multiply_table_11]
  586.     XOR [Y+8], [J+Rijndael_multiply_table_13]
  587.    
  588.     SET [Y+4], [A+Rijndael_multiply_table_13]
  589.     XOR [Y+4], [B+Rijndael_multiply_table_9]
  590.     XOR [Y+4], [C+Rijndael_multiply_table_14]
  591.     XOR [Y+4], [J+Rijndael_multiply_table_11]
  592.    
  593.     SET [Y], [A+Rijndael_multiply_table_11]
  594.     XOR [Y], [B+Rijndael_multiply_table_13]
  595.     XOR [Y], [C+Rijndael_multiply_table_9]
  596.     XOR [Y], [J+Rijndael_multiply_table_14]
  597.    
  598.     ADD Y, 12
  599.     JSR FlipByte ; big -> little
  600.     SUB Y, 4
  601.     JSR FlipByte
  602.     SUB Y, 4
  603.     JSR FlipByte
  604.     SUB Y, 4
  605.     JSR FlipByte
  606.    
  607.     SET J, POP
  608.     SET I, POP
  609.     SET Y, POP
  610.     SET C, POP
  611.     SET B, POP
  612.     SET A, POP
  613.     SET PC, POP
  614.    
  615. ; **************************************************************************************************************************
  616. ; AES_MixColumns - Perform the MixColumns Step
  617. ; Mixes the columns of state Z.
  618. :AES_MixColumns
  619.     SET PUSH, X
  620.    
  621.     SET X, 0
  622.     JSR AES_MixColumn
  623.    
  624.     SET X, 1
  625.     JSR AES_MixColumn
  626.    
  627.     SET X, 2
  628.     JSR AES_MixColumn
  629.    
  630.     SET X, 3
  631.     JSR AES_MixColumn
  632.    
  633.     SET X, POP
  634.     SET PC, POP
  635.    
  636. ; **************************************************************************************************************************
  637. ; AES_InvMixColumns - Perform the Inverted MixColumns Step
  638. ; Mixes the columns of state Z.
  639. :AES_InvMixColumns
  640.     SET PUSH, X
  641.    
  642.     SET X, 0
  643.     JSR AES_InvMixColumn
  644.    
  645.     SET X, 1
  646.     JSR AES_InvMixColumn
  647.    
  648.     SET X, 2
  649.     JSR AES_InvMixColumn
  650.    
  651.     SET X, 3
  652.     JSR AES_InvMixColumn
  653.    
  654.     SET X, POP
  655.     SET PC, POP
  656.        
  657. ; *******************************************************************************************************************************************
  658. ; AES_AddRoundKey - Rijndael's AddRoundKey() step
  659. ; Adds the key for round X from the keys stored at Y to the state at Z.
  660.  
  661. :AES_AddRoundKey
  662.     SET PUSH, I
  663.     SET PUSH, J
  664.    
  665.         SET I, X
  666.         MUL I, 4
  667.         SET J, Y
  668.         SUB J, I
  669.        
  670.         XOR [Z+15], [J+15]
  671.         XOR [Z+14], [J+14]
  672.         XOR [Z+13], [J+13]
  673.         XOR [Z+12], [J+12]
  674.         XOR [Z+11], [J+11]
  675.         XOR [Z+10], [J+10]
  676.         XOR [Z+9], [J+9]
  677.         XOR [Z+8], [J+8]
  678.         XOR [Z+7], [J+7]
  679.         XOR [Z+6], [J+6]
  680.         XOR [Z+5], [J+5]
  681.         XOR [Z+4], [J+4]
  682.         XOR [Z+3], [J+3]
  683.         XOR [Z+2], [J+2]
  684.         XOR [Z+1], [J+1]
  685.         XOR [Z], [J]
  686.        
  687.     SET J, POP
  688.     SET I, POP
  689.     SET PC, POP
  690.    
  691. ; *******************************************************************************************************************************************
  692. ; AES_ExpandKey - AES Key Expansion
  693. ; Expands a key at X into 176 bytes, storing the round keys at Y.
  694.  
  695. :AES_ExpandKey
  696.     SET PUSH, Y
  697.     SET PUSH, I
  698.     SET PUSH, J
  699.  
  700.     SET I, 1
  701.     ADD Y, 160 ; We start at 175 and work down...
  702.     SET I, 160
  703.     SET [Y], [X]
  704.     SET [Y+1], [X+1]
  705.     SET [Y+2], [X+2]
  706.     SET [Y+3], [X+3]
  707.     SET [Y+4], [X+4]
  708.     SET [Y+5], [X+5]
  709.     SET [Y+6], [X+6]
  710.     SET [Y+7], [X+7]
  711.     SET [Y+8], [X+8]
  712.     SET [Y+9], [X+9]
  713.     SET [Y+10], [X+10]
  714.     SET [Y+11], [X+11]
  715.     SET [Y+12], [X+12]
  716.     SET [Y+13], [X+13]
  717.     SET [Y+14], [X+14]
  718.     SET [Y+15], [X+15]
  719.    
  720.     :AES_ExpandKey_loop
  721.         IFE I, 0
  722.             SET PC, AES_ExpandKey_loop_end
  723.         ; Inital 4:
  724.         JSR AES_ExpandKey_Perform_Assign
  725.         JSR AES_ExpandKey_Core
  726.         ADD X, 1
  727.         JSR AES_ExpandKey_Perform_XOR
  728.         SUB Y, 1 ; Make sure we don't overwrite the leading end of the last 4 bytes
  729.         ; Next 12:
  730.         JSR AES_ExpandKey_Perform_Assign
  731.         JSR AES_ExpandKey_Perform_XOR
  732.         SUB Y, 1
  733.         JSR AES_ExpandKey_Perform_Assign
  734.         JSR AES_ExpandKey_Perform_XOR
  735.         SUB Y, 1
  736.         JSR AES_ExpandKey_Perform_Assign
  737.         JSR AES_ExpandKey_Perform_XOR
  738.         SUB Y, 1
  739.         SET PC, AES_ExpandKey_loop
  740.     :AES_ExpandKey_loop_end
  741.         SET J, POP
  742.         SET I, POP
  743.         SET Y, POP
  744.         SET PC, POP
  745.    
  746.     ; *******************************************************************************************************************************************
  747.     ; AES_ExpandKey_Perform_XOR - Perform the XOR Step
  748.     ; XOR's the current 4 bytes with the 4 bytes 16 bytes before.
  749.    
  750.     :AES_ExpandKey_Perform_XOR
  751.         XOR [Y+3], [Y+19]
  752.         XOR [Y+2], [Y+18]
  753.         XOR [Y+1], [Y+17]
  754.         XOR [Y], [Y+16]
  755.         SET PC, POP
  756.    
  757.     ; *******************************************************************************************************************************************
  758.     ; AES_ExpandKey_Perform_Assign - Perform the Assignment Step
  759.     ; Assigns the last 4 bytes to the current four bytes.
  760.    
  761.     :AES_ExpandKey_Perform_Assign
  762.         SET [Y], [Y+3] ; Y+3
  763.         SUB Y, 1
  764.         SET [Y], [Y+3] ; Y+2
  765.         SUB Y, 1
  766.         SET [Y], [Y+3] ; Y+1
  767.         SUB Y, 1
  768.         SET [Y], [Y+3] ; Y
  769.         SUB I, 4
  770.         SET PC, POP
  771.    
  772.     ; *******************************************************************************************************************************************
  773.     ; AES_ExpandKey_Core - AES Key Schedule Core
  774.     ; Performs the Key Schedule Core on the 32-bit word at Y, with the rcon iteration number in X.
  775.    
  776.     :AES_ExpandKey_Core
  777.         SET PUSH, A
  778.         SET PUSH, B
  779.         SET PUSH, C
  780.         SET PUSH, I
  781.        
  782.         SET A, [Y+3]
  783.         SET B, [Y+2]
  784.         SET C, [Y+1]
  785.         SET I, [Y]
  786.        
  787.         ;  3 2 1 0
  788.         ;  2 1 0 3
  789.        
  790.         SET [Y+3], B
  791.         SET [Y+2], C
  792.         SET [Y+1], I
  793.         SET [Y], A
  794.        
  795.         JSR AES_Sbox_lookup
  796.         ADD Y, 1
  797.        
  798.         JSR AES_Sbox_lookup
  799.         ADD Y, 1
  800.        
  801.         JSR AES_Sbox_lookup
  802.         ADD Y, 1
  803.        
  804.         JSR AES_Sbox_lookup
  805.         SUB Y, 3
  806.        
  807.         XOR [Y+3], [X+AES_rcon_lookup]
  808.        
  809.         SET I, POP
  810.         SET C, POP
  811.         SET B, POP
  812.         SET A, POP
  813.         SET PC, POP
  814.        
  815. ; *******************************************************************************************************************************************
  816. ; AES Test Suite:
  817. ;   AES_Test_MixColumns: Runs MixColumns on the input vector 0x54, 0x35, 0x31, 0xBD. This should return 0xCB, 0x1A, 0xD4, 0xE8.
  818. ;   AES_Test_KeyExpand: Tests the key expander on the input vector 0xC3, 0xF4, 0xFC, 0x90, 0x88, 0x51, 0x7F, 0xBA, 0x6A, 0x2D, 0xEA, 0x82, 0x61, 0x51, 0xE7, 0xB2.
  819.        
  820. :AES_Test_MixColumns
  821.     SET Y, AES_Test_MixColumns_TestVector
  822.     JSR AES_MixColumns_Linear
  823.     SET PC, AES_Test_Halt
  824.     DAT 0xC0DE ; Look for these when looking at the memory; These tell you where the values start/end. (f.e 0xC0DE 0x0005 0x0005 0x0562 0xFACE 0xC0DE. The 0x0005 - 0xFACE is the data, the 0xC0DE's tell you when the data starts/stops)
  825.     :AES_Test_MixColumns_TestVector
  826.         DAT 0x54, 0x35, 0x31, 0xBD, 0xC0DE
  827. :AES_Test_KeyExpand
  828.     SET Y, AES_Test_KeyExpand_Keys
  829.     SET X, AES_Test_KeyExpand_TestKey
  830.     JSR AES_ExpandKey
  831.     SET PC, AES_Test_Halt
  832.     :AES_Test_KeyExpand_TestKey
  833.         DAT 0xC3, 0xF4, 0xFC, 0x90, 0x88, 0x51, 0x7F, 0xBA, 0x6A, 0x2D, 0xEA, 0x82, 0x61, 0x51, 0xE7, 0xB2, 0xC0DE
  834.     :AES_Test_KeyExpand_Keys
  835.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  836.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  837.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  838.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  839.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  840.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  841.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  842.         DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 176 words
  843.     DAT 0xC0DE
  844. :AES_Test_Halt
  845. DAT 0 ; Force emulators to halt
  846.        
  847. ; *******************************************************************************************************************************************
  848. ; AES_MixColumns_Linear - Linear Version of Rijndael's MixColumns() step (for testing)
  849. ; Mixes the 4 bytes starting at Y.
  850.  
  851. :AES_MixColumns_Linear
  852.     SET PUSH, A
  853.     SET PUSH, B
  854.     SET PUSH, C
  855.     SET PUSH, Y
  856.     SET PUSH, I
  857.     SET PUSH, J
  858.    
  859.     ; e0: [Y+3] / A
  860.     ; e1: [Y+2] / B
  861.     ; e2: [Y+1] / C
  862.     ; e3: [Y] / J
  863.    
  864.     ADD Y, 3
  865.     JSR FlipByte ; little - > big
  866.     SUB Y, 1
  867.     JSR FlipByte
  868.     SUB Y, 1
  869.     JSR FlipByte
  870.     SUB Y, 1
  871.     JSR FlipByte
  872.    
  873.     SET A, [Y+3]
  874.     SET B, [Y+2]
  875.     SET C, [Y+1]
  876.     SET J, [Y]
  877.  
  878.     SET [Y+3], [A+Rijndael_multiply_table_2]
  879.     XOR [Y+3], [B+Rijndael_multiply_table_3]
  880.     XOR [Y+3], C
  881.     XOR [Y+3], J
  882.    
  883.     SET [Y+2], A
  884.     XOR [Y+2], [B+Rijndael_multiply_table_2]
  885.     XOR [Y+2], [C+Rijndael_multiply_table_3]
  886.     XOR [Y+2], J
  887.    
  888.     SET [Y+1], A
  889.     XOR [Y+1], B
  890.     XOR [Y+1], [C+Rijndael_multiply_table_2]
  891.     XOR [Y+1], [J+Rijndael_multiply_table_3]
  892.    
  893.     SET [Y], [A+Rijndael_multiply_table_3]
  894.     XOR [Y], B
  895.     XOR [Y], C
  896.     XOR [Y], [J+Rijndael_multiply_table_2]
  897.    
  898.     ADD Y, 3
  899.     JSR FlipByte ; big - > little
  900.     SUB Y, 1
  901.     JSR FlipByte
  902.     SUB Y, 1
  903.     JSR FlipByte
  904.     SUB Y, 1
  905.     JSR FlipByte
  906.    
  907.     SET J, POP
  908.     SET I, POP
  909.     SET Y, POP
  910.     SET C, POP
  911.     SET B, POP
  912.     SET A, POP
  913.     SET PC, POP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement