Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-avx2 platforms #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 16 additions & 3 deletions src/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -75,7 +77,10 @@ pub fn pem_to_der(pem: &str, guard: Option<&PemGuard>) -> Option<Vec<u8>> {
.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`
Expand All @@ -86,8 +91,16 @@ pub fn der_to_pem<T: ?Sized + AsRef<[u8]>>(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');
Expand Down