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",
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;
311 rb_scan_args(argc, argv,
"13", &vpass, &vsalt, &viter, &vdigest);
322 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
361 out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
370 if (!EVP_CipherUpdate(ctx, (
unsigned char *)
RSTRING_PTR(str), &out_len, in, in_len))
395 str =
rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
440 if (
RSTRING_LEN(key) < EVP_CIPHER_CTX_key_length(ctx))
472 if (
RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx))
501 if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
507 #if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
525 if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
530 #define ossl_cipher_set_padding rb_f_notimplement
533 #define CIPHER_0ARG_INT(func) \
535 ossl_cipher_##func(VALUE self) \
537 EVP_CIPHER_CTX *ctx; \
538 GetCipher(self, ctx); \
539 return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(ctx))); \
#define RSTRING_LEN(string)
static VALUE ossl_cipher_alloc(VALUE klass)
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)
#define RB_OBJ_STRING(obj)
#define RSTRING_PTR(string)
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 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)
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)
register unsigned int len
#define GetCipherInit(obj, ctx)
static VALUE ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
#define rb_str_set_len(str, length)
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)
static void ossl_cipher_free(EVP_CIPHER_CTX *ctx)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)