Skip to content

Commit 002d044

Browse files
committed
bcrypt: Initialize checksum context on demand with the first update to
properly handle all potential errors. The context is considered uninitialized if the handle is NULL. * subversion/libsvn_subr/checksum_bcrypt.c (bcrypt_ctx_cleanup): Check the handle for NULL. In this case, ignore cleanup entirely. (bcrypt_ctx_update, bcrypt_ctx_final): Call initialization if the handle is NULL. (bcrypt_ctx_update): Now also requires a pointer to algorithm state to perform initialization, if needed. (bcrypt_ctx_reset): Simply set ctx->handle to NULL. (bcrypt_checksum): Forward algorithm to bcrypt_ctx_update. (svn_checksum__md5_ctx_create, svn_checksum__sha1_ctx_create): Remove initialization. (svn_checksum__md5_ctx_update, svn_checksum__sha1_ctx_update): Forward algorithm parameter. git-svn-id: https://svn.apache.org/repos/asf/subversion/trunk@1931082 13f79535-47bb-0310-9956-ffa450edef68
1 parent 83af96e commit 002d044

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

subversion/libsvn_subr/checksum_bcrypt.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ bcrypt_ctx_cleanup(void *data)
9797
{
9898
bcrypt_ctx_t *ctx = (bcrypt_ctx_t *)data;
9999

100-
if (! BCRYPT_SUCCESS(BCryptDestroyHash(ctx->handle)))
101-
SVN_ERR_MALFUNCTION_NO_RETURN();
100+
if (ctx->handle)
101+
{
102+
NTSTATUS status = BCryptDestroyHash(ctx->handle);
103+
104+
if (! BCRYPT_SUCCESS(status))
105+
SVN_ERR_MALFUNCTION_NO_RETURN();
106+
}
102107

103108
return APR_SUCCESS;
104109
}
@@ -124,12 +129,16 @@ bcrypt_ctx_init(bcrypt_ctx_t *ctx,
124129
}
125130

126131
static svn_error_t *
127-
bcrypt_ctx_update(bcrypt_ctx_t *ctx,
132+
bcrypt_ctx_update(algorithm_state_t *algorithm,
133+
bcrypt_ctx_t *ctx,
128134
const void *data,
129135
apr_size_t len)
130136
{
131137
SVN_ERR_ASSERT(len <= ULONG_MAX);
132138

139+
if (! ctx->handle)
140+
SVN_ERR(bcrypt_ctx_init(ctx, algorithm));
141+
133142
SVN_ERR(handle_error(BCryptHashData(ctx->handle,
134143
(PUCHAR) data,
135144
(ULONG) len,
@@ -143,6 +152,9 @@ bcrypt_ctx_final(algorithm_state_t *algorithm,
143152
unsigned char *digest,
144153
bcrypt_ctx_t *ctx)
145154
{
155+
if (! ctx->handle)
156+
SVN_ERR(bcrypt_ctx_init(ctx, algorithm));
157+
146158
SVN_ERR(handle_error(BCryptFinishHash(ctx->handle,
147159
(PUCHAR) digest,
148160
algorithm->hash_length,
@@ -152,12 +164,9 @@ bcrypt_ctx_final(algorithm_state_t *algorithm,
152164
}
153165

154166
static svn_error_t *
155-
bcrypt_ctx_reset(algorithm_state_t *algorithm,
156-
bcrypt_ctx_t *ctx)
167+
bcrypt_ctx_reset(algorithm_state_t *algorithm, bcrypt_ctx_t *ctx)
157168
{
158-
bcrypt_ctx_cleanup(ctx);
159-
SVN_ERR(bcrypt_ctx_init(ctx, algorithm));
160-
169+
ctx->handle = NULL;
161170
return SVN_NO_ERROR;
162171
}
163172

@@ -172,7 +181,7 @@ bcrypt_checksum(algorithm_state_t *algorithm,
172181

173182
SVN_ERR(bcrypt_ctx_init(&bcrypt_ctx, algorithm));
174183

175-
err = bcrypt_ctx_update(&bcrypt_ctx, data, len);
184+
err = bcrypt_ctx_update(algorithm, &bcrypt_ctx, data, len);
176185
if (err)
177186
{
178187
bcrypt_ctx_cleanup(&bcrypt_ctx);
@@ -204,10 +213,6 @@ svn_checksum__md5_ctx_t *
204213
svn_checksum__md5_ctx_create(apr_pool_t *pool)
205214
{
206215
svn_checksum__md5_ctx_t *ctx = apr_pcalloc(pool, sizeof(*ctx));
207-
svn_error_t *err;
208-
209-
err = bcrypt_ctx_init(&ctx->bcrypt_ctx, &md5);
210-
SVN_ERR_ASSERT_NO_RETURN(err == SVN_NO_ERROR);
211216

212217
apr_pool_cleanup_register(pool, &ctx->bcrypt_ctx, bcrypt_ctx_cleanup, NULL);
213218

@@ -225,8 +230,8 @@ svn_checksum__md5_ctx_update(svn_checksum__md5_ctx_t *ctx,
225230
const void *data,
226231
apr_size_t len)
227232
{
228-
return svn_error_trace(bcrypt_ctx_update(&ctx->bcrypt_ctx, data,
229-
len));
233+
return svn_error_trace(bcrypt_ctx_update(&md5, &ctx->bcrypt_ctx,
234+
data, len));
230235
}
231236

232237
svn_error_t *
@@ -255,10 +260,6 @@ svn_checksum__sha1_ctx_t *
255260
svn_checksum__sha1_ctx_create(apr_pool_t *pool)
256261
{
257262
svn_checksum__sha1_ctx_t *ctx = apr_pcalloc(pool, sizeof(*ctx));
258-
svn_error_t *err;
259-
260-
err = bcrypt_ctx_init(&ctx->bcrypt_ctx, &sha1);
261-
SVN_ERR_ASSERT_NO_RETURN(err == SVN_NO_ERROR);
262263

263264
apr_pool_cleanup_register(pool, &ctx->bcrypt_ctx, bcrypt_ctx_cleanup, NULL);
264265

@@ -276,7 +277,8 @@ svn_checksum__sha1_ctx_update(svn_checksum__sha1_ctx_t *ctx,
276277
const void *data,
277278
apr_size_t len)
278279
{
279-
return svn_error_trace(bcrypt_ctx_update(&ctx->bcrypt_ctx, data, len));
280+
return svn_error_trace(bcrypt_ctx_update(&sha1, &ctx->bcrypt_ctx,
281+
data, len));
280282
}
281283

282284
svn_error_t *

0 commit comments

Comments
 (0)