rc5.inc
			
			上传用户:daicanfen
			上传日期:2009-08-02
			资源大小:1k
			文件大小:3k
			
			
			
			
		 
		
		
		
			
			
									
					
						
							
																- comment		*
- Algorithm		: RC5 Encryption Algorithm ( Block Cipher )
- Block		: 8bytes
- KeySize		: --
- Round		: between 12 and 32
- Usage		: invoke	rc5_setkey,addr ptrIndata,ptrIndata_length	( Setkey )
-  		  invoke    rc5_encrypt,addr ptrIndata,addr ptrOutdata	( Encrypt )
-  		  invoke	rc5_decrypt,addr ptrIndata,addr ptrOutdata	( Decrypt )
-  		  
-  Coded by x3chun	( 2004.04.20 ) 
-  		( x3chun@korea.com  or  x3chun@hanyang.ac.kr )  ( http://x3chun.wo.to )
-  		
-  comment		*
- encR		macro	A,B,key
- 		mov	ecx,B
- 		xor	A,B
- 		and	ecx,1Fh
- 		rol	A,cl
- 		add	A,key
- 		add	edi,4
- 		mov	ecx,A
- 		xor	B,A
- 		and	ecx,1Fh
- 		rol	B,cl
- 		add	B,[edi]
- 		add	edi,4
- endm
- decR		macro	A,B,key
- 		sub	B,[edi]
- 		mov	ecx,A
- 		and	ecx,1Fh
- 		ror	B,cl
- 		xor	B,A
- 		sub	edi,4
- 		sub	A,[edi]
- 		mov	ecx,B
- 		and	ecx,1Fh
- 		ror	A,cl
- 		xor	A,B
- 		sub	edi,4
- endm
- 		
- rc5_setkey	proto	:DWORD, :DWORD
- rc5_encrypt	proto	:DWORD, :DWORD
- rc5_decrypt	proto	:DWORD, :DWORD
- .const
- noRounds		equ	12	; ( 12<= noRounds <=32 )
- RC5_EXPANDED_KEYSIZE	equ	64*4
- P32_MAGIC		equ	0B7E15163h
- .data?
- rc5keytable		db	RC5_EXPANDED_KEYSIZE	dup(?)
- initkey			db	RC5_EXPANDED_KEYSIZE	dup(?)
- _looprc5			dd	?
- _c			dd	?
- _A			dd	?
- .code
- rc5_setkey	proc	ptrIndata:DWORD, ptrIndata_length:DWORD
- 		pushad
- 		mov	edx,[esp+2ch]
- 		lea	edx,[edx+3]
- 		sar	edx,2			; c
- 		mov	_c,edx
- 		mov	ebx,(noRounds*2+02)	; t
- 		cmp	edx,ebx
- 		ja	@_r1
- 		lea	ebp, [ebx*2+ebx]
- 		mov	_looprc5,ebp
- 		jmp	@_r2
- @_r1:
- 		lea	ebp,[edx*2+edx]
- 		mov	_looprc5,ebp
- @_r2:
- 		mov	esi,[esp+28h]
- 		mov	edi,offset rc5keytable
- 		mov	ecx,[esp+2ch]
- 		rep	movsb
- 		
- 		mov	edi,offset initkey
- 		mov	dword ptr [edi],P32_MAGIC
- @_r3:
- 		mov	eax,dword ptr [edi+ecx*4]
- 		sub	eax,61C88647h
- 		mov	[edi+ecx*4+4],eax
- 		inc	ecx
- 		cmp	ecx,(RC5_EXPANDED_KEYSIZE/4-1)
- 		jl	@_r3
- 		
- 		xor	eax,eax			; i
- 		xor	ecx,ecx
- 		xor	esi,esi			; j
- 		xor	ebp,ebp			; B
- 		mov	_A,ebp			; A
- @_r4:
- 		mov	ecx,[edi+eax*4]
- 		add	ecx,ebp
- 		add	ecx,_A
- 		rol	ecx,3
- 		mov	_A,ecx
- 		mov	[edi+eax*4],ecx			; A
- 		add	ebp,ecx
- 		mov	ecx,ebp
- 		and	ecx,1Fh
- 		add	ebp,dword ptr [rc5keytable+esi*4]
- 		rol	ebp,cl
- 		mov	dword ptr [rc5keytable+esi*4],ebp	; B
- 		lea	eax,[eax+1]
- 		cdq
- 		idiv	ebx
- 		mov	ecx,edx
- 		lea	eax,[esi+1]
- 		cdq
- 		idiv	_c
- 		mov	esi,edx
- 		mov	eax,ecx
- 		dec	_looprc5
- 		jnz	@_r4		
- 		popad
- 		ret
- 		
- rc5_setkey	endp
- rc5_encrypt	proc	ptrIndata:DWORD, ptrOutdata:DWORD
- 		pushad
- 		mov	esi,[esp+28h]
- 		mov	edi,offset initkey
- 		mov	eax,[esi]
- 		mov	ebx,[esi+4]
- 		add	eax,[edi]
- 		add	ebx,[edi+4]
- 		mov	edi,offset initkey+8
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		encR	eax,ebx,[edi]
- 		mov	edx,[esp+2ch]
- 		mov	[edx],eax
- 		mov	[edx+4],ebx
- 		popad
- 		ret
- rc5_encrypt	endp
- rc5_decrypt	proc	ptrIndata:DWORD, ptrOutdata:DWORD
- 		pushad
- 		mov	esi,[esp+28h]
- 		mov	edi,offset initkey
- 		mov	eax,[esi]
- 		mov	ebx,[esi+4]
- 		mov	edi,offset initkey+(noRounds*8+4)
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		decR	eax,ebx,[edi]
- 		sub	eax,[edi-4]
- 		sub	ebx,[edi]
- 		mov	edx,[esp+2ch]
- 		mov	[edx],eax
- 		mov	[edx+4],ebx	
- 		popad
- 		ret
- 		
- rc5_decrypt	endp