summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_cipher.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/openssl/ossl_cipher.c')
-rw-r--r--ext/openssl/ossl_cipher.c53
1 files changed, 19 insertions, 34 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index 93b3f6ccf97..209ebca604f 100644
--- a/ext/openssl/ossl_cipher.c
+++ b/ext/openssl/ossl_cipher.c
@@ -54,7 +54,7 @@ ossl_cipher_new(const EVP_CIPHER *cipher)
ret = ossl_cipher_alloc(cCipher);
GetCipher(ret, ctx);
EVP_CIPHER_CTX_init(ctx);
- if (EVP_CipherInit(ctx, cipher, NULL, NULL, -1) != 1)
+ if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return ret;
@@ -79,6 +79,7 @@ ossl_cipher_alloc(VALUE klass)
VALUE obj;
MakeCipher(obj, klass, ctx);
+ EVP_CIPHER_CTX_init(ctx);
return obj;
}
@@ -97,9 +98,8 @@ ossl_cipher_initialize(VALUE self, VALUE str)
if (!(cipher = EVP_get_cipherbyname(name))) {
ossl_raise(rb_eRuntimeError, "Unsupported cipher algorithm (%s).", name);
}
- EVP_CIPHER_CTX_init(ctx);
- if (EVP_CipherInit(ctx, cipher, NULL, NULL, -1) != 1)
- ossl_raise(eCipherError, NULL);
+ if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
+ ossl_raise(eCipherError, NULL);
return self;
}
@@ -113,8 +113,8 @@ ossl_cipher_copy(VALUE self, VALUE other)
GetCipher(self, ctx1);
SafeGetCipher(other, ctx2);
-
- memcpy(ctx1, ctx2, sizeof(EVP_CIPHER_CTX));
+ if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
+ ossl_raise(eCipherError, NULL);
return self;
}
@@ -125,7 +125,7 @@ ossl_cipher_reset(VALUE self)
EVP_CIPHER_CTX *ctx;
GetCipher(self, ctx);
- if (EVP_CipherInit(ctx, NULL, NULL, NULL, -1) != 1)
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return self;
@@ -166,7 +166,7 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
}
}
- if (EVP_CipherInit(ctx, NULL, NULL, NULL, 1) != 1) {
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1) != 1) {
ossl_raise(eCipherError, NULL);
}
@@ -175,7 +175,7 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL);
- if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) {
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1) {
ossl_raise(eCipherError, NULL);
}
}
@@ -211,7 +211,7 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
}
}
- if (EVP_CipherInit(ctx, NULL, NULL, NULL, 0) != 1) {
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 0) != 1) {
ossl_raise(eCipherError, NULL);
}
@@ -220,7 +220,7 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL);
- if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) {
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1) {
ossl_raise(eCipherError, NULL);
}
}
@@ -259,7 +259,7 @@ ossl_cipher_final(VALUE self)
GetCipher(self, ctx);
str = rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
- if (!EVP_CipherFinal(ctx, RSTRING(str)->ptr, &out_len))
+ if (!EVP_CipherFinal_ex(ctx, RSTRING(str)->ptr, &out_len))
ossl_raise(eCipherError, NULL);
assert(out_len <= RSTRING(str)->len);
RSTRING(str)->len = out_len;
@@ -289,7 +289,7 @@ ossl_cipher_set_key(VALUE self, VALUE key)
if (RSTRING(key)->len < EVP_CIPHER_CTX_key_length(ctx))
ossl_raise(eCipherError, "key length too short");
- if (EVP_CipherInit(ctx, NULL, RSTRING(key)->ptr, NULL, -1) != 1)
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, RSTRING(key)->ptr, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return key;
@@ -306,8 +306,8 @@ ossl_cipher_set_iv(VALUE self, VALUE iv)
if (RSTRING(iv)->len < EVP_CIPHER_CTX_iv_length(ctx))
ossl_raise(eCipherError, "iv length too short");
- if (EVP_CipherInit(ctx, NULL, NULL, RSTRING(iv)->ptr, -1) != 1)
- ossl_raise(eCipherError, NULL);
+ if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, RSTRING(iv)->ptr, -1) != 1)
+ ossl_raise(eCipherError, NULL);
return iv;
}
@@ -315,13 +315,12 @@ ossl_cipher_set_iv(VALUE self, VALUE iv)
static VALUE
ossl_cipher_set_padding(VALUE self, VALUE padding)
{
-#if defined(HAVE_ST_FLAGS)
+#if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
EVP_CIPHER_CTX *ctx;
GetCipher(self, ctx);
-
if (EVP_CIPHER_CTX_set_padding(ctx, NUM2INT(padding)) != 1)
- ossl_raise(eCipherError, NULL);
+ ossl_raise(eCipherError, NULL);
#else
rb_notimplement();
#endif
@@ -351,32 +350,18 @@ Init_ossl_cipher(void)
cCipher = rb_define_class_under(mCipher, "Cipher", rb_cObject);
rb_define_alloc_func(cCipher, ossl_cipher_alloc);
- rb_define_method(cCipher, "initialize", ossl_cipher_initialize, 1);
-
rb_define_copy_func(cCipher, ossl_cipher_copy);
-
+ rb_define_method(cCipher, "initialize", ossl_cipher_initialize, 1);
rb_define_method(cCipher, "reset", ossl_cipher_reset, 0);
-
rb_define_method(cCipher, "encrypt", ossl_cipher_encrypt, -1);
rb_define_method(cCipher, "decrypt", ossl_cipher_decrypt, -1);
rb_define_method(cCipher, "update", ossl_cipher_update, 1);
- rb_define_alias(cCipher, "<<", "update");
rb_define_method(cCipher, "final", ossl_cipher_final, 0);
-
rb_define_method(cCipher, "name", ossl_cipher_name, 0);
-
rb_define_method(cCipher, "key=", ossl_cipher_set_key, 1);
rb_define_method(cCipher, "key_len", ossl_cipher_key_length, 0);
-/*
- * TODO
- * int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
- */
rb_define_method(cCipher, "iv=", ossl_cipher_set_iv, 1);
rb_define_method(cCipher, "iv_len", ossl_cipher_iv_length, 0);
-
rb_define_method(cCipher, "block_size", ossl_cipher_block_size, 0);
-
rb_define_method(cCipher, "padding=", ossl_cipher_set_padding, 1);
-
-} /* Init_ossl_cipher */
-
+}