Skip to content

Commit 114395b

Browse files
committed
Add NSS implementation of sha2.h
This commit adds an alternative NSS implementation of functions from the `crapi/sha2.h` module. These functions were implemented only in the gcrypt version, and the implementation in the NSS variant was missing.
1 parent f1291ad commit 114395b

File tree

1 file changed

+71
-18
lines changed

1 file changed

+71
-18
lines changed

src/OVAL/probes/crapi/sha2.c

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,49 +102,102 @@ static int crapi_sha2_fd (HASH_HashType algo, int fd, void *dst, size_t *size)
102102
return (0);
103103
}
104104

105+
struct crapi_sha2_ctx {
106+
HASHContext *ctx;
107+
void *dst;
108+
size_t *size;
109+
};
110+
111+
static void *crapi_sha2_init (void *dst, void *size, int alg)
112+
{
113+
struct crapi_sha2_ctx *ctx = malloc(sizeof(struct crapi_sha2_ctx));
114+
115+
ctx->ctx = HASH_Create (alg);
116+
ctx->dst = dst;
117+
ctx->size = size;
118+
119+
if (ctx->ctx != NULL) {
120+
HASH_Begin (ctx->ctx);
121+
} else {
122+
free (ctx);
123+
ctx = NULL;
124+
}
125+
126+
return (ctx);
127+
}
128+
129+
static int crapi_sha2_update (void *ctxp, void *bptr, size_t blen)
130+
{
131+
struct crapi_sha2_ctx *ctx = (struct crapi_sha1_ctx *)ctxp;
132+
133+
HASH_Update (ctx->ctx, (const unsigned char *)bptr, (unsigned int)blen);
134+
return (0);
135+
}
136+
137+
static int crapi_sha2_fini (void *ctxp)
138+
{
139+
struct crapi_sha2_ctx *ctx = (struct crapi_sha2_ctx *)ctxp;
140+
141+
HASH_End (ctx->ctx, ctx->dst, (unsigned int *)ctx->size, *ctx->size);
142+
HASH_Destroy (ctx->ctx);
143+
free (ctx);
144+
145+
return (0);
146+
}
147+
148+
static void crapi_sha2_free (void *ctxp)
149+
{
150+
struct crapi_sha2_ctx *ctx = (struct crapi_sha1_ctx *)ctxp;
151+
152+
HASH_Destroy (ctx->ctx);
153+
free (ctx);
154+
155+
return;
156+
}
157+
105158
void *crapi_sha224_init (void *dst, void *size)
106159
{
107-
return (NULL);
160+
return crapi_sha2_init(dst, size, HASH_AlgSHA224);
108161
}
109162

110163
int crapi_sha224_update (void *ctxp, void *bptr, size_t blen)
111164
{
112-
return (-1);
165+
return crapi_sha2_update(ctxp, bptr, blen);
113166
}
114167

115168
int crapi_sha224_fini (void *ctxp)
116169
{
117-
return (-1);
170+
return crapi_sha2_fini(ctxp);
118171
}
119172

120173
void crapi_sha224_free (void *ctxp)
121174
{
122-
return;
175+
crapi_sha2_free(ctxp);
123176
}
124177

125178
int crapi_sha224_fd (int fd, void *dst, size_t *size)
126179
{
127-
return (-1);
180+
return crapi_sha2_fd (HASH_AlgSHA224, fd, dst, size);
128181
}
129182

130183
void *crapi_sha256_init (void *dst, void *size)
131184
{
132-
return (NULL);
185+
return crapi_sha2_init(dst, size, HASH_AlgSHA256);
133186
}
134187

135188
int crapi_sha256_update (void *ctxp, void *bptr, size_t blen)
136189
{
137-
return (-1);
190+
return crapi_sha2_update(ctxp, bptr, blen);
138191
}
139192

140193
int crapi_sha256_fini (void *ctxp)
141194
{
142-
return (-1);
195+
return crapi_sha2_fini(ctxp);
143196
}
144197

145198
void crapi_sha256_free (void *ctxp)
146199
{
147-
return;
200+
crapi_sha2_free(ctxp);
148201
}
149202

150203
int crapi_sha256_fd (int fd, void *dst, size_t *size)
@@ -154,47 +207,47 @@ int crapi_sha256_fd (int fd, void *dst, size_t *size)
154207

155208
void *crapi_sha384_init (void *dst, void *size)
156209
{
157-
return (NULL);
210+
return crapi_sha2_init(dst, size, HASH_AlgSHA384);
158211
}
159212

160213
int crapi_sha384_update (void *ctxp, void *bptr, size_t blen)
161214
{
162-
return (-1);
215+
return crapi_sha2_update(ctxp, bptr, blen);
163216
}
164217

165218
int crapi_sha384_fini (void *ctxp)
166219
{
167-
return (-1);
220+
return crapi_sha2_fini(ctxp);
168221
}
169222

170223
void crapi_sha384_free (void *ctxp)
171224
{
172-
return;
225+
crapi_sha2_free(ctxp);
173226
}
174227

175228
int crapi_sha384_fd (int fd, void *dst, size_t *size)
176229
{
177-
return (-1);
230+
return crapi_sha2_fd (HASH_AlgSHA384, fd, dst, size);
178231
}
179232

180233
void *crapi_sha512_init (void *dst, void *size)
181234
{
182-
return (NULL);
235+
return crapi_sha2_init(dst, size, HASH_AlgSHA512);
183236
}
184237

185238
int crapi_sha512_update (void *ctxp, void *bptr, size_t blen)
186239
{
187-
return (-1);
240+
return crapi_sha2_update(ctxp, bptr, blen);
188241
}
189242

190243
int crapi_sha512_fini (void *ctxp)
191244
{
192-
return (-1);
245+
return crapi_sha2_fini(ctxp);
193246
}
194247

195248
void crapi_sha512_free (void *ctxp)
196249
{
197-
return;
250+
crapi_sha2_free(ctxp);
198251
}
199252

200253
int crapi_sha512_fd (int fd, void *dst, size_t *size)

0 commit comments

Comments
 (0)