diff --git a/ChangeLog b/ChangeLog index 2fa6789f9b..dfb4a7cb6c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Sep 27 02:25:14 2002 Akinori MUSHA + + * ext/digest/rmd160: Use OpenSSL's RMD160 engine if available. It + is much faster than what we have now (rmd160.[ch]). Add a knob + (--with-bundled-rmd160) to extconf.rb which makes it use the + bundled one anyway. + Fri Sep 27 01:23:39 2002 Akinori MUSHA * ext/digest/md5: Use OpenSSL's MD5 engine if available. It is diff --git a/ext/digest/rmd160/depend b/ext/digest/rmd160/depend index 7cf1d5e639..0ca79c5f40 100644 --- a/ext/digest/rmd160/depend +++ b/ext/digest/rmd160/depend @@ -5,3 +5,4 @@ rmd160hl.o: rmd160hl.c rmd160.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \ rmd160init.o: rmd160init.c $(srcdir)/../digest.h $(hdrdir)/ruby.h \ $(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h \ rmd160.h $(srcdir)/../defs.h +rmd160ossl.o: rmd160ossl.h $(srcdir)/../defs.h diff --git a/ext/digest/rmd160/extconf.rb b/ext/digest/rmd160/extconf.rb index ce53c77b55..3f6a88e756 100644 --- a/ext/digest/rmd160/extconf.rb +++ b/ext/digest/rmd160/extconf.rb @@ -5,11 +5,17 @@ require "mkmf" $CFLAGS << " -DHAVE_CONFIG_H -I#{File.dirname(__FILE__)}/.." -$objs = [ - "rmd160.#{$OBJEXT}", - "rmd160hl.#{$OBJEXT}", - "rmd160init.#{$OBJEXT}", -] +$objs = [ "rmd160init.#{$OBJEXT}" ] + +dir_config("openssl") + +if !with_config("bundled-rmd160") && + have_library("crypto") && have_header("openssl/ripemd.h") + $objs << "rmd160ossl.#{$OBJEXT}" + $libs << " -lcrypto" +else + $objs << "rmd160.#{$OBJEXT}" << "rmd160hl.#{$OBJEXT}" +end have_header("sys/cdefs.h") diff --git a/ext/digest/rmd160/rmd160init.c b/ext/digest/rmd160/rmd160init.c index 6955c2b64b..b16cdbbed8 100644 --- a/ext/digest/rmd160/rmd160init.c +++ b/ext/digest/rmd160/rmd160init.c @@ -2,7 +2,11 @@ /* $Id$ */ #include "digest.h" +#if defined(HAVE_OPENSSL_RIPEMD_H) +#include "rmd160ossl.h" +#else #include "rmd160.h" +#endif static algo_t rmd160 = { RMD160_DIGEST_LENGTH, diff --git a/ext/digest/rmd160/rmd160ossl.c b/ext/digest/rmd160/rmd160ossl.c new file mode 100644 index 0000000000..be66d81ff9 --- /dev/null +++ b/ext/digest/rmd160/rmd160ossl.c @@ -0,0 +1,45 @@ +/* $Id$ */ + +#include "rmd160ossl.h" +#include "defs.h" +#include +#include + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +char * +RMD160_End(RMD160_CTX *ctx, char *buf) +{ + size_t i; + char *p = buf; + uint8_t digest[20]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != NULL); + /* buf may be NULL */ + + if (p == NULL && (p = malloc(41)) == NULL) + return 0; + + RMD160_Final(digest,ctx); + for (i = 0; i < 20; i++) { + p[i + i] = hex[(uint32_t)digest[i] >> 4]; + p[i + i + 1] = hex[digest[i] & 0x0f]; + } + p[i + i] = '\0'; + return(p); +} + +int RMD160_Equal(RMD160_CTX* pctx1, RMD160_CTX* pctx2) { + return pctx1->num == pctx2->num + && pctx1->A == pctx2->A + && pctx1->B == pctx2->B + && pctx1->C == pctx2->C + && pctx1->D == pctx2->D + && pctx1->E == pctx2->E + && pctx1->Nl == pctx2->Nl + && pctx1->Nh == pctx2->Nh + && memcmp(pctx1->data, pctx2->data, sizeof(pctx1->data)) == 0; +} diff --git a/ext/digest/rmd160/rmd160ossl.h b/ext/digest/rmd160/rmd160ossl.h new file mode 100644 index 0000000000..824a1bf32f --- /dev/null +++ b/ext/digest/rmd160/rmd160ossl.h @@ -0,0 +1,20 @@ +/* $Id$ */ + +#ifndef RMD160OSSL_H_INCLUDED +#define RMD160OSSL_H_INCLUDED + +#include + +#define RMD160_CTX RIPEMD160_CTX + +#define RMD160_Init RIPEMD160_Init +#define RMD160_Update RIPEMD160_Update +#define RMD160_Final RIPEMD160_Final + +#define RMD160_BLOCK_LENGTH RIPEMD160_CBLOCK +#define RMD160_DIGEST_LENGTH RIPEMD160_DIGEST_LENGTH + +char *RMD160_End(RMD160_CTX *ctx, char *buf); +int RMD160_Equal(RMD160_CTX *pctx1, RMD160_CTX *pctx2); + +#endif