Skip to content

Commit

Permalink
Rename FunctionErrorKind -> NaslFnError for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 7, 2024
1 parent c85da81 commit d801b48
Show file tree
Hide file tree
Showing 39 changed files with 299 additions and 396 deletions.
14 changes: 7 additions & 7 deletions rust/src/nasl/builtin/cryptographic/aes_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::nasl::prelude::*;
use super::{get_data, get_iv, get_key, get_len, Crypt};

/// Base function for en- and decrypting Cipher Block Chaining (CBC) mode
fn cbc<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FunctionErrorKind>
fn cbc<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, NaslFnError>
where
D: BlockCipher + BlockEncrypt + BlockDecrypt + KeyInit,
{
Expand Down Expand Up @@ -71,7 +71,7 @@ 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, FunctionErrorKind> {
fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes128>(register, Crypt::Encrypt)
}

Expand All @@ -83,7 +83,7 @@ fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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, FunctionErrorKind> {
fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes128>(register, Crypt::Decrypt)
}

Expand All @@ -94,7 +94,7 @@ fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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, FunctionErrorKind> {
fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes192>(register, Crypt::Encrypt)
}

Expand All @@ -106,7 +106,7 @@ fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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, FunctionErrorKind> {
fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes192>(register, Crypt::Decrypt)
}

Expand All @@ -117,7 +117,7 @@ fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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, FunctionErrorKind> {
fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes256>(register, Crypt::Encrypt)
}

Expand All @@ -129,7 +129,7 @@ fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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, FunctionErrorKind> {
fn aes256_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
cbc::<Aes256>(register, Crypt::Decrypt)
}

Expand Down
52 changes: 17 additions & 35 deletions rust/src/nasl/builtin/cryptographic/aes_ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use crate::nasl::utils::error::FunctionErrorKind;
use crate::nasl::utils::error::NaslFnError;
use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser};
use aes::{Aes128, Aes192, Aes256};
use ccm::{
Expand Down Expand Up @@ -41,7 +41,7 @@ where
}

/// Base function for ccm en- and decryption. Sets the tag length to 16.
fn ccm<D>(register: &Register, crypt: Crypt, auth: bool) -> Result<NaslValue, FunctionErrorKind>
fn ccm<D>(register: &Register, crypt: Crypt, auth: bool) -> Result<NaslValue, NaslFnError>
where
D: BlockCipher + BlockSizeUser<BlockSize = U16> + BlockEncrypt + BlockDecrypt + KeyInit,
{
Expand Down Expand Up @@ -71,7 +71,7 @@ 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, FunctionErrorKind> {
fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes128>(register, Crypt::Encrypt, false)
}

Expand All @@ -82,10 +82,7 @@ fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes128_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes128>(register, Crypt::Encrypt, true)
}

Expand All @@ -96,7 +93,7 @@ fn aes128_ccm_encrypt_auth(
/// - 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, FunctionErrorKind> {
fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes128>(register, Crypt::Decrypt, false)
}

Expand All @@ -107,10 +104,7 @@ fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes128_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes128>(register, Crypt::Decrypt, true)
}

Expand All @@ -121,7 +115,7 @@ fn aes128_ccm_decrypt_auth(
/// - 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, FunctionErrorKind> {
fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes192>(register, Crypt::Encrypt, false)
}

Expand All @@ -132,10 +126,7 @@ fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes192_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes192>(register, Crypt::Encrypt, true)
}

Expand All @@ -146,7 +137,7 @@ fn aes192_ccm_encrypt_auth(
/// - 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, FunctionErrorKind> {
fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes192>(register, Crypt::Decrypt, false)
}

Expand All @@ -157,10 +148,7 @@ fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes192_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes192>(register, Crypt::Decrypt, true)
}

Expand All @@ -171,7 +159,7 @@ fn aes192_ccm_decrypt_auth(
/// - 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, FunctionErrorKind> {
fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes256>(register, Crypt::Encrypt, false)
}

Expand All @@ -182,10 +170,7 @@ fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes256_ccm_encrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes256>(register, Crypt::Encrypt, true)
}

Expand All @@ -196,7 +181,7 @@ fn aes256_ccm_encrypt_auth(
/// - 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, FunctionErrorKind> {
fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes256>(register, Crypt::Decrypt, false)
}

Expand All @@ -207,16 +192,13 @@ fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// - 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, FunctionErrorKind> {
fn aes256_ccm_decrypt_auth(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ccm::<Aes256>(register, Crypt::Decrypt, true)
}

macro_rules! ccm_call_typed {
($(($t1s: expr, $t1: ty) => $(($t2s: expr, $t2: ty)),*);*) => {
fn ccm_typed<D>(tag_size: usize, iv_size: usize, crypt: Crypt, key: &[u8], nonce: &[u8], data: &[u8], aad: &[u8]) -> Result<Result<Vec<u8>, aError>, FunctionErrorKind>
fn ccm_typed<D>(tag_size: usize, iv_size: usize, crypt: Crypt, key: &[u8], nonce: &[u8], data: &[u8], aad: &[u8]) -> Result<Result<Vec<u8>, aError>, NaslFnError>
where D: BlockCipher + BlockSizeUser<BlockSize = U16> + BlockEncrypt + BlockDecrypt + KeyInit
{
match tag_size {
Expand All @@ -228,11 +210,11 @@ macro_rules! ccm_call_typed {
Ok(ccm_crypt::<D, $t1, $t2>(crypt, key, nonce, data, aad))
}
),*
other => Err(FunctionErrorKind::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str()))
other => Err(NaslFnError::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str()))
}
}
),*
other => Err(FunctionErrorKind::wrong_unnamed_argument("tag_size must be 4, 6, 8, 10, 12, 14 or 16", other.to_string().as_str()))
other => Err(NaslFnError::wrong_unnamed_argument("tag_size must be 4, 6, 8, 10, 12, 14 or 16", other.to_string().as_str()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/nasl/builtin/cryptographic/aes_cmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

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

Expand All @@ -16,7 +16,7 @@ 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, FunctionErrorKind> {
fn aes_cmac(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
let key = get_key(register)?;
let data = get_data(register)?;

Expand Down
14 changes: 7 additions & 7 deletions rust/src/nasl/builtin/cryptographic/aes_ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::nasl::prelude::*;

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

fn ctr<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FunctionErrorKind>
fn ctr<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, NaslFnError>
where
D: BlockSizeUser<BlockSize = U16>
+ aes::cipher::KeyInit
Expand Down Expand Up @@ -56,7 +56,7 @@ 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. It is used as the initial counter.
fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes128>(register, Crypt::Encrypt)
}

Expand All @@ -68,7 +68,7 @@ fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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. It is used as the initial counter.
fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes128>(register, Crypt::Decrypt)
}

Expand All @@ -79,7 +79,7 @@ fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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. It is used as the initial counter.
fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes192>(register, Crypt::Encrypt)
}

Expand All @@ -91,7 +91,7 @@ fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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. It is used as the initial counter.
fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes192>(register, Crypt::Decrypt)
}

Expand All @@ -102,7 +102,7 @@ fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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. It is used as the initial counter.
fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes256>(register, Crypt::Encrypt)
}

Expand All @@ -114,7 +114,7 @@ fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// 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. It is used as the initial counter.
fn aes256_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes256_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, NaslFnError> {
ctr::<Aes256>(register, Crypt::Decrypt)
}

Expand Down
Loading

0 comments on commit d801b48

Please sign in to comment.