static mrb_value mrb_des_decrypt(mrb_state *mrb, mrb_value self) {
  mrb_value mode, key, source, dest, iv;
  unsigned char output[100];
  des_context ctx;
  mrb_int len=8;

  memset(output, 0, sizeof(output));

  mrb_get_args(mrb, "SSSS", &mode, &key, &source, &iv);

  des_init(&ctx);
  des_setkey_dec(&ctx, RSTRING_PTR(key));

  if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "CBC", 3)) == 0) {
    des_crypt_cbc(&ctx, DES_DECRYPT, RSTRING_LEN(source), RSTRING_PTR(iv),
        RSTRING_PTR(source), output);
    len = RSTRING_LEN(source);
  } else if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "ECB", 3)) == 0) {
    des_crypt_ecb(&ctx, RSTRING_PTR(source), output);
  } else {
    des_free(&ctx);
    return mrb_nil_value();
  }

  des_free(&ctx);
  return mrb_str_new(mrb, output, len);
}
Exemple #2
0
void eid2_decrypt_block(u8 *block, u32 length)
{
	u8 tmp[0x08], iv[0x08];
	int block_size = 8;
	int num_blocks = (int)(length / block_size);

	memcpy(iv, eid2_des_iv, 0x08);

	if(num_blocks > 0)
	{
		int i, j;
		des_context ctx;
		des_setkey_dec(&ctx, eid2_des_key);
		for(i = 0; i < num_blocks - 1; ++i)
		{
			u8 *ptr = block + i * block_size;
			memcpy(tmp, ptr, block_size);
			des_crypt_ecb(&ctx, ptr, ptr);
			if(i > 0)
			{
				for(j = 0; j < block_size; ++j)
					ptr[j] = ptr[j] ^ iv[j];
			}
			memcpy(iv, tmp, block_size);
		}
	}
}
void
cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH],
    unsigned char *src,
    unsigned char *dst)
{
    des_context ctx;

    des_setkey_enc(&ctx, key);
    des_crypt_ecb(&ctx, src, dst);
}
void
cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH],
    unsigned char *src,
    unsigned char *dst)
{
    des_context ctx;

    ASSERT (polar_ok(des_setkey_enc(&ctx, key)));
    ASSERT (polar_ok(des_crypt_ecb(&ctx, src, dst)));
}
Exemple #5
0
void test_suite_des_decrypt_ecb( char *hex_key_string, char *hex_src_string, char *hex_dst_string ) {
    unsigned char key_str[100];
    unsigned char src_str[100];
    unsigned char dst_str[100];
    des_context ctx;

    memset(key_str, 0x00, 100);
    memset(src_str, 0x00, 100);
    memset(dst_str, 0x00, 100);

    memcpy(key_str, hex_key_string, strlen(hex_key_string) + 1);
    memcpy(src_str, hex_src_string, strlen(hex_src_string) + 1);

    des_setkey_dec( &ctx, key_str);
    TEST_ASSERT( des_crypt_ecb( &ctx, src_str, dst_str ) == 0 );

    memcpy(hex_dst_string, dst_str, strlen(dst_str) + 1);
}
Exemple #6
0
static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
        const unsigned char *input, unsigned char *output )
{
    ((void) operation);
    return des_crypt_ecb( (des_context *) ctx, input, output );
}