-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add libsodium support for decryption
- Loading branch information
Showing
14 changed files
with
227 additions
and
75 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,51 @@ | ||
use crate::errors::*; | ||
use sodiumoxide::crypto::secretbox::{self, Key, Nonce}; | ||
use std::iter; | ||
|
||
pub fn key_trunc_pad(mut key: &[u8], len: usize, pad: u8) -> Vec<u8> { | ||
if key.len() > len { | ||
key = &key[..len]; | ||
} | ||
|
||
let mut key = key.to_vec(); | ||
key.extend(iter::repeat(pad).take(len - key.len())); | ||
key | ||
} | ||
|
||
pub fn sodium_secretbox_open(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> { | ||
if encrypted.len() <= secretbox::NONCEBYTES { | ||
bail!("Encrypted message is too short"); | ||
} | ||
|
||
let key = Key::from_slice(key) | ||
.ok_or_else(|| format_err!("Key has wrong length"))?; | ||
let nonce = Nonce::from_slice(&encrypted[..secretbox::NONCEBYTES]) | ||
.ok_or_else(|| format_err!("Nonce has wrong length"))?; | ||
let ciphertext = &encrypted[secretbox::NONCEBYTES..]; | ||
let plain = secretbox::open(&ciphertext, &nonce, &key) | ||
.map_err(|_| format_err!("Failed to decrypt secretbox"))?; | ||
Ok(plain) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_key_equal() { | ||
let key = key_trunc_pad(&[1, 2, 3, 4, 5], 5, 0); | ||
assert_eq!(key, &[1, 2, 3, 4, 5]); | ||
} | ||
|
||
#[test] | ||
fn test_key_trunc() { | ||
let key = key_trunc_pad(&[1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 0); | ||
assert_eq!(key, &[1, 2, 3, 4, 5]); | ||
} | ||
|
||
#[test] | ||
fn test_key_pad() { | ||
let key = key_trunc_pad(&[1, 2, 3], 5, 0); | ||
assert_eq!(key, &[1, 2, 3, 0, 0]); | ||
} | ||
} |
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,30 @@ | ||
use crate::errors::*; | ||
|
||
use crate::engine::ctx::State; | ||
use crate::engine::structs::{byte_array, lua_bytes}; | ||
use crate::hlua::{self, AnyLuaValue}; | ||
use sn0int_std::crypto; | ||
use std::sync::Arc; | ||
|
||
pub fn key_trunc_pad(lua: &mut hlua::Lua, state: Arc<dyn State>) { | ||
lua.set("key_trunc_pad", hlua::function3(move |bytes: AnyLuaValue, len: u32, pad: u8| -> Result<AnyLuaValue> { | ||
let bytes = byte_array(bytes) | ||
.map_err(|err| state.set_error(err))?; | ||
let bytes = crypto::key_trunc_pad(&bytes, len as usize, pad); | ||
Ok(lua_bytes(&bytes)) | ||
})) | ||
} | ||
|
||
pub fn sodium_secretbox_open(lua: &mut hlua::Lua, state: Arc<dyn State>) { | ||
lua.set("sodium_secretbox_open", hlua::function2(move |encrypted: AnyLuaValue, key: AnyLuaValue| -> Result<AnyLuaValue> { | ||
let encrypted = byte_array(encrypted) | ||
.map_err(|err| state.set_error(err))?; | ||
let key = byte_array(key) | ||
.map_err(|err| state.set_error(err))?; | ||
|
||
let plain = crypto::sodium_secretbox_open(&encrypted, &key) | ||
.map_err(|err| state.set_error(err))?; | ||
|
||
Ok(lua_bytes(&plain)) | ||
})) | ||
} |
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