Skip to content

Commit

Permalink
Add struct for smb functions
Browse files Browse the repository at this point in the history
  • Loading branch information
puethenn committed Nov 26, 2024
1 parent 944d8a9 commit a7edc93
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rust/src/nasl/builtin/cryptographic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod hash;
pub mod hmac;
pub mod rc4;
pub mod rsa;
pub mod smb;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -123,6 +124,7 @@ impl IntoFunctionSet for Cryptographic {
set.add_set(des::Des);
set.add_set(rsa::Rsa);
set.add_set(bf_cbc::BfCbc);
set.add_set(smb::SMB);
set
}
}
45 changes: 45 additions & 0 deletions rust/src/nasl/builtin/cryptographic/smb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2024 Greenbone AG
//

// SPDX-License-Identifier: GPL-2.0-or-later
use crate::function_set;
use crate::nasl::FunctionErrorKind;
use crate::nasl::NaslValue;
use aes::Aes128;
use aes_gcm::aead::{Aead, KeyInit};
use aes_gcm::{Aes128Gcm, Nonce};
use cmac::Cmac;
use digest::Update;
use nasl_function_proc_macro::nasl_function;

#[nasl_function(named(key, buf))]
fn smb_cmac_aes_signature(key: &str, buf: &str) -> Result<NaslValue, FunctionErrorKind> {
let key_bytes = key.as_bytes();
let buf_bytes = buf.as_bytes();
let mut cmac = Cmac::<Aes128>::new_from_slice(&key_bytes)
.map_err(|e| FunctionErrorKind::Diagnostic(e.to_string(), None))?;
cmac.update(buf_bytes);
let finish = cmac::Mac::finalize(cmac).into_bytes();
Ok(finish.to_vec().into())
}

#[nasl_function(named(key, buf, iv))]
fn smb_gmac_aes_signature(key: &str, buf: &str, iv: &str) -> Result<NaslValue, FunctionErrorKind> {
let key_bytes = key.as_bytes();
let buf_bytes = buf.as_bytes();
let iv_bytes = iv.as_bytes();
let gmac = Aes128Gcm::new_from_slice(&key_bytes).unwrap();
let nonce = Nonce::from_slice(&iv_bytes);
let auth = gmac.encrypt(nonce, buf_bytes.as_ref()).unwrap();
Ok(auth.into())
}

pub struct SMB;
function_set! {
SMB,
sync_stateless,
(
(smb_gmac_aes_signature, "smb_gmac_aes_signature"),
(smb_cmac_aes_signature, "smb_cmac_aes_signature"),
)
}
1 change: 1 addition & 0 deletions rust/src/nasl/builtin/cryptographic/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ mod helper;
mod hmac;
mod rc4;
mod rsa;
mod smb;
16 changes: 16 additions & 0 deletions rust/src/nasl/builtin/cryptographic/tests/smb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2024 Greenbone AG
//
// SPDX-License-Identifier: GPL-2.0-or-later

#[cfg(test)]
mod tests {

use crate::nasl::builtin::cryptographic::tests::helper::decode_hex;
use crate::nasl::test_prelude::*;
use crate::nasl::test_utils::TestBuilder;

#[test]
fn smb_cmac_aes_signature() {
let mut t = TestBuilder::default();
}
}

0 comments on commit a7edc93

Please sign in to comment.