Esempio n. 1
1
void des::decrypt(char const* key, core::vector<byte> const& in, core::string& out)
{
    DES_cblock key_block;
    DES_key_schedule schedule;
    DES_string_to_key(key, &key_block);
    DES_set_key_checked(&key_block, &schedule);

    char* buf = (char*)malloc(in.size());

    core::vector<byte>::const_iterator iter = in.begin();
    DES_cblock* output = (DES_cblock*)buf;

    DES_cblock input;
    while (iter != in.end())
    {
        usize sz = in.end() - iter;
        if (sz >= 8)
            sz = 8;
        else
            memset(input, 0, sizeof(DES_cblock));
        memcpy(input, &*iter, sz);
        DES_ecb_encrypt(&input, output, &schedule, DES_DECRYPT);
        iter += sz;
        output += 1;
    }

    out = buf;

    free(buf);
}
Esempio n. 2
0
static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
			     const unsigned char *iv, int enc)
	{
	DES_cblock *deskey = (DES_cblock *)key;
#ifdef KSSL_DEBUG
	{
        int i;
        printf("des_ede3_init_key(ctx=%lx)\n", ctx);
	printf("\tKEY= ");
        for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n");
	printf("\t IV= ");
        for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n");
	}
#endif	/* KSSL_DEBUG */

#ifdef EVP_CHECK_DES_KEY
	if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1)
		|| DES_set_key_checked(&deskey[1],&data(ctx)->ks2)
		|| DES_set_key_checked(&deskey[2],&data(ctx)->ks3))
		return 0;
#else
	DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1);
	DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2);
	DES_set_key_unchecked(&deskey[2],&data(ctx)->ks3);
#endif
	return 1;
	}
Esempio n. 3
0
int perf()
{
        DES_cblock key1, key2, key3, iv;
        DES_key_schedule ks1, ks2, ks3;

        char* payload = (char*)malloc(64);
        int i;
        uint64_t start, end;

       	memcpy(payload, "hello world hello world hello world\n", 64);
 
	// 1. Key & IV Extract
        memcpy(key1, "aeaeaeae", 8);
        memcpy(key2, "aeaeaeae", 8);
        memcpy(key3, "aeaeaeae", 8);

        memcpy(iv, "aeaeaeae", 8);

        DES_set_odd_parity(&key1);
        DES_set_odd_parity(&key2);
        DES_set_odd_parity(&key3);

        // Key Validation Check
        if(DES_set_key_checked(&key1, &ks1) ||
           DES_set_key_checked(&key2, &ks2) ||
           DES_set_key_checked(&key3, &ks3))
        {
               printf("DES_set_key_checked Error\n");
        }
          
        start = cpu_tsc();

        for(i = 0; i < 1000000; i++)
        {       
                DES_ede3_cbc_encrypt((const unsigned char*)payload,
                                (unsigned char*)payload, 
                                64 , &ks1, &ks2, &ks3, &iv, DES_ENCRYPT);       
        }

        end = cpu_tsc();
	
        printf("encrpytion time : %ld\n", (end-start)/10000);
	
	start = cpu_tsc();

        for(i = 0; i < 1000000; i++)
        {
                DES_ede3_cbc_encrypt((const unsigned char*)payload,
                                (unsigned char*)payload,
                                64 , &ks1, &ks2, &ks3, &iv, DES_DECRYPT);
        }

        end = cpu_tsc();

        printf("decryption time : %ld\n", (end-start)/10000);

	return 0;
}
Esempio n. 4
0
static void desencode(char *content, char *content_in)
{
	char de_content[1024] = {0};
	DES_cblock key_cblock;
	DES_string_to_key(key, &key_cblock);
	DES_key_schedule schedule;
	DES_set_key_checked(&key_cblock, &schedule);
	DES_ncbc_encrypt(content, de_content, strlen(content), &schedule, &key_cblock, DES_ENCRYPT  );
	DES_string_to_key(key, &key_cblock);
	DES_set_key_checked(&key_cblock, &schedule);
	printf("key_cblock= %s\n", key_cblock);
	Base64Encode(de_content, strlen(de_content), content_in, sizeof(content_in));
}
Esempio n. 5
0
static void fb64_session(Session_Key *key, int server, struct fb *fbp)
{

	if (!key || key->type != SK_DES) {
		if (encrypt_debug_mode)
			printf("Can't set krbdes's session key (%d != %d)\r\n",
				key ? key->type : -1, SK_DES);
		return;
	}
	memcpy(fbp->krbdes_key, key->data, sizeof(DES_cblock));

	fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
	fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);

	RAND_seed(key->data, key->length);

	DES_set_key_checked((DES_cblock *)&fbp->krbdes_key,
			    &fbp->krbdes_sched);
	/*
	 * Now look to see if krbdes_start() was was waiting for
	 * the key to show up.  If so, go ahead an call it now
	 * that we have the key.
	 */
	if (fbp->need_start) {
		fbp->need_start = 0;
		fb64_start(fbp, DIR_ENCRYPT, server);
	}
}
Esempio n. 6
0
static int cfb64_test(unsigned char *cfb_cipher)
	{
	des_key_schedule ks;
	int err=0,i,n;

	DES_set_key_checked(&cfb_key,&ks);
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	n=0;
	des_cfb64_encrypt(plain,cfb_buf1,12,ks,&cfb_tmp,&n,DES_ENCRYPT);
	des_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,
			  &cfb_tmp,&n,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt encrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	n=0;
	des_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,&cfb_tmp,&n,DES_DECRYPT);
	des_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
			  sizeof(plain)-17,ks,&cfb_tmp,&n,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(plain,cfb_buf2,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt decrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf2[i])));
		}
	return(err);
	}
Esempio n. 7
0
static int cfb_test(int bits, unsigned char *cfb_cipher)
	{
	des_key_schedule ks;
	int i,err=0;

	DES_set_key_checked(&cfb_key,&ks);
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	des_cfb_encrypt(plain,cfb_buf1,bits,sizeof(plain),ks,&cfb_tmp,
			DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt encrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	des_cfb_encrypt(cfb_buf1,cfb_buf2,bits,sizeof(plain),ks,&cfb_tmp,
			DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(plain,cfb_buf2,sizeof(plain)) != 0)
		{
		err=1;
		TINYCLR_SSL_PRINTF("cfb_encrypt decrypt error\n");
		for (i=0; i<24; i+=8)
			TINYCLR_SSL_PRINTF("%s\n",pt(&(cfb_buf1[i])));
		}
	return(err);
	}
Esempio n. 8
0
int main(int argc,char **argv)
{
    DES_cblock key;
    /* DES_random_key(&key); */ /* generate a random key */
    DES_string_to_key("pass", &key);

    DES_key_schedule schedule;
    DES_set_key_checked(&key, &schedule);

    const_DES_cblock input = "hehehe";
    DES_cblock output;

    printf("cleartext:%s ", input);
    printf("\r\n ");

    DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
    printf("Encrypted! ");

    printf("ciphertext:");
    int i;
    for (i = 0; i < sizeof(input); i++)
        printf("%02x", output[i]);
    printf("\r\n ");

    DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
    printf("Decrypted! ");
    printf("cleartext:%s ", input);
    printf("\r\n ");

    return 0;
}
Esempio n. 9
0
static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
			    const unsigned char *iv, int enc)
	{
	DES_cblock *deskey = (DES_cblock *)key;
#ifdef EVP_CHECK_DES_KEY
	if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1)
		!! DES_set_key_checked(&deskey[1],&data(ctx)->ks2))
		return 0;
#else
	DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1);
	DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2);
