6 #include "rpmio_internal.h"
10 #define DPRINTF(_a) fprintf _a
17 * MD5/SHA1 digest private data.
20 rpmDigestFlags flags; /*!< Bit(s) to control digest operation. */
21 uint32_t datalen; /*!< No. bytes in block of plaintext data. */
22 uint32_t paramlen; /*!< No. bytes of digest parameters. */
23 uint32_t digestlen; /*!< No. bytes of digest. */
24 void * param; /*!< Digest parameters. */
25 int (*Reset) (void * param); /*!< Digest initialize. */
26 int (*Update) (void * param, const byte * data, size_t size); /*!< Digest transform. */
27 int (*Digest) (void * param, byte * digest); /*!< Digest finish. */
31 rpmDigestDup(DIGEST_CTX octx)
34 nctx = memcpy(xcalloc(1, sizeof(*nctx)), octx, sizeof(*nctx));
35 nctx->param = memcpy(xcalloc(1, nctx->paramlen), octx->param, nctx->paramlen);
40 rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
42 DIGEST_CTX ctx = xcalloc(1, sizeof(*ctx));
51 /* FIX: union, not void pointer */
52 ctx->paramlen = sizeof(md5Param);
53 ctx->param = xcalloc(1, ctx->paramlen);
55 ctx->Reset = (void *) md5Reset;
56 ctx->Update = (void *) md5Update;
57 ctx->Digest = (void *) md5Digest;
59 case PGPHASHALGO_SHA1:
62 /* FIX: union, not void pointer */
63 ctx->paramlen = sizeof(sha1Param);
64 ctx->param = xcalloc(1, ctx->paramlen);
66 ctx->Reset = (void *) sha1Reset;
67 ctx->Update = (void *) sha1Update;
68 ctx->Digest = (void *) sha1Digest;
70 #if HAVE_BEECRYPT_API_H
71 case PGPHASHALGO_SHA256:
74 /* FIX: union, not void pointer */
75 ctx->paramlen = sizeof(sha256Param);
76 ctx->param = xcalloc(1, ctx->paramlen);
78 ctx->Reset = (void *) sha256Reset;
79 ctx->Update = (void *) sha256Update;
80 ctx->Digest = (void *) sha256Digest;
82 case PGPHASHALGO_SHA384:
85 /* FIX: union, not void pointer */
86 ctx->paramlen = sizeof(sha384Param);
87 ctx->param = xcalloc(1, ctx->paramlen);
89 ctx->Reset = (void *) sha384Reset;
90 ctx->Update = (void *) sha384Update;
91 ctx->Digest = (void *) sha384Digest;
93 case PGPHASHALGO_SHA512:
96 /* FIX: union, not void pointer */
97 ctx->paramlen = sizeof(sha512Param);
98 ctx->param = xcalloc(1, ctx->paramlen);
100 ctx->Reset = (void *) sha512Reset;
101 ctx->Update = (void *) sha512Update;
102 ctx->Digest = (void *) sha512Digest;
105 case PGPHASHALGO_RIPEMD160:
106 case PGPHASHALGO_MD2:
107 case PGPHASHALGO_TIGER192:
108 case PGPHASHALGO_HAVAL_5_160:
115 xx = (*ctx->Reset) (ctx->param);
117 DPRINTF((stderr, "*** Init(%x) ctx %p param %p\n", flags, ctx, ctx->param));
121 /* LCL: ctx->param may be modified, but ctx is abstract @*/
123 rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len)
128 DPRINTF((stderr, "*** Update(%p,%p,%d) param %p \"%s\"\n", ctx, data, len, ctx->param, ((char *)data)));
129 return (*ctx->Update) (ctx->param, data, len);
133 rpmDigestFinal(DIGEST_CTX ctx, void ** datap, size_t *lenp, int asAscii)
141 digest = xmalloc(ctx->digestlen);
143 DPRINTF((stderr, "*** Final(%p,%p,%p,%d) param %p digest %p\n", ctx, datap, lenp, asAscii, ctx->param, digest));
145 (void) (*ctx->Digest) (ctx->param, digest);
147 /* Return final digest. */
149 if (lenp) *lenp = ctx->digestlen;
155 if (lenp) *lenp = (2*ctx->digestlen) + 1;
157 const byte * s = (const byte *) digest;
158 static const char hex[] = "0123456789abcdef";
160 *datap = t = xmalloc((2*ctx->digestlen) + 1);
161 for (i = 0 ; i < ctx->digestlen; i++) {
162 *t++ = hex[ (unsigned)((*s >> 4) & 0x0f) ];
163 *t++ = hex[ (unsigned)((*s++ ) & 0x0f) ];
169 memset(digest, 0, ctx->digestlen); /* In case it's sensitive */
172 memset(ctx->param, 0, ctx->paramlen); /* In case it's sensitive */
174 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */