|
|
cd5466 |
From 8253d7c9cea16c2aa009b59db4f5d93afb74c6eb Mon Sep 17 00:00:00 2001
|
|
|
cd5466 |
From: Kazuki Yamaguchi <k@rhe.jp>
|
|
|
cd5466 |
Date: Tue, 30 Jun 2020 14:27:13 +0900
|
|
|
cd5466 |
Subject: [PATCH 1/2] hmac: add a test case for OpenSSL::HMAC singleton methods
|
|
|
cd5466 |
|
|
|
cd5466 |
---
|
|
|
cd5466 |
test/openssl/test_hmac.rb | 9 +++++++++
|
|
|
cd5466 |
1 file changed, 9 insertions(+)
|
|
|
cd5466 |
|
|
|
cd5466 |
diff --git a/test/openssl/test_hmac.rb b/test/openssl/test_hmac.rb
|
|
|
cd5466 |
index 9cb3c5a86..7202a5902 100644
|
|
|
cd5466 |
--- a/test/openssl/test_hmac.rb
|
|
|
cd5466 |
+++ b/test/openssl/test_hmac.rb
|
|
|
cd5466 |
@@ -49,6 +49,15 @@ def test_eq
|
|
|
cd5466 |
refute_equal h1, h2.digest
|
|
|
cd5466 |
refute_equal h1, h3
|
|
|
cd5466 |
end
|
|
|
cd5466 |
+
|
|
|
cd5466 |
+ def test_singleton_methods
|
|
|
cd5466 |
+ # RFC 2202 2. Test Cases for HMAC-MD5
|
|
|
cd5466 |
+ key = ["0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"].pack("H*")
|
|
|
cd5466 |
+ digest = OpenSSL::HMAC.digest("MD5", key, "Hi There")
|
|
|
cd5466 |
+ assert_equal ["9294727a3638bb1c13f48ef8158bfc9d"].pack("H*"), digest
|
|
|
cd5466 |
+ hexdigest = OpenSSL::HMAC.hexdigest("MD5", key, "Hi There")
|
|
|
cd5466 |
+ assert_equal "9294727a3638bb1c13f48ef8158bfc9d", hexdigest
|
|
|
cd5466 |
+ end
|
|
|
cd5466 |
end
|
|
|
cd5466 |
|
|
|
cd5466 |
end
|
|
|
cd5466 |
|
|
|
cd5466 |
From 0317e2fc028be40a7d64d0e4337d3e21539613ce Mon Sep 17 00:00:00 2001
|
|
|
cd5466 |
From: Kazuki Yamaguchi <k@rhe.jp>
|
|
|
cd5466 |
Date: Mon, 18 May 2020 16:15:07 +0900
|
|
|
cd5466 |
Subject: [PATCH 2/2] hmac: migrate from the low-level HMAC API to the EVP API
|
|
|
cd5466 |
|
|
|
cd5466 |
Use the EVP API instead of the low-level HMAC API. Use of the HMAC API
|
|
|
cd5466 |
has been discouraged and is being marked as deprecated starting from
|
|
|
cd5466 |
OpenSSL 3.0.0.
|
|
|
cd5466 |
|
|
|
cd5466 |
The two singleton methods OpenSSL::HMAC, HMAC.digest and HMAC.hexdigest
|
|
|
cd5466 |
are now in lib/openssl/hmac.rb.
|
|
|
cd5466 |
---
|
|
|
cd5466 |
ext/openssl/extconf.rb | 3 +-
|
|
|
cd5466 |
ext/openssl/lib/openssl/hmac.rb | 40 +++++++
|
|
|
cd5466 |
ext/openssl/openssl_missing.c | 26 -----
|
|
|
cd5466 |
ext/openssl/openssl_missing.h | 10 +-
|
|
|
cd5466 |
ext/openssl/ossl.h | 1 -
|
|
|
cd5466 |
ext/openssl/ossl_hmac.c | 179 ++++++++------------------------
|
|
|
cd5466 |
6 files changed, 89 insertions(+), 170 deletions(-)
|
|
|
cd5466 |
|
|
|
cd5466 |
diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb
|
|
|
cd5466 |
index 693e55cd9..063498a76 100644
|
|
|
cd5466 |
--- a/ext/openssl/extconf.rb
|
|
|
cd5466 |
+++ b/ext/openssl/extconf.rb
|
|
|
cd5466 |
@@ -141,8 +141,7 @@ def find_openssl_library
|
|
|
cd5466 |
have_func("BN_GENCB_get_arg")
|
|
|
cd5466 |
have_func("EVP_MD_CTX_new")
|
|
|
cd5466 |
have_func("EVP_MD_CTX_free")
|
|
|
cd5466 |
-have_func("HMAC_CTX_new")
|
|
|
cd5466 |
-have_func("HMAC_CTX_free")
|
|
|
cd5466 |
+have_func("EVP_MD_CTX_pkey_ctx")
|
|
|
cd5466 |
have_func("X509_STORE_get_ex_data")
|
|
|
cd5466 |
have_func("X509_STORE_set_ex_data")
|
|
|
cd5466 |
have_func("X509_STORE_get_ex_new_index")
|
|
|
cd5466 |
diff --git a/ext/openssl/lib/openssl/hmac.rb b/ext/openssl/lib/openssl/hmac.rb
|
|
|
cd5466 |
index 3d4427611d..9bc8bc8df3 100644
|
|
|
cd5466 |
--- a/ext/openssl/lib/openssl/hmac.rb
|
|
|
cd5466 |
+++ b/ext/openssl/lib/openssl/hmac.rb
|
|
|
cd5466 |
@@ -9,5 +9,45 @@ def ==(other)
|
|
|
cd5466 |
|
|
|
cd5466 |
OpenSSL.fixed_length_secure_compare(self.digest, other.digest)
|
|
|
cd5466 |
end
|
|
|
cd5466 |
+
|
|
|
cd5466 |
+ class << self
|
|
|
cd5466 |
+ # :call-seq:
|
|
|
cd5466 |
+ # HMAC.digest(digest, key, data) -> aString
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # Returns the authentication code as a binary string. The _digest_ parameter
|
|
|
cd5466 |
+ # specifies the digest algorithm to use. This may be a String representing
|
|
|
cd5466 |
+ # the algorithm name or an instance of OpenSSL::Digest.
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # === Example
|
|
|
cd5466 |
+ # key = 'key'
|
|
|
cd5466 |
+ # data = 'The quick brown fox jumps over the lazy dog'
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # hmac = OpenSSL::HMAC.digest('SHA1', key, data)
|
|
|
cd5466 |
+ # #=> "\xDE|\x9B\x85\xB8\xB7\x8A\xA6\xBC\x8Az6\xF7\n\x90p\x1C\x9D\xB4\xD9"
|
|
|
cd5466 |
+ def digest(digest, key, data)
|
|
|
cd5466 |
+ hmac = new(key, digest)
|
|
|
cd5466 |
+ hmac << data
|
|
|
cd5466 |
+ hmac.digest
|
|
|
cd5466 |
+ end
|
|
|
cd5466 |
+
|
|
|
cd5466 |
+ # :call-seq:
|
|
|
cd5466 |
+ # HMAC.hexdigest(digest, key, data) -> aString
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # Returns the authentication code as a hex-encoded string. The _digest_
|
|
|
cd5466 |
+ # parameter specifies the digest algorithm to use. This may be a String
|
|
|
cd5466 |
+ # representing the algorithm name or an instance of OpenSSL::Digest.
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # === Example
|
|
|
cd5466 |
+ # key = 'key'
|
|
|
cd5466 |
+ # data = 'The quick brown fox jumps over the lazy dog'
|
|
|
cd5466 |
+ #
|
|
|
cd5466 |
+ # hmac = OpenSSL::HMAC.hexdigest('SHA1', key, data)
|
|
|
cd5466 |
+ # #=> "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
|
|
|
cd5466 |
+ def hexdigest(digest, key, data)
|
|
|
cd5466 |
+ hmac = new(key, digest)
|
|
|
cd5466 |
+ hmac << data
|
|
|
cd5466 |
+ hmac.hexdigest
|
|
|
cd5466 |
+ end
|
|
|
cd5466 |
+ end
|
|
|
cd5466 |
end
|
|
|
cd5466 |
end
|
|
|
cd5466 |
diff --git a/ext/openssl/openssl_missing.c b/ext/openssl/openssl_missing.c
|
|
|
cd5466 |
index b36ef0288..010c158dc 100644
|
|
|
cd5466 |
--- a/ext/openssl/openssl_missing.c
|
|
|
cd5466 |
+++ b/ext/openssl/openssl_missing.c
|
|
|
cd5466 |
@@ -13,9 +13,6 @@
|
|
|
cd5466 |
#if !defined(OPENSSL_NO_ENGINE)
|
|
|
cd5466 |
# include <openssl/engine.h>
|
|
|
cd5466 |
#endif
|
|
|
cd5466 |
-#if !defined(OPENSSL_NO_HMAC)
|
|
|
cd5466 |
-# include <openssl/hmac.h>
|
|
|
cd5466 |
-#endif
|
|
|
cd5466 |
#include <openssl/x509_vfy.h>
|
|
|
cd5466 |
|
|
|
cd5466 |
#include "openssl_missing.h"
|
|
|
cd5466 |
@@ -58,29 +55,6 @@ ossl_EC_curve_nist2nid(const char *name)
|
|
|
cd5466 |
#endif
|
|
|
cd5466 |
|
|
|
cd5466 |
/*** added in 1.1.0 ***/
|
|
|
cd5466 |
-#if !defined(HAVE_HMAC_CTX_NEW)
|
|
|
cd5466 |
-HMAC_CTX *
|
|
|
cd5466 |
-ossl_HMAC_CTX_new(void)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
|
|
cd5466 |
- if (!ctx)
|
|
|
cd5466 |
- return NULL;
|
|
|
cd5466 |
- HMAC_CTX_init(ctx);
|
|
|
cd5466 |
- return ctx;
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-#endif
|
|
|
cd5466 |
-
|
|
|
cd5466 |
-#if !defined(HAVE_HMAC_CTX_FREE)
|
|
|
cd5466 |
-void
|
|
|
cd5466 |
-ossl_HMAC_CTX_free(HMAC_CTX *ctx)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- if (ctx) {
|
|
|
cd5466 |
- HMAC_CTX_cleanup(ctx);
|
|
|
cd5466 |
- OPENSSL_free(ctx);
|
|
|
cd5466 |
- }
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-#endif
|
|
|
cd5466 |
-
|
|
|
cd5466 |
#if !defined(HAVE_X509_CRL_GET0_SIGNATURE)
|
|
|
cd5466 |
void
|
|
|
cd5466 |
ossl_X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
|
|
|
cd5466 |
diff --git a/ext/openssl/openssl_missing.h b/ext/openssl/openssl_missing.h
|
|
|
cd5466 |
index 7d218f86f..06d2a9082 100644
|
|
|
cd5466 |
--- a/ext/openssl/openssl_missing.h
|
|
|
cd5466 |
+++ b/ext/openssl/openssl_missing.h
|
|
|
cd5466 |
@@ -54,14 +54,8 @@ int ossl_EC_curve_nist2nid(const char *);
|
|
|
cd5466 |
# define EVP_MD_CTX_free EVP_MD_CTX_destroy
|
|
|
cd5466 |
#endif
|
|
|
cd5466 |
|
|
|
cd5466 |
-#if !defined(HAVE_HMAC_CTX_NEW)
|
|
|
cd5466 |
-HMAC_CTX *ossl_HMAC_CTX_new(void);
|
|
|
cd5466 |
-# define HMAC_CTX_new ossl_HMAC_CTX_new
|
|
|
cd5466 |
-#endif
|
|
|
cd5466 |
-
|
|
|
cd5466 |
-#if !defined(HAVE_HMAC_CTX_FREE)
|
|
|
cd5466 |
-void ossl_HMAC_CTX_free(HMAC_CTX *);
|
|
|
cd5466 |
-# define HMAC_CTX_free ossl_HMAC_CTX_free
|
|
|
cd5466 |
+#if !defined(HAVE_EVP_MD_CTX_PKEY_CTX)
|
|
|
cd5466 |
+# define EVP_MD_CTX_pkey_ctx(x) (x)->pctx
|
|
|
cd5466 |
#endif
|
|
|
cd5466 |
|
|
|
cd5466 |
#if !defined(HAVE_X509_STORE_GET_EX_DATA)
|
|
|
cd5466 |
diff --git a/ext/openssl/ossl.h b/ext/openssl/ossl.h
|
|
|
cd5466 |
index c20f506bd..577eb6d6b 100644
|
|
|
cd5466 |
--- a/ext/openssl/ossl.h
|
|
|
cd5466 |
+++ b/ext/openssl/ossl.h
|
|
|
cd5466 |
@@ -24,7 +24,6 @@
|
|
|
cd5466 |
#include <openssl/ssl.h>
|
|
|
cd5466 |
#include <openssl/pkcs12.h>
|
|
|
cd5466 |
#include <openssl/pkcs7.h>
|
|
|
cd5466 |
-#include <openssl/hmac.h>
|
|
|
cd5466 |
#include <openssl/rand.h>
|
|
|
cd5466 |
#include <openssl/conf.h>
|
|
|
cd5466 |
#ifndef OPENSSL_NO_TS
|
|
|
cd5466 |
diff --git a/ext/openssl/ossl_hmac.c b/ext/openssl/ossl_hmac.c
|
|
|
cd5466 |
index 70e9fb819..a21db6c48 100644
|
|
|
cd5466 |
--- a/ext/openssl/ossl_hmac.c
|
|
|
cd5466 |
+++ b/ext/openssl/ossl_hmac.c
|
|
|
cd5466 |
@@ -7,14 +7,12 @@
|
|
|
cd5466 |
* This program is licensed under the same licence as Ruby.
|
|
|
cd5466 |
* (See the file 'LICENCE'.)
|
|
|
cd5466 |
*/
|
|
|
cd5466 |
-#if !defined(OPENSSL_NO_HMAC)
|
|
|
cd5466 |
-
|
|
|
cd5466 |
#include "ossl.h"
|
|
|
cd5466 |
|
|
|
cd5466 |
#define NewHMAC(klass) \
|
|
|
cd5466 |
TypedData_Wrap_Struct((klass), &ossl_hmac_type, 0)
|
|
|
cd5466 |
#define GetHMAC(obj, ctx) do { \
|
|
|
cd5466 |
- TypedData_Get_Struct((obj), HMAC_CTX, &ossl_hmac_type, (ctx)); \
|
|
|
cd5466 |
+ TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_hmac_type, (ctx)); \
|
|
|
cd5466 |
if (!(ctx)) { \
|
|
|
cd5466 |
ossl_raise(rb_eRuntimeError, "HMAC wasn't initialized"); \
|
|
|
cd5466 |
} \
|
|
|
cd5466 |
@@ -36,7 +34,7 @@ VALUE eHMACError;
|
|
|
cd5466 |
static void
|
|
|
cd5466 |
ossl_hmac_free(void *ctx)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX_free(ctx);
|
|
|
cd5466 |
+ EVP_MD_CTX_free(ctx);
|
|
|
cd5466 |
}
|
|
|
cd5466 |
|
|
|
cd5466 |
static const rb_data_type_t ossl_hmac_type = {
|
|
|
cd5466 |
@@ -51,12 +49,12 @@ static VALUE
|
|
|
cd5466 |
ossl_hmac_alloc(VALUE klass)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
VALUE obj;
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
|
|
|
cd5466 |
obj = NewHMAC(klass);
|
|
|
cd5466 |
- ctx = HMAC_CTX_new();
|
|
|
cd5466 |
+ ctx = EVP_MD_CTX_new();
|
|
|
cd5466 |
if (!ctx)
|
|
|
cd5466 |
- ossl_raise(eHMACError, NULL);
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_MD_CTX");
|
|
|
cd5466 |
RTYPEDDATA_DATA(obj) = ctx;
|
|
|
cd5466 |
|
|
|
cd5466 |
return obj;
|
|
|
cd5466 |
@@ -76,8 +74,7 @@ ossl_hmac_alloc(VALUE klass)
|
|
|
cd5466 |
* === Example
|
|
|
cd5466 |
*
|
|
|
cd5466 |
* key = 'key'
|
|
|
cd5466 |
- * digest = OpenSSL::Digest.new('sha1')
|
|
|
cd5466 |
- * instance = OpenSSL::HMAC.new(key, digest)
|
|
|
cd5466 |
+ * instance = OpenSSL::HMAC.new(key, 'SHA1')
|
|
|
cd5466 |
* #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
|
|
|
cd5466 |
* instance.class
|
|
|
cd5466 |
* #=> OpenSSL::HMAC
|
|
|
cd5466 |
@@ -86,7 +83,7 @@ ossl_hmac_alloc(VALUE klass)
|
|
|
cd5466 |
*
|
|
|
cd5466 |
* Two instances can be securely compared with #== in constant time:
|
|
|
cd5466 |
*
|
|
|
cd5466 |
- * other_instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
|
|
|
cd5466 |
+ * other_instance = OpenSSL::HMAC.new('key', 'SHA1')
|
|
|
cd5466 |
* #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
|
|
|
cd5466 |
* instance == other_instance
|
|
|
cd5466 |
* #=> true
|
|
|
cd5466 |
@@ -95,12 +92,23 @@ ossl_hmac_alloc(VALUE klass)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
+ EVP_PKEY *pkey;
|
|
|
cd5466 |
|
|
|
cd5466 |
- StringValue(key);
|
|
|
cd5466 |
GetHMAC(self, ctx);
|
|
|
cd5466 |
- HMAC_Init_ex(ctx, RSTRING_PTR(key), RSTRING_LENINT(key),
|
|
|
cd5466 |
- ossl_evp_get_digestbyname(digest), NULL);
|
|
|
cd5466 |
+ StringValue(key);
|
|
|
cd5466 |
+ pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
|
|
|
cd5466 |
+ (unsigned char *)RSTRING_PTR(key),
|
|
|
cd5466 |
+ RSTRING_LENINT(key));
|
|
|
cd5466 |
+ if (!pkey)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_PKEY_new_mac_key");
|
|
|
cd5466 |
+ if (EVP_DigestSignInit(ctx, NULL, ossl_evp_get_digestbyname(digest),
|
|
|
cd5466 |
+ NULL, pkey) != 1) {
|
|
|
cd5466 |
+ EVP_PKEY_free(pkey);
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_DigestSignInit");
|
|
|
cd5466 |
+ }
|
|
|
cd5466 |
+ /* Decrement reference counter; EVP_MD_CTX still keeps it */
|
|
|
cd5466 |
+ EVP_PKEY_free(pkey);
|
|
|
cd5466 |
|
|
|
cd5466 |
return self;
|
|
|
cd5466 |
}
|
|
|
cd5466 |
@@ -108,16 +116,15 @@ ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_copy(VALUE self, VALUE other)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx1, *ctx2;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx1, *ctx2;
|
|
|
cd5466 |
|
|
|
cd5466 |
rb_check_frozen(self);
|
|
|
cd5466 |
if (self == other) return self;
|
|
|
cd5466 |
|
|
|
cd5466 |
GetHMAC(self, ctx1);
|
|
|
cd5466 |
GetHMAC(other, ctx2);
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- if (!HMAC_CTX_copy(ctx1, ctx2))
|
|
|
cd5466 |
- ossl_raise(eHMACError, "HMAC_CTX_copy");
|
|
|
cd5466 |
+ if (EVP_MD_CTX_copy(ctx1, ctx2) != 1)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_MD_CTX_copy");
|
|
|
cd5466 |
return self;
|
|
|
cd5466 |
}
|
|
|
cd5466 |
|
|
|
cd5466 |
@@ -142,33 +149,16 @@ ossl_hmac_copy(VALUE self, VALUE other)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_update(VALUE self, VALUE data)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
|
|
|
cd5466 |
StringValue(data);
|
|
|
cd5466 |
GetHMAC(self, ctx);
|
|
|
cd5466 |
- HMAC_Update(ctx, (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data));
|
|
|
cd5466 |
+ if (EVP_DigestSignUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data)) != 1)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_DigestSignUpdate");
|
|
|
cd5466 |
|
|
|
cd5466 |
return self;
|
|
|
cd5466 |
}
|
|
|
cd5466 |
|
|
|
cd5466 |
-static void
|
|
|
cd5466 |
-hmac_final(HMAC_CTX *ctx, unsigned char *buf, unsigned int *buf_len)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- HMAC_CTX *final;
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- final = HMAC_CTX_new();
|
|
|
cd5466 |
- if (!final)
|
|
|
cd5466 |
- ossl_raise(eHMACError, "HMAC_CTX_new");
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- if (!HMAC_CTX_copy(final, ctx)) {
|
|
|
cd5466 |
- HMAC_CTX_free(final);
|
|
|
cd5466 |
- ossl_raise(eHMACError, "HMAC_CTX_copy");
|
|
|
cd5466 |
- }
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- HMAC_Final(final, buf, buf_len);
|
|
|
cd5466 |
- HMAC_CTX_free(final);
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-
|
|
|
cd5466 |
/*
|
|
|
cd5466 |
* call-seq:
|
|
|
cd5466 |
* hmac.digest -> string
|
|
|
cd5466 |
@@ -176,7 +166,7 @@ hmac_final(HMAC_CTX *ctx, unsigned char *buf, unsigned int *buf_len)
|
|
|
cd5466 |
* Returns the authentication code an instance represents as a binary string.
|
|
|
cd5466 |
*
|
|
|
cd5466 |
* === Example
|
|
|
cd5466 |
- * instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
|
|
|
cd5466 |
+ * instance = OpenSSL::HMAC.new('key', 'SHA1')
|
|
|
cd5466 |
* #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
|
|
|
cd5466 |
* instance.digest
|
|
|
cd5466 |
* #=> "\xF4+\xB0\xEE\xB0\x18\xEB\xBDE\x97\xAEr\x13q\x1E\xC6\a`\x84?"
|
|
|
cd5466 |
@@ -184,15 +174,16 @@ hmac_final(HMAC_CTX *ctx, unsigned char *buf, unsigned int *buf_len)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_digest(VALUE self)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
- unsigned int buf_len;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
+ size_t buf_len;
|
|
|
cd5466 |
VALUE ret;
|
|
|
cd5466 |
|
|
|
cd5466 |
GetHMAC(self, ctx);
|
|
|
cd5466 |
ret = rb_str_new(NULL, EVP_MAX_MD_SIZE);
|
|
|
cd5466 |
- hmac_final(ctx, (unsigned char *)RSTRING_PTR(ret), &buf_len);
|
|
|
cd5466 |
- assert(buf_len <= EVP_MAX_MD_SIZE);
|
|
|
cd5466 |
- rb_str_set_len(ret, buf_len);
|
|
|
cd5466 |
+ if (EVP_DigestSignFinal(ctx, (unsigned char *)RSTRING_PTR(ret),
|
|
|
cd5466 |
+ &buf_len) != 1)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_DigestSignFinal");
|
|
|
cd5466 |
+ rb_str_set_len(ret, (long)buf_len);
|
|
|
cd5466 |
|
|
|
cd5466 |
return ret;
|
|
|
cd5466 |
}
|
|
|
cd5466 |
@@ -207,13 +198,14 @@ ossl_hmac_digest(VALUE self)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_hexdigest(VALUE self)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
unsigned char buf[EVP_MAX_MD_SIZE];
|
|
|
cd5466 |
- unsigned int buf_len;
|
|
|
cd5466 |
+ size_t buf_len;
|
|
|
cd5466 |
VALUE ret;
|
|
|
cd5466 |
|
|
|
cd5466 |
GetHMAC(self, ctx);
|
|
|
cd5466 |
- hmac_final(ctx, buf, &buf_len);
|
|
|
cd5466 |
+ if (EVP_DigestSignFinal(ctx, buf, &buf_len) != 1)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_DigestSignFinal");
|
|
|
cd5466 |
ret = rb_str_new(NULL, buf_len * 2);
|
|
|
cd5466 |
ossl_bin2hex(buf, RSTRING_PTR(ret), buf_len);
|
|
|
cd5466 |
|
|
|
cd5466 |
@@ -230,7 +222,7 @@ ossl_hmac_hexdigest(VALUE self)
|
|
|
cd5466 |
* === Example
|
|
|
cd5466 |
*
|
|
|
cd5466 |
* data = "The quick brown fox jumps over the lazy dog"
|
|
|
cd5466 |
- * instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
|
|
|
cd5466 |
+ * instance = OpenSSL::HMAC.new('key', 'SHA1')
|
|
|
cd5466 |
* #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
|
|
|
cd5466 |
*
|
|
|
cd5466 |
* instance.update(data)
|
|
|
cd5466 |
@@ -242,84 +234,17 @@ ossl_hmac_hexdigest(VALUE self)
|
|
|
cd5466 |
static VALUE
|
|
|
cd5466 |
ossl_hmac_reset(VALUE self)
|
|
|
cd5466 |
{
|
|
|
cd5466 |
- HMAC_CTX *ctx;
|
|
|
cd5466 |
+ EVP_MD_CTX *ctx;
|
|
|
cd5466 |
+ EVP_PKEY *pkey;
|
|
|
cd5466 |
|
|
|
cd5466 |
GetHMAC(self, ctx);
|
|
|
cd5466 |
- HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
|
|
|
cd5466 |
+ pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
|
|
|
cd5466 |
+ if (EVP_DigestSignInit(ctx, NULL, EVP_MD_CTX_md(ctx), NULL, pkey) != 1)
|
|
|
cd5466 |
+ ossl_raise(eHMACError, "EVP_DigestSignInit");
|
|
|
cd5466 |
|
|
|
cd5466 |
return self;
|
|
|
cd5466 |
}
|
|
|
cd5466 |
|
|
|
cd5466 |
-/*
|
|
|
cd5466 |
- * call-seq:
|
|
|
cd5466 |
- * HMAC.digest(digest, key, data) -> aString
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * Returns the authentication code as a binary string. The _digest_ parameter
|
|
|
cd5466 |
- * specifies the digest algorithm to use. This may be a String representing
|
|
|
cd5466 |
- * the algorithm name or an instance of OpenSSL::Digest.
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * === Example
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * key = 'key'
|
|
|
cd5466 |
- * data = 'The quick brown fox jumps over the lazy dog'
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * hmac = OpenSSL::HMAC.digest('sha1', key, data)
|
|
|
cd5466 |
- * #=> "\xDE|\x9B\x85\xB8\xB7\x8A\xA6\xBC\x8Az6\xF7\n\x90p\x1C\x9D\xB4\xD9"
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- */
|
|
|
cd5466 |
-static VALUE
|
|
|
cd5466 |
-ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- unsigned char *buf;
|
|
|
cd5466 |
- unsigned int buf_len;
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- StringValue(key);
|
|
|
cd5466 |
- StringValue(data);
|
|
|
cd5466 |
- buf = HMAC(ossl_evp_get_digestbyname(digest), RSTRING_PTR(key),
|
|
|
cd5466 |
- RSTRING_LENINT(key), (unsigned char *)RSTRING_PTR(data),
|
|
|
cd5466 |
- RSTRING_LEN(data), NULL, &buf_len);
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- return rb_str_new((const char *)buf, buf_len);
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-
|
|
|
cd5466 |
-/*
|
|
|
cd5466 |
- * call-seq:
|
|
|
cd5466 |
- * HMAC.hexdigest(digest, key, data) -> aString
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * Returns the authentication code as a hex-encoded string. The _digest_
|
|
|
cd5466 |
- * parameter specifies the digest algorithm to use. This may be a String
|
|
|
cd5466 |
- * representing the algorithm name or an instance of OpenSSL::Digest.
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * === Example
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * key = 'key'
|
|
|
cd5466 |
- * data = 'The quick brown fox jumps over the lazy dog'
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- * hmac = OpenSSL::HMAC.hexdigest('sha1', key, data)
|
|
|
cd5466 |
- * #=> "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
|
|
|
cd5466 |
- *
|
|
|
cd5466 |
- */
|
|
|
cd5466 |
-static VALUE
|
|
|
cd5466 |
-ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- unsigned char buf[EVP_MAX_MD_SIZE];
|
|
|
cd5466 |
- unsigned int buf_len;
|
|
|
cd5466 |
- VALUE ret;
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- StringValue(key);
|
|
|
cd5466 |
- StringValue(data);
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- if (!HMAC(ossl_evp_get_digestbyname(digest), RSTRING_PTR(key),
|
|
|
cd5466 |
- RSTRING_LENINT(key), (unsigned char *)RSTRING_PTR(data),
|
|
|
cd5466 |
- RSTRING_LEN(data), buf, &buf_len))
|
|
|
cd5466 |
- ossl_raise(eHMACError, "HMAC");
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- ret = rb_str_new(NULL, buf_len * 2);
|
|
|
cd5466 |
- ossl_bin2hex(buf, RSTRING_PTR(ret), buf_len);
|
|
|
cd5466 |
-
|
|
|
cd5466 |
- return ret;
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-
|
|
|
cd5466 |
/*
|
|
|
cd5466 |
* INIT
|
|
|
cd5466 |
*/
|
|
|
cd5466 |
@@ -353,8 +278,7 @@ Init_ossl_hmac(void)
|
|
|
cd5466 |
* data1 = File.read("file1")
|
|
|
cd5466 |
* data2 = File.read("file2")
|
|
|
cd5466 |
* key = "key"
|
|
|
cd5466 |
- * digest = OpenSSL::Digest.new('SHA256')
|
|
|
cd5466 |
- * hmac = OpenSSL::HMAC.new(key, digest)
|
|
|
cd5466 |
+ * hmac = OpenSSL::HMAC.new(key, 'SHA256')
|
|
|
cd5466 |
* hmac << data1
|
|
|
cd5466 |
* hmac << data2
|
|
|
cd5466 |
* mac = hmac.digest
|
|
|
cd5466 |
@@ -364,8 +288,6 @@ Init_ossl_hmac(void)
|
|
|
cd5466 |
cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject);
|
|
|
cd5466 |
|
|
|
cd5466 |
rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
|
|
|
cd5466 |
- rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3);
|
|
|
cd5466 |
- rb_define_singleton_method(cHMAC, "hexdigest", ossl_hmac_s_hexdigest, 3);
|
|
|
cd5466 |
|
|
|
cd5466 |
rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
|
|
|
cd5466 |
rb_define_method(cHMAC, "initialize_copy", ossl_hmac_copy, 1);
|
|
|
cd5466 |
@@ -378,12 +300,3 @@ Init_ossl_hmac(void)
|
|
|
cd5466 |
rb_define_alias(cHMAC, "inspect", "hexdigest");
|
|
|
cd5466 |
rb_define_alias(cHMAC, "to_s", "hexdigest");
|
|
|
cd5466 |
}
|
|
|
cd5466 |
-
|
|
|
cd5466 |
-#else /* NO_HMAC */
|
|
|
cd5466 |
-# warning >>> OpenSSL is compiled without HMAC support <<<
|
|
|
cd5466 |
-void
|
|
|
cd5466 |
-Init_ossl_hmac(void)
|
|
|
cd5466 |
-{
|
|
|
cd5466 |
- rb_warning("HMAC is not available: OpenSSL is compiled without HMAC.");
|
|
|
cd5466 |
-}
|
|
|
cd5466 |
-#endif /* NO_HMAC */
|
|
|
cd5466 |
--
|
|
|
cd5466 |
2.34.1
|
|
|
cd5466 |
|