#endif
	memcpy(&data(ctx)->ks3,&data(ctx)->ks1,
	       sizeof(data(ctx)->ks1));
	return 1;
	}
Esempio n. 10
0
static int cfb_test(int bits, unsigned char *cfb_cipher)
{
    DES_key_schedule ks;
    int i, err = 0;

    DES_set_key_checked(&cfb_key, &ks);
    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    DES_cfb_encrypt(plain, cfb_buf1, bits, sizeof(plain), &ks, &cfb_tmp,
                    DES_ENCRYPT);
    if (memcmp(cfb_cipher, cfb_buf1, sizeof(plain)) != 0) {
        err = 1;
        printf("cfb_encrypt encrypt error\n");
        for (i = 0; i < 24; i += 8)
            printf("%s\n", pt(&(cfb_buf1[i])));
    }
    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    DES_cfb_encrypt(cfb_buf1, cfb_buf2, bits, sizeof(plain), &ks, &cfb_tmp,
                    DES_DECRYPT);
    if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0) {
        err = 1;
        printf("cfb_encrypt decrypt error\n");
        for (i = 0; i < 24; i += 8)
            printf("%s\n", pt(&(cfb_buf1[i])));
    }
    return (err);
}
void initDes(){
    int i;
    DES_string_to_key("558LFin@l", &key);
    DES_set_key_checked(&key, &schedule);
    for (i = 0; i < 8 ; i++)
        ivdata[i] = 0x00;
}
Esempio n. 12
0
static int ede_cfb64_test(unsigned char *cfb_cipher)
{
    DES_key_schedule ks;
    int err = 0, i, n;

    DES_set_key_checked(&cfb_key, &ks);
    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    n = 0;
    DES_ede3_cfb64_encrypt(plain, cfb_buf1, 12, &ks, &ks, &ks, &cfb_tmp, &n,
                           DES_ENCRYPT);
    DES_ede3_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]),
                           sizeof(plain) - 12, &ks, &ks, &ks,
                           &cfb_tmp, &n, DES_ENCRYPT);
    if (memcmp(cfb_cipher, cfb_buf1, sizeof(plain)) != 0) {
        err = 1;
        printf("ede_cfb_encrypt encrypt error\n");
        for (i = 0; i < 24; i += 8)
            printf("%s\n", pt(&(cfb_buf1[i])));
    }
    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    n = 0;
    DES_ede3_cfb64_encrypt(cfb_buf1, cfb_buf2, (long)17, &ks, &ks, &ks,
                           &cfb_tmp, &n, DES_DECRYPT);
    DES_ede3_cfb64_encrypt(&(cfb_buf1[17]), &(cfb_buf2[17]),
                           sizeof(plain) - 17, &ks, &ks, &ks,
                           &cfb_tmp, &n, DES_DECRYPT);
    if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0) {
        err = 1;
        printf("ede_cfb_encrypt decrypt error\n");
        for (i = 0; i < 24; i += 8)
            printf("%s\n", pt(&(cfb_buf2[i])));
    }
    return (err);
}
Esempio n. 13
0
void openssl_des_crypt()
{
	int size;
	DES_cblock key;
	DES_cblock outputs;
	const_DES_cblock inputs;
	DES_key_schedule schedule;
	unsigned char tmp[16] = "des crypt";

	DES_random_key(&key);
	DES_string_to_key("beike2012", &key);
	DES_set_odd_parity(&key);
	des_check_key_parity(&key);
	DES_set_key_checked(&key, &schedule);
	DES_is_weak_key((const_DES_cblock *)tmp);
	DES_ecb_encrypt((const_DES_cblock *)tmp, &outputs, &schedule, DES_ENCRYPT);
	printf("\nDES_ecb_encrypt(%s) = ", tmp);
	for (size = 0; size < sizeof(outputs); size++)
		printf("%02x", outputs[size]);
	printf("\n");

	DES_ecb_encrypt(&outputs, &inputs, &schedule, DES_DECRYPT);
	printf("DES_ecb_decrypt(");
	for (size = 0; size < sizeof(outputs); size++)
		printf("%02x", outputs[size]);
	printf(") = %s\n", inputs);
}
Esempio n. 14
0
void fb64_stream_key(DES_cblock key, struct stinfo *stp)
{
	memcpy(stp->str_ikey, key, sizeof(DES_cblock));
	DES_set_key_checked((DES_cblock*)key, &stp->str_sched);

	memcpy(stp->str_output, stp->str_iv, sizeof(DES_cblock));

	stp->str_index = sizeof(DES_cblock);
}
Esempio n. 15
0
int main() {
   DES_cblock cb1 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
   DES_cblock cb2 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
   DES_cblock cb3 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };

   DES_key_schedule ks1,ks2,ks3;

   DES_cblock cblock = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

   char string[] = "I am a software developer, bbbxxxxxxxxxx, gjeogjeogjeojsb";
   int  ret = -1;
   // ---------------------------------------------
   // I use sizeof instead of strlen because I want
   // to count the '\0' at the end, strlen would
   // not count it
   int stringLen = strlen(string) + 1;
   int cipherLen = stringLen *2;

   printf("Plain Text : %s\n",string);
   char *cipher = (char*)malloc(cipherLen);
   char *text = (char*)malloc(stringLen);
   memset(cipher, 0, cipherLen);
   memset(text,0,stringLen);

   DES_set_odd_parity(&cblock);
   if (DES_set_key_checked(&cb1, &ks1) ||
        DES_set_key_checked(&cb2, &ks2) ||
         DES_set_key_checked(&cb3, &ks3)) {
      printf("Key error, exiting ....\n");
      return 1;
   }
   // ks1的值是根据解密是的数据变化的,所以该方法失败。
   ret = encrypt_des(&cblock, &ks1, &ks2, &ks3, cipher, string, stringLen);
   if (ret < 0) {
       return ret;
   }

   ret = decrypt_des(cipher, text, &ks1, &ks2, &ks3, &cblock, cipherLen);
   if (ret < 0) {
       return ret;
   }

   return 0;
}
Esempio n. 16
0
int initDes(unsigned char* str, DES_key_schedule* schedule){
	//fprintf(stdout, "key = %s\n", str);
    int i, ret;
	unsigned char key[8];
    DES_string_to_key(str, &key);
    ret = DES_set_key_checked(key, schedule);
    for (i = 0; i < 8 ; i++)
        ivdata[i] = 0x00;
	return ret;
}
Esempio n. 17
0
//des解密函数
bool CRsaDesManager::DESDecode(const string& strDesKey, unsigned char *pcInData, unsigned long ulInLen, unsigned char *pcOutData)
{
	DES_cblock key;
	DES_key_schedule key_schedule;
	DES_string_to_key(strDesKey.c_str(), &key);
	if (DES_set_key_checked(&key, &key_schedule) != 0)
		return false;

	size_t len = (ulInLen+7)/8 * 8;

	return DESDecode(key_schedule, pcInData, ulInLen, pcOutData);
}
Esempio n. 18
0
static uint64_t feistel_enc(uint32_t a, uint32_t b, uint32_t m, uint8_t *key[], uint32_t round)
{
    uint64_t L, R;
    uint64_t tmp, nval;
    uint64_t mod;
    uint8_t val[8];
    uint8_t res[8];
    int i;
    DES_cblock ckey;
    DES_key_schedule schd;
    
    L = m % a;
    R = (uint64_t)floor((double)m / (double)a);
    
    memset(val, 0, 8 * sizeof(uint8_t));
    for(i = 0; i < round; i ++)
    {
        memcpy(val, &R, 8 * sizeof(uint8_t));

        memset(&ckey, 0, sizeof(DES_cblock));
        memcpy(&ckey, key[i], PERMKEYLENGTH);
        memset(res, 0, 8 * sizeof(char));
        memset(&schd, 0, sizeof(DES_key_schedule));

        DES_set_odd_parity(&ckey);
        DES_set_key_checked(&ckey, &schd);
        DES_ecb_encrypt((const_DES_cblock *)val, (DES_cblock *)res, 
                         &schd, DES_ENCRYPT);

        nval = *((uint64_t *)res);
        
        mod = ((i & 0x1) == 0) ? a : b;
        tmp = ((L % mod)+ (nval % mod)) % mod;

        L = R;
        R = tmp;

        memset(val, 0, 8 * sizeof(uint8_t));
    }

    if((round & 0x1) == 1)
    {
        return a * L + R;
    }
    else
    {
        return a * R + L;
    }

}
Esempio n. 19
0
void tdes(int choice)
{
	unsigned char input_data[100] = "iamflag";
	int len = strlen(input_data);
	DES_cblock Key1 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	DES_cblock Key2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	DES_cblock Key3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	unsigned char *text = malloc(len);


	
	if ( -2 == (DES_set_key_checked(&Key1, &SchKey1) || DES_set_key_checked(&Key2, &SchKey2) || DES_set_key_checked(&Key3, &SchKey3)))
	{
		printf(" Weak key ....\n");
		exit(0);
	}

	if(choice == EN)
	{
		int i;
		for(i=0; i<len + 4; i += 8)
		{
			DES_ecb3_encrypt( (C_Block *)(input_data + i), (C_Block *)(text + i), &SchKey1, &SchKey2, &SchKey3, DES_ENCRYPT);
		}
		print_data("\n Encrypted",text,8*((len + 4) / 8));
	}
	else if(choice == DE)
	{
		int i;
		for(i=0; i<len + 4; i += 8)
		{
			DES_ecb3_encrypt( (C_Block *)(input_data + i), (C_Block *)(text + i), &SchKey1, &SchKey2, &SchKey3, DES_DECRYPT);
		}
		print_data("\n Decrypted",text,8*((len + 4) / 8));
	}
}
PbeMd5AndDesEncryptor::PbeMd5AndDesKey PbeMd5AndDesEncryptor::generateKey(const std::string& password,
        const std::string salt, long iterations) {
    std::string keyHash = generateDataHash(password, salt, iterations);

    PbeMd5AndDesKey retVal;
    DES_cblock key;

    keyHash.copy(reinterpret_cast<char *>(&key), ALGO_BLOCK_SIZE, 0);
    keyHash.copy(reinterpret_cast<char *>(&retVal.ivec), ALGO_BLOCK_SIZE, ALGO_BLOCK_SIZE);

    DES_set_odd_parity(&key);
    DES_set_key_checked(&key, &retVal.schedule);

    return retVal;
}
Esempio n. 21
0
static int encrypt( char *key, char *msg, int size, char *res)
{
    int n=0;
    DES_cblock key2;
    DES_key_schedule schedule;

    /* Prepare the key for use with DES_cfb64_encrypt */
    memcpy( key2, key,8);
    DES_set_odd_parity( &key2 );
    DES_set_key_checked( &key2, &schedule );

    /* Encryption occurs here */
    DES_cfb64_encrypt( ( unsigned char * ) msg, ( unsigned char * ) res,
                       size, &schedule, &key2, &n, DES_ENCRYPT );

    return size;
}
char *
Decrypt( char *Key, char *Msg, int size)
{
    static char*    Res;
    int             n=0;
    DES_cblock      Key2;
    DES_key_schedule schedule;
    Res = ( char * ) malloc( size );
    /* Prepare the key for use with DES_cfb64_encrypt */
    memcpy( Key2, Key,8);
    DES_set_odd_parity( &Key2 );
    DES_set_key_checked( &Key2, &schedule );
    /* Decryption occurs here */
    DES_cfb64_encrypt( ( unsigned char * ) Msg, ( unsigned char * ) Res,
                       size, &schedule, &Key2, &n, DES_DECRYPT );
    return (Res);
}
Esempio n. 23
0
//des加密函数
string CRsaDesManager::DESEncode(const string& strDesKey, const string& strInData)
{
	DES_cblock key;
	DES_key_schedule key_schedule;
	DES_string_to_key(strDesKey.c_str(), &key);
	if (DES_set_key_checked(&key, &key_schedule) != 0)
		return "";

	size_t len = (strInData.size()+7)/8 * 8;

	unsigned char pcOutData[4096];
	if(DESEncode(key_schedule, (unsigned char *)strInData.c_str(), len, pcOutData))
	{
		string strEncoded;
		strEncoded.append((char*)pcOutData, len);
		return strEncoded;
	}
	else
	{
		return "";
	}
}
Esempio n. 24
0
obj rs_des_make_key_schedule( obj key )
{
  obj sched;
  int rc;

  sched = bvec_alloc( sizeof( DES_key_schedule ), byte_vector_class );
  
  rc = DES_set_key_checked( (DES_cblock *)PTR_TO_DATAPTR( key ), 
                            (DES_key_schedule *)PTR_TO_DATAPTR( sched ) );
  if (rc < 0) {
    if (rc == -2) {
      scheme_error( "DES_set_key_checked: weak key: ~s", 1, key );
    } else if (rc == -1) {
      scheme_error( "DES_set_key_checked: parity error: ~s", 1, key );
    } else {
      scheme_error( "DES_set_key_checked: error ~d", 1, int2fx( rc ) );
    }
    return FALSE_OBJ;
  } else {
    return sched;
  }
}
Esempio n. 25
0
File: des_R.c Progetto: kgadek/kpfp
int main(int argc, char** args) {
	if(argc!=5 || (strcmp(args[1], "cbc") && strcmp(args[1], "ecb")) || (strcmp(args[2], "encrypt") && strcmp(args[2], "decrypt"))) {
		printf("uzycie: des cbc/ecb encrypt/decrypt plik.in plik.key\ndane wyjsciowe przekierowywane sa do pliku plik.out\n");
		return 1;
	}
	FILE* in = fopen(args[3], "r");
	if(!in) {
		printf("blad przy otwieraniu pliku...\n");
		return 1;
	}
	FILE* key;
	if(strcmp(args[2], "encrypt"))
		key = fopen(args[4], "r");
	else 
		key = fopen(args[4], "w");
	DES_key_schedule klucz;
	DES_cblock sekwencja;
	DES_random_key(&sekwencja);
	
	if(DES_set_key_checked(&sekwencja, &klucz)<0) {
		printf("blad przy generacji klucza!\n");
		return 1;
	}

	if(strcmp(args[1], "cbc")) {
		if(strcmp(args[2], "encrypt"))
			ecb(in, key, klucz, 0);
		else 
			ecb(in, key, klucz, 1);
	} else {
		if(strcmp(args[2], "encrypt"))
			cbc(in, key, klucz, 0); 
		else 
			cbc(in, key, klucz, 1);
	}
	fclose(in); fclose(key);
	return 0;
}
Esempio n. 26
0
NNT_BEGIN_HEADER_C

