00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "ossl.h"
00012
00013 #define GetDigest(obj, ctx) do { \
00014 Data_Get_Struct((obj), EVP_MD_CTX, (ctx)); \
00015 if (!(ctx)) { \
00016 ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
00017 } \
00018 } while (0)
00019 #define SafeGetDigest(obj, ctx) do { \
00020 OSSL_Check_Kind((obj), cDigest); \
00021 GetDigest((obj), (ctx)); \
00022 } while (0)
00023
00024
00025
00026
00027 VALUE cDigest;
00028 VALUE eDigestError;
00029
00030 static VALUE ossl_digest_alloc(VALUE klass);
00031
00032
00033
00034
00035 const EVP_MD *
00036 GetDigestPtr(VALUE obj)
00037 {
00038 const EVP_MD *md;
00039 ASN1_OBJECT *oid = NULL;
00040
00041 if (TYPE(obj) == T_STRING) {
00042 const char *name = StringValueCStr(obj);
00043
00044 md = EVP_get_digestbyname(name);
00045 if (!md) {
00046 oid = OBJ_txt2obj(name, 0);
00047 md = EVP_get_digestbyobj(oid);
00048 ASN1_OBJECT_free(oid);
00049 }
00050 if(!md)
00051 ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
00052 } else {
00053 EVP_MD_CTX *ctx;
00054
00055 SafeGetDigest(obj, ctx);
00056
00057 md = EVP_MD_CTX_md(ctx);
00058 }
00059
00060 return md;
00061 }
00062
00063 VALUE
00064 ossl_digest_new(const EVP_MD *md)
00065 {
00066 VALUE ret;
00067 EVP_MD_CTX *ctx;
00068
00069 ret = ossl_digest_alloc(cDigest);
00070 GetDigest(ret, ctx);
00071 if (EVP_DigestInit_ex(ctx, md, NULL) != 1) {
00072 ossl_raise(eDigestError, "Digest initialization failed.");
00073 }
00074
00075 return ret;
00076 }
00077
00078
00079
00080
00081 static VALUE
00082 ossl_digest_alloc(VALUE klass)
00083 {
00084 EVP_MD_CTX *ctx;
00085 VALUE obj;
00086
00087 ctx = EVP_MD_CTX_create();
00088 if (ctx == NULL)
00089 ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed");
00090 obj = Data_Wrap_Struct(klass, 0, EVP_MD_CTX_destroy, ctx);
00091
00092 return obj;
00093 }
00094
00095 VALUE ossl_digest_update(VALUE, VALUE);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 static VALUE
00116 ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
00117 {
00118 EVP_MD_CTX *ctx;
00119 const EVP_MD *md;
00120 VALUE type, data;
00121
00122 rb_scan_args(argc, argv, "11", &type, &data);
00123 md = GetDigestPtr(type);
00124 if (!NIL_P(data)) StringValue(data);
00125
00126 GetDigest(self, ctx);
00127 if (EVP_DigestInit_ex(ctx, md, NULL) != 1) {
00128 ossl_raise(eDigestError, "Digest initialization failed.");
00129 }
00130
00131 if (!NIL_P(data)) return ossl_digest_update(self, data);
00132 return self;
00133 }
00134
00135 static VALUE
00136 ossl_digest_copy(VALUE self, VALUE other)
00137 {
00138 EVP_MD_CTX *ctx1, *ctx2;
00139
00140 rb_check_frozen(self);
00141 if (self == other) return self;
00142
00143 GetDigest(self, ctx1);
00144 SafeGetDigest(other, ctx2);
00145
00146 if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
00147 ossl_raise(eDigestError, NULL);
00148 }
00149 return self;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 static VALUE
00161 ossl_digest_reset(VALUE self)
00162 {
00163 EVP_MD_CTX *ctx;
00164
00165 GetDigest(self, ctx);
00166 if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL) != 1) {
00167 ossl_raise(eDigestError, "Digest initialization failed.");
00168 }
00169
00170 return self;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 VALUE
00189 ossl_digest_update(VALUE self, VALUE data)
00190 {
00191 EVP_MD_CTX *ctx;
00192
00193 StringValue(data);
00194 GetDigest(self, ctx);
00195 EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data));
00196
00197 return self;
00198 }
00199
00200
00201
00202
00203
00204
00205 static VALUE
00206 ossl_digest_finish(int argc, VALUE *argv, VALUE self)
00207 {
00208 EVP_MD_CTX *ctx;
00209 VALUE str;
00210
00211 rb_scan_args(argc, argv, "01", &str);
00212
00213 GetDigest(self, ctx);
00214
00215 if (NIL_P(str)) {
00216 str = rb_str_new(NULL, EVP_MD_CTX_size(ctx));
00217 } else {
00218 StringValue(str);
00219 rb_str_resize(str, EVP_MD_CTX_size(ctx));
00220 }
00221
00222 EVP_DigestFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), NULL);
00223
00224 return str;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 static VALUE
00239 ossl_digest_name(VALUE self)
00240 {
00241 EVP_MD_CTX *ctx;
00242
00243 GetDigest(self, ctx);
00244
00245 return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 static VALUE
00261 ossl_digest_size(VALUE self)
00262 {
00263 EVP_MD_CTX *ctx;
00264
00265 GetDigest(self, ctx);
00266
00267 return INT2NUM(EVP_MD_CTX_size(ctx));
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 static VALUE
00284 ossl_digest_block_length(VALUE self)
00285 {
00286 EVP_MD_CTX *ctx;
00287
00288 GetDigest(self, ctx);
00289
00290 return INT2NUM(EVP_MD_CTX_block_size(ctx));
00291 }
00292
00293
00294
00295
00296 void
00297 Init_ossl_digest()
00298 {
00299 rb_require("digest");
00300
00301 #if 0
00302 mOSSL = rb_define_module("OpenSSL");
00303 #endif
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 cDigest = rb_define_class_under(mOSSL, "Digest", rb_path2class("Digest::Class"));
00419
00420
00421
00422
00423
00424 eDigestError = rb_define_class_under(cDigest, "DigestError", eOSSLError);
00425
00426 rb_define_alloc_func(cDigest, ossl_digest_alloc);
00427
00428 rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
00429 rb_define_copy_func(cDigest, ossl_digest_copy);
00430 rb_define_method(cDigest, "reset", ossl_digest_reset, 0);
00431 rb_define_method(cDigest, "update", ossl_digest_update, 1);
00432 rb_define_alias(cDigest, "<<", "update");
00433 rb_define_private_method(cDigest, "finish", ossl_digest_finish, -1);
00434 rb_define_method(cDigest, "digest_length", ossl_digest_size, 0);
00435 rb_define_method(cDigest, "block_length", ossl_digest_block_length, 0);
00436
00437 rb_define_method(cDigest, "name", ossl_digest_name, 0);
00438 }
00439