Skip to content

Commit

Permalink
Move all function_set calls to new macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 26, 2024
1 parent 418f2a5 commit 271150a
Show file tree
Hide file tree
Showing 33 changed files with 223 additions and 306 deletions.
62 changes: 10 additions & 52 deletions rust/crates/nasl-function-proc-macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ impl<'a> ArgsStruct<'a> {
})
}

fn has_register_arg(&self) -> bool {
self.args
.iter()
.any(|arg| matches!(arg.kind, ArgKind::Register))
}

fn get_args(&self) -> TokenStream {
self
.args.iter().map(|arg| {
Expand Down Expand Up @@ -144,6 +150,9 @@ impl<'a> ArgsStruct<'a> {
}

fn gen_checks(&self) -> TokenStream {
if self.has_register_arg() {
return quote! {};
}
let named_array = self.make_array_of_names(ArgKind::get_named_arg_name);
let maybe_named_array = self.make_array_of_names(ArgKind::get_maybe_named_arg_name);
let num_allowed_positional_args = if self.has_positional_iterator_arg() {
Expand All @@ -158,57 +167,6 @@ impl<'a> ArgsStruct<'a> {
}
}

pub fn impl_nasl_function_args(&self) -> TokenStream {
let ItemFn {
attrs,
vis,
sig,
block,
} = self.function;
let stmts = &block.stmts;
let get_args = self.get_args();
let fn_args = &sig.inputs;
let Signature {
fn_token,
ident,
generics,
output,
..
} = sig;
let self_arg = match self.receiver_type {
ReceiverType::None => quote! {},
ReceiverType::RefSelf => quote! {&self,},
ReceiverType::RefMutSelf => quote! {&mut self,},
};
let inputs = quote! {
#self_arg
_register: &crate::nasl::Register,
_context: &crate::nasl::Context<'_>,
};
let output_ty = match output {
syn::ReturnType::Default => quote! { () },
syn::ReturnType::Type(_, ty) => quote! { #ty },
};
let asyncness = sig.asyncness;
let checks = self.gen_checks();
let mangled_name = format!("_internal_{}", ident);
let mangled_ident = Ident::new(&mangled_name, ident.span());
let inner_call = self.get_inner_call_expr(&mangled_ident, asyncness);
quote! {
#[allow(clippy::too_many_arguments)]
#asyncness fn #mangled_ident #generics ( #fn_args ) -> #output_ty {
#(#stmts)*
}

#(#attrs)* #vis #asyncness #fn_token #ident #generics ( #inputs ) -> crate::nasl::NaslResult {
#checks
#get_args
let _result = #inner_call;
<#output_ty as crate::nasl::ToNaslResult>::to_nasl_result(_result)
}
}
}

fn impl_add_to_set(
&self,
ident: &Ident,
Expand Down Expand Up @@ -246,7 +204,7 @@ impl<'a> ArgsStruct<'a> {
}
}

pub fn impl_nasl_function_args_2(&self) -> TokenStream {
pub fn impl_nasl_function_args(&self) -> TokenStream {
let ItemFn {
attrs,
vis,
Expand Down
17 changes: 0 additions & 17 deletions rust/crates/nasl-function-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,7 @@ pub fn nasl_function(
.into()
}

#[proc_macro_attribute]
pub fn nasl_function_2(
attrs: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let function = parse_macro_input!(input as syn::ItemFn);
let attrs = parse_macro_input!(attrs as Attrs);
nasl_function_internal_2(function, attrs)
.unwrap_or_else(|e| e.emit())
.into()
}

fn nasl_function_internal(function: ItemFn, attrs: Attrs) -> Result<TokenStream> {
let args = ArgsStruct::try_parse(&function, &attrs)?;
Ok(args.impl_nasl_function_args())
}

fn nasl_function_internal_2(function: ItemFn, attrs: Attrs) -> Result<TokenStream> {
let args = ArgsStruct::try_parse(&function, &attrs)?;
Ok(args.impl_nasl_function_args_2())
}
1 change: 0 additions & 1 deletion rust/src/nasl/builtin/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub struct Array;

function_set! {
Array,
sync_stateless,
(
make_array,
make_list,
Expand Down
19 changes: 12 additions & 7 deletions rust/src/nasl/builtin/cryptographic/aes_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ where
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_cbc_encrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes128>(register, Crypt::Encrypt)
}

Expand All @@ -83,7 +84,8 @@ fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_cbc_decrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes128>(register, Crypt::Decrypt)
}

Expand All @@ -94,7 +96,8 @@ fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_cbc_encrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes192>(register, Crypt::Encrypt)
}

Expand All @@ -106,7 +109,8 @@ fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_cbc_decrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes192>(register, Crypt::Decrypt)
}

Expand All @@ -117,7 +121,8 @@ fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_cbc_encrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes256>(register, Crypt::Encrypt)
}

Expand All @@ -129,15 +134,15 @@ fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes256_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_cbc_decrypt(register: &Register) -> Result<NaslValue, FnError> {
cbc::<Aes256>(register, Crypt::Decrypt)
}

pub struct AesCbc;

function_set! {
AesCbc,
sync_stateless,
(
aes128_cbc_encrypt,
aes128_cbc_decrypt,
Expand Down
43 changes: 25 additions & 18 deletions rust/src/nasl/builtin/cryptographic/aes_ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use crate::nasl::utils::error::FnError;
use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser};
use aes::{Aes128, Aes192, Aes256};
use ccm::{
Expand All @@ -12,10 +11,7 @@ use ccm::{
};
use digest::generic_array::ArrayLength;

use crate::nasl::syntax::NaslValue;
use crate::nasl::utils::{Context, Register};

use crate::function_set;
use crate::nasl::prelude::*;

use super::{get_aad, get_data, get_iv, get_key, get_len, Crypt, CryptographicError};

Expand Down Expand Up @@ -71,7 +67,8 @@ where
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_ccm_encrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes128>(register, Crypt::Encrypt, false)
}

Expand All @@ -82,7 +79,8 @@ fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes128_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_ccm_encrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes128>(register, Crypt::Encrypt, true)
}

Expand All @@ -93,7 +91,8 @@ fn aes128_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_ccm_decrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes128>(register, Crypt::Decrypt, false)
}

Expand All @@ -104,7 +103,8 @@ fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes128_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes128_ccm_decrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes128>(register, Crypt::Decrypt, true)
}

Expand All @@ -115,7 +115,8 @@ fn aes128_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_ccm_encrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes192>(register, Crypt::Encrypt, false)
}