# include <openssl/des.h>
# include <math.h>

NNT_END_HEADER_C

NNT_BEGIN_CXX

void des::encrypt(char const* key, core::string const& in, core::vector<byte>& out)
{
    DES_cblock key_block;
    DES_key_schedule schedule;
    DES_string_to_key(key, &key_block);
    DES_set_key_checked(&key_block, &schedule);

    usize sz = (usize)ceil((double)in.size() / sizeof(DES_cblock)) * sizeof(DES_cblock);
    out.resize(sz);

    core::string::const_iterator iter = in.begin();
    DES_cblock* output = (DES_cblock*)core::pointer(out);

    DES_cblock input;
    while (iter != in.end())
    {
        usize sz = in.end() - iter;
        if (sz >= 8)
            sz = 8;
        else
            memset(input, 0, sizeof(DES_cblock));
        memcpy(input, &*iter, sz);
        DES_ecb_encrypt(&input, output, &schedule, DES_ENCRYPT);
        iter += sz;
        output += 1;
    }
}
Esempio n. 27
0
int main(int argc, char *argv[])
{
    int j, err = 0;
    unsigned int i;
    DES_cblock in, out, outin, iv3;
    DES_key_schedule ks, ks2, ks3;
    unsigned char cbc_in[40];
    unsigned char cbc_out[40];
    DES_LONG cs;
    unsigned char cret[8];
    DES_LONG lqret[4];
    int num;
    char *str;

    printf("Doing ecb\n");
    for (i = 0; i < NUM_TESTS; i++) {
        DES_set_key_unchecked(&key_data[i], &ks);
        memcpy(in, plain_data[i], 8);
        memset(out, 0, 8);
        memset(outin, 0, 8);
        DES_ecb_encrypt(&in, &out, &ks, DES_ENCRYPT);
        DES_ecb_encrypt(&out, &outin, &ks, DES_DECRYPT);

        if (memcmp(out, cipher_data[i], 8) != 0) {
            printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(in), pt(cipher_data[i]),
                   pt(out));
            err = 1;
        }
        if (memcmp(in, outin, 8) != 0) {
            printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(out), pt(in), pt(outin));
            err = 1;
        }
    }

