Skip to content

Commit

Permalink
Support no_std
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
randombit committed Jan 31, 2019
1 parent 391a040 commit 69d26eb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions botan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ status = "actively-developed"
botan-sys = { version = "0.5.0", path = "../botan-sys" }
# we don't need std support
libc = { version = "0.2", default-features = false }
cstr_core = { version = "0.1", optional = true }

[features]
default = []
no-std = ["cstr_core/alloc"]
10 changes: 10 additions & 0 deletions botan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

//! A wrapper for the Botan cryptography library

#![cfg_attr(feature = "no-std", feature(alloc))]
#![cfg_attr(feature = "no-std", no_std)]

#[cfg(feature = "no-std")]
#[macro_use]
extern crate alloc;

#[cfg(feature = "no-std")]
extern crate cstr_core;

extern crate botan_sys;
extern crate libc;

Expand Down
14 changes: 7 additions & 7 deletions botan/src/mp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::utils::*;

use crate::rng::RandomNumberGenerator;

use std::cmp::{Eq, Ord, Ordering};
use std::fmt;
use std::str::FromStr;
use core::cmp::{Eq, Ord, Ordering};
use core::fmt;
use core::str::FromStr;

use std::ops::{Add, AddAssign,
use core::ops::{Add, AddAssign,
Sub, SubAssign,
Mul, MulAssign,
Div, DivAssign,
Expand Down Expand Up @@ -137,7 +137,7 @@ impl MPI {
/// Return value of self as decimal string
pub fn to_string(&self) -> Result<String> {
let bit_count = self.bit_count()? as f64;
let log_base = (10f64).log2();
let log_base = 3.3219; // log2(10)
let bn_digits = 1 + (bit_count / log_base) as usize;

call_botan_ffi_returning_string(bn_digits, &|out_buf, out_len| {
Expand Down Expand Up @@ -369,7 +369,7 @@ impl MPI {
/// # Examples
///
/// ```
/// use std::str::FromStr;
/// use core::str::FromStr;
/// let n = botan::MPI::from_str("1111111111111111111").unwrap();
/// let rng = botan::RandomNumberGenerator::new_system().unwrap();
/// assert!(n.is_prime(&rng, 128).unwrap());
Expand All @@ -387,7 +387,7 @@ impl MPI {
/// # Examples
///
/// ```
/// use std::str::FromStr;
/// use core::str::FromStr;
/// let x = botan::MPI::from_str("1111111111111111").unwrap();
/// let y = botan::MPI::from_str("111111111111").unwrap();
/// assert_eq!(botan::MPI::gcd(&x, &y).unwrap(), botan::MPI::from_str("1111").unwrap());
Expand Down
1 change: 0 additions & 1 deletion botan/src/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl HOTP {
/// Check an HOTP code
pub fn check(&self, code: u32, counter: u64) -> Result<bool> {
let cmp_code = self.generate(counter)?;
println!("{} {}", code, cmp_code);
Ok(cmp_code == code)
}

Expand Down
16 changes: 12 additions & 4 deletions botan/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
use botan_sys::*;

pub(crate) use libc::{c_char, c_int, c_void};
#[cfg(feature = "no-std")]
pub(crate) use alloc::prelude::*;

#[cfg(feature = "no-std")]
pub(crate) use cstr_core::{CStr, CString};

#[cfg(not(feature = "no-std"))]
pub(crate) use std::ffi::{CStr, CString};
pub(crate) use std::ptr;
pub(crate) use std::mem;

pub(crate) use libc::{c_char, c_int, c_void};
pub(crate) use core::ptr;
pub(crate) use core::mem;

/// The result of calling an operation on the library
pub type Result<T> = ::std::result::Result<T, Error>;
pub type Result<T> = ::core::result::Result<T, Error>;

pub(crate) fn make_cstr(input: &str) -> Result<CString> {
let cstr = CString::new(input).map_err(|_| Error::ConversionError)?;
Expand Down

0 comments on commit 69d26eb

Please sign in to comment.