ext/openssl/ossl_hmac.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ossl_hmac.c 42416 2013-08-06 23:32:42Z zzak $
00003  * 'OpenSSL for Ruby' project
00004  * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
00005  * All rights reserved.
00006  */
00007 /*
00008  * This program is licenced under the same licence as Ruby.
00009  * (See the file 'LICENCE'.)
00010  */
00011 #if !defined(OPENSSL_NO_HMAC)
00012 
00013 #include "ossl.h"
00014 
00015 #define MakeHMAC(obj, klass, ctx) \
00016     (obj) = Data_Make_Struct((klass), HMAC_CTX, 0, ossl_hmac_free, (ctx))
00017 #define GetHMAC(obj, ctx) do { \
00018     Data_Get_Struct((obj), HMAC_CTX, (ctx)); \
00019     if (!(ctx)) { \
00020         ossl_raise(rb_eRuntimeError, "HMAC wasn't initialized"); \
00021     } \
00022 } while (0)
00023 #define SafeGetHMAC(obj, ctx) do { \
00024     OSSL_Check_Kind((obj), cHMAC); \
00025     GetHMAC((obj), (ctx)); \
00026 } while (0)
00027 
00028 /*
00029  * Classes
00030  */
00031 VALUE cHMAC;
00032 VALUE eHMACError;
00033 
00034 /*
00035  * Public
00036  */
00037 
00038 /*
00039  * Private
00040  */
00041 static void
00042 ossl_hmac_free(HMAC_CTX *ctx)
00043 {
00044     HMAC_CTX_cleanup(ctx);
00045     ruby_xfree(ctx);
00046 }
00047 
00048 static VALUE
00049 ossl_hmac_alloc(VALUE klass)
00050 {
00051     HMAC_CTX *ctx;
00052     VALUE obj;
00053 
00054     MakeHMAC(obj, klass, ctx);
00055     HMAC_CTX_init(ctx);
00056 
00057     return obj;
00058 }
00059 
00060 
00061 /*
00062  *  call-seq:
00063  *     HMAC.new(key, digest) -> hmac
00064  *
00065  * Returns an instance of OpenSSL::HMAC set with the key and digest
00066  * algorithm to be used. The instance represents the initial state of
00067  * the message authentication code before any data has been processed.
00068  * To process data with it, use the instance method #update with your
00069  * data as an argument.
00070  *
00071  * === Example
00072  *
00073  *      key = 'key'
00074  *      digest = OpenSSL::Digest.new('sha1')
00075  *      instance = OpenSSL::HMAC.new(key, digest)
00076  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00077  *      instance.class
00078  *      #=> OpenSSL::HMAC
00079  *
00080  * === A note about comparisons
00081  *
00082  * Two instances won't be equal when they're compared, even if they have the
00083  * same value. Use #to_s or #hexdigest to return the authentication code that
00084  * the instance represents. For example:
00085  *
00086  *      other_instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
00087  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00088  *      instance
00089  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00090  *      instance == other_instance
00091  *      #=> false
00092  *      instance.to_s == other_instance.to_s
00093  *      #=> true
00094  *
00095  */
00096 static VALUE
00097 ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
00098 {
00099     HMAC_CTX *ctx;
00100 
00101     StringValue(key);
00102     GetHMAC(self, ctx);
00103     HMAC_Init(ctx, RSTRING_PTR(key), RSTRING_LENINT(key),
00104                  GetDigestPtr(digest));
00105 
00106     return self;
00107 }
00108 
00109 static VALUE
00110 ossl_hmac_copy(VALUE self, VALUE other)
00111 {
00112     HMAC_CTX *ctx1, *ctx2;
00113 
00114     rb_check_frozen(self);
00115     if (self == other) return self;
00116 
00117     GetHMAC(self, ctx1);
00118     SafeGetHMAC(other, ctx2);
00119 
00120     HMAC_CTX_copy(ctx1, ctx2);
00121     return self;
00122 }
00123 
00124 /*
00125  *  call-seq:
00126  *     hmac.update(string) -> self
00127  *
00128  * Returns +self+ updated with the message to be authenticated.
00129  * Can be called repeatedly with chunks of the message.
00130  *
00131  * === Example
00132  *
00133  *      first_chunk = 'The quick brown fox jumps '
00134  *      second_chunk = 'over the lazy dog'
00135  *
00136  *      instance.update(first_chunk)
00137  *      #=> 5b9a8038a65d571076d97fe783989e52278a492a
00138  *      instance.update(second_chunk)
00139  *      #=> de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
00140  *
00141  */
00142 static VALUE
00143 ossl_hmac_update(VALUE self, VALUE data)
00144 {
00145     HMAC_CTX *ctx;
00146 
00147     StringValue(data);
00148     GetHMAC(self, ctx);
00149     HMAC_Update(ctx, (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data));
00150 
00151     return self;
00152 }
00153 
00154 static void
00155 hmac_final(HMAC_CTX *ctx, unsigned char **buf, unsigned int *buf_len)
00156 {
00157     HMAC_CTX final;
00158 
00159     HMAC_CTX_copy(&final, ctx);
00160     if (!(*buf = OPENSSL_malloc(HMAC_size(&final)))) {
00161         HMAC_CTX_cleanup(&final);
00162         OSSL_Debug("Allocating %d mem", HMAC_size(&final));
00163         ossl_raise(eHMACError, "Cannot allocate memory for hmac");
00164     }
00165     HMAC_Final(&final, *buf, buf_len);
00166     HMAC_CTX_cleanup(&final);
00167 }
00168 
00169 /*
00170  *  call-seq:
00171  *     hmac.digest -> string
00172  *
00173  * Returns the authentication code an instance represents as a binary string.
00174  *
00175  * === Example
00176  *
00177  *      instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
00178  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00179  *      instance.digest
00180  *      #=> "\xF4+\xB0\xEE\xB0\x18\xEB\xBDE\x97\xAEr\x13q\x1E\xC6\a`\x84?"
00181  *
00182  */
00183 static VALUE
00184 ossl_hmac_digest(VALUE self)
00185 {
00186     HMAC_CTX *ctx;
00187     unsigned char *buf;
00188     unsigned int buf_len;
00189     VALUE digest;
00190 
00191     GetHMAC(self, ctx);
00192     hmac_final(ctx, &buf, &buf_len);
00193     digest = ossl_buf2str((char *)buf, buf_len);
00194 
00195     return digest;
00196 }
00197 
00198 /*
00199  *  call-seq:
00200  *     hmac.hexdigest -> string
00201  *
00202  * Returns the authentication code an instance represents as a hex-encoded
00203  * string.
00204  *
00205  */
00206 static VALUE
00207 ossl_hmac_hexdigest(VALUE self)
00208 {
00209     HMAC_CTX *ctx;
00210     unsigned char *buf;
00211     char *hexbuf;
00212     unsigned int buf_len;
00213     VALUE hexdigest;
00214 
00215     GetHMAC(self, ctx);
00216     hmac_final(ctx, &buf, &buf_len);
00217     if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
00218         OPENSSL_free(buf);
00219         ossl_raise(eHMACError, "Memory alloc error");
00220     }
00221     OPENSSL_free(buf);
00222     hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
00223 
00224     return hexdigest;
00225 }
00226 
00227 /*
00228  *  call-seq:
00229  *     hmac.reset -> self
00230  *
00231  * Returns +self+ as it was when it was first initialized, with all processed
00232  * data cleared from it.
00233  *
00234  * === Example
00235  *
00236  *      data = "The quick brown fox jumps over the lazy dog"
00237  *      instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
00238  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00239  *
00240  *      instance.update(data)
00241  *      #=> de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
00242  *      instance.reset
00243  *      #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
00244  *
00245  */
00246 static VALUE
00247 ossl_hmac_reset(VALUE self)
00248 {
00249     HMAC_CTX *ctx;
00250 
00251     GetHMAC(self, ctx);
00252     HMAC_Init(ctx, NULL, 0, NULL);
00253 
00254     return self;
00255 }
00256 
00257 /*
00258  *  call-seq:
00259  *     HMAC.digest(digest, key, data) -> aString
00260  *
00261  * Returns the authentication code as a binary string. The +digest+ parameter
00262  * must be an instance of OpenSSL::Digest.
00263  *
00264  * === Example
00265  *
00266  *      key = 'key'
00267  *      data = 'The quick brown fox jumps over the lazy dog'
00268  *      digest = OpenSSL::Digest.new('sha1')
00269  *
00270  *      hmac = OpenSSL::HMAC.digest(digest, key, data)
00271  *      #=> "\xDE|\x9B\x85\xB8\xB7\x8A\xA6\xBC\x8Az6\xF7\n\x90p\x1C\x9D\xB4\xD9"
00272  *
00273  */
00274 static VALUE
00275 ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
00276 {
00277     unsigned char *buf;
00278     unsigned int buf_len;
00279 
00280     StringValue(key);
00281     StringValue(data);
00282     buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
00283                (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
00284 
00285     return rb_str_new((const char *)buf, buf_len);
00286 }
00287 
00288 /*
00289  *  call-seq:
00290  *     HMAC.hexdigest(digest, key, data) -> aString
00291  *
00292  * Returns the authentication code as a hex-encoded string. The +digest+
00293  * parameter must be an instance of OpenSSL::Digest.
00294  *
00295  * === Example
00296  *
00297  *      key = 'key'
00298  *      data = 'The quick brown fox jumps over the lazy dog'
00299  *      digest = OpenSSL::Digest.new('sha1')
00300  *
00301  *      hmac = OpenSSL::HMAC.hexdigest(digest, key, data)
00302  *      #=> "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
00303  *
00304  */
00305 static VALUE
00306 ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data)
00307 {
00308     unsigned char *buf;
00309     char *hexbuf;
00310     unsigned int buf_len;
00311     VALUE hexdigest;
00312 
00313     StringValue(key);
00314     StringValue(data);
00315 
00316     buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
00317                (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
00318     if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
00319         ossl_raise(eHMACError, "Cannot convert buf to hexbuf");
00320     }
00321     hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
00322 
00323     return hexdigest;
00324 }
00325 
00326 /*
00327  * INIT
00328  */
00329 void
00330 Init_ossl_hmac()
00331 {
00332 #if 0
00333     /* :nodoc: */
00334     mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
00335 #endif
00336 
00337     eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError);
00338 
00339     cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject);
00340 
00341     rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
00342     rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3);
00343     rb_define_singleton_method(cHMAC, "hexdigest", ossl_hmac_s_hexdigest, 3);
00344 
00345     rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
00346     rb_define_copy_func(cHMAC, ossl_hmac_copy);
00347 
00348     rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
00349     rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
00350     rb_define_alias(cHMAC, "<<", "update");
00351     rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0);
00352     rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0);
00353     rb_define_alias(cHMAC, "inspect", "hexdigest");
00354     rb_define_alias(cHMAC, "to_s", "hexdigest");
00355 }
00356 
00357 #else /* NO_HMAC */
00358 #  warning >>> OpenSSL is compiled without HMAC support <<<
00359 void
00360 Init_ossl_hmac()
00361 {
00362     rb_warning("HMAC will NOT be avaible: OpenSSL is compiled without HMAC.");
00363 }
00364 #endif /* NO_HMAC */
00365 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7