Exemplo n.º 1
0
static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                               const unsigned char *iv, int enc)
{
    RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
                   key, data(ctx)->rounds);
    return 1;
}
Exemplo n.º 2
0
/* 
 * Standard CSPContext init, called from CSPFullPluginSession::init().
 * Reusable, e.g., query followed by en/decrypt.
 */
void RC5Context::init( 
	const Context &context, 
	bool encrypting)
{
	CSSM_SIZE	keyLen;
	uint8 		*keyData 	= NULL;
	uint32		rounds = RC5_16_ROUNDS;
	
	/* obtain key from context */
	symmetricKeyBits(context, session(), CSSM_ALGID_RC5, 
		encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
		keyData, keyLen);
	if((keyLen < RC5_MIN_KEY_SIZE_BYTES) || (keyLen > RC5_MAX_KEY_SIZE_BYTES)) {
		CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
	}
	
	/* 
	 * Optional rounds
	 */
	rounds = context.getInt(CSSM_ATTRIBUTE_ROUNDS);
	if(rounds == 0) {
		/* default */
		rounds = RC5_16_ROUNDS;
	}

	/* init the low-level state */
	RC5_32_set_key(&rc5Key, keyLen, keyData, rounds);

	/* Finally, have BlockCryptor do its setup */
	setup(RC5_BLOCK_SIZE_BYTES, context);
}	
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    int i, n, err = 0;
    RC5_32_KEY key;
    unsigned char buf[8], buf2[8], ivb[8];

    for (n = 0; n < 5; n++) {
        RC5_32_set_key(&key, 16, &(RC5key[n][0]), 12);

        RC5_32_ecb_encrypt(&(RC5plain[n][0]), buf, &key, RC5_ENCRYPT);
        if (memcmp(&(RC5cipher[n][0]), buf, 8) != 0) {
            printf("ecb RC5 error encrypting (%d)\n", n + 1);
            printf("got     :");
            for (i = 0; i < 8; i++)
                printf("%02X ", buf[i]);
            printf("\n");
            printf("expected:");
            for (i = 0; i < 8; i++)
                printf("%02X ", RC5cipher[n][i]);
            err = 20;
            printf("\n");
        }

        RC5_32_ecb_encrypt(buf, buf2, &key, RC5_DECRYPT);
        if (memcmp(&(RC5plain[n][0]), buf2, 8) != 0) {
            printf("ecb RC5 error decrypting (%d)\n", n + 1);
            printf("got     :");
            for (i = 0; i < 8; i++)
                printf("%02X ", buf2[i]);
            printf("\n");
            printf("expected:");
            for (i = 0; i < 8; i++)
                printf("%02X ", RC5plain[n][i]);
            printf("\n");
            err = 3;
        }
    }
    if (err == 0)
        printf("ecb RC5 ok\n");

    for (n = 0; n < RC5_CBC_NUM; n++) {
        i = rc5_cbc_rounds[n];
        if (i < 8)
            continue;

        RC5_32_set_key(&key, rc5_cbc_key[n][0], &(rc5_cbc_key[n][1]), i);

        memcpy(ivb, &(rc5_cbc_iv[n][0]), 8);
        RC5_32_cbc_encrypt(&(rc5_cbc_plain[n][0]), buf, 8,
                           &key, &(ivb[0]), RC5_ENCRYPT);

        if (memcmp(&(rc5_cbc_cipher[n][0]), buf, 8) != 0) {
            printf("cbc RC5 error encrypting (%d)\n", n + 1);
            printf("got     :");
            for (i = 0; i < 8; i++)
                printf("%02X ", buf[i]);
            printf("\n");
            printf("expected:");
            for (i = 0; i < 8; i++)
                printf("%02X ", rc5_cbc_cipher[n][i]);
            err = 30;
            printf("\n");
        }

        memcpy(ivb, &(rc5_cbc_iv[n][0]), 8);
        RC5_32_cbc_encrypt(buf, buf2, 8, &key, &(ivb[0]), RC5_DECRYPT);
        if (memcmp(&(rc5_cbc_plain[n][0]), buf2, 8) != 0) {
            printf("cbc RC5 error decrypting (%d)\n", n + 1);
            printf("got     :");
            for (i = 0; i < 8; i++)
                printf("%02X ", buf2[i]);
            printf("\n");
            printf("expected:");
            for (i = 0; i < 8; i++)
                printf("%02X ", rc5_cbc_plain[n][i]);
            printf("\n");
            err = 3;
        }
    }
    if (err == 0)
        printf("cbc RC5 ok\n");

    EXIT(err);
    return (err);
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
    long count;
    static unsigned char buf[BUFSIZE];
    static unsigned char key[] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };
    RC5_32_KEY sch;
    double a, b, c, d;
#ifndef SIGALRM
    long ca, cb, cc;
#endif

#ifndef TIMES
    printf("To get the most accurate results, try to run this\n");
    printf("program when this computer is idle.\n");
#endif

#ifndef SIGALRM
    printf("First we calculate the approximate speed ...\n");
    RC5_32_set_key(&sch, 16, key, 12);
    count = 10;
    do {
        long i;
        RC5_32_INT data[2];

        count *= 2;
        Time_F(START);
        for (i = count; i; i--)
            RC5_32_encrypt(data, &sch);
        d = Time_F(STOP);
    } while (d < 3.0);
    ca = count / 512;
    cb = count;
    cc = count * 8 / BUFSIZE + 1;
    printf("Doing RC5_32_set_key %ld times\n", ca);
