RC6 (TM) sample code

New Message Reply About this list Date view Thread view Subject view Author view

Anonymous (nobody@replay.com)
Fri, 19 Jun 1998 23:44:01 +0200


-----BEGIN PGP SIGNED MESSAGE-----

/* rc6 (TM)
 * Unoptimized sample implementation of Ron Rivest's submission to the
 * AES bakeoff.
 *
 * Salvo Salasio, 18 June 1998
 *
 * Intellectual property notes: The name of the algorithm (RC6) is
 * trademarked; any property rights to the algorithm or the trademark
 * should be discussed with discussed with the authors of the defining
 * paper "The RC6(TM) Block Cipher": Ronald L. Rivest (MIT),
 * M.J.B. Robshaw (RSA Labs), R. Sidney (RSA Labs), and Y.L. Yin (RSA Labs),
 * distributed 18 June 1998 and available from the lead author's web site.
 *
 * This sample implementation is placed in the public domain by the author,
 * Salvo Salasio. The ROTL and ROTR definitions were cribbed from RSA Labs'
 * RC5 reference implementation.
 */

#include <stdio.h>

/* RC6 is parameterized for w-bit words, b bytes of key, and
 * r rounds. The AES version of RC6 specifies b=16, 24, or 32;
 * w=32; and r=20.
 */
  
#define w 32 /* word size in bits */
#define r 20 /* based on security estimates */

#define P32 0xB7E15163 /* Magic constants for key setup */
#define Q32 0x9E3779B9

/* derived constants */
#define bytes (w / 8) /* bytes per word */
#define c ((b + bytes - 1) / bytes) /* key in words, rounded up */
#define R24 (2 * r + 4)
#define lgw 5 /* log2(w) -- wussed out */

/* Rotations */
#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)))))

unsigned int S[R24 - 1]; /* Key schedule */

void rc6_key_setup(unsigned char *K, int b)
{
        int i, j, s, v;
        unsigned int L[(32 + bytes - 1) / bytes]; /* Big enough for max b */
        unsigned int A, B;

        L[c - 1] = 0;
        for (i = b - 1; i >= 0; i--)
                L[i / bytes] = (L[i / bytes] << 8) + K[i];

        S[0] = P32;
        for (i = 1; i <= 2 * r + 3; i++)
                S[i] = S[i - 1] + Q32;

        A = B = i = j = 0;
        v = R24;
        if (c > v) v = c;
        v *= 3;

        for (s = 1; s <= v; s++)
        {
                A = S[i] = ROTL(S[i] + A + B, 3);
                B = L[j] = ROTL(L[j] + A + B, A + B);
                i = (i + 1) % R24;
                j = (j + 1) % c;
        }
}

void rc6_block_encrypt(unsigned int *pt, unsigned int *ct)
{
        unsigned int A, B, C, D, t, u, x;
        int i, j;

        A = pt[0];
        B = pt[1];
        C = pt[2];
        D = pt[3];
        B += S[0];
        D += S[1];
        for (i = 2; i <= 2 * r; i += 2)
        {
                t = ROTL(B * (2 * B + 1), lgw);
                u = ROTL(D * (2 * D + 1), lgw);
                A = ROTL(A ^ t, u) + S[i];
                C = ROTL(C ^ u, t) + S[i + 1];
                x = A;
                A = B;
                B = C;
                C = D;
                D = x;
        }
        A += S[2 * r + 2];
        C += S[2 * r + 3];
        ct[0] = A;
        ct[1] = B;
        ct[2] = C;
        ct[3] = D;
}

int
main()
{
        /* test keys */
        unsigned char key1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char key2[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                                 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78};
        unsigned char key3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char key4[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                                 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
                                0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0};
        unsigned char key5[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char key6[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                                 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
                                0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
                                0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe};

        unsigned int pt1[] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
        unsigned int pt2[] = {0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce};

        unsigned int ct[4];
        int i;

        rc6_key_setup(key1, sizeof(key1));
        rc6_block_encrypt(pt1, ct);
        printf("Test 1: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: 36a5c38f 78f7b156 4edf29c1 1ea44898\n\n");

        rc6_key_setup(key2, sizeof(key2));
        rc6_block_encrypt(pt2, ct);
        printf("Test 2: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: 2f194e52 23c61547 36f6511f 183fa47e\n\n");

        rc6_key_setup(key3, sizeof(key3));
        rc6_block_encrypt(pt1, ct);
        printf("Test 3: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: cb1bd66c 38300b19 163f8a4e 82ae9086\n\n");

        rc6_key_setup(key4, sizeof(key4));
        rc6_block_encrypt(pt2, ct);
        printf("Test 4: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: d0298368 0405e519 2ae9521e d49152f9\n\n");

        rc6_key_setup(key5, sizeof(key5));
        rc6_block_encrypt(pt1, ct);
        printf("Test 5: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: 05bd5f8f a85fd110 da3ffa93 c27e856e\n\n");

        rc6_key_setup(key6, sizeof(key6));
        rc6_block_encrypt(pt2, ct);
        printf("Test 6: ");
        for (i = 0; i < 4; i++) printf(" %08x", ct[i]);
        printf("\nShould be: 161824c8 89e4d7f0 a116ad20 485d4e67\n");

        return 0;
}

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQEPAwUBNYqpOcNH+A3/////AQGXuQfMC1h9ckKf8pdran5ujOJPe596NY6/Z6es
C2cpwbeWlTCbD4V94ze4UzUGvK/RPuG9/2p3IxZBtTvFXX8pdq7tW0XELNOSVe12
suDBybFZAst1STZdOHNiHuEaBaChQrv7qkLiN2dEJgKJbEM21FcUyOslC+ciNr8I
5WKCo1vvAnWsCyiuCekPqKVdzTBmwk+9SbyYjNeCNxxrKRUjwZvKk3MirM8PlDXF
2PmEjCAXKSw2pPMf5oP/3qyWVaB0tqjmhV6SNSd3sZ4jhLhti8Wd4rRKeVGwNd1+
TmlLXy5A3DxoIsypXw3qscSPvyaKM3KP/hkm0HXKvDv/Jw==
=Tk6B
-----END PGP SIGNATURE-----


New Message Reply About this list Date view Thread view Subject view Author view

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

The following archive was created by hippie-mail 7.98617-22 on Fri Aug 21 1998 - 17:18:46 ADT