-
Notifications
You must be signed in to change notification settings - Fork 177
Add support for CMAC #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kmfukuda
wants to merge
8
commits into
ruby:master
Choose a base branch
from
kmfukuda:mac
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add support for CMAC #891
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4e7b6d7
mac: add OpenSSL::MAC
kmfukuda ff27f8c
cmac: add OpenSSL::MAC::CMAC
kmfukuda 1e60515
mac: include ossl_mac.h at the correct position
kmfukuda 48ae091
mac: prevent resource leaks in OpenSSL::MAC#initialize and #initializ…
kmfukuda c5ad15d
mac: change OpenSSL::MAC to be defined if EVP_MAC is available
kmfukuda 97025e7
mac: remove OpenSSL::MAC#inspect
kmfukuda 0cedc00
mac: remove OpenSSL::MAC#==
kmfukuda 0cc2e61
mac: rename OpenSSL::MAC#*mac to #*digest
kmfukuda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
#include "ossl.h" | ||
|
||
#ifdef HAVE_TYPE_EVP_MAC_P | ||
|
||
#define NewMAC(klass) \ | ||
TypedData_Wrap_Struct((klass), &ossl_mac_type, 0) | ||
#define SetMAC(obj, ctx) do { \ | ||
if (!(ctx)) \ | ||
ossl_raise(rb_eRuntimeError, "MAC wasn't initialized"); \ | ||
RTYPEDDATA_DATA(obj) = (ctx); \ | ||
} while (0) | ||
#define GetMAC(obj, ctx) do { \ | ||
TypedData_Get_Struct((obj), EVP_MAC_CTX, &ossl_mac_type, (ctx)); \ | ||
if (!(ctx)) \ | ||
ossl_raise(rb_eRuntimeError, "MAC wasn't initialized"); \ | ||
} while (0) | ||
|
||
/* | ||
* Classes | ||
*/ | ||
static VALUE cMAC; | ||
static VALUE cCMAC; | ||
static VALUE eMACError; | ||
|
||
/* | ||
* Public | ||
*/ | ||
|
||
/* | ||
* Private | ||
*/ | ||
static void | ||
ossl_mac_free(void *ctx) | ||
{ | ||
EVP_MAC_CTX_free(ctx); | ||
} | ||
|
||
static const rb_data_type_t ossl_mac_type = { | ||
"OpenSSL/MAC", | ||
{ | ||
0, ossl_mac_free, | ||
}, | ||
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED, | ||
}; | ||
|
||
static VALUE | ||
ossl_mac_alloc(VALUE klass) | ||
{ | ||
return NewMAC(klass); | ||
} | ||
|
||
static VALUE | ||
ossl_mac_initialize(VALUE self, VALUE algorithm) | ||
{ | ||
EVP_MAC *mac; | ||
EVP_MAC_CTX *ctx; | ||
|
||
TypedData_Get_Struct(self, EVP_MAC_CTX, &ossl_mac_type, ctx); | ||
if (ctx) | ||
rb_raise(rb_eTypeError, "MAC already initialized"); | ||
|
||
mac = EVP_MAC_fetch(NULL, StringValueCStr(algorithm), NULL); | ||
if (!mac) | ||
ossl_raise(eMACError, "EVP_MAC_fetch"); | ||
ctx = EVP_MAC_CTX_new(mac); | ||
if (!ctx) { | ||
EVP_MAC_free(mac); | ||
ossl_raise(eMACError, "EVP_MAC_CTX_new"); | ||
} | ||
SetMAC(self, ctx); | ||
rhenium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return self; | ||
} | ||
|
||
static VALUE | ||
ossl_cmac_initialize(VALUE self, VALUE cipher, VALUE key) | ||
{ | ||
EVP_MAC_CTX *ctx; | ||
VALUE algorithm; | ||
OSSL_PARAM params[2]; | ||
|
||
algorithm = rb_str_new_literal("CMAC"); | ||
rb_call_super(1, &algorithm); | ||
|
||
GetMAC(self, ctx); | ||
StringValue(key); | ||
params[0] = OSSL_PARAM_construct_utf8_string("cipher", StringValueCStr(cipher), 0); | ||
params[1] = OSSL_PARAM_construct_end(); | ||
if (EVP_MAC_init(ctx, (unsigned char *)RSTRING_PTR(key), RSTRING_LEN(key), params) != 1) | ||
ossl_raise(eMACError, "EVP_MAC_init"); | ||
|
||
return self; | ||
} | ||
|
||
static VALUE | ||
ossl_mac_copy(VALUE self, VALUE other) | ||
{ | ||
EVP_MAC_CTX *ctx1, *ctx2; | ||
|
||
TypedData_Get_Struct(self, EVP_MAC_CTX, &ossl_mac_type, ctx2); | ||
if (ctx2) | ||
rb_raise(rb_eTypeError, "MAC already initialized"); | ||
|
||
rb_check_frozen(self); | ||
if (self == other) | ||
return self; | ||
|
||
GetMAC(other, ctx1); | ||
ctx2 = EVP_MAC_CTX_dup(ctx1); | ||
if (!ctx2) | ||
ossl_raise(eMACError, "EVP_MAC_CTX_dup"); | ||
SetMAC(self, ctx2); | ||
|
||
return self; | ||
} | ||
|
||
static VALUE | ||
ossl_mac_update(VALUE self, VALUE chunk) | ||
{ | ||
EVP_MAC_CTX *ctx; | ||
|
||
GetMAC(self, ctx); | ||
StringValue(chunk); | ||
if (EVP_MAC_update(ctx, (unsigned char *)RSTRING_PTR(chunk), RSTRING_LEN(chunk)) != 1) | ||
ossl_raise(eMACError, "EVP_MAC_update"); | ||
|
||
return self; | ||
} | ||
|
||
static VALUE | ||
ossl_mac_digest(VALUE self) | ||
{ | ||
VALUE ret; | ||
EVP_MAC_CTX *ctx1, *ctx2; | ||
size_t len; | ||
|
||
GetMAC(self, ctx1); | ||
if (EVP_MAC_final(ctx1, NULL, &len, 0) != 1) | ||
ossl_raise(eMACError, "EVP_MAC_final"); | ||
ret = rb_str_new(NULL, len); | ||
ctx2 = EVP_MAC_CTX_dup(ctx1); | ||
if (!ctx2) | ||
ossl_raise(eMACError, "EVP_MAC_CTX_dup"); | ||
if (EVP_MAC_final(ctx2, (unsigned char *)RSTRING_PTR(ret), &len, RSTRING_LEN(ret)) != 1) { | ||
EVP_MAC_CTX_free(ctx2); | ||
ossl_raise(eMACError, "EVP_MAC_final"); | ||
} | ||
EVP_MAC_CTX_free(ctx2); | ||
|
||
return ret; | ||
} | ||
|
||
/* | ||
* INIT | ||
*/ | ||
void | ||
Init_ossl_mac(void) | ||
{ | ||
#if 0 | ||
mOSSL = rb_define_module("OpenSSL"); | ||
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError); | ||
#endif | ||
|
||
cMAC = rb_define_class_under(mOSSL, "MAC", rb_cObject); | ||
rb_define_alloc_func(cMAC, ossl_mac_alloc); | ||
rb_define_method(cMAC, "initialize", ossl_mac_initialize, 1); | ||
rb_define_method(cMAC, "initialize_copy", ossl_mac_copy, 1); | ||
rb_define_method(cMAC, "update", ossl_mac_update, 1); | ||
rb_define_alias(cMAC, "<<", "update"); | ||
rb_define_method(cMAC, "digest", ossl_mac_digest, 0); | ||
|
||
cCMAC = rb_define_class_under(cMAC, "CMAC", cMAC); | ||
rb_define_method(cCMAC, "initialize", ossl_cmac_initialize, 2); | ||
|
||
eMACError = rb_define_class_under(mOSSL, "MACError", eOSSLError); | ||
} | ||
#else | ||
void | ||
Init_ossl_mac(void) | ||
{ | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#if !defined(_OSSL_MAC_H_) | ||
#define _OSSL_MAC_H_ | ||
|
||
void Init_ossl_mac(void); | ||
|
||
#endif /* _OSSL_MAC_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module OpenSSL | ||
if defined?(MAC) | ||
class MAC | ||
def hexdigest | ||
digest.unpack1('H*') | ||
end | ||
alias to_s hexdigest | ||
|
||
def base64digest | ||
[digest].pack('m0') | ||
end | ||
|
||
class CMAC < MAC | ||
class << self | ||
def digest(cipher, key, message) | ||
cmac = new(cipher, key) | ||
cmac << message | ||
cmac.send(__callee__) | ||
end | ||
alias hexdigest digest | ||
alias base64digest digest | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
require_relative "utils" | ||
|
||
if defined?(OpenSSL::MAC::CMAC) | ||
|
||
class OpenSSL::TestCMAC < OpenSSL::TestCase | ||
def test_cmac | ||
cmac = OpenSSL::MAC::CMAC.new("AES-128-CBC", ["2b7e151628aed2a6abf7158809cf4f3c"].pack("H*")) | ||
cmac.update(["6bc1bee22e409f96e93d7e117393172a"].pack("H*")) | ||
assert_equal ["070a16b46b4d4144f79bdd9dd04a287c"].pack("H*"), cmac.digest | ||
assert_equal "070a16b46b4d4144f79bdd9dd04a287c", cmac.hexdigest | ||
assert_equal "BwoWtGtNQUT3m92d0EoofA==", cmac.base64digest | ||
end | ||
|
||
def test_dup | ||
cmac1 = OpenSSL::MAC::CMAC.new("AES-192-CBC", ["8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"].pack("H*")) | ||
cmac2 = cmac1.dup | ||
assert_equal cmac2.digest, cmac1.digest | ||
|
||
cmac1.update("message") | ||
assert_not_equal cmac2.digest, cmac1.digest | ||
|
||
cmac2.update("message") | ||
assert_equal cmac2.digest, cmac1.digest | ||
end | ||
|
||
def test_class_methods | ||
assert_equal ["28a7023f452e8f82bd4bf28d8c37c35c"].pack("H*"), OpenSSL::MAC::CMAC.digest("AES-256-CBC", ["603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"].pack("H*"), ["6bc1bee22e409f96e93d7e117393172a"].pack("H*")) | ||
assert_equal "28a7023f452e8f82bd4bf28d8c37c35c", OpenSSL::MAC::CMAC.hexdigest("AES-256-CBC", ["603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"].pack("H*"), ["6bc1bee22e409f96e93d7e117393172a"].pack("H*")) | ||
assert_equal "KKcCP0Uuj4K9S/KNjDfDXA==", OpenSSL::MAC::CMAC.base64digest("AES-256-CBC", ["603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"].pack("H*"), ["6bc1bee22e409f96e93d7e117393172a"].pack("H*")) | ||
end | ||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.