13 #define WrapCipher(obj, klass, ctx) \ 14 (obj) = Data_Wrap_Struct((klass), 0, ossl_cipher_free, (ctx)) 15 #define MakeCipher(obj, klass, ctx) \ 16 (obj) = Data_Make_Struct((klass), EVP_CIPHER_CTX, 0, ossl_cipher_free, (ctx)) 17 #define AllocCipher(obj, ctx) \ 18 memset(DATA_PTR(obj) = (ctx) = ALLOC(EVP_CIPHER_CTX), 0, sizeof(EVP_CIPHER_CTX)) 19 #define GetCipherInit(obj, ctx) do { \ 20 Data_Get_Struct((obj), EVP_CIPHER_CTX, (ctx)); \ 22 #define GetCipher(obj, ctx) do { \ 23 GetCipherInit((obj), (ctx)); \ 25 ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \ 28 #define SafeGetCipher(obj, ctx) do { \ 29 OSSL_Check_Kind((obj), cCipher); \ 30 GetCipher((obj), (ctx)); \ 51 return EVP_CIPHER_CTX_cipher(ctx);
62 EVP_CIPHER_CTX_init(ctx);
76 EVP_CIPHER_CTX_cleanup(ctx);
103 const EVP_CIPHER *cipher;
105 unsigned char key[EVP_MAX_KEY_LENGTH];
113 EVP_CIPHER_CTX_init(ctx);
114 if (!(cipher = EVP_get_cipherbyname(
name))) {
123 memset(
key, 0, EVP_MAX_KEY_LENGTH);
133 EVP_CIPHER_CTX *ctx1, *ctx2;
136 if (
self == other)
return self;
149 #ifdef HAVE_OBJ_NAME_DO_ALL_SORTED 151 add_cipher_name_to_ary(
const OBJ_NAME *
name,
VALUE ary)
158 #ifdef HAVE_OBJ_NAME_DO_ALL_SORTED 171 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
172 (
void(*)(
const OBJ_NAME*,
void*))add_cipher_name_to_ary,
178 #define ossl_s_ciphers rb_f_notimplement 206 unsigned char key[EVP_MAX_KEY_LENGTH], *p_key =
NULL;
207 unsigned char iv[EVP_MAX_IV_LENGTH], *p_iv =
NULL;
218 "use %"PRIsVALUE"#pkcs5_keyivgen to derive key and IV",
219 cname, cname, cname);
222 if (
NIL_P(init_v)) memcpy(iv,
"OpenSSL for Ruby rulez!",
sizeof(iv));
226 memset(iv, 0, EVP_MAX_IV_LENGTH);
231 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
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,
334 const unsigned char *in,
long in_len)
338 #define UPDATE_LENGTH_LIMIT INT_MAX 340 #if SIZEOF_LONG > UPDATE_LENGTH_LIMIT 344 if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
345 &out_part_len, in, in_part_len))
347 out_len += out_part_len;
352 if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
353 &out_part_len, in, (
int)in_len))
355 if (out_len_ptr) *out_len_ptr = out_len += out_part_len;
377 long in_len, out_len;
387 out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
390 "data too big to make output buffer: %ld bytes", in_len);
430 str =
rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
507 if (
RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx))
516 #ifdef HAVE_AUTHENTICATED_ENCRYPTION 540 long in_len, out_len;
555 #define ossl_is_gcm(nid) (nid) == NID_aes_128_gcm || \ 556 (nid) == NID_aes_192_gcm || \ 557 (nid) == NID_aes_256_gcm 560 ossl_get_gcm_auth_tag(EVP_CIPHER_CTX *ctx,
int len)
565 tag =
ALLOC_N(
unsigned char, len);
567 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, len, tag))
603 nid = EVP_CIPHER_CTX_nid(ctx);
605 if (ossl_is_gcm(nid)) {
606 return ossl_get_gcm_auth_tag(ctx, tag_len);
614 ossl_set_gcm_auth_tag(EVP_CIPHER_CTX *ctx,
unsigned char *tag,
int tag_len)
616 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag_len, tag))
645 nid = EVP_CIPHER_CTX_nid(ctx);
647 if (ossl_is_gcm(nid)) {
648 ossl_set_gcm_auth_tag(ctx, tag, tag_len);
670 nid = EVP_CIPHER_CTX_nid(ctx);
672 if (ossl_is_gcm(nid)) {
679 #define ossl_cipher_set_auth_data rb_f_notimplement 680 #define ossl_cipher_get_auth_tag rb_f_notimplement 681 #define ossl_cipher_set_auth_tag rb_f_notimplement 682 #define ossl_cipher_is_authenticated rb_f_notimplement 704 if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
710 #if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING) 728 if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
733 #define ossl_cipher_set_padding rb_f_notimplement 736 #define CIPHER_0ARG_INT(func) \ 738 ossl_cipher_##func(VALUE self) \ 740 EVP_CIPHER_CTX *ctx; \ 741 GetCipher(self, ctx); \ 742 return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(ctx))); \ static VALUE ossl_cipher_alloc(VALUE klass)
#define ossl_cipher_set_auth_data
static VALUE ossl_cipher_final(VALUE self)
#define ossl_cipher_set_padding
#define OPENSSL_cleanse(p, l)
static VALUE ossl_cipher_init(int argc, VALUE *argv, VALUE self, int mode)
#define AllocCipher(obj, ctx)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE ossl_cipher_update(int argc, VALUE *argv, VALUE self)
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 EVP_CipherFinal_ex(ctx, outm, outl)
#define GetCipher(obj, ctx)
#define WrapCipher(obj, klass, ctx)
static VALUE ossl_cipher_set_key_length(VALUE self, VALUE key_length)
#define rb_define_copy_func(klass, func)
static VALUE ossl_cipher_initialize(VALUE self, VALUE str)
VALUE ossl_cipher_new(const EVP_CIPHER *cipher)
#define CIPHER_0ARG_INT(func)
#define SafeGetCipher(obj, ctx)
RUBY_EXTERN VALUE rb_cObject
#define EVP_CipherInit_ex(ctx, type, impl, key, iv, enc)
#define ossl_cipher_get_auth_tag
#define EVP_CIPHER_name(e)
const EVP_MD * GetDigestPtr(VALUE obj)
const EVP_CIPHER * GetCipherPtr(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.
static VALUE ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
static int ossl_cipher_update_long(EVP_CIPHER_CTX *ctx, unsigned char *out, long *out_len_ptr, const unsigned char *in, long in_len)
VALUE rb_class_path(VALUE)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static VALUE ossl_cipher_copy(VALUE self, VALUE other)
static VALUE ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
#define ossl_cipher_set_auth_tag
#define GetCipherInit(obj, ctx)
static VALUE ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
#define UPDATE_LENGTH_LIMIT
void ossl_raise(VALUE exc, const char *fmt,...)
static VALUE ossl_cipher_reset(VALUE self)
#define assert(condition)
#define StringValuePtr(v)
void Init_ossl_cipher(void)
#define RSTRING_LENINT(str)
#define rb_check_frozen(obj)
VALUE rb_define_module(const char *name)
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, EVP_CIPHER_CTX *in)
static VALUE ossl_cipher_set_key(VALUE self, VALUE key)
static VALUE ossl_cipher_name(VALUE self)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_new2(const char *)
void rb_warn(const char *fmt,...)
static VALUE ossl_cipher_set_iv(VALUE self, VALUE iv)
#define ossl_cipher_is_authenticated
static void ossl_cipher_free(EVP_CIPHER_CTX *ctx)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)