Encryption Algorithm Implementations
====================================

DISCLAIMER:
This implementation was developed totally outside of the USA and so it is free
from export restrictions. However if you have laws in your country which prevent
the use of strong cryptographical products then please remove this archive from
your computer. I retain no responsibly for this implementation.

This implementation is FREEWARE, you may use this implementation freely. You
have permission to modify any of the files included in this archive provided you
do not redistribute these files. If you wish to redistribute these 
implementations you may do so provided all the files in this archive are kept
intact and unmodified. If you do redistribute these files please email me and 
tell me (if you upload to a BBS/web site/ftp site etc), this is not a requirement
but it is nice to know who is using these implementations.

For the lastest updates/information try
http://web.ukonline.co.uk/david.w32/delphi.html
or email davebarton@bigfoot.com


Algorithm Details
-----------------
Name        RC5 block cipher
Author      Ron Rivest
Patented    Yes, see RSA Labs for licensing (http://www.rsa.com)
Block size  64bit
Key size    Variable - upto 2048bit
Modes       ECB, CBC, CFB 8bit, OFB, OFB counter 8bit


Procedures
----------
function RC5SelfTest: Boolean;
  Performs a self test
procedure RC5Init;
  Initializes a TRC5Data record with key information
procedure RC5Burn;
  Clears a TRC5Data record of any key information
procedure RC5Reset;
  Resets any chaining mode information (needed for CBC, CFB, OFB, OFBC)
  
procedure RC5EncryptECB;
  Encrypts the data in a 64bit block using the ECB mode
procedure RC5DecryptECB;
  Decrypts the data in a 64bit block using the ECB mode

procedure RC5EncryptCBC;
  Encrypts the data in a 64bit block using the CBC chaining mode 
procedure RC5DecryptCBC;
  Decrypts the data in a 64bit block using the CBC chaining mode 
procedure RC5EncryptOFB;
  Encrypts the data in a 64bit block using the OFB chaining mode 
procedure RC5DecryptOFB;
  Decrypts the data in a 64bit block using the OFB chaining mode 
procedure RC5EncryptCFB;
  Encrypts Len bytes of data using the CFB chaining mode 
procedure RC5DecryptCFB;
  Decrypts Len bytes of data using the CFB chaining mode 
procedure RC5EncryptOFBC;
  Encrypts Len bytes of data using the OFB counter chaining mode
procedure RC5DecryptOFBC;
  Decrypts Len bytes of data using the OFB counter chaining mode
   
 
Usage
-----
Before usage I recommend that you call the SelfTest function to test that the
implementation is performing correctly.

Before you can use the encryption routines you must call Init to perform the
keysetup routines, if you are planning on using any of the chaining modes 
(CBC, CFB, OFB, OFBC) then you need to supply an initialization vector (IV) which
is the same size as the block size (64bit). The IV is just a block of data used
to initialize the chaining modes - it doesn't have to be kept secret.

If you only want to use the ECB encryption mode you can then just call the 
encryption and decryption routines as you want, to encrypt data in blocks.

If you want to use the chaining modes (which hides patterns in the data) you must
call the Reset procedure after a series of encryptions/decryptions.

eg.
procedure EncryptAndDecrypt;
const
  Key: array[0..7] of byte= ($11, $22, $33, $44, $55, $66, $77, $88);
  IV: array[0..7] of byte= ($11, $22, $33, $44, $55, $66, $77, $88);
var
  Data: array[1..8192] of byte;
  i: integer;
  KeyData: TRC5Data;
begin
  RC5Init(KeyData,@Key,Sizeof(Key),@IV);
  for i:= 1 to (8192 div 8) do
    RC5EncryptCBC(KeyData,@Data[(i-1)*8],@Data[(i-1)*8]);
  RC5Reset(KeyData);
  for i:= 1 to (8192 div 8) do
    RC5DecryptCBC(KeyData,@Data[(i-1)*8],@Data[(i-1)*8]);
  RC5Reset(KeyData);   // not really necessary but just to demonstrate
  RC5Burn;
end;

Finally you should always call Burn.


Notes On Encryption Modes
-------------------------
ECB, CBC, OFB: These modes encrypt data in blocks of 64bits (8bytes)
CFB, OFBC: These modes encrypt data in blocks of 8bits (1byte)


I hope you find this implementation useful!

Dave
davebarton@bigfoot.com
http://web.ukonline.co.uk/david.w32/delphi.html

Copyright (c) 1998 David Barton
