diff options
author | Michal Rokos <m.rokos@sh.cvut.cz> | 2002-03-11 17:20:04 +0000 |
---|---|---|
committer | Michal Rokos <m.rokos@sh.cvut.cz> | 2002-03-11 17:20:04 +0000 |
commit | 7ed17afc8c327bc88d1510255d284487d06f226c (patch) | |
tree | 52aa2aed34535a040ba155fef373e4394449dd33 /ossl_hmac.c | |
parent | 91e607a11b3862c0aa966712149b2afd2a00d90d (diff) | |
download | ruby-openssl-history-7ed17afc8c327bc88d1510255d284487d06f226c.tar.gz |
* Big internal cleanup (all structs with only 1 member rearranged)
* improved getting time_t from cTime
Diffstat (limited to 'ossl_hmac.c')
-rw-r--r-- | ossl_hmac.c | 64 |
1 files changed, 22 insertions, 42 deletions
diff --git a/ossl_hmac.c b/ossl_hmac.c index e49072b..f09775f 100644 --- a/ossl_hmac.c +++ b/ossl_hmac.c @@ -12,10 +12,8 @@ #include "ossl.h" -#define MakeHMAC(obj, hmacp) {\ - obj = Data_Make_Struct(cHMAC, ossl_hmac, 0, ossl_hmac_free, hmacp);\ -} -#define GetHMAC(obj, hmacp) Data_Get_Struct(obj, ossl_hmac, hmacp) +#define WrapHMAC(obj, ctx) obj = Data_Wrap_Struct(cHMAC, 0, CRYPTO_free, ctx) +#define GetHMAC(obj, ctx) Data_Get_Struct(obj, HMAC_CTX, ctx) /* * Classes @@ -24,36 +22,23 @@ VALUE cHMAC; VALUE eHMACError; /* - * Struct + * Public */ -typedef struct ossl_hmac_st { - HMAC_CTX *hmac; -} ossl_hmac; - -static void -ossl_hmac_free(ossl_hmac *hmacp) -{ - if (hmacp) { - if (hmacp->hmac) OPENSSL_free(hmacp->hmac); - hmacp->hmac = NULL; - free(hmacp); - } -} /* - * PUBLIC - */ - -/* - * PRIVATE + * Private */ static VALUE ossl_hmac_s_new(int argc, VALUE *argv, VALUE klass) { - ossl_hmac *hmacp = NULL; + HMAC_CTX *ctx = NULL; VALUE obj; - MakeHMAC(obj, hmacp); + if (!(ctx = OPENSSL_malloc(sizeof(HMAC_CTX)))) { + OSSL_Raise(eHMACError, ""); + } + WrapHMAC(obj, ctx); + rb_obj_call_init(obj, argc, argv); return obj; @@ -62,21 +47,18 @@ ossl_hmac_s_new(int argc, VALUE *argv, VALUE klass) static VALUE ossl_hmac_initialize(int argc, VALUE *argv, VALUE self) { - ossl_hmac *hmacp = NULL; + HMAC_CTX *ctx = NULL; const EVP_MD *md = NULL; VALUE key, digest; - GetHMAC(self, hmacp); + GetHMAC(self, ctx); rb_scan_args(argc, argv, "20", &key, &digest); key = rb_String(key); md = ossl_digest_get_EVP_MD(digest); - if (!(hmacp->hmac = OPENSSL_malloc(sizeof(HMAC_CTX)))) { - OSSL_Raise(eHMACError, ""); - } - HMAC_Init(hmacp->hmac, RSTRING(key)->ptr, RSTRING(key)->len, md); + HMAC_Init(ctx, RSTRING(key)->ptr, RSTRING(key)->len, md); return self; } @@ -84,13 +66,13 @@ ossl_hmac_initialize(int argc, VALUE *argv, VALUE self) static VALUE ossl_hmac_update(VALUE self, VALUE data) { - ossl_hmac *hmacp = NULL; + HMAC_CTX *ctx = NULL; - GetHMAC(self, hmacp); + GetHMAC(self, ctx); data = rb_String(data); - HMAC_Update(hmacp->hmac, RSTRING(data)->ptr, RSTRING(data)->len); + HMAC_Update(ctx, RSTRING(data)->ptr, RSTRING(data)->len); return self; } @@ -98,15 +80,14 @@ ossl_hmac_update(VALUE self, VALUE data) static VALUE ossl_hmac_hmac(VALUE self) { - ossl_hmac *hmacp = NULL; + HMAC_CTX *ctx = NULL, final; char *buf = NULL; int buf_len = 0; - HMAC_CTX final; VALUE str; - GetHMAC(self, hmacp); + GetHMAC(self, ctx); - if (!HMAC_CTX_copy(&final, hmacp->hmac)) { + if (!HMAC_CTX_copy(&final, ctx)) { OSSL_Raise(eHMACError, ""); } if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) { @@ -123,16 +104,15 @@ ossl_hmac_hmac(VALUE self) static VALUE ossl_hmac_hexhmac(VALUE self) { - ossl_hmac *hmacp = NULL; + HMAC_CTX *ctx = NULL, final; static const char hex[]="0123456789abcdef"; char *buf = NULL, *hexbuf = NULL; int i,buf_len = 0; - HMAC_CTX final; VALUE str; - GetHMAC(self, hmacp); + GetHMAC(self, ctx); - if (!HMAC_CTX_copy(&final, hmacp->hmac)) { + if (!HMAC_CTX_copy(&final, ctx)) { OSSL_Raise(eHMACError, "Cannot copy HMAC CTX"); } if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) { |