RC4 implementation produce wrong test vectors

Just starting out? Need help? Post your questions and find answers here.
Karl-Uwe Frank
User
User
Posts: 17
Joined: Sat Sep 03, 2011 12:33 am

RC4 implementation produce wrong test vectors

Post by Karl-Uwe Frank »

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

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
but according to https://tools.ietf.org/html/rfc6229 it should read

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
This is the actual source code

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()
Anyone any idea where the fault is?

Thanks,
Karl-Uwe
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RC4 implementation produce wrong test vectors

Post by infratec »

Hi,

at home I don't use linux, but you can try this:

Code: Select all

t = x((x(a) + x(b)) % 256)
instead of

Code: Select all

t = (x(a) + x(b))
Bernd
Karl-Uwe Frank
User
User
Posts: 17
Joined: Sat Sep 03, 2011 12:33 am

Re: RC4 implementation produce wrong test vectors

Post by Karl-Uwe Frank »

Thanks Bernd, but due to the fact that the array x as well as a, b and t are defined as 8bit unsigned integers the compiler does the mod 256 automatically.

The problem was that I missed the skipping of rows in the official test vectors.

the command

dd if=/dev/zero bs=1 count=256 | ./rc4_stdio_pb 0102030405 | xxd -g 1

does produce the correct test vector output, but including what was skip in the official test vectors( 0000020: to 00000e0:)

Code: Select all

0000000: b2 39 63 05 f0 3d c0 27 cc c3 52 4a 0a 11 18 a8  .9c..=.'..RJ....
0000010: 69 82 94 4f 18 fc 82 d5 89 c4 03 a4 7a 0d 09 19  i..O........z...
0000020: b6 8f 78 c2 8d 15 d8 22 23 4c 6c b8 c5 f6 c4 ac  ..x...."#Ll.....
0000030: b0 b9 96 a4 13 fe da 54 28 b0 78 86 b2 a0 85 12  .......T(.x.....
0000040: 56 a0 85 25 07 80 58 d0 73 35 4a 84 76 ff e7 72  V..%..X.s5J.v..r
0000050: 61 e4 f3 71 0f 03 0b 0c 14 89 69 4b b9 89 28 17  a..q......iK..(.
0000060: 65 92 a5 e2 2f 3e 85 11 35 dc 1e 6a b1 24 2b 8c  e.../>..5..j.$+.
0000070: 27 75 04 30 e3 1c 13 5d c8 39 98 de 2d 93 0f 3e  'u.0...].9..-..>
0000080: cf 51 67 0e 04 0b 5d 98 60 5c fa c5 5b 6e 7b 25  .Qg...].`\..[n{%
0000090: fa 45 19 e1 b3 5a bf 3e 54 21 87 3b b4 73 ab 77  .E...Z.>T!.;.s.w
00000a0: 73 3f 31 64 df 75 89 53 57 52 ae ea 32 1a cb db  s?1d.u.SWR..2...
00000b0: ef ea 52 aa 04 11 ef 3d 0a 19 8c 43 0e 98 18 de  ..R....=...C....
00000c0: 40 02 73 a8 7e 0e b4 97 6a d0 2d 5e 06 72 23 c4  @.s.~...j.-^.r#.
00000d0: a4 07 2f 2c a1 6e c8 b9 3f ab a9 6e 78 3f 8d 28  ../,.n..?..nx?.(
00000e0: 77 bc 64 18 be fa 92 ca cc f9 6c 2f 77 b5 cd c3  w.d.......l/w...
00000f0: 28 cb 11 32 c9 6c e2 86 42 1d ca ad b8 b6 9e ae  (..2.l..B.......
Post Reply