Пример #1
0
Файл: main.c Проект: dzruyk/spam
int
main(int argc, char *argv[])
{
	int i;
	struct blowfish_context *ctx;
	uint32_t tmp[2];

	ctx = blowfish_context_new();
	
	blowfish_set_p(ctx, newP);

	blowfish_set_key(ctx, (unsigned char*) "TESTKEY", 7);
	
	printf("\nbefore encr = ");
	for (i = 0; i < 8; i++)
		printf("%x.",((unsigned char*) var)[i]);
	
	memcpy(tmp, var, sizeof(var));
	
	blowfish_encrypt(ctx, (uint32_t*) var);
	
	printf("\nafter encr = ");
	for (i = 0; i < 8; i++)
		printf("%x.",((unsigned char*) var)[i]);
	
	blowfish_decrypt(ctx,(uint32_t*) var);

	printf("\nafter decr = ");
	for (i = 0; i < 8; i++)
		printf("%x.",((unsigned char*) var)[i]);	
	printf("\n");
	
	if (strncmp((char*)tmp, (char*)var, sizeof(var)))
		printf("%s%s", "\n===================================",
		    "\ntest failed!!!!!!!!!!\n\n===================================\n");
	else
		printf("%s%s", "\n===================================",
		    "\nTEST PASSED\n\n===================================\n");
	return 0;
}
Пример #2
0
void cipher_set_key(CipherContext * context, int cipher, const unsigned char *key, int keylen, int for_encryption)
{
    unsigned char padded[32];

    /* Clear the context to remove any traces of old keys. */
    memset(context, 0, sizeof(*context));

    /* Set cipher type. */
    context->type = cipher;

    /* Get 32 bytes of key data.  Pad if necessary.  (So that code below does
       not need to worry about key size). */
    memset(padded, 0, sizeof(padded));
    memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));

    /* Initialize the initialization vector. */
    switch (cipher) {
    case SSH_CIPHER_NONE:
        break;

#ifdef WITH_IDEA
    case SSH_CIPHER_IDEA:
        if (keylen < 16)
            error("Key length %d is insufficient for IDEA.", keylen);
        idea_set_key(&context->u.idea.key, padded);
        memset(context->u.idea.iv, 0, sizeof(context->u.idea.iv));
        break;
#endif                          /* WITH_IDEA */

#ifdef WITH_DES
    case SSH_CIPHER_DES:
        /* Note: the least significant bit of each byte of key is parity, 
           and must be ignored by the implementation.  8 bytes of key are
           used. */
        if (keylen < 8)
            error("Key length %d is insufficient for DES.", keylen);
        des_set_key(padded, &context->u.des.key);
        memset(context->u.des.iv, 0, sizeof(context->u.des.iv));
        break;
#endif                          /* WITH_DES */

    case SSH_CIPHER_3DES:
        /* Note: the least significant bit of each byte of key is parity, 
           and must be ignored by the implementation.  16 bytes of key are
           used (first and last keys are the same). */
        if (keylen < 16)
            error("Key length %d is insufficient for 3DES.", keylen);
        des_set_key(padded, &context->u.des3.key1);
        des_set_key(padded + 8, &context->u.des3.key2);
        if (keylen <= 16)
            des_set_key(padded, &context->u.des3.key3);
        else
            des_set_key(padded + 16, &context->u.des3.key3);
        memset(context->u.des3.iv1, 0, sizeof(context->u.des3.iv1));
        memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
        memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
        break;

#ifdef WITH_ARCFOUR
    case SSH_CIPHER_ARCFOUR:
        arcfour_init(&context->u.arcfour, key, keylen);
        break;
#endif                          /* WITH_ARCFOUR */

#ifdef WITH_BLOWFISH
    case SSH_CIPHER_BLOWFISH:
        if (keylen < 8)
            error("Key length %d is insufficient for Blowfish", keylen);
        blowfish_set_key(&context->u.blowfish, key, keylen, for_encryption);
        break;
#endif                          /* WITH_BLOWFISH */
    default:
        fatal("cipher_set_key: unknown cipher: %d", cipher);
    }
    memset(padded, 0, sizeof(padded));
}