-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
774 additions
and
1,384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::sec_string::SecString; | ||
|
||
/// A memory-secure wrapper for shipping around Bytes | ||
#[derive(Clone)] | ||
pub struct SecBuffer { | ||
inner: Vec<u8> | ||
} | ||
|
||
impl SecBuffer { | ||
/// Creates a new SecBytes container | ||
pub fn new() -> Self { | ||
Self::from(Vec::new()) | ||
} | ||
|
||
/// Returns the inner element without dropping the memory | ||
pub fn into_buffer(mut self) -> Vec<u8> { | ||
self.unlock(); | ||
std::mem::take(&mut self.inner) | ||
} | ||
|
||
/// returns the length of the buffer | ||
pub fn len(&self) -> usize { | ||
self.inner.len() | ||
} | ||
|
||
fn lock(&self) { | ||
unsafe { crate::misc::mlock(self.inner.as_ptr(), self.inner.len()) } | ||
} | ||
|
||
fn unlock(&self) { | ||
unsafe { crate::misc::munlock(self.inner.as_ptr(), self.inner.len()) } | ||
} | ||
|
||
fn zeroize(&mut self) { | ||
unsafe { crate::misc::zeroize(self.inner.as_ptr(), self.inner.len()) } | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for SecBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
self.inner.as_ref() | ||
} | ||
} | ||
|
||
impl<T: Into<Vec<u8>>> From<T> for SecBuffer { | ||
fn from(inner: T) -> Self { | ||
let this = Self { inner: inner.into() }; | ||
this.lock(); | ||
this | ||
} | ||
} | ||
|
||
impl From<SecString> for SecBuffer { | ||
fn from(inner: SecString) -> Self { | ||
Self::from(inner.into_buffer().into_bytes()) | ||
} | ||
} | ||
|
||
impl Drop for SecBuffer { | ||
fn drop(&mut self) { | ||
self.unlock(); | ||
self.zeroize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::ops::Deref; | ||
use std::fmt::{Debug, Display}; | ||
use serde::export::Formatter; | ||
|
||
/// Allows mutable access | ||
pub struct SecString { | ||
inner: String | ||
} | ||
|
||
impl SecString { | ||
/// Creates a new instance SecString | ||
pub fn new() -> Self { | ||
Self::from(String::new()) | ||
} | ||
|
||
/// Safely pushes a new character | ||
pub fn push(&mut self, val: char) { | ||
self.unlock(); | ||
self.inner.push(val); | ||
self.lock(); | ||
} | ||
|
||
/// Clears and zeroizes the vector. Keeps the allocation in-tact | ||
pub fn clear(&mut self) { | ||
self.unlock(); | ||
self.zeroize(); | ||
self.inner.clear(); | ||
self.lock(); | ||
} | ||
|
||
/// Inserts a char at `pos` | ||
pub fn insert(&mut self, pos: usize, val: char) { | ||
self.unlock(); | ||
self.inner.insert(pos, val); | ||
self.lock(); | ||
} | ||
|
||
/// removes a char at `pos` | ||
pub fn remove(&mut self, pos: usize) -> char { | ||
self.unlock(); | ||
let val = self.inner.remove(pos); | ||
self.lock(); | ||
val | ||
} | ||
|
||
/// Gets the inner string | ||
pub fn into_buffer(mut self) -> String { | ||
self.unlock(); | ||
std::mem::take(&mut self.inner) | ||
} | ||
|
||
fn lock(&self) { | ||
let (ptr, len) = decompose(&self.inner); | ||
unsafe { crate::misc::mlock(ptr, len) } | ||
} | ||
|
||
fn unlock(&self) { | ||
let (ptr, len) = decompose(&self.inner); | ||
unsafe { crate::misc::munlock(ptr, len) } | ||
} | ||
|
||
fn zeroize(&mut self) { | ||
unsafe { crate::misc::zeroize(self.inner.as_ptr(), self.inner.len()) } | ||
} | ||
} | ||
|
||
impl From<String> for SecString { | ||
fn from(inner: String) -> Self { | ||
let this = Self { inner }; | ||
this.lock(); | ||
this | ||
} | ||
} | ||
|
||
impl Drop for SecString { | ||
fn drop(&mut self) { | ||
self.unlock(); | ||
self.zeroize(); | ||
} | ||
} | ||
|
||
impl Clone for SecString { | ||
fn clone(&self) -> Self { | ||
Self::from(self.inner.clone()) | ||
} | ||
} | ||
|
||
impl Default for SecString { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Deref for SecString { | ||
type Target = String; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl Debug for SecString { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "***SECRET***") | ||
} | ||
} | ||
|
||
impl Display for SecString { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
Debug::fmt(self, f) | ||
} | ||
} | ||
|
||
fn decompose(input: &String) -> (*const u8, usize) { | ||
let ptr = input.as_ptr(); | ||
let len = input.capacity(); | ||
(ptr, len) | ||
} |
Oops, something went wrong.