# ifndef LIBDES_LIT
    printf("Doing ede ecb\n");
    for (i = 0; i < (NUM_TESTS - 2); i++) {
        DES_set_key_unchecked(&key_data[i], &ks);
        DES_set_key_unchecked(&key_data[i + 1], &ks2);
        DES_set_key_unchecked(&key_data[i + 2], &ks3);
        memcpy(in, plain_data[i], 8);
        memset(out, 0, 8);
        memset(outin, 0, 8);
        DES_ecb3_encrypt(&in,&out,&ks,&ks2,&ks,DES_ENCRYPT);
        DES_ecb3_encrypt(&out,&outin,&ks,&ks2,&ks,DES_DECRYPT);

        if (memcmp(out, cipher_ecb2[i], 8) != 0) {
            printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(in), pt(cipher_ecb2[i]),
                   pt(out));
            err = 1;
        }
        if (memcmp(in, outin, 8) != 0) {
            printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(out), pt(in), pt(outin));
            err = 1;
        }
    }
# endif

    printf("Doing cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, DES_ENCRYPT);
    if (memcmp(cbc_out, cbc_ok, 32) != 0) {
        printf("cbc_encrypt encrypt error\n");
        err = 1;
    }

    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data)) != 0) {
        printf("cbc_encrypt decrypt error\n");
        err = 1;
    }
# ifndef LIBDES_LIT
    printf("Doing desx cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_xcbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, &cbc2_key, &cbc3_key, DES_ENCRYPT);
    if (memcmp(cbc_out, xcbc_ok, 32) != 0) {
        printf("des_xcbc_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_xcbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, &cbc2_key, &cbc3_key, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        printf("des_xcbc_encrypt decrypt error\n");
        err = 1;
    }
# endif

    printf("Doing ede cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    if ((j = DES_set_key_checked(&cbc2_key, &ks2)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    if ((j = DES_set_key_checked(&cbc3_key, &ks3)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    i = strlen((char *)cbc_data) + 1;
    /* i=((i+7)/8)*8; */
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));

    DES_ede3_cbc_encrypt(cbc_data, cbc_out, 16L, &ks, &ks2, &ks3, &iv3,
                         DES_ENCRYPT);
    DES_ede3_cbc_encrypt(&(cbc_data[16]), &(cbc_out[16]), i - 16, &ks, &ks2,
                         &ks3, &iv3, DES_ENCRYPT);
    if (memcmp
        (cbc_out, cbc3_ok,
         (unsigned int)(strlen((char *)cbc_data) + 1 + 7) / 8 * 8) != 0) {
        unsigned int n;

        printf("des_ede3_cbc_encrypt encrypt error\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_out[n]);
        printf("\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc3_ok[n]);
        printf("\n");
        err = 1;
    }

    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ede3_cbc_encrypt(cbc_out, cbc_in, i, &ks, &ks2, &ks3, &iv3, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        unsigned int n;

        printf("DES_ede3_cbc_encrypt decrypt error\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_data[n]);
        printf("\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_in[n]);
        printf("\n");
        err = 1;
    }
# ifndef LIBDES_LIT
    printf("Doing pcbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    DES_pcbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &cbc_iv, DES_ENCRYPT);
    if (memcmp(cbc_out, pcbc_ok, 32) != 0) {
        printf("pcbc_encrypt encrypt error\n");
        err = 1;
    }
    DES_pcbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &cbc_iv, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        printf("pcbc_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing ");
    printf("cfb8 ");
    err += cfb_test(8, cfb_cipher8);
    printf("cfb16 ");
    err += cfb_test(16, cfb_cipher16);
    printf("cfb32 ");
    err += cfb_test(32, cfb_cipher32);
    printf("cfb48 ");
    err += cfb_test(48, cfb_cipher48);
    printf("cfb64 ");
    err += cfb_test(64, cfb_cipher64);

    printf("cfb64() ");
    err += cfb64_test(cfb_cipher64);

    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    for (i = 0; i < sizeof(plain); i++)
        DES_cfb_encrypt(&(plain[i]), &(cfb_buf1[i]),
                        8, 1, &ks, &cfb_tmp, DES_ENCRYPT);
    if (memcmp(cfb_cipher8, cfb_buf1, sizeof(plain)) != 0) {
        printf("cfb_encrypt small encrypt error\n");
        err = 1;
    }

    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    for (i = 0; i < sizeof(plain); i++)
        DES_cfb_encrypt(&(cfb_buf1[i]), &(cfb_buf2[i]),
                        8, 1, &ks, &cfb_tmp, DES_DECRYPT);
    if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0) {
        printf("cfb_encrypt small decrypt error\n");
        err = 1;
    }

    printf("ede_cfb64() ");
    err += ede_cfb64_test(cfb_cipher64);

    printf("done\n");

    printf("Doing ofb\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    DES_ofb_encrypt(plain, ofb_buf1, 64, sizeof(plain) / 8, &ks, &ofb_tmp);
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ofb_encrypt encrypt error\n");
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
               ofb_buf1[8 + 0], ofb_buf1[8 + 1], ofb_buf1[8 + 2],
               ofb_buf1[8 + 3], ofb_buf1[8 + 4], ofb_buf1[8 + 5],
               ofb_buf1[8 + 6], ofb_buf1[8 + 7]);
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", ofb_buf1[8 + 0],
               ofb_cipher[8 + 1], ofb_cipher[8 + 2], ofb_cipher[8 + 3],
               ofb_buf1[8 + 4], ofb_cipher[8 + 5], ofb_cipher[8 + 6],
               ofb_cipher[8 + 7]);
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    DES_ofb_encrypt(ofb_buf1, ofb_buf2, 64, sizeof(ofb_buf1) / 8, &ks,
                    &ofb_tmp);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ofb_encrypt decrypt error\n");
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
               ofb_buf2[8 + 0], ofb_buf2[8 + 1], ofb_buf2[8 + 2],
               ofb_buf2[8 + 3], ofb_buf2[8 + 4], ofb_buf2[8 + 5],
               ofb_buf2[8 + 6], ofb_buf2[8 + 7]);
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", plain[8 + 0],
               plain[8 + 1], plain[8 + 2], plain[8 + 3], plain[8 + 4],
               plain[8 + 5], plain[8 + 6], plain[8 + 7]);
        err = 1;
    }

    printf("Doing ofb64\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    memset(ofb_buf1, 0, sizeof(ofb_buf1));
    memset(ofb_buf2, 0, sizeof(ofb_buf1));
    num = 0;
    for (i = 0; i < sizeof(plain); i++) {
        DES_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, &ks, &ofb_tmp, &num);
    }
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ofb64_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    num = 0;
    DES_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ofb_tmp,
                      &num);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ofb64_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing ede_ofb64\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    memset(ofb_buf1, 0, sizeof(ofb_buf1));
    memset(ofb_buf2, 0, sizeof(ofb_buf1));
    num = 0;
    for (i = 0; i < sizeof(plain); i++) {
        DES_ede3_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, &ks, &ks,
                               &ks, &ofb_tmp, &num);
    }
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ede_ofb64_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    num = 0;
    DES_ede3_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ks, &ks,
                           &ofb_tmp, &num);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ede_ofb64_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing cbc_cksum\n");
    DES_set_key_checked(&cbc_key, &ks);
    cs = DES_cbc_cksum(cbc_data, &cret, strlen((char *)cbc_data), &ks,
                       &cbc_iv);
    if (cs != cbc_cksum_ret) {
        printf("bad return value (%08lX), should be %08lX\n",
               (unsigned long)cs, (unsigned long)cbc_cksum_ret);
        err = 1;
    }
    if (memcmp(cret, cbc_cksum_data, 8) != 0) {
        printf("bad cbc_cksum block returned\n");
        err = 1;
    }

    printf("Doing quad_cksum\n");
    cs = DES_quad_cksum(cbc_data, (DES_cblock *)lqret,
                        (long)strlen((char *)cbc_data), 2,
                        (DES_cblock *)cbc_iv);
    if (cs != 0x70d7a63aL) {
        printf("quad_cksum error, ret %08lx should be 70d7a63a\n",
               (unsigned long)cs);
        err = 1;
    }
    if (lqret[0] != 0x327eba8dL) {
        printf("quad_cksum error, out[0] %08lx is not %08lx\n",
               (unsigned long)lqret[0], 0x327eba8dUL);
        err = 1;
    }
    if (lqret[1] != 0x201a49ccL) {
        printf("quad_cksum error, out[1] %08lx is not %08lx\n",
               (unsigned long)lqret[1], 0x201a49ccUL);
        err = 1;
    }
    if (lqret[2] != 0x70d7a63aL) {
        printf("quad_cksum error, out[2] %08lx is not %08lx\n",
               (unsigned long)lqret[2], 0x70d7a63aUL);
        err = 1;
    }
    if (lqret[3] != 0x501c2c26L) {
        printf("quad_cksum error, out[3] %08lx is not %08lx\n",
               (unsigned long)lqret[3], 0x501c2c26UL);
        err = 1;
    }
