12 #define NewCipher(klass) \
13 TypedData_Wrap_Struct((klass), &ossl_cipher_type, 0)
14 #define AllocCipher(obj, ctx) do { \
15 (ctx) = EVP_CIPHER_CTX_new(); \
17 ossl_raise(rb_eRuntimeError, NULL); \
18 RTYPEDDATA_DATA(obj) = (ctx); \
20 #define GetCipherInit(obj, ctx) do { \
21 TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
23 #define GetCipher(obj, ctx) do { \
24 GetCipherInit((obj), (ctx)); \
26 ossl_raise(rb_eRuntimeError, "Cipher not initialized!"); \
35 static ID id_auth_tag_len, id_key_set;
38 static void ossl_cipher_free(
void *
ptr);
59 return EVP_CIPHER_CTX_cipher(ctx);
62 const EVP_CIPHER *cipher;
80 ret = ossl_cipher_alloc(
cCipher);
82 if (EVP_CipherInit_ex(ctx, cipher,
NULL,
NULL,
NULL, -1) != 1)
92 ossl_cipher_free(
void *
ptr)
94 EVP_CIPHER_CTX_free(
ptr);
115 const EVP_CIPHER *cipher;
124 if (!(cipher = EVP_get_cipherbyname(
name))) {
127 if (EVP_CipherInit_ex(ctx, cipher,
NULL,
NULL,
NULL, -1) != 1)
136 EVP_CIPHER_CTX *ctx1, *ctx2;
139 if (
self == other)
return self;
146 if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
153 add_cipher_name_to_ary(
const OBJ_NAME *
name,
VALUE ary)
166 ossl_s_ciphers(
VALUE self)
171 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
172 (
void(*)(
const OBJ_NAME*,
void*))add_cipher_name_to_ary,
188 ossl_cipher_reset(
VALUE self)
203 unsigned char key[EVP_MAX_KEY_LENGTH], *p_key =
NULL;
204 unsigned char iv[EVP_MAX_IV_LENGTH], *p_iv =
NULL;
215 "use %"PRIsVALUE"#pkcs5_keyivgen to derive key and IV",
216 cname, cname, cname);
219 if (
NIL_P(init_v))
memcpy(iv,
"OpenSSL for Ruby rulez!",
sizeof(iv));
223 memset(iv, 0, EVP_MAX_IV_LENGTH);
228 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
236 if (EVP_CipherInit_ex(ctx,
NULL,
NULL, p_key, p_iv, mode) != 1) {
261 return ossl_cipher_init(
argc,
argv,
self, 1);
279 return ossl_cipher_init(
argc,
argv,
self, 0);
306 const EVP_MD *digest;
307 VALUE vpass, vsalt, viter, vdigest;
308 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH], *salt =
NULL;
324 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
326 if (EVP_CipherInit_ex(ctx,
NULL,
NULL,
key, iv, -1) != 1)
328 OPENSSL_cleanse(
key,
sizeof key);
329 OPENSSL_cleanse(iv,
sizeof iv);
337 ossl_cipher_update_long(EVP_CIPHER_CTX *ctx,
unsigned char *out,
long *out_len_ptr,
338 const unsigned char *in,
long in_len)
345 int in_part_len = in_len > limit ? limit : (
int)in_len;
347 if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
348 &out_part_len, in, in_part_len))
351 out_len += out_part_len;
353 }
while ((in_len -= limit) > 0);
356 *out_len_ptr = out_len;
378 long in_len, out_len;
391 out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
394 "data too big to make output buffer: %ld bytes", in_len);
404 if (!ossl_cipher_update_long(ctx, (
unsigned char *)
RSTRING_PTR(
str), &out_len, in, in_len))
427 ossl_cipher_final(
VALUE self)
435 if (!EVP_CipherFinal_ex(ctx, (
unsigned char *)
RSTRING_PTR(
str), &out_len))
451 ossl_cipher_name(
VALUE self)
457 return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx)));
480 key_len = EVP_CIPHER_CTX_key_length(ctx);
513 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER)
514 iv_len = (
int)(
VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
516 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
534 ossl_cipher_is_authenticated(
VALUE self)
540 return (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) ?
Qtrue :
Qfalse;
562 ossl_cipher_set_auth_data(
VALUE self,
VALUE data)
566 long in_len, out_len;
574 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
577 if (!ossl_cipher_update_long(ctx,
NULL, &out_len, in, in_len))
608 if (!
NIL_P(vtag_len))
613 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
637 ossl_cipher_set_auth_tag(
VALUE self,
VALUE vtag)
648 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
669 ossl_cipher_set_auth_tag_len(
VALUE self,
VALUE vlen)
675 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
696 ossl_cipher_set_iv_length(
VALUE self,
VALUE iv_length)
702 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
712 EVP_CIPHER_CTX_set_app_data(ctx, (
void *)(
VALUE)
len);
730 ossl_cipher_set_key_length(
VALUE self,
VALUE key_length)
736 if (EVP_CIPHER_CTX_set_key_length(ctx,
len) != 1)
753 ossl_cipher_set_padding(
VALUE self,
VALUE padding)
759 if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
771 ossl_cipher_key_length(
VALUE self)
777 return INT2NUM(EVP_CIPHER_CTX_key_length(ctx));
787 ossl_cipher_iv_length(
VALUE self)
793 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER)
794 len = (
int)(
VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
796 len = EVP_CIPHER_CTX_iv_length(ctx);
808 ossl_cipher_block_size(
VALUE self)
814 return INT2NUM(EVP_CIPHER_CTX_block_size(ctx));