2000-11-01 00:18:34 +08:00
|
|
|
/** \ingroup signature
|
|
|
|
* \file rpmio/digest.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
2002-04-11 04:00:40 +08:00
|
|
|
#include "rpmio_internal.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2001-09-26 22:45:50 +08:00
|
|
|
#ifdef SHA_DEBUG
|
|
|
|
#define DPRINTF(_a) fprintf _a
|
|
|
|
#else
|
|
|
|
#define DPRINTF(_a)
|
|
|
|
#endif
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2001-04-29 09:05:43 +08:00
|
|
|
/*@access DIGEST_CTX@*/
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
/**
|
|
|
|
* MD5/SHA1 digest private data.
|
|
|
|
*/
|
|
|
|
struct DIGEST_CTX_s {
|
|
|
|
rpmDigestFlags flags; /*!< Bit(s) to control digest operation. */
|
2003-04-29 05:10:07 +08:00
|
|
|
uint32_t datalen; /*!< No. bytes in block of plaintext data. */
|
|
|
|
uint32_t paramlen; /*!< No. bytes of digest parameters. */
|
|
|
|
uint32_t digestlen; /*!< No. bytes of digest. */
|
2001-09-26 22:45:50 +08:00
|
|
|
void * param; /*!< Digest parameters. */
|
|
|
|
int (*Reset) (void * param)
|
|
|
|
/*@modifies param @*/; /*!< Digest initialize. */
|
2003-04-29 05:10:07 +08:00
|
|
|
int (*Update) (void * param, const byte * data, size_t size)
|
2001-09-26 22:45:50 +08:00
|
|
|
/*@modifies param @*/; /*!< Digest transform. */
|
2003-04-29 05:10:07 +08:00
|
|
|
int (*Digest) (void * param, /*@out@*/ byte * digest)
|
2001-09-26 22:45:50 +08:00
|
|
|
/*@modifies param, digest @*/; /*!< Digest finish. */
|
2000-11-01 00:18:34 +08:00
|
|
|
};
|
|
|
|
|
2002-07-03 07:54:35 +08:00
|
|
|
/*@-boundsread@*/
|
2001-10-06 04:39:50 +08:00
|
|
|
DIGEST_CTX
|
|
|
|
rpmDigestDup(DIGEST_CTX octx)
|
|
|
|
{
|
2003-04-03 05:16:26 +08:00
|
|
|
DIGEST_CTX nctx;
|
|
|
|
nctx = memcpy(xcalloc(1, sizeof(*nctx)), octx, sizeof(*nctx));
|
2001-10-19 09:35:57 +08:00
|
|
|
nctx->param = memcpy(xcalloc(1, nctx->paramlen), octx->param, nctx->paramlen);
|
2001-10-06 04:39:50 +08:00
|
|
|
return nctx;
|
|
|
|
}
|
2002-07-03 07:54:35 +08:00
|
|
|
/*@=boundsread@*/
|
2001-10-06 04:39:50 +08:00
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
DIGEST_CTX
|
2001-10-19 09:35:57 +08:00
|
|
|
rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
|
2000-11-01 00:18:34 +08:00
|
|
|
{
|
|
|
|
DIGEST_CTX ctx = xcalloc(1, sizeof(*ctx));
|
2001-10-19 09:35:57 +08:00
|
|
|
int xx;
|
2000-11-01 00:18:34 +08:00
|
|
|
|
|
|
|
ctx->flags = flags;
|
|
|
|
|
2001-10-19 09:35:57 +08:00
|
|
|
switch (hashalgo) {
|
|
|
|
case PGPHASHALGO_MD5:
|
2000-11-01 00:18:34 +08:00
|
|
|
ctx->digestlen = 16;
|
|
|
|
ctx->datalen = 64;
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-sizeoftype@*/ /* FIX: union, not void pointer */
|
2001-10-19 09:35:57 +08:00
|
|
|
ctx->paramlen = sizeof(md5Param);
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=sizeoftype@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
ctx->param = xcalloc(1, ctx->paramlen);
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-type@*/ /* FIX: cast? */
|
2001-09-26 22:45:50 +08:00
|
|
|
ctx->Reset = (void *) md5Reset;
|
|
|
|
ctx->Update = (void *) md5Update;
|
|
|
|
ctx->Digest = (void *) md5Digest;
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=type@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
break;
|
|
|
|
case PGPHASHALGO_SHA1:
|
2000-11-01 00:18:34 +08:00
|
|
|
ctx->digestlen = 20;
|
|
|
|
ctx->datalen = 64;
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-sizeoftype@*/ /* FIX: union, not void pointer */
|
2001-10-19 09:35:57 +08:00
|
|
|
ctx->paramlen = sizeof(sha1Param);
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=sizeoftype@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
ctx->param = xcalloc(1, ctx->paramlen);
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-type@*/ /* FIX: cast? */
|
2001-09-26 22:45:50 +08:00
|
|
|
ctx->Reset = (void *) sha1Reset;
|
|
|
|
ctx->Update = (void *) sha1Update;
|
|
|
|
ctx->Digest = (void *) sha1Digest;
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=type@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
break;
|
|
|
|
case PGPHASHALGO_RIPEMD160:
|
|
|
|
case PGPHASHALGO_MD2:
|
|
|
|
case PGPHASHALGO_TIGER192:
|
|
|
|
case PGPHASHALGO_HAVAL_5_160:
|
|
|
|
default:
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
/*@notreached@*/ break;
|
2000-11-01 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2002-07-03 07:54:35 +08:00
|
|
|
/*@-boundsread@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
xx = (*ctx->Reset) (ctx->param);
|
2002-07-03 07:54:35 +08:00
|
|
|
/*@=boundsread@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2001-09-26 22:45:50 +08:00
|
|
|
DPRINTF((stderr, "*** Init(%x) ctx %p param %p\n", flags, ctx, ctx->param));
|
2000-11-01 00:18:34 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2001-10-19 09:35:57 +08:00
|
|
|
/*@-mustmod@*/ /* LCL: ctx->param may be modified, but ctx is abstract @*/
|
|
|
|
int
|
2000-11-01 00:18:34 +08:00
|
|
|
rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len)
|
|
|
|
{
|
2003-04-03 05:16:26 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2001-09-26 22:45:50 +08:00
|
|
|
DPRINTF((stderr, "*** Update(%p,%p,%d) param %p \"%s\"\n", ctx, data, len, ctx->param, ((char *)data)));
|
2002-07-08 22:21:26 +08:00
|
|
|
/*@-boundsread@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
return (*ctx->Update) (ctx->param, data, len);
|
2002-07-08 22:21:26 +08:00
|
|
|
/*@=boundsread@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
}
|
2001-10-19 09:35:57 +08:00
|
|
|
/*@=mustmod@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@-boundswrite@*/
|
2001-10-19 09:35:57 +08:00
|
|
|
int
|
2003-04-03 05:16:26 +08:00
|
|
|
rpmDigestFinal(DIGEST_CTX ctx, void ** datap, size_t *lenp, int asAscii)
|
2000-11-01 00:18:34 +08:00
|
|
|
{
|
2003-04-29 05:10:07 +08:00
|
|
|
byte * digest;
|
2001-09-26 22:45:50 +08:00
|
|
|
char * t;
|
|
|
|
int i;
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return -1;
|
|
|
|
digest = xmalloc(ctx->digestlen);
|
|
|
|
|
2001-09-26 22:45:50 +08:00
|
|
|
DPRINTF((stderr, "*** Final(%p,%p,%p,%d) param %p digest %p\n", ctx, datap, lenp, asAscii, ctx->param, digest));
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-noeffectuncon@*/ /* FIX: check rc */
|
2001-09-26 22:45:50 +08:00
|
|
|
(void) (*ctx->Digest) (ctx->param, digest);
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=noeffectuncon@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
|
2001-07-22 03:44:22 +08:00
|
|
|
/* Return final digest. */
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@-branchstate@*/
|
2000-11-01 00:18:34 +08:00
|
|
|
if (!asAscii) {
|
|
|
|
if (lenp) *lenp = ctx->digestlen;
|
|
|
|
if (datap) {
|
2001-09-26 22:45:50 +08:00
|
|
|
*datap = digest;
|
|
|
|
digest = NULL;
|
2000-11-01 00:18:34 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (lenp) *lenp = (2*ctx->digestlen) + 1;
|
|
|
|
if (datap) {
|
2001-09-26 22:45:50 +08:00
|
|
|
const byte * s = (const byte *) digest;
|
2000-11-01 00:18:34 +08:00
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
*datap = t = xmalloc((2*ctx->digestlen) + 1);
|
|
|
|
for (i = 0 ; i < ctx->digestlen; i++) {
|
|
|
|
*t++ = hex[ (unsigned)((*s >> 4) & 0x0f) ];
|
|
|
|
*t++ = hex[ (unsigned)((*s++ ) & 0x0f) ];
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 05:46:31 +08:00
|
|
|
/*@=branchstate@*/
|
2001-09-26 22:45:50 +08:00
|
|
|
if (digest) {
|
|
|
|
memset(digest, 0, ctx->digestlen); /* In case it's sensitive */
|
|
|
|
free(digest);
|
|
|
|
}
|
2001-10-19 09:35:57 +08:00
|
|
|
memset(ctx->param, 0, ctx->paramlen); /* In case it's sensitive */
|
2001-09-26 22:45:50 +08:00
|
|
|
free(ctx->param);
|
2000-11-01 00:18:34 +08:00
|
|
|
memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
|
|
|
|
free(ctx);
|
2001-10-19 09:35:57 +08:00
|
|
|
return 0;
|
2000-11-01 00:18:34 +08:00
|
|
|
}
|
2002-06-24 03:47:08 +08:00
|
|
|
/*@=boundswrite@*/
|