Skip to content

Commit

Permalink
Add: support for MAC_CMAC_AES (AES-128/192/256-CMAC) hash function
Browse files Browse the repository at this point in the history
To be able to support protocols based on AES-128-CMAC a new function
calles `aes_cmac_cbc` is introduced.

Currently gcrypt supports: AES-128, AES-192, AES-256.

To use this functions the caller needs to ensure that the keylen is
either 16, 24 or 32 bytes long.

Example aes128_cmac.nasl:

```
data = "foobar";
key = "/B?E(H+MbQeThWmZ";
encrypt = aes_cmac_cbc(key : key, data : data);
display(data);
display(encrypt);
```
  • Loading branch information
nichtsfrei committed May 4, 2022
1 parent b42420d commit 1574628
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
88 changes: 88 additions & 0 deletions nasl/nasl_crypto2.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gvm/base/logging.h>
#include <stddef.h>

#define INTBLOB_LEN 20
#define SIGBLOB_LEN (2 * INTBLOB_LEN)
Expand Down Expand Up @@ -1868,6 +1869,93 @@ nasl_open_rc4_cipher (lex_ctxt *lexic)
GCRY_CIPHER_MODE_STREAM, "open_rc4_cipher");
}

static int
hmac_aes_cbc (const char *key, const size_t key_len, const char *msg,
const size_t msg_len, char **output, size_t *outputlen)
{
gcry_mac_hd_t hd;
int result = 0;

if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
{
return -1;
}
// GCRY_MAC_CMAC_AES is capable of handling multiple key lengths
// https://github.com/gpg/libgcrypt/blob/master/cipher/mac-cmac.c#L306
// for more details
if (gcry_mac_open (&hd, GCRY_MAC_CMAC_AES, GCRY_MAC_FLAG_SECURE, NULL))
{
return -2;
}

if (gcry_mac_setkey (hd, key, (size_t) key_len))
{
result = -3;
}
else if (gcry_mac_write (hd, msg, msg_len))
{
result = -4;
}
else
{
*outputlen = gcry_mac_get_algo_maclen (GCRY_MAC_CMAC_AES);
if ((*output = calloc (*outputlen, sizeof (**output))) == NULL)
{
result = -5;
}
else if (gcry_mac_read (hd, *output, outputlen))
{
result = -6;
free (*output);
*output = NULL;
}
}
gcry_mac_close (hd);
return result;
}

tree_cell *
nasl_aes_cmac_cbc (lex_ctxt *lexic)
{
tree_cell *retc = NULL;
char *data, *key;
char *result = NULL;
size_t datalen, keylen, resultlen;

data = get_str_var_by_name (lexic, "data");
datalen = get_var_size_by_name (lexic, "data");
key = get_str_var_by_name (lexic, "key");
keylen = get_var_size_by_name (lexic, "key");

switch (hmac_aes_cbc (key, keylen, data, datalen, &result, &resultlen))
{
case 0:
retc = alloc_typed_cell (CONST_DATA);
retc->x.str_val = result;
retc->size = resultlen;
nasl_trace (lexic, "created hash: %s (%lu)", result, resultlen);
break;
case -1:
nasl_perror (lexic, "gcrypt is not initialized");
break;
case -2:
nasl_perror (lexic, "gcrypt version does not support MAC_CMAC_AES");
break;
case -3:
nasl_perror (lexic, "MAC_CMAC_AES does not support keylen %lu", keylen);
break;
case -4:
nasl_perror (lexic, "MAC_CMAC_AES unable to write hash");
break;
case -5:
nasl_perror (lexic, "insufficient memory to allocate result");
break;
case -6:
nasl_perror (lexic, "unable to get hash");
}
return retc;
}

tree_cell *
nasl_aes128_cbc_encrypt (lex_ctxt *lexic)
{
Expand Down
2 changes: 2 additions & 0 deletions nasl/nasl_crypto2.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ nasl_rc4_encrypt (lex_ctxt *lexic);
tree_cell *
nasl_aes128_cbc_encrypt (lex_ctxt *lexic);

tree_cell *
nasl_aes_cmac_cbc (lex_ctxt *lexic);
tree_cell *
nasl_aes256_cbc_encrypt (lex_ctxt *lexic);

Expand Down
1 change: 1 addition & 0 deletions nasl/nasl_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ static init_func libfuncs[] = {
{"bf_cbc_decrypt", nasl_bf_cbc_decrypt},
{"rc4_encrypt", nasl_rc4_encrypt},
{"aes128_cbc_encrypt", nasl_aes128_cbc_encrypt},
{"aes_cmac_cbc", nasl_aes_cmac_cbc},
{"aes256_cbc_encrypt", nasl_aes256_cbc_encrypt},
{"aes128_ctr_encrypt", nasl_aes128_ctr_encrypt},
{"aes256_ctr_encrypt", nasl_aes256_ctr_encrypt},
Expand Down

0 comments on commit 1574628

Please sign in to comment.