Skip to content

Commit d3fab50

Browse files
committed
bcrypt: Eliminate heap allocations when computing checksum contextless of a
single data block. Use forbidden alloca() function to create the buffer and pass it to the initialization function. According to the documentation [1]#pbHashObject, we should not call BCryptDestroyHash() if non-null buffer is supplied. * subversion/libsvn_subr/checksum_bcrypt.c (bcrypt_checksum): Manually initialize the context and the algorithm. Remove BCryptDestroyHash() call. [1] https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptcreatehash git-svn-id: https://svn.apache.org/repos/asf/subversion/trunk@1931093 13f79535-47bb-0310-9956-ffa450edef68
1 parent 366e9c6 commit d3fab50

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

subversion/libsvn_subr/checksum_bcrypt.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,29 @@ bcrypt_checksum(algorithm_state_t *algorithm,
177177
const void *data,
178178
apr_size_t len)
179179
{
180-
bcrypt_ctx_t bcrypt_ctx;
181-
svn_error_t *err = SVN_NO_ERROR;
180+
bcrypt_ctx_t bcrypt_ctx = { 0 };
181+
void *object_buf;
182+
183+
SVN_ERR(svn_atomic__init_once(&algorithm->initialized, algorithm_init,
184+
algorithm, NULL));
182185

183-
SVN_ERR(bcrypt_ctx_init(algorithm, &bcrypt_ctx));
186+
SVN_ERR_ASSERT(algorithm->object_length < 4096);
187+
object_buf = alloca(algorithm->object_length);
184188

185-
err = bcrypt_ctx_update(algorithm, &bcrypt_ctx, data, len);
186-
if (err)
187-
{
188-
bcrypt_ctx_cleanup(&bcrypt_ctx);
189-
return err;
190-
}
189+
SVN_ERR(handle_error(BCryptCreateHash(algorithm->alg_handle,
190+
&bcrypt_ctx.handle,
191+
object_buf, algorithm->object_length,
192+
/* pbSecret */ NULL,
193+
/* cbSecret */ 0,
194+
/* dwFlags */ 0)));
195+
196+
SVN_ERR(bcrypt_ctx_update(algorithm, &bcrypt_ctx,
197+
data, len));
191198

192-
SVN_ERR(bcrypt_ctx_final(algorithm, &bcrypt_ctx, digest));
199+
SVN_ERR(bcrypt_ctx_final(algorithm, &bcrypt_ctx,
200+
digest));
193201

194-
bcrypt_ctx_cleanup(&bcrypt_ctx);
195-
return err;
202+
return SVN_NO_ERROR;
196203
}
197204

198205

0 commit comments

Comments
 (0)