RC5RC5 has a variable block size (32, 64 or 128 bits), a variable key size (0 to 2040 bits) and variable number of rounds (0 to 255). In the following we use 32-bit blocks, 12 rounds and a 16 byte key [paper]: |
Test vectors
RC5-32/12/16 examples: 1. key = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 plaintext 00000000 00000000 ---> ciphertext EEDBA521 6D8F4B15 2. key = 91 5F 46 19 BE 41 B2 51 63 55 A5 01 10 A9 CE 91 plaintext EEDBA521 6D8F4B15 ---> ciphertext AC13C0F7 52892B5B 3. key = 78 33 48 E7 5A EB 0F 2F D7 B1 69 BB 8D C1 67 87 plaintext AC13C0F7 52892B5B ---> ciphertext B7B3422F 92FC6903 4. key = DC 49 DB 13 75 A5 58 4F 64 85 B4 13 B5 F1 2B AF plaintext B7B3422F 92FC6903 ---> ciphertext B278C165 CC97D184 5. key = 52 69 F1 49 D4 1B A0 15 24 97 57 4D 7F 15 31 25 plaintext B278C165 CC97D184 ---> ciphertext 15E444EB 249831DA
Outline
The following provides some outline code:
/* RC5REF.C -- Reference implementation of RC5-32/12/16 in C. */ /* Copyright (C) 1995 RSA Data Security, Inc. */ #include stdio.h #include time.h #include string.h #include stdlib.h typedef unsigned long int WORD; /* Should be 32-bit = 4 bytes */ #define w 32 /* word size in bits */ #define r 12 /* number of rounds */ #define b 16 /* number of bytes in key */ #define c 4 /* number words in key = ceil(8*b/w)*/ #define t 26 /* size of table S = 2*(r+1) words */ WORD S[t]; /* expanded key table */ WORD P = 0xb7e15163, Q = 0x9e3779b9; /* magic constants */ /* Rotation operators. x must be unsigned, to get logical right shift*/ #define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1))))) #define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1))))) void RC5_ENCRYPT(WORD *pt, WORD *ct) /* 2 WORD input pt/output ct */ { WORD i, A=pt[0]+S[0], B=pt[1]+S[1]; for (i=1; i<=r; i++) { A = ROTL(A^B,B)+S[2*i]; B = ROTL(B^A,A)+S[2*i+1]; } ct[0] = A; ct[1] = B; } void RC5_DECRYPT(WORD *ct, WORD *pt) /* 2 WORD input ct/output 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]; } void RC5_SETUP(unsigned char *K) /* secret input key K[0...b-1] */ { WORD i, j, k, u=w/8, A, B, L[c]; /* Initialize L, then S, then mix key into S */ for (i=b-1,L[c-1]=0; i!=-1; i--) L[i/u] = (L[i/u]<<8)+K[i]; for (S[0]=P,i=1; i<t; i++) S[i] = S[i-1]+Q; for (A=B=i=j=k=0; k<3*t; k++,i=(i+1)%t,j=(j+1)%c) { A = S[i] = ROTL(S[i]+(A+B),3); B = L[j] = ROTL(L[j]+(A+B),(A+B)); } } void main(int argc, char *argv[]) { WORD i, j, pt1[2], pt2[2], ct[2] = {0,0}; unsigned char key[b]; unsigned char val1[100]; unsigned char val2[100]; time_t t0, t1; if (sizeof(WORD)!=4) printf("RC5-32/12/16\n"); pt1[0]=ct[0]; pt1[1]=ct[1]; for (j=0;j<b;j++) key[j] = 1; char *key1 = "00000000000000000000000000"; strcpy(val1, "00000000"); strcpy(val2 , "00000000"); if (argc>1) key1=argv[1]; if (argc>2) strcpy(val1, argv[2]); if (argc>3) strcpy(val2, argv[3]); pt1[0] = (int)strtol(val1, NULL, 16); pt1[1] = (int)strtol(val2, NULL, 16); unsigned int bytearray[12]; int str_len = strlen(key1); for (i = 0; i < (str_len / 2); i++) { sscanf(key1 + 2 * i, "%02x", &key[i]); } /* Setup, encrypt, and decrypt */ RC5_SETUP(key); RC5_ENCRYPT(pt1,ct); RC5_DECRYPT(ct,pt2); /* Print out results, checking for decryption failure */ for (j = 0; j<str_len/2; j++) printf("%.2X ", key[j]); printf("\n plaintext %.8lX %.8lX ---> ciphertext %.8lX %.8lX \n", pt1[0], pt1[1], ct[0], ct[1]); if (pt1[0]!=pt2[0] || pt1[1]!=pt2[1]) printf("Decryption Error!"); }