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
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;
311 rb_scan_args(argc, argv,
"13", &vpass, &vsalt, &viter, &vdigest);
322 EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
360 out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
369 if (!EVP_CipherUpdate(ctx, (
unsigned char *)
RSTRING_PTR(str), &out_len, in, in_len))
399 str =
rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
444 if (
RSTRING_LEN(key) < EVP_CIPHER_CTX_key_length(ctx))
476 if (
RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx))
485 #ifdef HAVE_AUTHENTICATED_ENCRYPTION
519 if (!EVP_CipherUpdate(ctx,
NULL, &out_len, in, in_len))
525 #define ossl_is_gcm(nid) (nid) == NID_aes_128_gcm || \
526 (nid) == NID_aes_192_gcm || \
527 (nid) == NID_aes_256_gcm
530 ossl_get_gcm_auth_tag(EVP_CIPHER_CTX *ctx,
int len)
535 tag =
ALLOC_N(
unsigned char, len);
537 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, len, tag))
573 nid = EVP_CIPHER_CTX_nid(ctx);
575 if (ossl_is_gcm(nid)) {
576 return ossl_get_gcm_auth_tag(ctx, tag_len);
584 ossl_set_gcm_auth_tag(EVP_CIPHER_CTX *ctx,
unsigned char *tag,
int tag_len)
586 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag_len, tag))
615 nid = EVP_CIPHER_CTX_nid(ctx);
617 if (ossl_is_gcm(nid)) {
618 ossl_set_gcm_auth_tag(ctx, tag, tag_len);
640 nid = EVP_CIPHER_CTX_nid(ctx);
642 if (ossl_is_gcm(nid)) {
649 #define ossl_cipher_set_auth_data rb_f_notimplement
650 #define ossl_cipher_get_auth_tag rb_f_notimplement
651 #define ossl_cipher_set_auth_tag rb_f_notimplement
652 #define ossl_cipher_is_authenticated rb_f_notimplement
674 if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
680 #if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
698 if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
703 #define ossl_cipher_set_padding rb_f_notimplement
706 #define CIPHER_0ARG_INT(func) \
708 ossl_cipher_##func(VALUE self) \
710 EVP_CIPHER_CTX *ctx; \
711 GetCipher(self, ctx); \
712 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 rb_check_frozen(obj)
#define AllocCipher(obj, ctx)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE ossl_cipher_update(int argc, VALUE *argv, VALUE self)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_obj_class(VALUE)
#define EVP_CipherFinal_ex(ctx, outm, outl)
#define GetCipher(obj, ctx)
VALUE rb_class_path(VALUE)
#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)
memset(y->frac+ix+1, 0,(y->Prec-(ix+1))*sizeof(BDIGIT))
#define StringValuePtr(v)
#define CIPHER_0ARG_INT(func)
#define SafeGetCipher(obj, ctx)
#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)
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)
RUBY_EXTERN VALUE rb_cObject
void ossl_raise(VALUE exc, const char *fmt,...)
#define RSTRING_LENINT(str)
static VALUE ossl_cipher_reset(VALUE self)
VALUE rb_str_new(const char *, long)
#define assert(condition)
void Init_ossl_cipher(void)
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)
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)
void rb_str_set_len(VALUE, long)