mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2026-03-01 11:19:52 +03:00
[PR #2910] [MERGED] Use constant size generic parameter for random bytes generation #3152
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/dani-garcia/vaultwarden/pull/2910
Author: @samueltardieu
Created: 11/11/2022
Status: ✅ Merged
Merged: 11/27/2022
Merged by: @dani-garcia
Base:
main← Head:const-crypto📝 Commits (2)
d0baa23Use constant size generic parameter for random bytes generation7445ee4Remove get_random_64()📊 Changes
8 files changed (+21 additions, -19 deletions)
View changed files
📝
src/api/core/two_factor/authenticator.rs(+1 -1)📝
src/api/core/two_factor/mod.rs(+1 -1)📝
src/api/notifications.rs(+1 -1)📝
src/config.rs(+1 -2)📝
src/crypto.rs(+13 -10)📝
src/db/models/device.rs(+2 -2)📝
src/db/models/send.rs(+1 -1)📝
src/db/models/user.rs(+1 -1)📄 Description
All uses of
get_random()were in the form of:&get_random(vec![0u8; SIZE])with
SIZEbeing a constant.Building a
Vecis unnecessary for two reasons. First, it uses a very short-lived dynamic memory allocation. Second, aVecis a resizable object, which is useless in those context when random data have a fixed size and will only be read.get_random_bytes()takes a constant as a generic parameter and returns an array with the requested number of random bytes.Stack safety analysis: the random bytes will be allocated on the caller stack for a very short time (until the encoding function has been called on the data). In some cases, the random bytes take less room than the
Vecdid (aVecis 24 bytes on a 64 bit computer). The maximum used size is 180 bytes, which makes it for 0.008% of the default stack size for a Rust thread (2MiB), so this is a non-issue.Also, most of the uses of those random bytes are to encode them using an
Encoding. The functioncrypto::encode_random_bytes()generates random bytes and encode them with the providedEncoding, leading to code deduplication.generate_id()has also been converted to use a constant generic parameter as well since the length of the requested String is always a constant.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.