RC4 implementation produce wrong test vectors
Posted: Tue Feb 28, 2017 11:22 pm
Hi,
I need some help on the following RC4 implementation.
Whenever I call the program like
dd if=/dev/zero bs=1 count=128 | ./rc4_stdio_pb 0102030405 | xxd -g 1
I get
but according to https://tools.ietf.org/html/rfc6229 it should read
key: 0x0102030405
This is the actual source code
Anyone any idea where the fault is?
Thanks,
Karl-Uwe
I need some help on the following RC4 implementation.
Whenever I call the program like
dd if=/dev/zero bs=1 count=128 | ./rc4_stdio_pb 0102030405 | xxd -g 1
I get
Code: Select all
b2 39 63 05 f0 3d c0 27 cc c3 52 4a 0a 11 18 a8
69 82 94 4f 18 fc 82 d5 89 c4 03 a4 7a 0d 09 19
b6 8f 78 c2 8d 15 d8 22 23 4c 6c b8 c5 f6 c4 ac
b0 b9 96 a4 13 fe da 54 28 b0 78 86 b2 a0 85 12
56 a0 85 25 07 80 58 d0 73 35 4a 84 76 ff e7 72
61 e4 f3 71 0f 03 0b 0c 14 89 69 4b b9 89 28 17
65 92 a5 e2 2f 3e 85 11 35 dc 1e 6a b1 24 2b 8c
27 75 04 30 e3 1c 13 5d c8 39 98 de 2d 93 0f 3e
key: 0x0102030405
Code: Select all
b2 39 63 05 f0 3d c0 27 cc c3 52 4a 0a 11 18 a8
69 82 94 4f 18 fc 82 d5 89 c4 03 a4 7a 0d 09 19
28 cb 11 32 c9 6c e2 86 42 1d ca ad b8 b6 9e ae
1c fc f6 2b 03 ed db 64 1d 77 df cf 7f 8d 8c 93
42 b7 d0 cd d9 18 a8 a3 3d d5 17 81 c8 1f 40 41
64 59 84 44 32 a7 da 92 3c fb 3e b4 98 06 61 f6
ec 10 32 7b de 2b ee fd 18 f9 27 76 80 45 7e 22
eb 62 63 8d 4f 0b a1 fe 9f ca 20 e0 5b f8 ff 2b
Code: Select all
Macro ub: a: EndMacro ;unsigned byte and word
Global Dim KeyWord.ub(256)
Global KeyLen.ub
Global Dim x.ub(256)
Global a.ub, b.ub
Declare KSA()
Declare PRGA()
Procedure KSA()
Define i.ub, j.ub, k.ub
; Prefill the Arrays
For i = 0 To 255
x(i) = i
Next
For i = 0 To 255
k = (i % KeyLen)
j = (j + x(i) + KeyWord(k))
Swap x(i), x(j)
Next
; Reset the Array Indices Start Point
a = 0
b = 0
EndProcedure
Procedure PRGA()
Define t.ub
; Update the Global Carry on Array Indices
a = a +1;
b = b + x(a)
Swap x(a), x(b)
; xor Plaintext/Ciphertext
t = (x(a) + x(b))
ProcedureReturn x(t)
EndProcedure
Procedure ShowUsage(ThisName.s)
ConsoleError("Usage : "+ ThisName.s +" Key(in Hex) <in >out")
ConsoleError("Example: "+ ThisName.s +" 7d0ef66789aca2cfa6c76db7560554 <Plainfile >CipherFile")
ConsoleError(" "+ ThisName.s +" $(echo -en 'egN99T8eK6peC2UC' | md5) < Plainfile > CipherFile")
ConsoleError(" cat CipherFile | "+ ThisName.s +" 7d0ef66789aca2cfa6c76db7560554 > Plainfile")
EndProcedure
;-----------------------------------------
; Main
;
OpenConsole()
If (CountProgramParameters() < 1) ; Check If a Parameter are passed through
PrgName.s = "rc4_stdio_pb"
ShowUsage(PrgName.s)
CloseConsole()
EndIf
KeyHash.s = ProgramParameter(0) ; Read the Encryption Keyword
KeyLen = (Len(KeyHash) /2)
For i = 0 To KeyLen-1
KeyWord(i) = Val("$"+Mid(KeyHash.s, (i*2)+1, 2)) ; convert hex keyhash into keyword
Next
KSA() ; perform the Key Schedule
;-----------------------------------------
; Encrypt/Decrypt Binary File(buffered Read/Write)
;
#BLOCKSIZE = 65535*8
Define *inBuffer = AllocateMemory(#BLOCKSIZE)
Define *outBuffer = AllocateMemory(#BLOCKSIZE)
If (*inBuffer) And (*outBuffer)
Repeat
byteRead.i = ReadConsoleData(*inBuffer, #BLOCKSIZE)
If (byteRead)
For i = 0 To byteRead-1
PokeB(*outBuffer+i, (PeekB(*inBuffer+i) ! PRGA()))
Next
WriteConsoleData(*outBuffer, byteRead)
EndIf
Until (byteRead = 0)
FreeMemory(*inBuffer)
FreeMemory(*outBuffer)
Else
PrintN("Error allocating memory")
EndIf
CloseConsole()
Thanks,
Karl-Uwe