# endif

    printf("input word alignment test");
    for (i = 0; i < 4; i++) {
        printf(" %d", i);
        DES_ncbc_encrypt(&(cbc_out[i]), cbc_in,
                         strlen((char *)cbc_data) + 1, &ks,
                         &cbc_iv, DES_ENCRYPT);
    }
    printf("\noutput word alignment test");
    for (i = 0; i < 4; i++) {
        printf(" %d", i);
        DES_ncbc_encrypt(cbc_out, &(cbc_in[i]),
                         strlen((char *)cbc_data) + 1, &ks,
                         &cbc_iv, DES_ENCRYPT);
    }
    printf("\n");
    printf("fast crypt test ");
    str = crypt("testing", "ef");
    if (strcmp("efGnQx2725bI2", str) != 0) {
        printf("fast crypt error, %s should be efGnQx2725bI2\n", str);
        err = 1;
    }
    str = crypt("bca76;23", "yA");
    if (strcmp("yA1Rp/1hZXIJk", str) != 0) {
        printf("fast crypt error, %s should be yA1Rp/1hZXIJk\n", str);
        err = 1;
    }
    printf("\n");
    return (err);
}
Esempio n. 28
0
int main(int argc, char *argv[])
	{
	int j,err=0;
	unsigned int i;
	des_cblock in,out,outin,iv3,iv2;
	des_key_schedule ks,ks2,ks3;
	unsigned char cbc_in[40];
	unsigned char cbc_out[40];
	DES_LONG cs;
	unsigned char cret[8];
#ifdef _CRAY
        struct {
            int a:32;
            int b:32;
        } lqret[2];
#else
        DES_LONG lqret[4];
#endif
	int num;
	char *str;

#ifndef OPENSSL_NO_DESCBCM
	TINYCLR_SSL_PRINTF("Doing cbcm\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	TINYCLR_SSL_MEMSET(cbc_out,0,40);
	TINYCLR_SSL_MEMSET(cbc_in,0,40);
	i=TINYCLR_SSL_STRLEN((char *)cbc_data)+1;
	/* i=((i+7)/8)*8; */
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	TINYCLR_SSL_MEMSET(iv2,'\0',sizeof iv2);

	DES_ede3_cbcm_encrypt(cbc_data,cbc_out,16L,&ks,&ks2,&ks3,&iv3,&iv2,
			      DES_ENCRYPT);
	DES_ede3_cbcm_encrypt(&cbc_data[16],&cbc_out[16],i-16,&ks,&ks2,&ks3,
			      &iv3,&iv2,DES_ENCRYPT);
	/*	if (memcmp(cbc_out,cbc3_ok,
		(unsigned int)(TINYCLR_SSL_STRLEN((char *)cbc_data)+1+7)/8*8) != 0)
		{
		TINYCLR_SSL_PRINTF("des_ede3_cbc_encrypt encrypt error\n");
		err=1;
		}
	*/
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	TINYCLR_SSL_MEMSET(iv2,'\0',sizeof iv2);
	DES_ede3_cbcm_encrypt(cbc_out,cbc_in,i,&ks,&ks2,&ks3,&iv3,&iv2,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN((char *)cbc_data)+1) != 0)
		{
		unsigned int n;

		TINYCLR_SSL_PRINTF("des_ede3_cbcm_encrypt decrypt error\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc_data[n]);
		TINYCLR_SSL_PRINTF("\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc_in[n]);
		TINYCLR_SSL_PRINTF("\n");
		err=1;
		}
#endif

	TINYCLR_SSL_PRINTF("Doing ecb\n");
	for (i=0; i<NUM_TESTS; i++)
		{
		DES_set_key_unchecked(&key_data[i],&ks);
		TINYCLR_SSL_MEMCPY(in,plain_data[i],8);
		TINYCLR_SSL_MEMSET(out,0,8);
		TINYCLR_SSL_MEMSET(outin,0,8);
		des_ecb_encrypt(&in,&out,ks,DES_ENCRYPT);
		des_ecb_encrypt(&out,&outin,ks,DES_DECRYPT);

		if (TINYCLR_SSL_MEMCMP(out,cipher_data[i],8) != 0)
			{
			TINYCLR_SSL_PRINTF("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(in),pt(cipher_data[i]),
				pt(out));
			err=1;
			}
		if (TINYCLR_SSL_MEMCMP(in,outin,8) != 0)
			{
			TINYCLR_SSL_PRINTF("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
			err=1;
			}
		}

#ifndef LIBDES_LIT
	TINYCLR_SSL_PRINTF("Doing ede ecb\n");
	for (i=0; i<(NUM_TESTS-2); i++)
		{
		DES_set_key_unchecked(&key_data[i],&ks);
		DES_set_key_unchecked(&key_data[i+1],&ks2);
		DES_set_key_unchecked(&key_data[i+2],&ks3);
		TINYCLR_SSL_MEMCPY(in,plain_data[i],8);
		TINYCLR_SSL_MEMSET(out,0,8);
		TINYCLR_SSL_MEMSET(outin,0,8);
		des_ecb2_encrypt(&in,&out,ks,ks2,DES_ENCRYPT);
		des_ecb2_encrypt(&out,&outin,ks,ks2,DES_DECRYPT);

		if (TINYCLR_SSL_MEMCMP(out,cipher_ecb2[i],8) != 0)
			{
			TINYCLR_SSL_PRINTF("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(in),pt(cipher_ecb2[i]),
				pt(out));
			err=1;
			}
		if (TINYCLR_SSL_MEMCMP(in,outin,8) != 0)
			{
			TINYCLR_SSL_PRINTF("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
			err=1;
			}
		}
#endif

	TINYCLR_SSL_PRINTF("Doing cbc\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	TINYCLR_SSL_MEMSET(cbc_out,0,40);
	TINYCLR_SSL_MEMSET(cbc_in,0,40);
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	des_ncbc_encrypt(cbc_data,cbc_out,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
			 &iv3,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_out,cbc_ok,32) != 0)
		{
		TINYCLR_SSL_PRINTF("cbc_encrypt encrypt error\n");
		err=1;
		}

	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	des_ncbc_encrypt(cbc_out,cbc_in,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
			 &iv3,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN((char *)cbc_data)) != 0)
		{
		TINYCLR_SSL_PRINTF("cbc_encrypt decrypt error\n");
		err=1;
		}

#ifndef LIBDES_LIT
	TINYCLR_SSL_PRINTF("Doing desx cbc\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	TINYCLR_SSL_MEMSET(cbc_out,0,40);
	TINYCLR_SSL_MEMSET(cbc_in,0,40);
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	des_xcbc_encrypt(cbc_data,cbc_out,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
			 &iv3,&cbc2_key,&cbc3_key, DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_out,xcbc_ok,32) != 0)
		{
		TINYCLR_SSL_PRINTF("des_xcbc_encrypt encrypt error\n");
		err=1;
		}
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	des_xcbc_encrypt(cbc_out,cbc_in,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
			 &iv3,&cbc2_key,&cbc3_key, DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN((char *)cbc_data)+1) != 0)
		{
		TINYCLR_SSL_PRINTF("des_xcbc_encrypt decrypt error\n");
		err=1;
		}
#endif

	TINYCLR_SSL_PRINTF("Doing ede cbc\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	TINYCLR_SSL_MEMSET(cbc_out,0,40);
	TINYCLR_SSL_MEMSET(cbc_in,0,40);
	i=TINYCLR_SSL_STRLEN((char *)cbc_data)+1;
	/* i=((i+7)/8)*8; */
	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));

	des_ede3_cbc_encrypt(cbc_data,cbc_out,16L,ks,ks2,ks3,&iv3,
			     DES_ENCRYPT);
	des_ede3_cbc_encrypt(&(cbc_data[16]),&(cbc_out[16]),i-16,ks,ks2,ks3,
			     &iv3,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_out,cbc3_ok,
		(unsigned int)(TINYCLR_SSL_STRLEN((char *)cbc_data)+1+7)/8*8) != 0)
		{
		unsigned int n;

		TINYCLR_SSL_PRINTF("des_ede3_cbc_encrypt encrypt error\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc_out[n]);
		TINYCLR_SSL_PRINTF("\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc3_ok[n]);
		TINYCLR_SSL_PRINTF("\n");
		err=1;
		}

	TINYCLR_SSL_MEMCPY(iv3,cbc_iv,sizeof(cbc_iv));
	des_ede3_cbc_encrypt(cbc_out,cbc_in,i,ks,ks2,ks3,&iv3,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN((char *)cbc_data)+1) != 0)
		{
		unsigned int n;

		TINYCLR_SSL_PRINTF("des_ede3_cbc_encrypt decrypt error\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc_data[n]);
		TINYCLR_SSL_PRINTF("\n");
		for(n=0 ; n < i ; ++n)
		    TINYCLR_SSL_PRINTF(" %02x",cbc_in[n]);
		TINYCLR_SSL_PRINTF("\n");
		err=1;
		}

#ifndef LIBDES_LIT
	TINYCLR_SSL_PRINTF("Doing pcbc\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		TINYCLR_SSL_PRINTF("Key error %d\n",j);
		err=1;
		}
	TINYCLR_SSL_MEMSET(cbc_out,0,40);
	TINYCLR_SSL_MEMSET(cbc_in,0,40);
	des_pcbc_encrypt(cbc_data,cbc_out,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
			 &cbc_iv,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_out,pcbc_ok,32) != 0)
		{
		TINYCLR_SSL_PRINTF("pcbc_encrypt encrypt error\n");
		err=1;
		}
	des_pcbc_encrypt(cbc_out,cbc_in,TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,&cbc_iv,
			 DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(cbc_in,cbc_data,TINYCLR_SSL_STRLEN((char *)cbc_data)+1) != 0)
		{
		TINYCLR_SSL_PRINTF("pcbc_encrypt decrypt error\n");
		err=1;
		}

	TINYCLR_SSL_PRINTF("Doing ");
	TINYCLR_SSL_PRINTF("cfb8 ");
	err+=cfb_test(8,cfb_cipher8);
	TINYCLR_SSL_PRINTF("cfb16 ");
	err+=cfb_test(16,cfb_cipher16);
	TINYCLR_SSL_PRINTF("cfb32 ");
	err+=cfb_test(32,cfb_cipher32);
	TINYCLR_SSL_PRINTF("cfb48 ");
	err+=cfb_test(48,cfb_cipher48);
	TINYCLR_SSL_PRINTF("cfb64 ");
	err+=cfb_test(64,cfb_cipher64);

	TINYCLR_SSL_PRINTF("cfb64() ");
	err+=cfb64_test(cfb_cipher64);

	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	for (i=0; i<sizeof(plain); i++)
		des_cfb_encrypt(&(plain[i]),&(cfb_buf1[i]),
			8,1,ks,&cfb_tmp,DES_ENCRYPT);
	if (TINYCLR_SSL_MEMCMP(cfb_cipher8,cfb_buf1,sizeof(plain)) != 0)
		{
		TINYCLR_SSL_PRINTF("cfb_encrypt small encrypt error\n");
		err=1;
		}

	TINYCLR_SSL_MEMCPY(cfb_tmp,cfb_iv,sizeof(cfb_iv));
	for (i=0; i<sizeof(plain); i++)
		des_cfb_encrypt(&(cfb_buf1[i]),&(cfb_buf2[i]),
			8,1,ks,&cfb_tmp,DES_DECRYPT);
	if (TINYCLR_SSL_MEMCMP(plain,cfb_buf2,sizeof(plain)) != 0)
		{
		TINYCLR_SSL_PRINTF("cfb_encrypt small decrypt error\n");
		err=1;
		}

	TINYCLR_SSL_PRINTF("ede_cfb64() ");
	err+=ede_cfb64_test(cfb_cipher64);

	TINYCLR_SSL_PRINTF("done\n");

	TINYCLR_SSL_PRINTF("Doing ofb\n");
	DES_set_key_checked(&ofb_key,&ks);
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	des_ofb_encrypt(plain,ofb_buf1,64,sizeof(plain)/8,ks,&ofb_tmp);
	if (TINYCLR_SSL_MEMCMP(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
		{
		TINYCLR_SSL_PRINTF("ofb_encrypt encrypt error\n");
TINYCLR_SSL_PRINTF("%02X %02X %02X %02X %02X %02X %02X %02X\n",
ofb_buf1[8+0], ofb_buf1[8+1], ofb_buf1[8+2], ofb_buf1[8+3],
ofb_buf1[8+4], ofb_buf1[8+5], ofb_buf1[8+6], ofb_buf1[8+7]);
TINYCLR_SSL_PRINTF("%02X %02X %02X %02X %02X %02X %02X %02X\n",
ofb_buf1[8+0], ofb_cipher[8+1], ofb_cipher[8+2], ofb_cipher[8+3],
ofb_buf1[8+4], ofb_cipher[8+5], ofb_cipher[8+6], ofb_cipher[8+7]);
		err=1;
		}
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	des_ofb_encrypt(ofb_buf1,ofb_buf2,64,sizeof(ofb_buf1)/8,ks,&ofb_tmp);
	if (TINYCLR_SSL_MEMCMP(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
		{
		TINYCLR_SSL_PRINTF("ofb_encrypt decrypt error\n");
TINYCLR_SSL_PRINTF("%02X %02X %02X %02X %02X %02X %02X %02X\n",
ofb_buf2[8+0], ofb_buf2[8+1], ofb_buf2[8+2], ofb_buf2[8+3],
ofb_buf2[8+4], ofb_buf2[8+5], ofb_buf2[8+6], ofb_buf2[8+7]);
TINYCLR_SSL_PRINTF("%02X %02X %02X %02X %02X %02X %02X %02X\n",
plain[8+0], plain[8+1], plain[8+2], plain[8+3],
plain[8+4], plain[8+5], plain[8+6], plain[8+7]);
		err=1;
		}

	TINYCLR_SSL_PRINTF("Doing ofb64\n");
	DES_set_key_checked(&ofb_key,&ks);
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	TINYCLR_SSL_MEMSET(ofb_buf1,0,sizeof(ofb_buf1));
	TINYCLR_SSL_MEMSET(ofb_buf2,0,sizeof(ofb_buf1));
	num=0;
	for (i=0; i<sizeof(plain); i++)
		{
		des_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,&ofb_tmp,
				  &num);
		}
	if (TINYCLR_SSL_MEMCMP(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
		{
		TINYCLR_SSL_PRINTF("ofb64_encrypt encrypt error\n");
		err=1;
		}
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	num=0;
	des_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,&ofb_tmp,
			  &num);
	if (TINYCLR_SSL_MEMCMP(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
		{
		TINYCLR_SSL_PRINTF("ofb64_encrypt decrypt error\n");
		err=1;
		}

	TINYCLR_SSL_PRINTF("Doing ede_ofb64\n");
	DES_set_key_checked(&ofb_key,&ks);
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	TINYCLR_SSL_MEMSET(ofb_buf1,0,sizeof(ofb_buf1));
	TINYCLR_SSL_MEMSET(ofb_buf2,0,sizeof(ofb_buf1));
	num=0;
	for (i=0; i<sizeof(plain); i++)
		{
		des_ede3_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,ks,
				       ks,&ofb_tmp,&num);
		}
	if (TINYCLR_SSL_MEMCMP(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
		{
		TINYCLR_SSL_PRINTF("ede_ofb64_encrypt encrypt error\n");
		err=1;
		}
	TINYCLR_SSL_MEMCPY(ofb_tmp,ofb_iv,sizeof(ofb_iv));
	num=0;
	des_ede3_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,ks,ks,
			       &ofb_tmp,&num);
	if (TINYCLR_SSL_MEMCMP(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
		{
		TINYCLR_SSL_PRINTF("ede_ofb64_encrypt decrypt error\n");
		err=1;
		}

	TINYCLR_SSL_PRINTF("Doing cbc_cksum\n");
	DES_set_key_checked(&cbc_key,&ks);
	cs=des_cbc_cksum(cbc_data,&cret,TINYCLR_SSL_STRLEN((char *)cbc_data),ks,&cbc_iv);
	if (cs != cbc_cksum_ret)
		{
		TINYCLR_SSL_PRINTF("bad return value (%08lX), should be %08lX\n",
			(unsigned long)cs,(unsigned long)cbc_cksum_ret);
		err=1;
		}
	if (TINYCLR_SSL_MEMCMP(cret,cbc_cksum_data,8) != 0)
		{
		TINYCLR_SSL_PRINTF("bad cbc_cksum block returned\n");
		err=1;
		}

	TINYCLR_SSL_PRINTF("Doing quad_cksum\n");
	cs=des_quad_cksum(cbc_data,(des_cblock *)lqret,
		(long)TINYCLR_SSL_STRLEN((char *)cbc_data),2,(des_cblock *)cbc_iv);
	if (cs != 0x70d7a63aL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, ret %08lx should be 70d7a63a\n",
			(unsigned long)cs);
		err=1;
		}
#ifdef _CRAY
	if (lqret[0].a != 0x327eba8dL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[0] %08lx is not %08lx\n",
			(unsigned long)lqret[0].a,0x327eba8dUL);
		err=1;
		}
	if (lqret[0].b != 0x201a49ccL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[1] %08lx is not %08lx\n",
			(unsigned long)lqret[0].b,0x201a49ccUL);
		err=1;
		}
	if (lqret[1].a != 0x70d7a63aL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[2] %08lx is not %08lx\n",
			(unsigned long)lqret[1].a,0x70d7a63aUL);
		err=1;
		}
	if (lqret[1].b != 0x501c2c26L)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[3] %08lx is not %08lx\n",
			(unsigned long)lqret[1].b,0x501c2c26UL);
		err=1;
		}
#else
	if (lqret[0] != 0x327eba8dL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[0] %08lx is not %08lx\n",
			(unsigned long)lqret[0],0x327eba8dUL);
		err=1;
		}
	if (lqret[1] != 0x201a49ccL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[1] %08lx is not %08lx\n",
			(unsigned long)lqret[1],0x201a49ccUL);
		err=1;
		}
	if (lqret[2] != 0x70d7a63aL)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[2] %08lx is not %08lx\n",
			(unsigned long)lqret[2],0x70d7a63aUL);
		err=1;
		}
	if (lqret[3] != 0x501c2c26L)
		{
		TINYCLR_SSL_PRINTF("quad_cksum error, out[3] %08lx is not %08lx\n",
			(unsigned long)lqret[3],0x501c2c26UL);
		err=1;
		}
