From 5d95b6d614a4b8e75957d4d9a6c72c1d9fcee871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=95=B8=E5=AE=87?= <68480064+saltymango2619@users.noreply.github.com> Date: Sat, 24 Dec 2022 18:38:13 -0700 Subject: [PATCH 1/2] Add support for non-avx2 platforms Use conditional compilation based on the target_feature to use the base64 crate instead of b64-ct. --- Cargo.toml | 5 +++++ src/lib.rs | 3 +++ src/pem.rs | 21 ++++++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1960dad..189a5b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,12 @@ num-integer = { version = "0.1", default-features = false } bit-vec = "0.6" lazy_static = "1" chrono = "0.4.23" + +[target.'cfg(target_feature = "avx2")'.dependencies] b64-ct = "0.1.1" +[target.'cfg(not(target_feature = "avx2"))'.dependencies] +base64 = "0.20.0" + [dev-dependencies] rand = "0.3" diff --git a/src/lib.rs b/src/lib.rs index fd58491..89704c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,10 @@ pub extern crate yasna; pub extern crate num_bigint; +#[cfg(target_feature = "avx2")] extern crate b64_ct; +#[cfg(not(target_feature = "avx2"))] +extern crate base64; extern crate num_integer; pub extern crate bit_vec; #[macro_use] diff --git a/src/pem.rs b/src/pem.rs index e5bfc10..7533dff 100644 --- a/src/pem.rs +++ b/src/pem.rs @@ -4,6 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#[cfg(target_feature = "avx2")] use b64_ct::{FromBase64, ToBase64}; /// Type of the various `PEM_*` constants supplied to `pem_to_der` / `der_to_pem`. @@ -32,6 +33,7 @@ pub const PEM_CRL: &'static PemGuard = pem_guard!("X509 CRL"); const BASE64_PEM_WRAP: usize = 64; +#[cfg(target_feature = "avx2")] lazy_static!{ static ref BASE64_PEM: b64_ct::Config = b64_ct::Config { char_set: b64_ct::CharacterSet::Standard, @@ -40,6 +42,8 @@ lazy_static!{ line_length: Some(BASE64_PEM_WRAP), }; } +//#[cfg(not(target_feature = "avx2"))] + /// Convert PEM to DER. If `guard` is specified (e.g. as PEM_CERTIFICATE), then the guardlines are /// verified to match the expected string. Otherwise, the guardlines are verified to generally have @@ -75,7 +79,10 @@ pub fn pem_to_der(pem: &str, guard: Option<&PemGuard>) -> Option> { .next().unwrap().0; let body_end = pem.rmatch_indices(&end).next().unwrap().0; - pem[body_start..body_end].from_base64().ok() + #[cfg(target_feature = "avx2")] + return pem[body_start..body_end].from_base64().ok(); + #[cfg(not(target_feature = "avx2"))] + return base64::decode(pem[body_start..body_end].replace("\n", "").as_bytes()).ok(); } /// Convert DER to PEM. The guardlines use the identifying string chosen by `guard` @@ -86,8 +93,16 @@ pub fn der_to_pem>(der: &T, guard: &PemGuard) -> String pem.push_str(guard.begin); pem.push('\n'); if der.as_ref().len() > 0 { - pem.push_str(&der.as_ref().to_base64(*BASE64_PEM)); - pem.push('\n'); + #[cfg(target_feature = "avx2")] + { + pem.push_str(&der.as_ref().to_base64(*BASE64_PEM)); + pem.push('\n'); + } + #[cfg(not(target_feature = "avx2"))] + for chunk in base64::encode(der.as_ref()).as_bytes().chunks(BASE64_PEM_WRAP) { + pem.push_str(std::str::from_utf8(chunk).unwrap()); + pem.push('\n'); + } } pem.push_str(guard.end); pem.push('\n'); From d14028a57aba5643dc0e843ff3c3a161889c83e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=95=B8=E5=AE=87?= <68480064+saltymango2619@users.noreply.github.com> Date: Sun, 25 Dec 2022 01:53:36 -0700 Subject: [PATCH 2/2] Update pem.rs --- src/pem.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pem.rs b/src/pem.rs index 7533dff..9e24eb4 100644 --- a/src/pem.rs +++ b/src/pem.rs @@ -42,8 +42,6 @@ lazy_static!{ line_length: Some(BASE64_PEM_WRAP), }; } -//#[cfg(not(target_feature = "avx2"))] - /// Convert PEM to DER. If `guard` is specified (e.g. as PEM_CERTIFICATE), then the guardlines are /// verified to match the expected string. Otherwise, the guardlines are verified to generally have