1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/** \ingroup signature
* \file rpmio/digest.c
*/
#include "system.h"
#include "beecrypt.h"
#include "md5.h"
#include "endianness.h"
#include "sha1.h"
#include "rpmio_internal.h"
#include "debug.h"
#ifdef SHA_DEBUG
#define DPRINTF(_a) fprintf _a
#else
#define DPRINTF(_a)
#endif
/*@access DIGEST_CTX@*/
/**
* MD5/SHA1 digest private data.
*/
struct DIGEST_CTX_s {
rpmDigestFlags flags; /*!< Bit(s) to control digest operation. */
uint32 datalen; /*!< No. bytes in block of plaintext data. */
uint32 paramlen; /*!< No. bytes of digest parameters. */
uint32 digestlen; /*!< No. bytes of digest. */
void * param; /*!< Digest parameters. */
int (*Reset) (void * param)
/*@modifies param @*/; /*!< Digest initialize. */
int (*Update) (void * param, const byte * data, int len)
/*@modifies param @*/; /*!< Digest transform. */
int (*Digest) (void * param, /*@out@*/ uint32 * digest)
/*@modifies param, digest @*/; /*!< Digest finish. */
};
/*@-boundsread@*/
DIGEST_CTX
rpmDigestDup(DIGEST_CTX octx)
{
DIGEST_CTX nctx = memcpy(xcalloc(1, sizeof(*nctx)), octx, sizeof(*nctx));
nctx->param = memcpy(xcalloc(1, nctx->paramlen), octx->param, nctx->paramlen);
return nctx;
}
/*@=boundsread@*/
DIGEST_CTX
rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
{
DIGEST_CTX ctx = xcalloc(1, sizeof(*ctx));
int xx;
ctx->flags = flags;
switch (hashalgo) {
case PGPHASHALGO_MD5:
ctx->digestlen = 16;
ctx->datalen = 64;
/*@-sizeoftype@*/ /* FIX: union, not void pointer */
ctx->paramlen = sizeof(md5Param);
/*@=sizeoftype@*/
ctx->param = xcalloc(1, ctx->paramlen);
/*@-type@*/ /* FIX: cast? */
ctx->Reset = (void *) md5Reset;
ctx->Update = (void *) md5Update;
ctx->Digest = (void *) md5Digest;
/*@=type@*/
break;
case PGPHASHALGO_SHA1:
ctx->digestlen = 20;
ctx->datalen = 64;
/*@-sizeoftype@*/ /* FIX: union, not void pointer */
ctx->paramlen = sizeof(sha1Param);
/*@=sizeoftype@*/
ctx->param = xcalloc(1, ctx->paramlen);
/*@-type@*/ /* FIX: cast? */
ctx->Reset = (void *) sha1Reset;
ctx->Update = (void *) sha1Update;
ctx->Digest = (void *) sha1Digest;
/*@=type@*/
break;
case PGPHASHALGO_RIPEMD160:
case PGPHASHALGO_MD2:
case PGPHASHALGO_TIGER192:
case PGPHASHALGO_HAVAL_5_160:
default:
free(ctx);
return NULL;
/*@notreached@*/ break;
}
/*@-boundsread@*/
xx = (*ctx->Reset) (ctx->param);
/*@=boundsread@*/
DPRINTF((stderr, "*** Init(%x) ctx %p param %p\n", flags, ctx, ctx->param));
return ctx;
}
/*@-mustmod@*/ /* LCL: ctx->param may be modified, but ctx is abstract @*/
int
rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len)
{
DPRINTF((stderr, "*** Update(%p,%p,%d) param %p \"%s\"\n", ctx, data, len, ctx->param, ((char *)data)));
/*@-boundsread@*/
return (*ctx->Update) (ctx->param, data, len);
/*@=boundsread@*/
}
/*@=mustmod@*/
/*@unchecked@*/
static int _ie = 0x44332211;
/*@-redef@*/
/*@unchecked@*/
static union _dendian {
/*@unused@*/ int i;
char b[4];
} *_endian = (union _dendian *)&_ie;
/*@=redef@*/
#define IS_BIG_ENDIAN() (_endian->b[0] == '\x44')
#define IS_LITTLE_ENDIAN() (_endian->b[0] == '\x11')
/*@-boundswrite@*/
int
rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx, /*@out@*/ void ** datap,
/*@out@*/ size_t *lenp, int asAscii)
{
uint32 * digest = xmalloc(ctx->digestlen);
char * t;
int i;
DPRINTF((stderr, "*** Final(%p,%p,%p,%d) param %p digest %p\n", ctx, datap, lenp, asAscii, ctx->param, digest));
/*@-noeffectuncon@*/ /* FIX: check rc */
(void) (*ctx->Digest) (ctx->param, digest);
/*@=noeffectuncon@*/
/*@-sizeoftype@*/
if (IS_LITTLE_ENDIAN())
for (i = 0; i < (ctx->digestlen/sizeof(uint32)); i++)
digest[i] = swapu32(digest[i]);
/*@=sizeoftype@*/
/* Return final digest. */
/*@-branchstate@*/
if (!asAscii) {
if (lenp) *lenp = ctx->digestlen;
if (datap) {
*datap = digest;
digest = NULL;
}
} else {
if (lenp) *lenp = (2*ctx->digestlen) + 1;
if (datap) {
const byte * s = (const byte *) digest;
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';
}
}
/*@=branchstate@*/
if (digest) {
memset(digest, 0, ctx->digestlen); /* In case it's sensitive */
free(digest);
}
memset(ctx->param, 0, ctx->paramlen); /* In case it's sensitive */
free(ctx->param);
memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
free(ctx);
return 0;
}
/*@=boundswrite@*/
|