Add hmac functions
This commit is contained in:
476
Cargo.lock
generated
476
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -77,6 +77,8 @@ blake2 = "0.8.0"
|
||||
md-5 = "0.8.0"
|
||||
sha-1 = "0.8.1"
|
||||
sha2 = "0.8.0"
|
||||
sha3 = "0.8.0"
|
||||
hmac = "0.7"
|
||||
|
||||
image = "0.21"
|
||||
kamadak-exif = "0.3.1"
|
||||
@@ -87,7 +89,7 @@ nude = "0.1.0"
|
||||
caps = "0.3"
|
||||
#syscallz = { path="../syscallz-rs" }
|
||||
syscallz = "0.11"
|
||||
nix = "0.13"
|
||||
nix = "0.14"
|
||||
|
||||
[target.'cfg(target_os="openbsd")'.dependencies]
|
||||
pledge = "0.3.1"
|
||||
|
||||
11
README.md
11
README.md
@@ -113,6 +113,7 @@ For everything else please have a look at the [detailed list][1].
|
||||
- [Limitations](https://sn0int.readthedocs.io/en/latest/sandbox.html#limitations)
|
||||
- [Diagnosing a sandbox failure](https://sn0int.readthedocs.io/en/latest/sandbox.html#diagnosing-a-sandbox-failure)
|
||||
- [Function reference](https://sn0int.readthedocs.io/en/latest/reference.html)
|
||||
- [asn_lookup](https://sn0int.readthedocs.io/en/latest/reference.html#asn-lookup)
|
||||
- [clear_err](https://sn0int.readthedocs.io/en/latest/reference.html#clear-err)
|
||||
- [create_blob](https://sn0int.readthedocs.io/en/latest/reference.html#create-blob)
|
||||
- [datetime](https://sn0int.readthedocs.io/en/latest/reference.html#datetime)
|
||||
@@ -122,8 +123,14 @@ For everything else please have a look at the [detailed list][1].
|
||||
- [db_update](https://sn0int.readthedocs.io/en/latest/reference.html#db-update)
|
||||
- [dns](https://sn0int.readthedocs.io/en/latest/reference.html#dns)
|
||||
- [error](https://sn0int.readthedocs.io/en/latest/reference.html#error)
|
||||
- [asn_lookup](https://sn0int.readthedocs.io/en/latest/reference.html#asn-lookup)
|
||||
- [geoip_lookup](https://sn0int.readthedocs.io/en/latest/reference.html#geoip-lookup)
|
||||
- [hex](https://sn0int.readthedocs.io/en/latest/reference.html#hex)
|
||||
- [hmac_md5](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-md5)
|
||||
- [hmac_sha1](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-sha1)
|
||||
- [hmac_sha2_256](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-sha2-256)
|
||||
- [hmac_sha2_512](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-sha2-512)
|
||||
- [hmac_sha3_256](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-sha3-256)
|
||||
- [hmac_sha3_512](https://sn0int.readthedocs.io/en/latest/reference.html#hmac-sha3-512)
|
||||
- [html_select](https://sn0int.readthedocs.io/en/latest/reference.html#html-select)
|
||||
- [html_select_list](https://sn0int.readthedocs.io/en/latest/reference.html#html-select-list)
|
||||
- [http_mksession](https://sn0int.readthedocs.io/en/latest/reference.html#http-mksession)
|
||||
@@ -148,6 +155,8 @@ For everything else please have a look at the [detailed list][1].
|
||||
- [sha1](https://sn0int.readthedocs.io/en/latest/reference.html#sha1)
|
||||
- [sha2_256](https://sn0int.readthedocs.io/en/latest/reference.html#sha2-256)
|
||||
- [sha2_512](https://sn0int.readthedocs.io/en/latest/reference.html#sha2-512)
|
||||
- [sha3_256](https://sn0int.readthedocs.io/en/latest/reference.html#sha3-256)
|
||||
- [sha3_512](https://sn0int.readthedocs.io/en/latest/reference.html#sha3-512)
|
||||
- [sleep](https://sn0int.readthedocs.io/en/latest/reference.html#sleep)
|
||||
- [sock_connect](https://sn0int.readthedocs.io/en/latest/reference.html#sock-connect)
|
||||
- [sock_send](https://sn0int.readthedocs.io/en/latest/reference.html#sock-send)
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
Function reference
|
||||
==================
|
||||
|
||||
asn_lookup
|
||||
----------
|
||||
|
||||
Run an ASN lookup for a given ip address. The function returns ``asn`` and
|
||||
``as_org``. This function may fail.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
lookup = asn_lookup('1.1.1.1')
|
||||
if last_err() then return end
|
||||
|
||||
clear_err
|
||||
---------
|
||||
|
||||
@@ -135,17 +146,6 @@ Log an error to the terminal.
|
||||
|
||||
error('ohai')
|
||||
|
||||
asn_lookup
|
||||
----------
|
||||
|
||||
Run an ASN lookup for a given ip address. The function returns ``asn`` and
|
||||
``as_org``. This function may fail.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
lookup = asn_lookup('1.1.1.1')
|
||||
if last_err() then return end
|
||||
|
||||
geoip_lookup
|
||||
------------
|
||||
|
||||
@@ -166,6 +166,69 @@ This function may fail.
|
||||
lookup = geoip_lookup('1.1.1.1')
|
||||
if last_err() then return end
|
||||
|
||||
hex
|
||||
---
|
||||
|
||||
Hex encode a list of bytes.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hex("\x6F\x68\x61\x69\x0A\x00")
|
||||
|
||||
hmac_md5
|
||||
--------
|
||||
|
||||
Calculate an hmac with md5. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_md5("secret", "my authenticated message")
|
||||
|
||||
hmac_sha1
|
||||
---------
|
||||
|
||||
Calculate an hmac with sha1. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_sha1("secret", "my authenticated message")
|
||||
|
||||
hmac_sha2_256
|
||||
-------------
|
||||
|
||||
Calculate an hmac with sha2_256. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_sha2_256("secret", "my authenticated message")
|
||||
|
||||
hmac_sha2_512
|
||||
-------------
|
||||
|
||||
Calculate an hmac with sha2_512. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_sha2_512("secret", "my authenticated message")
|
||||
|
||||
hmac_sha3_256
|
||||
-------------
|
||||
|
||||
Calculate an hmac with sha3_256. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_sha3_256("secret", "my authenticated message")
|
||||
|
||||
hmac_sha3_512
|
||||
-------------
|
||||
|
||||
Calculate an hmac with sha3_512. Returns a binary array.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hmac_sha3_512("secret", "my authenticated message")
|
||||
|
||||
html_select
|
||||
-----------
|
||||
|
||||
@@ -519,6 +582,24 @@ Hash a byte array with sha2_512 and return the results as bytes.
|
||||
|
||||
hex(sha2_512("\x00\xff"))
|
||||
|
||||
sha3_256
|
||||
--------
|
||||
|
||||
Hash a byte array with sha3_256 and return the results as bytes.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hex(sha3_256("\x00\xff"))
|
||||
|
||||
sha3_512
|
||||
--------
|
||||
|
||||
Hash a byte array with sha3_512 and return the results as bytes.
|
||||
|
||||
.. code-block:: lua
|
||||
|
||||
hex(sha3_512("\x00\xff"))
|
||||
|
||||
sleep
|
||||
-----
|
||||
|
||||
|
||||
@@ -334,6 +334,12 @@ fn ctx<'a>(env: Environment, logger: Arc<Mutex<Box<Reporter>>>) -> (hlua::Lua<'a
|
||||
runtime::geoip_lookup(&mut lua, state.clone());
|
||||
runtime::getopt(&mut lua, state.clone());
|
||||
runtime::hex(&mut lua, state.clone());
|
||||
runtime::hmac_md5(&mut lua, state.clone());
|
||||
runtime::hmac_sha1(&mut lua, state.clone());
|
||||
runtime::hmac_sha2_256(&mut lua, state.clone());
|
||||
runtime::hmac_sha2_512(&mut lua, state.clone());
|
||||
runtime::hmac_sha3_256(&mut lua, state.clone());
|
||||
runtime::hmac_sha3_512(&mut lua, state.clone());
|
||||
runtime::html_select(&mut lua, state.clone());
|
||||
runtime::html_select_list(&mut lua, state.clone());
|
||||
runtime::http_mksession(&mut lua, state.clone());
|
||||
@@ -359,6 +365,8 @@ fn ctx<'a>(env: Environment, logger: Arc<Mutex<Box<Reporter>>>) -> (hlua::Lua<'a
|
||||
runtime::sha1(&mut lua, state.clone());
|
||||
runtime::sha2_256(&mut lua, state.clone());
|
||||
runtime::sha2_512(&mut lua, state.clone());
|
||||
runtime::sha3_256(&mut lua, state.clone());
|
||||
runtime::sha3_512(&mut lua, state.clone());
|
||||
runtime::sleep(&mut lua, state.clone());
|
||||
runtime::sn0int_version(&mut lua, state.clone());
|
||||
runtime::sock_connect(&mut lua, state.clone());
|
||||
|
||||
@@ -2,10 +2,9 @@ use crate::errors::*;
|
||||
use crate::engine::ctx::State;
|
||||
use crate::engine::structs::{byte_array, lua_bytes};
|
||||
use crate::hlua::{self, AnyLuaValue};
|
||||
use digest::Digest;
|
||||
use md5::Md5;
|
||||
use sha1::Sha1;
|
||||
use sha2::{Sha256, Sha512};
|
||||
use digest::{Digest, Input, BlockInput, FixedOutput, Reset};
|
||||
use digest::generic_array::ArrayLength;
|
||||
use hmac::{Hmac, Mac};
|
||||
use std::sync::Arc;
|
||||
|
||||
|
||||
@@ -13,7 +12,7 @@ pub fn md5(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("md5", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&Md5::digest(&bytes)))
|
||||
.map(|bytes| lua_bytes(&md5::Md5::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -21,7 +20,7 @@ pub fn sha1(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("sha1", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&Sha1::digest(&bytes)))
|
||||
.map(|bytes| lua_bytes(&sha1::Sha1::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -29,7 +28,7 @@ pub fn sha2_256(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("sha2_256", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&Sha256::digest(&bytes)))
|
||||
.map(|bytes| lua_bytes(&sha2::Sha256::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -37,7 +36,83 @@ pub fn sha2_512(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("sha2_512", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&Sha512::digest(&bytes)))
|
||||
.map(|bytes| lua_bytes(&sha2::Sha512::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn sha3_256(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("sha3_256", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&sha3::Sha3_256::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn sha3_512(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("sha3_512", hlua::function1(move |bytes: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
byte_array(bytes)
|
||||
.map_err(|err| state.set_error(err))
|
||||
.map(|bytes| lua_bytes(&sha3::Sha3_512::digest(&bytes)))
|
||||
}))
|
||||
}
|
||||
|
||||
fn hmac<D>(secret: AnyLuaValue, msg: AnyLuaValue) -> Result<AnyLuaValue>
|
||||
where
|
||||
D: Input + BlockInput + FixedOutput + Reset + Default + Clone,
|
||||
D::BlockSize: ArrayLength<u8> + Clone,
|
||||
D::OutputSize: ArrayLength<u8>,
|
||||
{
|
||||
let secret = byte_array(secret)?;
|
||||
let msg = byte_array(msg)?;
|
||||
|
||||
let mut mac = match Hmac::<D>::new_varkey(&secret) {
|
||||
Ok(mac) => mac,
|
||||
Err(_) => bail!("Invalid key length"),
|
||||
};
|
||||
mac.input(&msg);
|
||||
let result = mac.result();
|
||||
Ok(lua_bytes(&result.code()))
|
||||
}
|
||||
|
||||
pub fn hmac_md5(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_md5", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<md5::Md5>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn hmac_sha1(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_sha1", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<sha1::Sha1>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn hmac_sha2_256(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_sha2_256", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<sha2::Sha256>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn hmac_sha2_512(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_sha2_512", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<sha2::Sha512>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn hmac_sha3_256(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_sha3_256", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<sha3::Sha3_256>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn hmac_sha3_512(lua: &mut hlua::Lua, state: Arc<State>) {
|
||||
lua.set("hmac_sha3_512", hlua::function2(move |secret: AnyLuaValue, msg: AnyLuaValue| -> Result<AnyLuaValue> {
|
||||
hmac::<sha3::Sha3_512>(secret, msg)
|
||||
.map_err(|err| state.set_error(err))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -100,4 +175,116 @@ mod tests {
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_sha3_256() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(sha3_256("abcdef"))
|
||||
print(h)
|
||||
if h ~= '59890c1d183aa279505750422e6384ccb1499c793872d6f31bb3bcaa4bc9f5a5' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_sha3_512() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(sha3_512("abcdef"))
|
||||
print(h)
|
||||
if h ~= '01309a45c57cd7faef9ee6bb95fed29e5e2e0312af12a95fffeee340e5e5948b4652d26ae4b75976a53cc1612141af6e24df36517a61f46a1a05f59cf667046a' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_md5() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_md5("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= '0c7a250281315ab863549f66cd8a3a53' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_sha1() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_sha1("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= '46b4ec586117154dacd49d664e5d63fdc88efb51' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_sha2_256() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_sha2_256("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= 'f9320baf0249169e73850cd6156ded0106e2bb6ad8cab01b7bbbebe6d1065317' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_sha2_512() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_sha2_512("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= '114682914c5d017dfe59fdc804118b56a3a652a0b8870759cf9e792ed7426b08197076bf7d01640b1b0684df79e4b67e37485669e8ce98dbab60445f0db94fce' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_sha3_256() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_sha3_256("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= 'a7dc3fbbd45078239f0cb321e6902375d22b505f2c48722eb7009e7da2574893' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_sha3_512() {
|
||||
let script = Script::load_unchecked(r#"
|
||||
function run()
|
||||
h = hex(hmac_sha3_512("foo", "bar"))
|
||||
print(h)
|
||||
if h ~= '2da91b8227d106199fd06c5d8a6752796cf3c84dde5a427bd2aca384f0cffc19997e2584ed15c55542c2cb8918b987e2bcd9e77a9f3fdbb4dbea8a3d0136da2f' then
|
||||
return 'incorrect hash'
|
||||
end
|
||||
end
|
||||
"#).expect("Failed to load script");
|
||||
script.test().expect("Script failed");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user