From 6e5edfd18e18d35a6c90a092345e89c9d3e9f01b Mon Sep 17 00:00:00 2001 From: madomado Date: Thu, 23 May 2024 00:10:00 +0800 Subject: [PATCH 1/7] Implement `std::error::Error` for `cocoon::Error` This adds the `std::error::Error` trait implementation to `cocoon::Error` using the `thiserror` crate. Note that this is only limited to the presence of feature "std". --- Cargo.toml | 3 ++- src/error.rs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 31b8759..3735885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ hmac = "0.11" pbkdf2 = {version = "0.9", default-features = false, features = ["sha2", "hmac"]} rand = {version = "0.8", default-features = false, features = ["std_rng"]} sha2 = {version = "0.9", default-features = false} +thiserror = {version = "1.0.61", optional = true} zeroize = {version = "1", default-features = false} [dev-dependencies] @@ -31,7 +32,7 @@ default = ["std"] # Enables all features, including support of simplified Cocoon API, using `rand::thread_rng`, # and API related to `std::io`: wrap to writer, unwrap from reader. -std = ["alloc", "rand/std"] +std = ["alloc", "rand/std", "dep:thiserror"] # Enables `Vec` container. Can be used without `std` crate (in "no std" build). alloc = ["chacha20poly1305/alloc"] diff --git a/src/error.rs b/src/error.rs index 8b7c397..2cc6ee8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,20 +1,26 @@ /// Error variants produced by the Cocoon API. #[derive(Debug)] +#[cfg_attr(feature = "std", derive(thiserror::Error))] pub enum Error { /// I/o error during read/write operation (`Cocoon::dump`, `Cocoon::parse`). #[cfg(feature = "std")] + #[error("Input/output error")] Io(std::io::Error), /// Format is not recognized. Probably corrupted. + #[cfg_attr(feature = "std", error("Unrecognized format"))] UnrecognizedFormat, /// Cryptographic error. There could be a few reasons: /// 1. Integrity is compromised. /// 2. Password is invalid. + #[cfg_attr(feature = "std", error("Cryptographic error: bad integrity/password?"))] Cryptography, /// Container is too large to get processed on the current architecture. /// E.g. it's not possible to process a container larger than 4 GB on 32-bit architecture. + #[cfg_attr(feature = "std", error("Container size exceeds architectural limit"))] TooLarge, /// Buffer is too short and barely holds all data to decrypt, inconsistent length /// encoded to the header. + #[cfg_attr(feature = "std", error("Insufficient buffer size for decyrpted data"))] TooShort, } From d56dc290e2ca6847401b5241f276c0002b635e82 Mon Sep 17 00:00:00 2001 From: madomado Date: Thu, 23 May 2024 20:28:32 +0800 Subject: [PATCH 2/7] Better error message on Error::Cryptography Co-authored-by: Alexander Fadeev --- src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 2cc6ee8..a8043de 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,7 +12,7 @@ pub enum Error { /// Cryptographic error. There could be a few reasons: /// 1. Integrity is compromised. /// 2. Password is invalid. - #[cfg_attr(feature = "std", error("Cryptographic error: bad integrity/password?"))] + #[cfg_attr(feature = "std", error("Cryptographic error: bad integrity/password"))] Cryptography, /// Container is too large to get processed on the current architecture. /// E.g. it's not possible to process a container larger than 4 GB on 32-bit architecture. From 6f5304cef39a866625ead5ebff8b91a5accafd4d Mon Sep 17 00:00:00 2001 From: madomado Date: Thu, 23 May 2024 20:29:15 +0800 Subject: [PATCH 3/7] Fix error message typo Error::TooSmall Co-authored-by: Alexander Fadeev --- src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index a8043de..3a5f5bc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,7 +20,7 @@ pub enum Error { TooLarge, /// Buffer is too short and barely holds all data to decrypt, inconsistent length /// encoded to the header. - #[cfg_attr(feature = "std", error("Insufficient buffer size for decyrpted data"))] + #[cfg_attr(feature = "std", error("Insufficient buffer size for decrypted data"))] TooShort, } From a8bba7acb7d48833bed3177c20ecf88a4c864554 Mon Sep 17 00:00:00 2001 From: madomado Date: Thu, 23 May 2024 23:36:03 +0800 Subject: [PATCH 4/7] Use classic dep variant for std/thiserror feature --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3735885..60f1ed1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,8 @@ default = ["std"] # Enables all features, including support of simplified Cocoon API, using `rand::thread_rng`, # and API related to `std::io`: wrap to writer, unwrap from reader. -std = ["alloc", "rand/std", "dep:thiserror"] +std = ["alloc", "rand/std", "thiserror"] +thiserror = ["std"] # Enables `Vec` container. Can be used without `std` crate (in "no std" build). alloc = ["chacha20poly1305/alloc"] From c99117ae92f2254e21540e753fc0cc935a29a258 Mon Sep 17 00:00:00 2001 From: madomado Date: Fri, 24 May 2024 22:37:41 +0800 Subject: [PATCH 5/7] Fix build and std/thiserror feature --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 60f1ed1..382c66a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,6 @@ default = ["std"] # Enables all features, including support of simplified Cocoon API, using `rand::thread_rng`, # and API related to `std::io`: wrap to writer, unwrap from reader. std = ["alloc", "rand/std", "thiserror"] -thiserror = ["std"] # Enables `Vec` container. Can be used without `std` crate (in "no std" build). alloc = ["chacha20poly1305/alloc"] From 160f83cec1c30d1f03f2d4084d6cc7d9a97e0a21 Mon Sep 17 00:00:00 2001 From: Alexander Fadeev Date: Sun, 26 May 2024 20:24:42 +0300 Subject: [PATCH 6/7] Make `thiserror` an optional feature --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 382c66a..f45e2ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ default = ["std"] # Enables all features, including support of simplified Cocoon API, using `rand::thread_rng`, # and API related to `std::io`: wrap to writer, unwrap from reader. -std = ["alloc", "rand/std", "thiserror"] +std = ["alloc", "rand/std"] # Enables `Vec` container. Can be used without `std` crate (in "no std" build). alloc = ["chacha20poly1305/alloc"] From ebf484fb60d5a9bbb515eddfee86912bd5031296 Mon Sep 17 00:00:00 2001 From: Alexander Fadeev Date: Sun, 26 May 2024 20:43:29 +0300 Subject: [PATCH 7/7] Make `thiserror` derives optional --- src/error.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index 3a5f5bc..c7102db 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,26 +1,39 @@ /// Error variants produced by the Cocoon API. #[derive(Debug)] -#[cfg_attr(feature = "std", derive(thiserror::Error))] +#[cfg_attr(feature = "thiserror", derive(thiserror::Error))] pub enum Error { /// I/o error during read/write operation (`Cocoon::dump`, `Cocoon::parse`). #[cfg(feature = "std")] - #[error("Input/output error")] + #[cfg_attr(feature = "thiserror", error("Input/output error"))] Io(std::io::Error), + /// Format is not recognized. Probably corrupted. - #[cfg_attr(feature = "std", error("Unrecognized format"))] + #[cfg_attr(feature = "thiserror", error("Unrecognized format"))] UnrecognizedFormat, + /// Cryptographic error. There could be a few reasons: /// 1. Integrity is compromised. /// 2. Password is invalid. - #[cfg_attr(feature = "std", error("Cryptographic error: bad integrity/password"))] + #[cfg_attr( + feature = "thiserror", + error("Cryptographic error: bad integrity/password") + )] Cryptography, + /// Container is too large to get processed on the current architecture. /// E.g. it's not possible to process a container larger than 4 GB on 32-bit architecture. - #[cfg_attr(feature = "std", error("Container size exceeds architectural limit"))] + #[cfg_attr( + feature = "thiserror", + error("Container size exceeds architectural limit") + )] TooLarge, + /// Buffer is too short and barely holds all data to decrypt, inconsistent length /// encoded to the header. - #[cfg_attr(feature = "std", error("Insufficient buffer size for decrypted data"))] + #[cfg_attr( + feature = "thiserror", + error("Insufficient buffer size for decrypted data") + )] TooShort, }