Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multi-key"
version = "1.0.3"
version = "1.0.5"
edition = "2021"
authors = ["Dave Grantham <dwg@linuxprogrammer.org>"]
description = "Multikey self-describing cryptographic key data"
Expand Down
10 changes: 10 additions & 0 deletions src/attrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub enum AttrId {
/// CBOR-encoded `ThresholdMetaCipher` (cipher codec + nonce) for decrypting
/// `EncryptedThresholdMeta`.
ThresholdMetaCipher,
/// An algorithm string name for non-standard keys (optional)
AlgorithmName,
/// An arbitrary key type for non-standard keys (optional)
KeyType,
}

impl AttrId {
Expand Down Expand Up @@ -116,6 +120,8 @@ impl AttrId {
AttrId::ThresholdDisclosure => "threshold-disclosure",
AttrId::EncryptedThresholdMeta => "encrypted-threshold-meta",
AttrId::ThresholdMetaCipher => "threshold-meta-cipher",
AttrId::AlgorithmName => "algorithm-name",
AttrId::KeyType => "key-type",
}
}
}
Expand Down Expand Up @@ -158,6 +164,8 @@ impl TryFrom<u8> for AttrId {
24 => Ok(AttrId::ThresholdDisclosure),
25 => Ok(AttrId::EncryptedThresholdMeta),
26 => Ok(AttrId::ThresholdMetaCipher),
27 => Ok(AttrId::AlgorithmName),
28 => Ok(AttrId::KeyType),
_ => Err(AttributesError::InvalidAttributeValue(c).into()),
}
}
Expand Down Expand Up @@ -220,6 +228,8 @@ impl TryFrom<&str> for AttrId {
"threshold-disclosure" => Ok(AttrId::ThresholdDisclosure),
"encrypted-threshold-meta" => Ok(AttrId::EncryptedThresholdMeta),
"threshold-meta-cipher" => Ok(AttrId::ThresholdMetaCipher),
"algorithm-name" => Ok(AttrId::AlgorithmName),
"key-type" => Ok(AttrId::KeyType),
_ => Err(AttributesError::InvalidAttributeName(s.to_string()).into()),
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
use crate::{mk::Attributes, AttrId, Error, Multikey};
use crate::{error::CipherError, mk::Attributes, AttrId, Error, Multikey};
use multi_codec::Codec;
use rand_core::CryptoRng;
use zeroize::Zeroizing;
Expand All @@ -24,12 +24,13 @@ impl Builder {

/// initialize from a multikey with cipher attributes in it
pub fn try_from_multikey(mut self, mk: &Multikey) -> Result<Self, Error> {
// try to look up the cipher codec in the multikey attributes
if let Some(v) = mk.attributes.get(&AttrId::CipherCodec) {
if let Ok(codec) = Codec::try_from(v.as_slice()) {
self.codec = codec;
}
}
// look up the cipher codec in the multikey attributes
let v = mk
.attributes
.get(&AttrId::CipherCodec)
.ok_or(CipherError::MissingCodec)?;
self.codec = Codec::try_from(v.as_slice())?;

// try to look up the key_length in the multikey attributes
if let Some(v) = mk.attributes.get(&AttrId::CipherKeyLen) {
self.key_length = Some(v.clone());
Expand Down
15 changes: 8 additions & 7 deletions src/kdf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
use crate::{mk::Attributes, AttrId, Error, Multikey};
use crate::{error::KdfError, mk::Attributes, AttrId, Error, Multikey};
use multi_codec::Codec;
use multi_util::Varuint;
use rand_core::CryptoRng;
Expand All @@ -24,12 +24,13 @@ impl Builder {

/// initialize from a multikey with kdf attributes in it
pub fn try_from_multikey(mut self, mk: &Multikey) -> Result<Self, Error> {
// try to look up the kdf codec in the multikey attributes
if let Some(v) = mk.attributes.get(&AttrId::KdfCodec) {
if let Ok(codec) = Codec::try_from(v.as_slice()) {
self.codec = codec;
}
}
// look up the kdf codec in the multikey attributes
let v = mk
.attributes
.get(&AttrId::KdfCodec)
.ok_or(KdfError::MissingCodec)?;
self.codec = Codec::try_from(v.as_slice())?;

// try to look up the salt in the multikey attributes
if let Some(v) = mk.attributes.get(&AttrId::KdfSalt) {
self.salt = Some(v.clone());
Expand Down
3 changes: 2 additions & 1 deletion src/mk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use std::{collections::BTreeMap, fmt};
use zeroize::Zeroizing;

/// the list of key codecs supported for key generation
pub const KEY_CODECS: [Codec; 7] = [
pub const KEY_CODECS: [Codec; 8] = [
Codec::Identity, // used for non-standard key types
Codec::Ed25519Priv,
Codec::Secp256K1Priv,
Codec::Bls12381G1Priv,
Expand Down
12 changes: 6 additions & 6 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'de> Deserialize<'de> for Nonce {
deserializer.deserialize_struct(nonce::SIGIL.as_str(), FIELDS, NonceVisitor)
} else {
let b: &'de [u8] = Deserialize::deserialize(deserializer)?;
Ok(Self::try_from(b).map_err(|e| Error::custom(e.to_string()))?)
Ok(Self::try_from(b).map_err(D::Error::custom)?)
}
}
}
Expand All @@ -84,28 +84,28 @@ impl<'de> Deserialize<'de> for AttrId {
where
E: Error,
{
AttrId::try_from(c).map_err(|e| Error::custom(e.to_string()))
AttrId::try_from(c).map_err(E::custom)
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
AttrId::try_from(s).map_err(E::custom)
}

fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
AttrId::try_from(s).map_err(E::custom)
}

fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: Error,
{
AttrId::try_from(s.as_str()).map_err(|e| Error::custom(e.to_string()))
AttrId::try_from(s.as_str()).map_err(E::custom)
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'de> Deserialize<'de> for Multikey {
deserializer.deserialize_struct(mk::SIGIL.as_str(), FIELDS, MultikeyVisitor)
} else {
let b: &'de [u8] = Deserialize::deserialize(deserializer)?;
Ok(Self::try_from(b).map_err(|e| Error::custom(e.to_string()))?)
Ok(Self::try_from(b).map_err(D::Error::custom)?)
}
}
}
Loading