;void RC4D (unsigned char *buffer, int len, rc4Record *key) ; ; Key shall be expanded in the rc4Record using a traditional ARC4 ; algorithm key expansion algorithm used in cryptlib. ; For Peter Gutmann's source of it search the Web for rc4ExpandKey. ; ; rc4Record ; { ; unsigned char state[256]; ; unsigned char x; ; unsigned char y; ; } ; ; Note the alignment and other assumptions: ; 1) Encryption done in-place ; 2) len shall be even ; 3) rc4Record shall be aligned to 256-byte boundary ; ; Register usage (explanations are in terms of Gutmann's ARC4 implementation): ; eax == state+x ; ebx == state+y ; edx == state+tmp ; ecx - scratch ; esi == source & destination (we need in-place anyhow) ; edi == loop count (1/2 the bytes) RC4Dima: push eax push ebx push ecx push edx push esi push edi mov esi, [esp + 28] ; esi := buffer mov edi, [esp + 32] ; edi := len sar edi, 1 ; edi /= 2 mov eax, [esp + 36] ; eax := key->state mov ebx, eax ; ebx := key->state mov edx, eax ; edx := key->state mov al, [ebx + 100h] ; eax := key->state + key->x mov bl, [ebx + 101h] ; ebx := key->state + key->y inc al ; Pre-execution for x++ ; while( size -- ) ; { ; x ++; again: ; tmp = rc4->state[ x ]; mov dl, [eax] ; y += tmp; add bl, dl ; rc4->state[ x ] = rc4->state[ y ]; mov cl, [ebx] ; rc4->state[ y ] = tmp; mov [ebx], dl mov [eax], cl ; Delayed state[x] assignment ; tmp += rc4->state[ x ]; add dl, cl ; *dst ++ ^= rc4->state[ tmp ] ; mov ch, [edx] ; postpone XOR for the future, just place rvalue in ch ; <<< repeat again, only place result into cl this time >>> ; again: ; x ++; inc al ; tmp = rc4->state[ x ]; mov dl, [eax] ; y += tmp; add bl, dl ; rc4->state[ x ] = rc4->state[ y ]; mov cl, [ebx] mov [ebx], dl ; Pre-execution of state[y] assignment mov [eax], cl ; rc4->state[ y ] = tmp; ; tmp += rc4->state[ x ]; add dl, cl ; *dst ++ = *src ++ ^ rc4->state[ tmp ]; mov cl, [edx] ; postpone XOR for the future, just rvalue in cl ; <<< end of repeat, now xor two bytes >>> mov dl,[esi] xor ch,dl mov [esi],ch mov dl,[esi+1] xor cl, dl inc al mov [esi+1],cl add esi,2 dec edi jnz again pop edi pop esi pop edx pop ecx pop ebx pop eax ret