#endif
#endif

	TINYCLR_SSL_PRINTF("input word alignment test");
	for (i=0; i<4; i++)
		{
		TINYCLR_SSL_PRINTF(" %d",i);
		des_ncbc_encrypt(&(cbc_out[i]),cbc_in,
				 TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
				 &cbc_iv,DES_ENCRYPT);
		}
	TINYCLR_SSL_PRINTF("\noutput word alignment test");
	for (i=0; i<4; i++)
		{
		TINYCLR_SSL_PRINTF(" %d",i);
		des_ncbc_encrypt(cbc_out,&(cbc_in[i]),
				 TINYCLR_SSL_STRLEN((char *)cbc_data)+1,ks,
				 &cbc_iv,DES_ENCRYPT);
		}
	TINYCLR_SSL_PRINTF("\n");
	TINYCLR_SSL_PRINTF("fast crypt test ");
	str=crypt("testing","ef");
	if (TINYCLR_SSL_STRCMP("efGnQx2725bI2",str) != 0)
		{
		TINYCLR_SSL_PRINTF("fast crypt error, %s should be efGnQx2725bI2\n",str);
		err=1;
		}
	str=crypt("bca76;23","yA");
	if (TINYCLR_SSL_STRCMP("yA1Rp/1hZXIJk",str) != 0)
		{
		TINYCLR_SSL_PRINTF("fast crypt error, %s should be yA1Rp/1hZXIJk\n",str);
		err=1;
		}
