Exemplo n.º 1
0
static mrb_value mrb_des3_encrypt(mrb_state *mrb, mrb_value self) {
  mrb_value mode, key, source, dest, iv;
  unsigned char output[100];
  des3_context ctx;
  mrb_int len=16;

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

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

  des3_init(&ctx);
  if (RSTRING_LEN(key) == 16) {
    des3_set2key_enc(&ctx, RSTRING_PTR(key));
  } else if (RSTRING_LEN(key) == 24) {
    des3_set3key_enc(&ctx, RSTRING_PTR(key));
  } else {
    des3_free(&ctx);
    return mrb_nil_value();
  }

  if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "CBC", 3)) == 0) {
    des3_crypt_cbc(&ctx, DES_ENCRYPT, 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) {
    des3_crypt_ecb(&ctx, RSTRING_PTR(source), output);
    len = 8;
  } else {
    des3_free(&ctx);
    return mrb_nil_value();
  }

  des3_free(&ctx);
  return mrb_str_new(mrb, output, len);
}
Exemplo n.º 2
0
static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
                                  unsigned int key_length )
{
    ((void) key_length);

    return des3_set2key_enc( (des3_context *) ctx, key );
}
Exemplo n.º 3
0
int
encrypt_3DES(unsigned char *p_key_str, int len_p_key_str, unsigned char *p_src_str, unsigned char *p_iv_str, int p_len_src_str, unsigned char *output, int *len_output)
{
	int ret, src_len;
	unsigned char src_str[100];
	unsigned char iv_str[100];
	unsigned char key_str[100];
	des3_context ctx;

	memset(src_str, 0x00, sizeof(src_str));
	memset(iv_str, 0x00, sizeof(iv_str));
	memset(key_str, 0x00, sizeof(key_str));

	/* Initiate seeds or initiate vector */
	memcpy(iv_str, p_iv_str, 8);					// -> initial seeds

	/* copy all bytes master key */
	memcpy(key_str, p_key_str, len_p_key_str);

	/* prepare source to decrypt */
	src_len = p_len_src_str;
	memcpy(src_str, p_src_str, src_len);

	if( len_p_key_str == 16 )				//means 2 keys
		des3_set2key_enc( &ctx, key_str );
	else if( len_p_key_str == 24 )			//means 3 keys
	des3_set3key_enc( &ctx, key_str );

	*len_output = src_len;
	ret = des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output );
	if( ret == 0 )
		return d_SUCCESS;

	return d_ERR;
}