RC5 encryption and decryption both expand the random key into 2(r+1) words that will be used sequentially (and only once each) during the encryption and decryption processes. All of the below comes from Rivest's revised paper on RC5.
Key expansion The key expansion algorithm is illustrated below, first in
pseudocode, then example
C code copied directly from the reference paper's appendix. Following the naming scheme of the paper, the following variable names are used: • – The length of a word in bits, typically 16, 32 or 64. Encryption is done in 2-word blocks. • – The length of a word in bytes. • – The length of the key in bytes. • – The key, considered as an array of bytes (using 0-based indexing). • – The length of the key in words (or 1, if b = 0). • – A temporary working array used during key scheduling, initialized to the key in words. • – The number of rounds to use when encrypting data. • – the number of round subkeys required. • – The round subkey words. • – The first magic constant, defined as , where is the nearest odd integer to the given input, is the
base of the natural logarithm, and is defined above. For common values of , the associated values of are given here in hexadecimal: • For
w = 16: 0xB7E1 • For
w = 32: 0xB7E15163 • For
w = 64: 0xB7E151628AED2A6B • – The second magic constant, defined as , where is the nearest odd integer to the given input, where is the
golden ratio, and is defined above. For common values of , the associated values of are given here in hexadecimal: • For
w = 16: 0x9E37 • For
w = 32: 0x9E3779B9 • For
w = 64: 0x9E3779B97F4A7C15 • Break K into words • u = w / 8 c = ceiling(max(b, 1) / u) • L is initially a c-length list of 0-valued w-length words for i = b-1 down to 0 do: L[i / u] = (L[i / u] The example source code is provided from the appendix of Rivest's paper on RC5. The implementation is designed to work with w = 32, r = 12, and b = 16. void RC5_SETUP(unsigned char *K) { // w = 32, r = 12, b = 16 // c = max(1, ceil(8 * b/w)) // t = 2 * (r+1) WORD i, j, k, u = w/8, A, B, L[c]; for (i = b-1, L[c-1] = 0; i != -1; i--) L[i/u] = (L[i/u]
Encryption Encryption involved several rounds of a simple function, with 12 or 20 rounds seemingly recommended, depending on security needs and time considerations. Beyond the variables used above, the following variables are used in this algorithm: • A, B - The two words composing the block of
plaintext to be encrypted. A = A + S[0] B = B + S[1] for i = 1 to r do: A = ((A ^ B) The example C code given by Rivest is this. void RC5_ENCRYPT(WORD *pt, WORD *ct) { WORD i, A = pt[0] + S[0], B = pt[1] + S[1]; for (i = 1; i
Decryption Decryption is a fairly straightforward reversal of the encryption process. The below pseudocode shows the process. for i = r down to 1 do: B = ((B - S[2 * i + 1]) >>> A) ^ A A = ((A - S[2 * i]) >>> B) ^ B B = B - S[1] A = A - S[0] return A, B The example C code given by Rivest is this. void RC5_DECRYPT(WORD *ct, WORD *pt) { WORD i, B=ct[1], A=ct[0]; for (i = r; i > 0; i--) { B = ROTR(B - S[2*i + 1], A) ^ A; A = ROTR(A - S[2*i], B) ^ B; } pt[1] = B - S[1]; pt[0] = A - S[0]; } ==Cryptanalysis==