diff options
author | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-12-13 18:08:58 +0000 |
---|---|---|
committer | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-12-13 18:08:58 +0000 |
commit | 28e4b12c6ab858dc0e973b623d15d50a5ab043ae (patch) | |
tree | ef34d0d6bd966848814e25a883a63e706817d04b | |
parent | 1a069b3b70d564521f7ccd48a208caf17d3943d2 (diff) | |
download | ruby-openssl-history-28e4b12c6ab858dc0e973b623d15d50a5ab043ae.tar.gz |
* added bn methods (thanks to UNKNOWN <oss-ruby@technorama.net>
* simplify ossl_digest sources
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | openssl_missing.c | 4 | ||||
-rw-r--r-- | openssl_missing.h | 2 | ||||
-rw-r--r-- | ossl.h | 16 | ||||
-rw-r--r-- | ossl_bn.c | 284 | ||||
-rw-r--r-- | ossl_cipher.c | 56 | ||||
-rw-r--r-- | ossl_digest.c | 279 | ||||
-rw-r--r-- | ossl_pkey.c | 4 | ||||
-rw-r--r-- | strptime.c | 6 |
9 files changed, 417 insertions, 246 deletions
@@ -4,20 +4,24 @@ Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz> All rights reserved. $Log$ +Revision 1.11 2001/12/13 18:08:58 majkl + * added bn methods (thanks to UNKNOWN <oss-ruby@technorama.net> + * simplify ossl_digest sources + Revision 1.10 2001/11/29 13:17:24 majkl -* Make work under MS Windows (strptime added) + doc in README + * Make work under MS Windows (strptime added) + doc in README Revision 1.9 2001/11/26 11:48:36 majkl * Just forgot to add stuff to changelog... -Revision 1.8 2001/11/21 09:37:03 gotoyuzo - * buffering.rb went under ``openssl'' directory. - 2001/11/21 majkl * BN added (only proof of concept) * indent changed * rand.h to ossl.h added +Revision 1.8 2001/11/21 09:37:03 gotoyuzo + * buffering.rb went under ``openssl'' directory. + Revision 1.7 2001/11/20 19:39:37 majkl * HMAC added * Config came back diff --git a/openssl_missing.c b/openssl_missing.c index 5d6c0eb..577e1e0 100644 --- a/openssl_missing.c +++ b/openssl_missing.c @@ -12,11 +12,12 @@ #include "openssl_missing.h" /* to hmac.[ch] */ +#ifndef NO_HMAC int HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in) { if (in == NULL) { - /*HMACerr(HMAC_CTX_COPY,HMAC_R_INPUT_NOT_INITIALIZED);*/ + /* HMACerr(HMAC_CTX_COPY,HMAC_R_INPUT_NOT_INITIALIZED); */ return 0; } @@ -24,4 +25,5 @@ HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in) return 1; } +#endif diff --git a/openssl_missing.h b/openssl_missing.h index ed2f162..95f8925 100644 --- a/openssl_missing.h +++ b/openssl_missing.h @@ -46,7 +46,9 @@ extern "C" { (char *(*)())d2i_PKCS7_RECIP_INFO,(char *)ri) /* to hmac.[ch] */ +#ifndef NO_HMAC int HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in); +#endif #ifdef __cplusplus } @@ -73,29 +73,25 @@ extern VALUE cX509Store; extern VALUE eX509StoreError; extern VALUE cSPKI; extern VALUE eSPKIError; -extern VALUE cCipher; -extern VALUE eCipherError; extern VALUE cRandom; extern VALUE eRandomError; extern VALUE cSSLSocket; extern VALUE eSSLError; +/* Cipher */ +extern VALUE cCipher; +extern VALUE eCipherError; +extern VALUE cDES, cRC4, cIdea, cRC2, cBlowFish, cCast5, cRC5; /* Digest */ extern VALUE cDigest; extern VALUE eDigestError; -extern VALUE cMD2; -extern VALUE cMD5; -extern VALUE cMDC2; -extern VALUE cRIPEMD160; -extern VALUE cSHA; -extern VALUE cSHA1; -extern VALUE cDSS; -extern VALUE cDSS1; +extern VALUE cMD2, cMD4, cMD5, cMDC2, cRIPEMD160, cSHA, cSHA1, cDSS, cDSS1; /* PKey */ extern VALUE cPKey; extern VALUE ePKeyError; extern VALUE cRSA; extern VALUE eRSAError; extern VALUE cDSA; +extern VALUE cDSAError; /* PKCS7 */ extern VALUE cPKCS7; extern VALUE cPKCS7SignerInfo; @@ -1,13 +1,14 @@ /* * $Id$ * 'OpenSSL for Ruby' project - * Copyright (C) 2001 Michal Rokos <m.rokos@@sh.cvut.cz> + * Copyright (C) 2001 <oss-ruby@technorama.net> * All rights reserved. */ /* * This program is licenced under the same licence as Ruby. * (See the file 'LICENCE'.) */ +/* modified by Michal Rokos <m.rokos@sh.cvut.cz> */ #include "ossl.h" #define MakeBN(obj, bnp) {\ @@ -174,6 +175,258 @@ ossl_bn_to_i(VALUE self) return num; } +#define BIGNUM_BOOL1(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self) \ + { \ + ossl_bn *bnp = NULL; \ + \ + GetBN(self, bnp); \ + \ + if (BN_##func##(bnp->bignum) == 1) \ + return Qtrue; \ + \ + return Qfalse; \ + } +BIGNUM_BOOL1(is_zero); +BIGNUM_BOOL1(is_one); +BIGNUM_BOOL1(is_odd); + +#define BIGNUM_1c(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self) \ + { \ + ossl_bn *bnp = NULL; \ + BIGNUM *result = NULL; \ + BN_CTX ctx; \ + VALUE obj; \ + \ + GetBN(self, bnp); \ + \ + if (!(result = BN_new())) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + BN_CTX_init(&ctx); \ + if (BN_##func##(result, bnp->bignum, &ctx) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + obj = ossl_bn_new(result); \ + BN_free(result); \ + \ + return obj; \ + } +BIGNUM_1c(sqr); + +#define BIGNUM_2(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self, VALUE other) \ + { \ + ossl_bn *bn1p = NULL; \ + ossl_bn *bn2p = NULL; \ + BIGNUM *result = NULL; \ + VALUE obj; \ + \ + GetBN(self, bn1p); \ + \ + OSSL_Check_Type(other, cBN); \ + GetBN(other, bn2p); \ + \ + if (!(result = BN_new())) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + if (BN_##func##(result, bn1p->bignum, bn2p->bignum) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + obj = ossl_bn_new(result); \ + BN_free(result); \ + \ + return obj; \ + } +BIGNUM_2(add); +BIGNUM_2(sub); + +#define BIGNUM_2c(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self, VALUE other) \ + { \ + ossl_bn *bn1p = NULL, *bn2p = NULL; \ + BIGNUM *result = NULL; \ + BN_CTX ctx; \ + VALUE obj; \ + \ + GetBN(self, bn1p); \ + \ + OSSL_Check_Type(other, cBN); \ + GetBN(other, bn2p); \ + \ + if (!(result = BN_new())) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + BN_CTX_init(&ctx); \ + if (BN_##func##(result, bn1p->bignum, bn2p->bignum, &ctx) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + obj = ossl_bn_new(result); \ + BN_free(result); \ + \ + return obj; \ + } +BIGNUM_2c(mul); +BIGNUM_2c(mod); +BIGNUM_2c(exp); +BIGNUM_2c(gcd); + +static VALUE +ossl_bn_div(VALUE self, VALUE other) +{ + ossl_bn *bn1p = NULL, *bn2p = NULL; + BIGNUM *r1 = NULL, *r2 = NULL; + BN_CTX ctx; + VALUE obj1, obj2; + + GetBN(self, bn1p); + + OSSL_Check_Type(other, cBN); + GetBN(other, bn2p); + + if ((r1 = BN_new()) == NULL || (r2 = BN_new()) == NULL) + rb_raise(eBNError, "%s", ossl_error()); + + BN_CTX_init(&ctx); + if (BN_div(r1, r2, bn1p->bignum, bn2p->bignum, &ctx) != 1) + rb_raise(eBNError, "%s", ossl_error()); + + obj1 = ossl_bn_new(r1); + BN_free(r1); + obj2 = ossl_bn_new(r2); + BN_free(r2); + + return rb_ary_new3(2, obj1, obj2); +} + +static VALUE +ossl_bn_mod_inverse(VALUE self, VALUE other) +{ + ossl_bn *bn1p = NULL, *bn2p = NULL; + BIGNUM *result = NULL; + BN_CTX ctx; + VALUE obj; + + GetBN(self, bn1p); + + OSSL_Check_Type(other, cBN); + GetBN(other, bn2p); + + if (!(result = BN_new())) + rb_raise(eBNError, "%s", ossl_error()); + + BN_CTX_init(&ctx); + if (BN_mod_inverse(result, bn1p->bignum, bn2p->bignum, &ctx) == NULL) + rb_raise(eBNError, "%s", ossl_error()); + + obj = ossl_bn_new(result); + BN_free(result); + + return obj; +} + +#define BIGNUM_3c(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self, VALUE other1, VALUE other2) \ + { \ + ossl_bn *bn1p = NULL, *bn2p = NULL, *bn3p = NULL; \ + BIGNUM *result = NULL; \ + BN_CTX ctx; \ + VALUE obj; \ + \ + GetBN(self, bn1p); \ + \ + OSSL_Check_Type(other1, cBN); \ + OSSL_Check_Type(other2, cBN); \ + GetBN(other1, bn2p); \ + GetBN(other2, bn3p); \ + \ + if (!(result = BN_new())) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + BN_CTX_init(&ctx); \ + if (BN_##func##(result, bn1p->bignum, bn2p->bignum, bn3p->bignum, &ctx) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + obj = ossl_bn_new(result); \ + BN_free(result); \ + \ + return obj; \ + } +BIGNUM_3c(mod_mul); +BIGNUM_3c(mod_exp); + +#define BIGNUM_BIT_SETCLEAR(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self, VALUE bit) \ + { \ + ossl_bn *bnp = NULL; \ + \ + GetBN(self, bnp); \ + \ + if (BN_##func##(bnp->bignum, NUM2INT(bit)) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + return self; \ + } +BIGNUM_BIT_SETCLEAR(set_bit); +BIGNUM_BIT_SETCLEAR(clear_bit); + +static VALUE +ossl_bn_is_bit_set(VALUE self, VALUE bit) +{ + ossl_bn *bnp = NULL; + + GetBN(self, bnp); + + if (BN_is_bit_set(bnp->bignum, NUM2INT(bit)) == 1) + return Qtrue; + + return Qfalse; +} + +static VALUE +ossl_bn_mask_bits(VALUE self, VALUE bit) +{ + ossl_bn *bnp = NULL; + + GetBN(self, bnp); + + if (BN_mask_bits(bnp->bignum, NUM2INT(bit)) != 1) + rb_raise(eBNError, "%s", ossl_error()); + + return self; +} + +#define BIGNUM_SHIFT(func) \ + static VALUE \ + ossl_bn_##func##(VALUE self, VALUE bits) \ + { \ + ossl_bn *bnp = NULL; \ + BIGNUM *result = NULL; \ + VALUE obj; \ + \ + GetBN(self, bnp); \ + \ + if (!(result = BN_new())) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + if (BN_##func##(result, bnp->bignum, NUM2INT(bits)) != 1) \ + rb_raise(eBNError, "%s", ossl_error()); \ + \ + obj = ossl_bn_new(result); \ + BN_free(result); \ + \ + return obj; \ + } +BIGNUM_SHIFT(lshift); +BIGNUM_SHIFT(rshift); + /* * INIT */ @@ -186,5 +439,34 @@ Init_bn(VALUE mOSSL) rb_define_singleton_method(cBN, "new", ossl_bn_s_new, -1); rb_define_method(cBN, "initialize", ossl_bn_initialize, -1); rb_define_method(cBN, "to_i", ossl_bn_to_i, 0); + + rb_define_method(cBN, "zero?", ossl_bn_is_zero, 0); + rb_define_method(cBN, "one?", ossl_bn_is_one, 0); + rb_define_method(cBN, "odd?", ossl_bn_is_odd, 0); + + rb_define_method(cBN, "sqr", ossl_bn_sqr, 0); + + rb_define_method(cBN, "+", ossl_bn_add, 1); + rb_define_method(cBN, "-", ossl_bn_sub, 1); + + rb_define_method(cBN, "*", ossl_bn_mul, 1); + rb_define_method(cBN, "%", ossl_bn_mod, 1); + rb_define_method(cBN, "**", ossl_bn_exp, 1); + rb_define_method(cBN, "gcd", ossl_bn_gcd, 1); + + rb_define_method(cBN, "/", ossl_bn_div, 1); + rb_define_method(cBN, "mod_inverse", ossl_bn_mod_inverse, 1); + + rb_define_method(cBN, "mod_mul", ossl_bn_mod_mul, 1); + rb_define_method(cBN, "mod_exp", ossl_bn_mod_exp, 1); + + rb_define_method(cBN, "set_bit!", ossl_bn_set_bit, 1); + rb_define_method(cBN, "clear_bit!", ossl_bn_clear_bit, 1); + + rb_define_method(cBN, "bit_set?", ossl_bn_is_bit_set, 1); + rb_define_method(cBN, "mask_bits!", ossl_bn_mask_bits, 1); + + rb_define_method(cBN, "<<", ossl_bn_lshift, 1); + rb_define_method(cBN, ">>", ossl_bn_rshift, 1); } diff --git a/ossl_cipher.c b/ossl_cipher.c index 3671b4a..fa1b899 100644 --- a/ossl_cipher.c +++ b/ossl_cipher.c @@ -15,8 +15,6 @@ } #define GetCipher(obj, ciphp) Data_Get_Struct(obj, ossl_cipher, ciphp) -#define DefCipherConst(x) rb_define_const(module, #x, INT2FIX(##x)) - /* * Constants */ @@ -539,6 +537,11 @@ Init_ossl_cipher(VALUE module) rb_define_alias(cCipher, "<<", "update"); rb_define_method(cCipher, "cipher", ossl_cipher_cipher, 0); +/* + * Constants + */ +#define DefCipherConst(x) rb_define_const(module, #x, INT2FIX(##x)) + DefCipherConst(ECB); DefCipherConst(EDE); DefCipherConst(EDE3); @@ -547,26 +550,37 @@ Init_ossl_cipher(VALUE module) DefCipherConst(CBC); DefCipherConst(BIT40); DefCipherConst(BIT64); - - cDES = rb_define_class_under(module, "DES", cCipher); - rb_define_method(cDES, "initialize", ossl_des_initialize, -1); - cRC4 = rb_define_class_under(module, "RC4", cCipher); - rb_define_method(cRC4, "initialize", ossl_rc4_initialize, -1); - - cIdea = rb_define_class_under(module, "Idea", cCipher); - rb_define_method(cIdea, "initialize", ossl_idea_initialize, -1); - - cRC2 = rb_define_class_under(module, "RC2", cCipher); - rb_define_method(cRC2, "initialize", ossl_rc2_initialize, -1); - - cBlowFish = rb_define_class_under(module, "BlowFish", cCipher); - rb_define_method(cBlowFish, "initialize", ossl_bf_initialize, -1); - - cCast5 = rb_define_class_under(module, "Cast5", cCipher); - rb_define_method(cCast5, "initialize", ossl_cast5_initialize, -1); +/* + * automation for classes creation and initialize method binding + */ +#define DefCipher(name, func) \ + c##name## = rb_define_class_under(module, #name, cCipher); \ + rb_define_method(c##name##, "initialize", ossl_##func##_initialize, -1) - cRC5 = rb_define_class_under(module, "RC5", cCipher); - rb_define_method(cRC5, "initialize", ossl_rc5_initialize, -1); +/* + * create classes and bind initialize method + */ +#ifndef NO_DES + DefCipher(DES, des); +#endif +#ifndef NO_RC4 + DefCipher(RC4, rc4); +#endif +#ifndef NO_RC2 + DefCipher(RC2, rc2); +#endif +#ifndef NO_RC5 + DefCipher(RC5, rc5); +#endif +#ifndef NO_BF + DefCipher(BlowFish, bf); +#endif +#ifndef NO_CAST + DefCipher(Cast5, cast5); +#endif +#ifndef NO_IDEA + DefCipher(Idea, idea); +#endif } diff --git a/ossl_digest.c b/ossl_digest.c index 6d2d18a..948a6db 100644 --- a/ossl_digest.c +++ b/ossl_digest.c @@ -20,14 +20,7 @@ */ VALUE cDigest; VALUE eDigestError; -VALUE cMD2; -VALUE cMD5; -VALUE cMDC2; -VALUE cRIPEMD160; -VALUE cSHA; -VALUE cSHA1; -VALUE cDSS; -VALUE cDSS1; +VALUE cMD2, cMD4, cMD5, cMDC2, cRIPEMD160, cSHA, cSHA1, cDSS, cDSS1; /* * Struct @@ -197,180 +190,52 @@ ossl_digest_hexdigest(VALUE self) */ /* - * MD2 + * automation of digest initialization method */ -static VALUE -ossl_md2_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_md2()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * MD5 - */ -static VALUE -ossl_md5_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_md5()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * MDC2 - */ -static VALUE -ossl_mdc2_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); +#define DefDigestInit(dgst) \ + static VALUE \ + ossl_##dgst##_initialize(int argc, VALUE *argv, VALUE self) \ + { \ + ossl_digest *digestp = NULL; \ + VALUE data; \ + \ + GetDigest(self, digestp); \ + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { \ + rb_raise(eDigestError, "Cannot allocate memory for new digest"); \ + } \ + EVP_DigestInit(digestp->md, EVP_##dgst##()); \ + \ + if (rb_scan_args(argc, argv, "01", &data) == 1) { \ + Check_Type(data, T_STRING); \ + EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); \ + } \ + return self; \ } - EVP_DigestInit(digestp->md, EVP_mdc2()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} /* - * RIPEmd160 + * Define digest initialize methods */ -static VALUE -ossl_ripemd160_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_ripemd160()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * SHA - */ -static VALUE -ossl_sha_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_sha()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * SHA1 - */ -static VALUE -ossl_sha1_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_sha1()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * DSS = SHA for DSA - */ -static VALUE -ossl_dss_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_dss()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} - -/* - * DSS1 = SHA1 for DSA - */ -static VALUE -ossl_dss1_initialize(int argc, VALUE *argv, VALUE self) -{ - ossl_digest *digestp = NULL; - VALUE data; - - GetDigest(self, digestp); - if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { - rb_raise(eDigestError, "Cannot allocate memory for new digest"); - } - EVP_DigestInit(digestp->md, EVP_dss1()); - - if (rb_scan_args(argc, argv, "01", &data) == 1) { - Check_Type(data, T_STRING); - EVP_DigestUpdate(digestp->md, RSTRING(data)->ptr, RSTRING(data)->len); - } - return self; -} +#ifndef NO_MD2 + DefDigestInit(md2) +#endif +#ifndef NO_MD4 + DefDigestInit(md4) +#endif +#ifndef NO_MD5 + DefDigestInit(md5) +#endif +#ifndef NO_SHA + DefDigestInit(sha) + DefDigestInit(sha1) + DefDigestInit(dss) + DefDigestInit(dss1) +#endif +#ifndef NO_RIPEMD + DefDigestInit(ripemd160) +#endif +#ifndef NO_MDC2 + DefDigestInit(mdc2) +#endif /* * INIT @@ -395,36 +260,36 @@ Init_ossl_digest(VALUE module) rb_define_alias(cDigest, "to_str", "hexdigest"); /*rb_define_method(cDigest, "==", ossl_digest_equal, 1);*/ - /* MD2 */ - cMD2 = rb_define_class_under(module, "MD2", cDigest); - rb_define_method(cMD2, "initialize", ossl_md2_initialize, -1); - - /* MD5 */ - cMD5 = rb_define_class_under(module, "MD5", cDigest); - rb_define_method(cMD5, "initialize", ossl_md5_initialize, -1); - - /* MDC2 */ - cMDC2 = rb_define_class_under(module, "MDC2", cDigest); - rb_define_method(cMDC2, "initialize", ossl_mdc2_initialize, -1); - - /* RIPEmd160 */ - cRIPEMD160 = rb_define_class_under(module, "Ripemd160", cDigest); - rb_define_method(cRIPEMD160, "initialize", ossl_ripemd160_initialize, -1); - - /* SHA */ - cSHA = rb_define_class_under(module, "SHA", cDigest); - rb_define_method(cSHA, "initialize", ossl_sha_initialize, -1); - - /* SHA1 */ - cSHA1 = rb_define_class_under(module, "SHA1", cDigest); - rb_define_method(cSHA1, "initialize", ossl_sha1_initialize, -1); - - /* SHA for DSA */ - cDSS = rb_define_class_under(module, "DSS", cDigest); - rb_define_method(cDSS, "initialize", ossl_dss_initialize, -1); +/* + * automation for classes creation and initialize method binding + */ +#define DefDigest(name, func) \ + c##name## = rb_define_class_under(module, #name, cDigest); \ + rb_define_method(c##name##, "initialize", ossl_##func##_initialize, -1) - /* SHA1 for DSA */ - cDSS1 = rb_define_class_under(module, "DSS1", cDigest); - rb_define_method(cDSS1, "initialize", ossl_dss1_initialize, -1); +/* + * create classes and bind initialize method + */ +#ifndef NO_MD2 + DefDigest(MD2, md2); +#endif +#ifndef NO_MD4 + DefDigest(MD4, md4); +#endif +#ifndef NO_MD5 + DefDigest(MD5, md5); +#endif +#ifndef NO_SHA + DefDigest(SHA, sha); + DefDigest(SHA1, sha1); + DefDigest(DSS, dss); + DefDigest(DSS1, dss1); +#endif +#ifndef NO_RIPEMD + DefDigest(RIPEMD160, ripemd160); +#endif +#ifndef NO_MDC2 + DefDigest(MDC2, mdc2); +#endif } diff --git a/ossl_pkey.c b/ossl_pkey.c index 44b1fbf..c7a2119 100644 --- a/ossl_pkey.c +++ b/ossl_pkey.c @@ -37,10 +37,14 @@ ossl_pkey_new(EVP_PKEY *key) rb_raise(ePKeyError, "Empty key!"); switch (key->type) { +#ifndef NO_RSA case EVP_PKEY_RSA: return ossl_rsa_new(key->pkey.rsa); +#endif +#ifndef NO_DSA case EVP_PKEY_DSA: return ossl_dsa_new(key->pkey.dsa); +#endif } rb_raise(ePKeyError, "unsupported key type"); @@ -29,10 +29,11 @@ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifndef HAVE_STRPTIME #include <stdio.h> #include <stdlib.h> -/*#include <unistd.h>*/ +/* #include <unistd.h> */ #include <time.h> #include <ctype.h> #include <string.h> @@ -43,7 +44,6 @@ #endif */ -#ifndef HAVE_STRPTIME #define asizeof(a) (sizeof (a) / sizeof ((a)[0])) @@ -359,4 +359,6 @@ strptime(char *buf, char *fmt, struct tm *tm) return buf; } + #endif /* HAVE_STRPTIME */ + |