#ifdef OPENSSL_SYS_NETWARE
    if (err) TINYCLR_SSL_PRINTF("ERROR: %d\n", err);
#endif
	TINYCLR_SSL_PRINTF("\n");
	return(err);
	}
Esempio n. 29
0
bool ServerConf::isEligible(){
	mysqlpp::Connection con = mysqlpp::Connection(false);
	mysqlpp::Query q = NULL;
	mysqlpp::StoreQueryResult qr;

	char *dbKeyBytes;
	char *sharedKey;
	char buffer[25];

	int b64decodedSize;

	char yyyy[5], mm[3], dd[3], fdd[5];

	DES_cblock cb1, cb2, cb3;
	DES_key_schedule ks1, ks2, ks3;

	time_t now = time(0);
	time_t future;

	struct tm expire_time;
	struct tm creation_time;

	memset(buffer, 0x00, 25);
	memset(yyyy, 0x00, 5);
	memset(mm, 0x00, 3);
	memset(dd, 0x00, 3);
	memset(fdd, 0x00, 5);

	memset(&expire_time, 0x00, sizeof(struct tm));

	if( con.connect(mysql_sentry_dbname, mysql_sentry_ip, mysql_sentry_user, mysql_sentry_password, 3306) ){
		q = con.query("SELECT activationkey FROM commonconfiguration");
		//q = con.query("select 'et6hyYTfpfhqZqlPP6WRJRjI09MNuOSJ' as 'activationkey'");
		qr = q.store();
		if(qr.size() == 0){
			std::cerr << "Key info not found" << std::endl;
		}else{
			std::cout << "Base64 Encoded data is " << (*qr.data())[0] << std::endl;

			Base64Decode((char *)((*qr.data())[0]).c_str(), &dbKeyBytes, &b64decodedSize);

			if(b64decodedSize < 0x0A)
				std::cerr << "Invalid Key" << std::endl;
			else{
				sharedKey = getKey();



				memcpy((DES_cblock *)cb1, sharedKey,8);
				memcpy((DES_cblock *)cb2, sharedKey+8,8);
				memcpy((DES_cblock *)cb3, sharedKey+16,8);

				/*DES_set_key(&cb1,&ks1);
				DES_set_key(&cb2,&ks2);
				DES_set_key(&cb3,&ks3);*/

				des_set_odd_parity(&cb1);
				des_set_odd_parity(&cb2);
				des_set_odd_parity(&cb3);


				if(DES_set_key_checked(&cb1, &ks1) || DES_set_key_checked(&cb2, &ks2) || DES_set_key_checked(&cb3, &ks3) ){
					std::cerr << "3DES key scheduling error" << std::endl;
				}else{

					DES_ecb3_encrypt((C_Block *)dbKeyBytes,(C_Block *)buffer, &ks1, &ks2, &ks3, DES_DECRYPT);
					DES_ecb3_encrypt((C_Block *)dbKeyBytes+1,(C_Block *)buffer+1, &ks1, &ks2, &ks3, DES_DECRYPT);
					DES_ecb3_encrypt((C_Block *)dbKeyBytes+2,(C_Block *)buffer+2, &ks1, &ks2, &ks3, DES_DECRYPT);

					free(sharedKey);
					free(dbKeyBytes);

					//std::cout << "Buffer " << buffer << std::endl;

					strncpy(yyyy, buffer+8, 4);
					strncpy(mm, buffer+12, 2);
					strncpy(dd, buffer+14, 2);
					strncpy(fdd, buffer+16, 4);

					expire_time.tm_mday = atoi(dd);
					expire_time.tm_mon = atoi(mm)-1;
					expire_time.tm_year = atoi(yyyy) - 1900;

					creation_time = expire_time;

					expire_time.tm_mday +=   atoi(fdd);

					future = mktime(&creation_time);

					if(now > future)
					{
						future = mktime(&expire_time);

						if(future >= now)
							return true;
					}

				}
			}
		}
	}else{
		std::cerr << "Unable to check eligibility, Connection failed..." << std::endl;
	}

	if(con.connected())
		con.disconnect();

	return false;

}
Esempio n. 30
0
int main(void)
{
    int         i;
    BIO*        bio_out;

    DES_cblock          key;
    DES_key_schedule    schedule;

    unsigned char const iv_data[DES_KEY_SZ] = {
        0xcc, 0xfe, 0xcd, 0x3e, 0x21, 0xde, 0x1c, 0x31
    };

    unsigned char       iv[DES_KEY_SZ]; 

    char*   data    = "The worthwhile problems are the ones you can"
                      "really solve or help solve, the ones you can"
                      "really contribute something to. No "
                      "problem is too small or too trivial if we "
                      "can really do something about it."
                      "- Richard Feynman";

    int     length  = strlen(data);

    /* Intialise to '0' to indicate that '0' bytes of the IV has been used */
    int     num     = 0;

    /* Allocate the memory for the resulting ciphertext and plaintext */
    char*	ciphertext  = (char*) malloc(sizeof(char) * length); 
    char*	plaintext   = (char*) malloc(sizeof(char) * length); 

    /* Copy the IV data to the IV array. The IV array will be updated by the DES_ofb_encrypt call.*/
    memcpy(iv, iv_data, DES_KEY_SZ);

    /* In this example, we shall be generating a random key. Before this can
     * happen, we must seed the PRNG. OpenSSL ensures that the PRNG is transparently 
     * seeded on systems that provide the "/dev/urandom" file. 
     */

    /* Cater for seeding the PRNG in Windows */
#ifdef OPENSSL_SYS_WIN32
    /* Add entropy */
#endif

    /* Generate the random key (as expected by DES) */
    DES_random_key(&key);

    /* Check the odd parity of the key and its weakness. In doing so, 
     * convert to the architecture dependent format.  
     */
    DES_set_key_checked(&key, &schedule);

    DES_ofb64_encrypt(data, ciphertext, length, &schedule, (DES_cblock*)iv, &num);

    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    BIO_printf(bio_out, "Original plaintext: %s\n\n", data);

    BIO_printf(bio_out, "Ciphertext: ");

    /* print out the ciphertext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);

    BIO_printf(bio_out, "\n\n");

    /* Start the decryption process */

    /* Re-intialise to '0' to indicate that '0' bytes of the IV has been used */
    num = 0;

    /* First, copy the original IV data back to the IV array - as it was 
     * overwritten during the encryption process 
     */
    memcpy(iv, iv_data, DES_KEY_SZ);

    DES_ofb64_encrypt(ciphertext, plaintext, length, &schedule, (DES_cblock*)iv, &num);

    BIO_printf(bio_out, "Recovered plaintext: ");

    /* print out the plaintext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);

    BIO_printf(bio_out, "\n");

    BIO_free(bio_out);

    free(ciphertext);
    free(plaintext);

    return 0;

}