pvsecret: Fix some edge cases for plaintext keys

Fix two edge cases for generating and interpreting plaintext secrets:
1. The maximum payload size was two bytes to long. The space for the
   length header was forgotten to take into account.
2. One of the checks for if the plaintext secret has a size was too
   strict.

Fixes: fd024387d7 ("rust/pv: Retrievable secrets support")
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Steffen Eiden
2025-06-23 11:34:06 +02:00
parent e8237e6ae0
commit 8fadcd9fe5
2 changed files with 5 additions and 4 deletions

View File

@@ -239,7 +239,7 @@ fn extend_to_multiple(mut key: Vec<u8>, multiple: usize) -> Confidential<Vec<u8>
/// ```
fn plaintext(inp: Confidential<Vec<u8>>) -> Result<RetrKeyInfo> {
let key_len = inp.value().len();
if key_len > RetrieveCmd::MAX_SIZE {
if key_len > MAX_SIZE_PLAIN_PAYLOAD {
return Err(Error::RetrInvKey {
what: "key size",
value: key_len.to_string(),

View File

@@ -2,7 +2,7 @@
//
// Copyright IBM Corp. 2024
use crate::{pem::Pem, uvsecret::guest_secret::MAX_SIZE_PLAIN_PAYLOAD, Result};
use crate::{crypto::SymKeyType, pem::Pem, uvsecret::guest_secret::MAX_SIZE_PLAIN_PAYLOAD, Result};
use log::warn;
use pv_core::{
@@ -81,10 +81,11 @@ impl From<RetrieveCmd> for RetrievedSecret {
// Test if the plain text secret has a size:
// 1. len <= 8190
// 2. first two bytes are max 15 less than buffer-size+2
// 2. first two bytes are max 15 less than buffer-size+2 i.e. smaller than the
// block length
// 3. bytes after len + 2 are zero
match len <= MAX_SIZE_PLAIN_PAYLOAD
&& key.value().len() - (len + 2) < 15
&& key.value().len() - (len + 2) < SymKeyType::AES_256_GCM_BLOCK_LEN
&& key.value()[len + 2..].iter().all(|c| *c == 0)
{
false => Self::Plaintext(key),