2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is licenced under the same licence as Ruby.
|
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
#define GetDigest(obj, ctx) do { \
|
|
|
|
Data_Get_Struct(obj, EVP_MD_CTX, ctx); \
|
|
|
|
if (!ctx) { \
|
|
|
|
ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SafeGetDigest(obj, ctx) do { \
|
|
|
|
OSSL_Check_Kind(obj, cDigest); \
|
|
|
|
GetDigest(obj, ctx); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cDigest;
|
|
|
|
VALUE eDigestError;
|
|
|
|
|
2003-09-05 13:08:40 +04:00
|
|
|
static VALUE ossl_digest_alloc(VALUE klass);
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
|
|
|
const EVP_MD *
|
|
|
|
GetDigestPtr(VALUE obj)
|
|
|
|
{
|
2007-03-29 21:29:03 +04:00
|
|
|
const EVP_MD *md;
|
|
|
|
|
2010-02-23 20:33:39 +03:00
|
|
|
if (TYPE(obj) == T_STRING) {
|
|
|
|
const char *name = StringValueCStr(obj);
|
2010-02-23 18:51:01 +03:00
|
|
|
|
2010-02-23 20:33:39 +03:00
|
|
|
md = EVP_get_digestbyname(name);
|
|
|
|
if (!md)
|
|
|
|
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
|
|
|
|
} else {
|
2007-03-29 21:29:03 +04:00
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
SafeGetDigest(obj, ctx);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
md = EVP_MD_CTX_md(ctx);
|
2007-03-29 21:29:03 +04:00
|
|
|
}
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
return md;
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
2003-09-05 13:08:40 +04:00
|
|
|
VALUE
|
|
|
|
ossl_digest_new(const EVP_MD *md)
|
2010-04-22 12:04:13 +04:00
|
|
|
{
|
2003-09-05 13:08:40 +04:00
|
|
|
VALUE ret;
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
ret = ossl_digest_alloc(cDigest);
|
|
|
|
GetDigest(ret, ctx);
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 14:48:43 +04:00
|
|
|
EVP_DigestInit_ex(ctx, md, NULL);
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2003-09-05 13:08:40 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_digest_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
ctx = EVP_MD_CTX_create();
|
|
|
|
if (ctx == NULL)
|
|
|
|
ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed");
|
|
|
|
obj = Data_Wrap_Struct(klass, 0, EVP_MD_CTX_destroy, ctx);
|
2008-02-25 11:51:18 +03:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE ossl_digest_update(VALUE, VALUE);
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Digest.new(string) -> digest
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
const EVP_MD *md;
|
|
|
|
VALUE type, data;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &type, &data);
|
2008-02-25 11:51:18 +03:00
|
|
|
md = GetDigestPtr(type);
|
2003-07-23 20:12:24 +04:00
|
|
|
if (!NIL_P(data)) StringValue(data);
|
2008-02-25 11:51:18 +03:00
|
|
|
|
2004-12-15 09:35:55 +03:00
|
|
|
GetDigest(self, ctx);
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 14:48:43 +04:00
|
|
|
EVP_DigestInit_ex(ctx, md, NULL);
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
if (!NIL_P(data)) return ossl_digest_update(self, data);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_digest_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx1, *ctx2;
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_check_frozen(self);
|
|
|
|
if (self == other) return self;
|
|
|
|
|
|
|
|
GetDigest(self, ctx1);
|
|
|
|
SafeGetDigest(other, ctx2);
|
|
|
|
|
|
|
|
if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
|
|
|
|
ossl_raise(eDigestError, NULL);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* digest.reset -> self
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_digest_reset(VALUE self)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 14:48:43 +04:00
|
|
|
EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* digest.update(string) -> aString
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
VALUE
|
|
|
|
ossl_digest_update(VALUE self, VALUE data)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
StringValue(data);
|
2004-12-15 09:35:55 +03:00
|
|
|
GetDigest(self, ctx);
|
2006-08-31 14:30:33 +04:00
|
|
|
EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data));
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-02-25 11:51:18 +03:00
|
|
|
* digest.finish -> aString
|
2007-03-29 21:29:03 +04:00
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
2008-02-25 11:51:18 +03:00
|
|
|
ossl_digest_finish(int argc, VALUE *argv, VALUE self)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
2008-02-25 11:51:18 +03:00
|
|
|
VALUE str;
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
rb_scan_args(argc, argv, "01", &str);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
if (NIL_P(str)) {
|
|
|
|
str = rb_str_new(NULL, EVP_MD_CTX_size(ctx));
|
|
|
|
} else {
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_resize(str, EVP_MD_CTX_size(ctx));
|
|
|
|
}
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2008-07-22 19:34:23 +04:00
|
|
|
EVP_DigestFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), NULL);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
return str;
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-02-25 11:51:18 +03:00
|
|
|
* digest.name -> string
|
2007-03-29 21:29:03 +04:00
|
|
|
*
|
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
2008-02-25 11:51:18 +03:00
|
|
|
ossl_digest_name(VALUE self)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
2004-12-15 09:35:55 +03:00
|
|
|
GetDigest(self, ctx);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 21:29:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-02-25 11:51:18 +03:00
|
|
|
* digest.digest_size -> integer
|
2007-03-29 21:29:03 +04:00
|
|
|
*
|
2008-02-25 11:51:18 +03:00
|
|
|
* Returns the output size of the digest.
|
2007-03-29 21:29:03 +04:00
|
|
|
*/
|
2003-07-23 20:12:24 +04:00
|
|
|
static VALUE
|
2008-02-25 11:51:18 +03:00
|
|
|
ossl_digest_size(VALUE self)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
return INT2NUM(EVP_MD_CTX_size(ctx));
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-02-25 11:51:18 +03:00
|
|
|
ossl_digest_block_length(VALUE self)
|
2003-07-23 20:12:24 +04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
return INT2NUM(EVP_MD_CTX_block_size(ctx));
|
2003-07-23 20:12:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Init_ossl_digest()
|
|
|
|
{
|
2008-02-25 11:51:18 +03:00
|
|
|
rb_require("digest");
|
|
|
|
|
2007-03-12 05:01:19 +03:00
|
|
|
#if 0 /* let rdoc know about mOSSL */
|
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
#endif
|
|
|
|
|
2008-02-25 11:51:18 +03:00
|
|
|
cDigest = rb_define_class_under(mOSSL, "Digest", rb_path2class("Digest::Class"));
|
2007-04-05 09:59:22 +04:00
|
|
|
eDigestError = rb_define_class_under(cDigest, "DigestError", eOSSLError);
|
2003-07-23 20:12:24 +04:00
|
|
|
|
|
|
|
rb_define_alloc_func(cDigest, ossl_digest_alloc);
|
2008-02-25 11:51:18 +03:00
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
|
|
|
|
rb_define_copy_func(cDigest, ossl_digest_copy);
|
2008-02-25 11:51:18 +03:00
|
|
|
rb_define_method(cDigest, "reset", ossl_digest_reset, 0);
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_define_method(cDigest, "update", ossl_digest_update, 1);
|
|
|
|
rb_define_alias(cDigest, "<<", "update");
|
2008-02-25 11:51:18 +03:00
|
|
|
rb_define_private_method(cDigest, "finish", ossl_digest_finish, -1);
|
|
|
|
rb_define_method(cDigest, "digest_length", ossl_digest_size, 0);
|
|
|
|
rb_define_method(cDigest, "block_length", ossl_digest_block_length, 0);
|
|
|
|
|
2003-07-23 20:12:24 +04:00
|
|
|
rb_define_method(cDigest, "name", ossl_digest_name, 0);
|
|
|
|
}
|