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;
68 "unsupported cipher algorithm: %"PRIsVALUE, obj);
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);
98 ossl_cipher_alloc(
VALUE klass)
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;
322 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
324 if (EVP_CipherInit_ex(ctx,
NULL,
NULL, key, iv, -1) != 1)
326 OPENSSL_cleanse(key,
sizeof key);
327 OPENSSL_cleanse(iv,
sizeof iv);
335 ossl_cipher_update_long(EVP_CIPHER_CTX *ctx,
unsigned char *out,
long *out_len_ptr,
336 const unsigned char *in,
long in_len)
339 int limit = INT_MAX / 2 + 1;
343 int in_part_len = in_len > limit ? limit : (int)in_len;
345 if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
346 &out_part_len, in, in_part_len))
349 out_len += out_part_len;
351 }
while ((in_len -= limit) > 0);
354 *out_len_ptr = out_len;
376 long in_len, out_len;
389 out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
392 "data too big to make output buffer: %ld bytes", in_len);
402 if (!ossl_cipher_update_long(ctx, (
unsigned char *)
RSTRING_PTR(str), &out_len, in, in_len))
425 ossl_cipher_final(
VALUE self)
432 str =
rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
433 if (!EVP_CipherFinal_ex(ctx, (
unsigned char *)
RSTRING_PTR(str), &out_len))
449 ossl_cipher_name(
VALUE self)
455 return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx)));
478 key_len = EVP_CIPHER_CTX_key_length(ctx);
511 if (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER)
512 iv_len = (int)(
VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
514 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
532 ossl_cipher_is_authenticated(
VALUE self)
538 return (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER) ?
Qtrue :
Qfalse;
560 ossl_cipher_set_auth_data(
VALUE self,
VALUE data)
564 long in_len, out_len;
573 if (!ossl_cipher_update_long(ctx,
NULL, &out_len, in, in_len))
604 if (!
NIL_P(vtag_len))
609 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
633 ossl_cipher_set_auth_tag(
VALUE self,
VALUE vtag)
644 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
665 ossl_cipher_set_auth_tag_len(
VALUE self,
VALUE vlen)
671 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
692 ossl_cipher_set_iv_length(
VALUE self,
VALUE iv_length)
698 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
708 EVP_CIPHER_CTX_set_app_data(ctx, (
void *)(
VALUE)len);
726 ossl_cipher_set_key_length(
VALUE self,
VALUE key_length)
732 if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
749 ossl_cipher_set_padding(
VALUE self,
VALUE padding)
755 if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
767 ossl_cipher_key_length(
VALUE self)
773 return INT2NUM(EVP_CIPHER_CTX_key_length(ctx));
783 ossl_cipher_iv_length(
VALUE self)
789 if (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER)
790 len = (int)(
VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
792 len = EVP_CIPHER_CTX_iv_length(ctx);
804 ossl_cipher_block_size(
VALUE self)
810 return INT2NUM(EVP_CIPHER_CTX_block_size(ctx));
void rb_warn(const char *fmt,...)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define AllocCipher(obj, ctx)
VALUE rb_ary_push(VALUE ary, VALUE item)
void rb_str_set_len(VALUE, long)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
#define GetCipher(obj, ctx)
#define EVP_CTRL_AEAD_GET_TAG
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
VALUE ossl_cipher_new(const EVP_CIPHER *cipher)
RUBY_EXTERN VALUE rb_cObject
const EVP_CIPHER * ossl_evp_get_cipherbyname(VALUE obj)
VALUE rb_str_resize(VALUE, long)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
#define EVP_CTRL_AEAD_SET_TAG
const EVP_MD * ossl_evp_get_digestbyname(VALUE obj)
VALUE rb_class_path(VALUE)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_ivar_set(VALUE, ID, VALUE)
#define GetCipherInit(obj, ctx)
register unsigned int len
#define StringValueCStr(v)
#define EVP_CTRL_AEAD_SET_IVLEN
void ossl_raise(VALUE exc, const char *fmt,...)
void Init_ossl_cipher(void)
#define RSTRING_LENINT(str)
#define rb_check_frozen(obj)
#define rb_intern_const(str)
VALUE rb_define_module(const char *name)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_attr_get(VALUE, ID)
VALUE rb_str_new(const char *, long)