# define COND(d) (count != (d))
# define COUNT(d) (d)
#else
# define COND(c) (run)
# define COUNT(d) (count)
    signal(SIGALRM, sig_done);
    printf("Doing RC5_32_set_key for 10 seconds\n");
    alarm(10);
#endif

    Time_F(START);
    for (count = 0, run = 1; COND(ca); count += 4) {
        RC5_32_set_key(&sch, 16, key, 12);
        RC5_32_set_key(&sch, 16, key, 12);
        RC5_32_set_key(&sch, 16, key, 12);
        RC5_32_set_key(&sch, 16, key, 12);
    }
    d = Time_F(STOP);
    printf("%ld RC5_32_set_key's in %.2f seconds\n", count, d);
    a = ((double)COUNT(ca)) / d;

#ifdef SIGALRM
    printf("Doing RC5_32_encrypt's for 10 seconds\n");
    alarm(10);
#else
    printf("Doing RC5_32_encrypt %ld times\n", cb);
#endif
    Time_F(START);
    for (count = 0, run = 1; COND(cb); count += 4) {
        RC5_32_INT data[2];

        RC5_32_encrypt(data, &sch);
        RC5_32_encrypt(data, &sch);
        RC5_32_encrypt(data, &sch);
        RC5_32_encrypt(data, &sch);
    }
    d = Time_F(STOP);
    printf("%ld RC5_32_encrypt's in %.2f second\n", count, d);
    b = ((double)COUNT(cb) * 8) / d;

#ifdef SIGALRM
    printf("Doing RC5_32_cbc_encrypt on %ld byte blocks for 10 seconds\n",
           BUFSIZE);
    alarm(10);
#else
    printf("Doing RC5_32_cbc_encrypt %ld times on %ld byte blocks\n", cc,
           BUFSIZE);
#endif
    Time_F(START);
    for (count = 0, run = 1; COND(cc); count++)
        RC5_32_cbc_encrypt(buf, buf, BUFSIZE, &sch, &(key[0]), RC5_ENCRYPT);
    d = Time_F(STOP);
    printf("%ld RC5_32_cbc_encrypt's of %ld byte blocks in %.2f second\n",
           count, BUFSIZE, d);
    c = ((double)COUNT(cc) * BUFSIZE) / d;

    printf("RC5_32/12/16 set_key       per sec = %12.2f (%9.3fuS)\n", a,
           1.0e6 / a);
    printf("RC5_32/12/16 raw ecb bytes per sec = %12.2f (%9.3fuS)\n", b,
           8.0e6 / b);
    printf("RC5_32/12/16 cbc     bytes per sec = %12.2f (%9.3fuS)\n", c,
           8.0e6 / c);
    exit(0);
#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
    return (0);
#endif
}
int ssl_test_rc5(int argc, char *argv[])
	{
	int i,n,err=0;
	RC5_32_KEY key; 
	unsigned char buf[8],buf2[8],ivb[8];

	for (n=0; n<5; n++)
		{
		RC5_32_set_key(&key,16,&(RC5key[n][0]),12);

		RC5_32_ecb_encrypt(&(RC5plain[n][0]),buf,&key,RC5_ENCRYPT);
		if (TINYCLR_SSL_MEMCMP(&(RC5cipher[n][0]),buf,8) != 0)
			{
			TINYCLR_SSL_PRINTF("ecb RC5 error encrypting (%d)\n",n+1);
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",RC5cipher[n][i]);
			err=20;
			TINYCLR_SSL_PRINTF("\n");
			}

		RC5_32_ecb_encrypt(buf,buf2,&key,RC5_DECRYPT);
		if (TINYCLR_SSL_MEMCMP(&(RC5plain[n][0]),buf2,8) != 0)
			{
			TINYCLR_SSL_PRINTF("ecb RC5 error decrypting (%d)\n",n+1);
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf2[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",RC5plain[n][i]);
			TINYCLR_SSL_PRINTF("\n");
			err=3;
			}
		}
	if (err == 0) TINYCLR_SSL_PRINTF("ecb RC5 ok\n");

	for (n=0; n<RC5_CBC_NUM; n++)
		{
		i=rc5_cbc_rounds[n];
		if (i < 8) continue;

		RC5_32_set_key(&key,rc5_cbc_key[n][0],&(rc5_cbc_key[n][1]),i);

		TINYCLR_SSL_MEMCPY(ivb,&(rc5_cbc_iv[n][0]),8);
		RC5_32_cbc_encrypt(&(rc5_cbc_plain[n][0]),buf,8,
			&key,&(ivb[0]),RC5_ENCRYPT);

		if (TINYCLR_SSL_MEMCMP(&(rc5_cbc_cipher[n][0]),buf,8) != 0)
			{
			TINYCLR_SSL_PRINTF("cbc RC5 error encrypting (%d)\n",n+1);
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",rc5_cbc_cipher[n][i]);
			err=30;
			TINYCLR_SSL_PRINTF("\n");
			}

		TINYCLR_SSL_MEMCPY(ivb,&(rc5_cbc_iv[n][0]),8);
		RC5_32_cbc_encrypt(buf,buf2,8,
			&key,&(ivb[0]),RC5_DECRYPT);
		if (TINYCLR_SSL_MEMCMP(&(rc5_cbc_plain[n][0]),buf2,8) != 0)
			{
			TINYCLR_SSL_PRINTF("cbc RC5 error decrypting (%d)\n",n+1);
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf2[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",rc5_cbc_plain[n][i]);
			TINYCLR_SSL_PRINTF("\n");
			err=3;
			}
		}
	if (err == 0) TINYCLR_SSL_PRINTF("cbc RC5 ok\n");

	return(err);
	}