From 7723beeaf4948e03de652d30a5a38d77670bc623 Mon Sep 17 00:00:00 2001 From: jbj Date: Sat, 21 Jul 2001 19:44:22 +0000 Subject: - add sha1 test vectors, verify on ix86/alpha/sparc. - add (but disable for now) rpm-perl subpackage from Perl-RPM. - python: parameterize with PYVER to handle 1.5 and/or 2.1 builds. - add build dependency on zlib-devel (#49575). CVS patchset: 4969 CVS date: 2001/07/21 19:44:22 --- rpmio/digest.c | 43 +++++++++++++------ rpmio/rpmio_internal.h | 11 +++-- rpmio/tdigest.c | 113 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 139 insertions(+), 28 deletions(-) (limited to 'rpmio') diff --git a/rpmio/digest.c b/rpmio/digest.c index a0c4d2969..d01ea6b89 100644 --- a/rpmio/digest.c +++ b/rpmio/digest.c @@ -381,12 +381,21 @@ byteReverse(byte *buf, unsigned nbytes) { unsigned nlongs = nbytes / sizeof(uint32); uint32 t; - do { - t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(uint32 *) buf = t; - buf += 4; - } while (--nlongs); + if (IS_BIG_ENDIAN()) { + do { + t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + *(uint32 *) buf = t; + buf += 4; + } while (--nlongs); + } else { + do { + t = (uint32) ((unsigned) buf[0] << 8 | buf[1]) << 16 | + ((unsigned) buf[2] << 8 | buf[3]); + *(uint32 *) buf = t; + buf += 4; + } while (--nlongs); + } } /*@=shadow@*/ @@ -405,6 +414,8 @@ rpmDigestInit(rpmDigestFlags flags) ctx->digest[1] = 0xefcdab89; ctx->digest[2] = 0x98badcfe; ctx->digest[3] = 0x10325476; + /* md5 sums are little endian (no swap) so big endian needs the swap. */ + ctx->doByteReverse = (IS_BIG_ENDIAN()) ? 1 : 0; } if (flags & RPMDIGEST_SHA1) { @@ -416,12 +427,12 @@ rpmDigestInit(rpmDigestFlags flags) ctx->digest[ 2 ] = 0x98badcfe; ctx->digest[ 3 ] = 0x10325476; ctx->digest[ 4 ] = 0xc3d2e1f0; + /* md5 sums are little endian (no swap) so big endian needs the swap. */ + ctx->doByteReverse = (IS_BIG_ENDIAN()) ? 0 : 1; } - /* md5 sums are little endian (no swap) so big endian needs the swap. */ - ctx->doByteReverse = (IS_BIG_ENDIAN()) ? 1 : 0; - if (flags & RPMDIGEST_NATIVE) - ctx->doByteReverse = 0; + if (flags & RPMDIGEST_REVERSE) + ctx->doByteReverse ^= 1; ctx->bits[0] = 0; ctx->bits[1] = 0; @@ -507,16 +518,22 @@ rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx, /*@out@*/ void ** datap, memset(p, 0, count - sizeof(ctx->bits)); if (ctx->doByteReverse) byteReverse(ctx->in, ctx->datalen - sizeof(ctx->bits)); - ((uint32 *) ctx->in)[14] = ctx->bits[0]; - ((uint32 *) ctx->in)[15] = ctx->bits[1]; + + if (ctx->flags & (RPMDIGEST_MD5|RPMDIGEST_BCSWAP)) { + ((uint32 *) ctx->in)[14] = ctx->bits[0]; + ((uint32 *) ctx->in)[15] = ctx->bits[1]; + } else { + ((uint32 *) ctx->in)[14] = ctx->bits[1]; + ((uint32 *) ctx->in)[15] = ctx->bits[0]; + } /*@-moduncon@*/ ctx->transform(ctx); /*@=moduncon@*/ - /* Return final digest. */ if (ctx->doByteReverse) byteReverse((byte *) ctx->digest, ctx->digestlen); + /* Return final digest. */ if (!asAscii) { if (lenp) *lenp = ctx->digestlen; if (datap) { diff --git a/rpmio/rpmio_internal.h b/rpmio/rpmio_internal.h index 05cae0515..eae24d477 100644 --- a/rpmio/rpmio_internal.h +++ b/rpmio/rpmio_internal.h @@ -54,7 +54,8 @@ typedef /*@abstract@*/ struct { typedef enum rpmDigestFlags_e { RPMDIGEST_MD5 = (1 << 0), /*!< MD5 digest. */ RPMDIGEST_SHA1 = (1 << 1), /*!< SHA1 digest. */ - RPMDIGEST_NATIVE = (1 << 16), /*!< Should bytes be reversed? */ + RPMDIGEST_REVERSE = (1 << 16), /*!< Should bytes be reversed? */ + RPMDIGEST_BCSWAP = (1 << 17), /*!< Should bit count be reversed? */ } rpmDigestFlags; typedef /*@abstract@*/ struct DIGEST_CTX_s * DIGEST_CTX; @@ -417,7 +418,7 @@ FD_t c2f(/*@null@*/ void * cookie) void fdInitMD5(FD_t fd, int flags) /*@modifies fd @*/ { - if (flags) flags = RPMDIGEST_NATIVE; + if (flags) flags = RPMDIGEST_REVERSE; flags |= RPMDIGEST_MD5; fd->digest = rpmDigestInit(flags); } @@ -425,10 +426,12 @@ void fdInitMD5(FD_t fd, int flags) /** \ingroup rpmio */ /*@unused@*/ static inline -void fdInitSHA1(FD_t fd) +void fdInitSHA1(FD_t fd, int flags) /*@modifies fd @*/ { - fd->digest = rpmDigestInit(RPMDIGEST_SHA1); + if (flags) flags = RPMDIGEST_REVERSE; + flags |= RPMDIGEST_SHA1; + fd->digest = rpmDigestInit(flags); } /** \ingroup rpmio diff --git a/rpmio/tdigest.c b/rpmio/tdigest.c index 59b19a1e6..9e8e65be4 100644 --- a/rpmio/tdigest.c +++ b/rpmio/tdigest.c @@ -6,14 +6,26 @@ static rpmDigestFlags flags = RPMDIGEST_MD5; extern int _rpmio_debug; +static int fips = 0; + +const char * adigest = "a9993e364706816aba3e25717850c26c9cd0d89d"; +const char * bdigest = "84983e441c3bd26ebaae4aa1f95129e5e54670f1"; +const char * cdigest = "34aa973cd4c4daa4f61eeb2bdbad27316534016f"; + static struct poptOption optionsTable[] = { { "md5", '\0', POPT_BIT_SET, &flags, RPMDIGEST_MD5, NULL, NULL }, { "sha1",'\0', POPT_BIT_SET, &flags, RPMDIGEST_SHA1, NULL, NULL }, - { "native",'\0', POPT_BIT_SET, &flags, RPMDIGEST_NATIVE, NULL, NULL }, + { "reverse",'\0', POPT_BIT_SET, &flags, RPMDIGEST_REVERSE, NULL, NULL }, + { "fipsa",'\0', POPT_BIT_SET, &fips, 1, NULL, NULL }, + { "fipsb",'\0', POPT_BIT_SET, &fips, 2, NULL, NULL }, + { "fipsc",'\0', POPT_BIT_SET, &fips, 3, NULL, NULL }, { "debug",'d', POPT_ARG_VAL, &_rpmio_debug, -1, NULL, NULL }, POPT_TABLEEND }; +#define SHA1_CMD "/usr/bin/sha1sum" +#define MD5_CMD "/usr/bin/md5sum" + int main(int argc, const char *argv[]) { @@ -21,24 +33,94 @@ main(int argc, const char *argv[]) const char ** args; const char * ifn; const char * ofn = "/dev/null"; + DIGEST_CTX ctx; + const char * idigest; + const char * odigest; + const char * sdigest; + const char * digest; + size_t digestlen; + int asAscii = 1; + int reverse; int rc; + char appendix; + int i; optCon = poptGetContext(argv[0], argc, argv, optionsTable, 0); while ((rc = poptGetNextOpt(optCon)) > 0) ; + reverse = (flags & RPMDIGEST_REVERSE); + if (fips) { + flags &= ~RPMDIGEST_MD5; + flags |= RPMDIGEST_SHA1; + ctx = rpmDigestInit(flags); + ifn = NULL; + appendix = ' '; + sdigest = NULL; + switch (fips) { + case 1: + ifn = "abc"; + rpmDigestUpdate(ctx, ifn, strlen(ifn)); + sdigest = adigest; + appendix = 'A'; + break; + case 2: + ifn = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; + rpmDigestUpdate(ctx, ifn, strlen(ifn)); + sdigest = bdigest; + appendix = 'B'; + break; + case 3: + ifn = "aaaaaaaaaaa ..."; + for (i = 0; i < 1000000; i++) + rpmDigestUpdate(ctx, ifn, 1); + sdigest = cdigest; + appendix = 'C'; + break; + } + if (ifn == NULL) + return 1; + rpmDigestFinal(ctx, (void **)&digest, &digestlen, asAscii); + + if (digest) { + fprintf(stdout, "%s %s\n", digest, ifn); + fflush(stdout); + free((void *)digest); + } + if (sdigest) { + fprintf(stdout, "%s FIPS PUB 180-1 Appendix %c\n", sdigest, + appendix); + fflush(stdout); + } + return 0; + } + args = poptGetArgs(optCon); rc = 0; + if (args) while ((ifn = *args++) != NULL) { FD_t ifd; FD_t ofd; unsigned char buf[BUFSIZ]; ssize_t nb; - DIGEST_CTX ctx; - const char * idigest; - const char * odigest; - const char * digest; - size_t digestlen; + + sdigest = NULL; + { char *se; + FILE * sfp; + + se = buf; + *se = '\0'; + se = stpcpy(se, ((flags & RPMDIGEST_SHA1) ? SHA1_CMD : MD5_CMD)); + *se++ = ' '; + se = stpcpy(se, ifn); + if ((sfp = popen(buf, "r")) != NULL) { + fgets(buf, sizeof(buf), sfp); + if ((se = strchr(buf, ' ')) != NULL) + *se = '\0'; + sdigest = xstrdup(buf); + pclose(sfp); + } + } ifd = Fopen(ifn, "r.ufdio"); if (ifd == NULL || Ferror(ifd)) { @@ -48,7 +130,7 @@ main(int argc, const char *argv[]) continue; } idigest = NULL; - (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ifd) : fdInitMD5(ifd, 0); + (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ifd, reverse) : fdInitMD5(ifd, reverse); ofd = Fopen(ofn, "w.ufdio"); if (ofd == NULL || Ferror(ofd)) { @@ -59,7 +141,7 @@ main(int argc, const char *argv[]) continue; } odigest = NULL; - (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ofd) : fdInitMD5(ofd, 0); + (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ofd, reverse) : fdInitMD5(ofd, reverse); ctx = rpmDigestInit(flags); @@ -68,14 +150,18 @@ main(int argc, const char *argv[]) (void) Fwrite(buf, 1, nb, ofd); } - fdFiniMD5(ifd, (void **)&idigest, NULL, 1); + (flags & RPMDIGEST_SHA1) + ? fdFiniSHA1(ifd, (void **)&idigest, NULL, asAscii) + : fdFiniMD5(ifd, (void **)&idigest, NULL, asAscii); Fclose(ifd); Fflush(ofd); - fdFiniMD5(ofd, (void **)&odigest, NULL, 1); + (flags & RPMDIGEST_SHA1) + ? fdFiniSHA1(ofd, (void **)&odigest, NULL, asAscii) + : fdFiniMD5(ofd, (void **)&odigest, NULL, asAscii); Fclose(ofd); - rpmDigestFinal(ctx, (void **)&digest, &digestlen, 1); + rpmDigestFinal(ctx, (void **)&digest, &digestlen, asAscii); if (digest) { fprintf(stdout, "%s %s\n", digest, ifn); @@ -92,6 +178,11 @@ main(int argc, const char *argv[]) fflush(stdout); free((void *)odigest); } + if (sdigest) { + fprintf(stdout, "%s cmd %s\n", sdigest, ifn); + fflush(stdout); + free((void *)sdigest); + } } return rc; } -- cgit v1.2.3