[PR #2910] [MERGED] Use constant size generic parameter for random bytes generation #3152

Closed
opened 2026-02-05 05:15:11 +03:00 by OVERLORD · 0 comments
Owner

📋 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: mainHead: const-crypto


📝 Commits (2)

  • d0baa23 Use constant size generic parameter for random bytes generation
  • 7445ee4 Remove 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 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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/dani-garcia/vaultwarden/pull/2910 **Author:** [@samueltardieu](https://github.com/samueltardieu) **Created:** 11/11/2022 **Status:** ✅ Merged **Merged:** 11/27/2022 **Merged by:** [@dani-garcia](https://github.com/dani-garcia) **Base:** `main` ← **Head:** `const-crypto` --- ### 📝 Commits (2) - [`d0baa23`](https://github.com/dani-garcia/vaultwarden/commit/d0baa23f9a15152a370092e955c2cf87891c26e7) Use constant size generic parameter for random bytes generation - [`7445ee4`](https://github.com/dani-garcia/vaultwarden/commit/7445ee40f8930c2f72e87d5809d2ff584b0c4d30) Remove get_random_64() ### 📊 Changes **8 files changed** (+21 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
OVERLORD added the pull-request label 2026-02-05 05:15:11 +03:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/vaultwarden#3152