Skip to content

Commit

Permalink
Rename back to FunctionErrorKind for the draft PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 5, 2024
1 parent 9d6a97d commit 52b6113
Show file tree
Hide file tree
Showing 41 changed files with 459 additions and 325 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, NaslError>
fn cbc<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FunctionErrorKind>
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, NaslError> {
fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
cbc::<Aes128>(register, Crypt::Encrypt)
}

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

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

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

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

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

Expand Down
52 changes: 35 additions & 17 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::NaslError;
use crate::nasl::utils::error::FunctionErrorKind;
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, NaslError>
fn ccm<D>(register: &Register, crypt: Crypt, auth: bool) -> Result<NaslValue, FunctionErrorKind>
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, NaslError> {
fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ccm::<Aes128>(register, Crypt::Encrypt, false)
}

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

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

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

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

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

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

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

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

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

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

Expand All @@ -192,13 +207,16 @@ fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// - 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, NaslError> {
fn aes256_ccm_decrypt_auth(
register: &Register,
_: &Context,
) -> Result<NaslValue, FunctionErrorKind> {
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>, NaslError>
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>
where D: BlockCipher + BlockSizeUser<BlockSize = U16> + BlockEncrypt + BlockDecrypt + KeyInit
{
match tag_size {
Expand All @@ -210,11 +228,11 @@ macro_rules! ccm_call_typed {
Ok(ccm_crypt::<D, $t1, $t2>(crypt, key, nonce, data, aad))
}
),*
other => Err(NaslError::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str()))
other => Err(FunctionErrorKind::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str()))
}
}
),*
other => Err(NaslError::wrong_unnamed_argument("tag_size must be 4, 6, 8, 10, 12, 14 or 16", 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()))
}
}
}
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, NaslError, Register};
use crate::nasl::utils::{Context, FunctionErrorKind, 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, NaslError> {
fn aes_cmac(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
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, NaslError>
fn ctr<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FunctionErrorKind>
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, NaslError> {
fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes128>(register, Crypt::Encrypt)
}

Expand All @@ -68,7 +68,7 @@ fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// 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, NaslError> {
fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes128>(register, Crypt::Decrypt)
}

Expand All @@ -79,7 +79,7 @@ fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// 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, NaslError> {
fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes192>(register, Crypt::Encrypt)
}

Expand All @@ -91,7 +91,7 @@ fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// 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, NaslError> {
fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes192>(register, Crypt::Decrypt)
}

Expand All @@ -102,7 +102,7 @@ fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// 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, NaslError> {
fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes256>(register, Crypt::Encrypt)
}

Expand All @@ -114,7 +114,7 @@ fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Nas
/// 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, NaslError> {
fn aes256_ctr_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
ctr::<Aes256>(register, Crypt::Decrypt)
}

Expand Down
Loading

0 comments on commit 52b6113

Please sign in to comment.