00001
00002
00003
00004
00005
00006
00007
00008
00009
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
00030
00031 VALUE cHMAC;
00032 VALUE eHMACError;
00033
00034
00035
00036
00037
00038
00039
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
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
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
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
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
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
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
00200
00201
00202
00203
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
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
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
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
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
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
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
00328
00329 void
00330 Init_ossl_hmac()
00331 {
00332 #if 0
00333
00334 mOSSL = rb_define_module("OpenSSL");
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
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
00365