1
0
Fork 1
Spiegel von https://github.com/dani-garcia/vaultwarden.git synchronisiert 2024-06-28 10:15:41 +02:00
vaultwarden/src/db/models
Samuel Tardieu d0baa23f9a Use constant size generic parameter for random bytes generation
All uses of `get_random()` were in the form of:

  `&get_random(vec![0u8; SIZE])`

with `SIZE` being a constant.

Building a `Vec` is unnecessary for two reasons. First, it uses a
very short-lived dynamic memory allocation. Second, a `Vec` is 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 `Vec` did (a `Vec` is 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 function `crypto::encode_random_bytes()`
generates random bytes and encode them with the provided
`Encoding`, 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.
2022-11-11 11:59:27 +01:00
..
attachment.rs Update to diesel2 2022-10-23 00:49:23 +02:00
cipher.rs Update to diesel2 2022-10-23 00:49:23 +02:00
collection.rs Update to diesel2 2022-10-23 00:49:23 +02:00
device.rs Use constant size generic parameter for random bytes generation 2022-11-11 11:59:27 +01:00
emergency_access.rs Update to diesel2 2022-10-23 00:49:23 +02:00
favorite.rs Update to diesel2 2022-10-23 00:49:23 +02:00
folder.rs Update to diesel2 2022-10-23 00:49:23 +02:00
group.rs Update to diesel2 2022-10-23 00:49:23 +02:00
mod.rs group support 2022-10-20 15:31:53 +02:00
org_policy.rs Update to diesel2 2022-10-23 00:49:23 +02:00
organization.rs validate billing_email on save 2022-10-26 21:42:36 +02:00
send.rs Update to diesel2 2022-10-23 00:49:23 +02:00
two_factor.rs Update to diesel2 2022-10-23 00:49:23 +02:00
two_factor_incomplete.rs Update to diesel2 2022-10-23 00:49:23 +02:00
user.rs Update to diesel2 2022-10-23 00:49:23 +02:00