Expand All @@ -126,7 +127,8 @@ fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes192_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_ccm_encrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes192>(register, Crypt::Encrypt, true)
}

Expand All @@ -137,7 +139,8 @@ fn aes192_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_ccm_decrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes192>(register, Crypt::Decrypt, false)
}

Expand All @@ -148,7 +151,8 @@ fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes192_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes192_ccm_decrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes192>(register, Crypt::Decrypt, true)
}

Expand All @@ -159,7 +163,8 @@ fn aes192_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_ccm_encrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes256>(register, Crypt::Encrypt, false)
}

Expand All @@ -170,7 +175,8 @@ fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes256_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_ccm_encrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes256>(register, Crypt::Encrypt, true)
}

Expand All @@ -181,7 +187,8 @@ fn aes256_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_ccm_decrypt(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes256>(register, Crypt::Decrypt, false)
}

Expand All @@ -192,7 +199,8 @@ fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnE
/// - The length of the key should be 16 bytes long
/// - The iv must have a length of 7-13 bytes
/// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16
fn aes256_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes256_ccm_decrypt_auth(register: &Register) -> Result<NaslValue, FnError> {
ccm::<Aes256>(register, Crypt::Decrypt, true)
}

Expand Down Expand Up @@ -234,7 +242,6 @@ pub struct AesCcm;

function_set! {
AesCcm,
sync_stateless,
(
aes128_ccm_encrypt,
aes128_ccm_encrypt_auth,
Expand Down
8 changes: 3 additions & 5 deletions rust/src/nasl/builtin/cryptographic/aes_cmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use crate::nasl::syntax::NaslValue;
use crate::nasl::utils::{Context, FnError, Register};
use aes::Aes128;
use cmac::{Cmac, Mac};

use crate::function_set;
use crate::nasl::prelude::*;

use super::{get_data, get_key, CryptographicError};

Expand All @@ -16,7 +14,8 @@ use super::{get_data, get_key, CryptographicError};
/// This function expects 2 named arguments key and data either in a string or data type.
/// It is important to notice, that internally the CMAC algorithm is used and not, as the name
/// suggests, CBC-MAC.
fn aes_cmac(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
#[nasl_function]
fn aes_cmac(register: &Register) -> Result<NaslValue, FnError> {
let key = get_key(register)?;
let data = get_data(register)?;

Expand All @@ -31,7 +30,6 @@ pub struct AesCmac;

function_set! {
AesCmac,
sync_stateless,
(
(aes_cmac, "aes_mac_cbc"),
aes_cmac,
Expand Down
Loading

0 comments on commit 271150a

Please sign in to comment.