Da ich hier nun schonmal angefangen habe hier ein kompletter Encrypt-Vorgang für 16 Bytes. Vielleicht kann´s jemand als Grundlage oder Anregung gebrauchen. Die Verwendung von ASM scheint mir hier unerlässlich zu sein (Speed), also bitte nicht meckern!
Code: Alles auswählen
;- AES-Rijndael-Encryption-Test, Textblock-und Keyword-Länge jeweils 128 Bit
;- "Helle" Klaus Helbing, 13.12.2007, PB4.10
;- Ohne Gewähr! Lässt sich bestimmt noch optimieren
Global Array1.l
Global Array2.l
Array1=AllocateMemory(16) ;für (Zwischen-)Ergebnisse, so auch wegen einfache
Array2=AllocateMemory(16) ; Zugriffs-Möglichkeit für PB (Anzeige)
;- RoundKeys ermitteln
;- RoundKey1, Ergebnis steht in RoundKey
!lea edi,[CipherKey]
!mov eax,[edi+12]
!ror eax,8
!lea edi,[RoundKey]
!lea esi,[SBox]
!xor ebx,ebx
!mov ecx,4
!@@:
!mov bl,al
!mov dl,[esi+ebx]
!mov [edi],dl
!shr eax,8
!inc edi
!dec ecx
!jnz @b
!lea esi,[RoundKey]
!lea edi,[CipherKey]
!lea edx,[RCon]
!mov eax,[edi]
!xor [esi],eax
!mov ebx,[edx]
!xor [esi],ebx ;1.DWord von RoundKey1
!mov edx,[esi]
!mov eax,[edi+4]
!xor eax,edx
!mov [esi+4],eax ;2.DWord von RoundKey1
!xor eax,[edi+8]
!mov [esi+8],eax ;3.DWord von RoundKey1
!xor eax,[edi+12]
!mov [esi+12],eax ;4.DWord von RoundKey1
;- Ende RoundKey1
;- RoundKey2 - RoundKey10
!mov [Schleife1],9
!RoundKeys:
!add [RConZ],4
!ror eax,8
!lea esi,[SBox]
!mov ecx,4
!@@:
!mov ebx,eax
!and ebx,0ffh
!mov dl,[esi+ebx]
!dec ecx
!jz @f
!shl edx,8
!shr eax,8
!jmp @b
!@@:
!bswap edx
!lea edi,[RoundKey]
!add edi,[Round] ;1.DWord vom "VorKey"
!mov eax,[edi]
!xor eax,edx
!lea esi,[RCon]
!add esi,[RConZ]
!xor eax,[esi]
!add edi,16 ;hier soll abgespeichert werden
!mov [edi],eax
!add edi,4
!xor eax,[edi-16]
!mov [edi],eax ;2.DWord
!add edi,4
!xor eax,[edi-16]
!mov [edi],eax ;3.DWord Fehler beim Uru in RoundKey2 (Schreib-Fehler)!
!add edi,4
!xor eax,[edi-16]
!mov [edi],eax ;4.DWord
!add [Round],16
!dec [Schleife1]
!jnz RoundKeys
;- Ende RoundKey2 - RoundKey10
;- 1.Schritt State XOR Cipher Key -----
;- Ergebnis steht in Array1
!mov edi,[v_Array1]
!lea esi,[State]
!mov ecx,4
!@@:
!mov eax,[esi]
!xor eax,[esi+16]
!mov [edi],eax
!add edi,4
!add esi,4
!dec ecx
!jnz @b
;--------------------------------------
!mov [Round],0 ;für Schleife unten
!Schleife:
;- 2.Schritt Werte aus S-Box holen ----
;- Ergebnis steht in Array1
!mov edi,[v_Array1]
!lea esi,[SBox]
!xor ebx,ebx
!mov edx,4
!S0:
!mov ecx,4
!@@:
!mov bl,[edi]
!mov al,[esi+ebx]
!mov [edi],al
!inc edi
!dec ecx
!jnz @b
!dec edx
!jnz S0
;--------------------------------------
;- 3.Schritt Rotationen ---------------
;- Ergebnis steht in Array1
!mov esi,[v_Array1]
!mov ecx,8
!Rotat:
!inc esi
!mov edx,12
!@@:
!mov al,[esi+edx]
!sub edx,4
!jc @f
!shl eax,8
!jmp @b
!@@:
!ror eax,cl
!xor edx,edx
!@@:
!mov [esi+edx],al
!add edx,4
!cmp edx,16
!je @f
!shr eax,8
!jmp @b
!@@:
!add ecx,8
!cmp ecx,24
!jbe Rotat
;--------------------------------------
;- 4.Schritt Mix-Columns --------------
;- Ergebnis steht in Array2
!cmp [Round],144
!jne @f
!mov edi,[v_Array1]
!jmp NoMix ;letzte Runde nicht
!@@:
!mov esi,[v_Array1]
!mov edi,[v_Array2]
!mov [Schleife2],4
!Mix2:
!mov ebx,[esi]
!mov [Schleife1],4
!Mix1:
!xor eax,eax
!mov ecx,eax
!mov edx,ebx
!shl dl,1
!jnc @f
!xor dl,1bh
!@@:
!ror ebx,8
!mov al,bl
!shl ax,1
!mov cl,bl
!xor ax,cx
!or ah,ah
!je @f
!xor al,1bh
!@@:
!xor al,dl
!ror ebx,8
!xor al,bl
!ror ebx,8
!xor al,bl
!mov [edi],al
!inc edi
!ror ebx,16
!dec [Schleife1]
!jnz Mix1
!add esi,4
!dec [Schleife2]
!jnz Mix2
;--------------------------------------
;- 5.Schritt AddRoundKey --------------
;- Ergebnis steht in Array1 für Schleife
!mov edi,[v_Array2]
!NoMix:
!mov ebx,[v_Array1]
!lea esi,[RoundKey]
!add esi,[Round]
!mov ecx,4
!@@:
!mov eax,[esi]
!mov edx,[edi]
!xor eax,edx
!mov [ebx],eax
!add ebx,4
!add edi,4
!add esi,4
!dec ecx
!jnz @b
!add [Round],16 ;Zeiger in RoundKey
!cmp [Round],160
!jne Schleife
;--------------------------------------
;- Anzeige des Ergebnisses
For k=0 To 15
X.c=PeekB(Array1+k)
X$=Hex(X)
Erg$+RSet(X$,2,"0")
Next
MessageRequester("AES-Rijndael-Encryption-Test", "Die verschlüsselten 16 Bytes lauten : "+Erg$)
End
;--------
!section '.data' Data readable writeable
!State: ;16 Bytes des zu verschlüsselnden Textes
!DB 32h, 43h, 0F6h, 0A8h, 88h, 5Ah, 30h, 8Dh, 31h, 31h, 98h, 0A2h, 0E0h, 37h, 07h, 34h
!CipherKey: ;das Passwort (16 Bytes)
!DB 2Bh, 7Eh, 15h, 16h, 28h, 0AEh, 0D2h, 0A6h, 0ABh, 0F7h, 15h, 88h, 09h, 0CFh, 4Fh, 3Ch
!SBox:
!DB 63h, 7ch, 77h, 7bh, 0f2h, 6bh, 6fh, 0c5h, 30h, 01h, 67h, 2bh, 0feh, 0d7h, 0abh, 76h
!DB 0cah, 82h, 0c9h, 7dh, 0fah, 59h, 47h, 0f0h, 0adh, 0d4h, 0a2h, 0afh, 9ch, 0a4h, 72h, 0c0h
!DB 0b7h, 0fdh, 93h, 26h, 36h, 3fh, 0f7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B 8ch, 0a1h, 89h, 0dh, 0bfh, 0e6h, 42h, 68h, 41h, 99h, 2dh, 0fh, 0b0h, 54h, 0bbh, 16h
!RCon:
!DD 1h, 2h, 4h, 8h, 10h, 20h, 40h, 80h, 1bh, 36h
!RoundKey RB 160
!Round DD 0 ;spielt den Rundenzähler
!RConZ DD 0
!Schleife1 DD ?
!Schleife2 DD ?