Reputation: 1285
I'm trying to simplify some code which uses the psa_crypto
library.
More in details I'm using their first encryption example from their docs from their docs:
use psa_crypto::types::algorithm::{Aead, AeadWithDefaultLengthTag};
use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags};
use psa_crypto::operations::{key_management, aead};
let alg = Aead::AeadWithDefaultLengthTag(AeadWithDefaultLengthTag::Ccm);
let mut usage_flags: UsageFlags = Default::default();
usage_flags.set_encrypt();
let attributes = Attributes {
key_type: Type::Aes,
bits: 0,
lifetime: Lifetime::Volatile,
policy: Policy {
usage_flags,
permitted_algorithms: alg.into(),
},
};
psa_crypto::init().unwrap();
let my_key = key_management::import(attributes, None, &KEY_DATA).unwrap();
let output_buffer_size = attributes.aead_encrypt_output_size(alg.into(), INPUT_DATA.len()).unwrap();
let mut output_buffer = vec![0; output_buffer_size];
let length = aead::encrypt(my_key, alg, &NONCE, &ADDITIONAL_DATA, &INPUT_DATA, &mut output_buffer).unwrap();
output_buffer.resize(length, 0);
I put this code inside a function using anyhow::Result
I would like to replace the unwrap
s with some more idiomatic code. Unfortunately if I try to change the first unwrap
with `.context("error") I get an error:
let my_key = key_management::import(attributes, None, &KEY_DATA).context("error")?;
^^^^^^^ method cannot be called on `Result<Id, psa_crypto::types::status::Error>` due to unsatisfied trait bounds
|
504 | pub enum Result<T, E> {
| --------------------- doesn't satisfy `_: anyhow::Context<Id, psa_crypto::types::status::Error>`
|
::: /home/fedboz01/.cargo/registry/src/github.com-1ecc6299db9ec823/psa-crypto-0.9.1/src/types/status.rs:50:1
|
50 | pub enum Error {
| -------------- doesn't satisfy `_: anyhow::context::ext::StdError`
|
= note: the following trait bounds were not satisfied:
`psa_crypto::types::status::Error: anyhow::context::ext::StdError`
which is required by `Result<Id, psa_crypto::types::status::Error>: anyhow::Context<Id, psa_crypto::types::status::Error>`
What does this error mean? How can I change this code to make it work?
Upvotes: 0
Views: 1174
Reputation: 26569
anyhow::Error
requires that the error it wraps implement std::error::Error
. psa_crypto::types::status::Error
does not implement that trait, so in turn, it does not implement the Context
extension trait.
This part of the error message hints at this, though it's a bit obscured, since it prints anyhow's internal alias for std::error::Error
:
50 | pub enum Error {
| -------------- doesn't satisfy `_: anyhow::context::ext::StdError`
|
= note: the following trait bounds were not satisfied:
`psa_crypto::types::status::Error: anyhow::context::ext::StdError`
which is required by `Result<Id, psa_crypto::types::status::Error>: anyhow::Context<Id, psa_crypto::types::status::Error>`
This seems like an oversight in the psa_crypto
library. It may be worth filing a bug asking them to implement that trait. As a workaround, you have a few options:
psa_crypto::types::status::Error
implements std::fmt::Debug
, you can use this to get a String
error message: psa_crypto::init().map_err(|e| format!("{:?}", e).context("While initializing")
Error
(and its requirement, Display
).Upvotes: 2