6 #include "../misc//support.h"
14 #include <gpg-error.h>
15 #include <gvm/base/logging.h>
22 void *signature = g_malloc0 (16);
26 hmac = g_hmac_new (G_CHECKSUM_MD5, key, keylen);
27 g_hmac_update (hmac, buf, buflen);
28 g_hmac_get_digest (hmac, signature, &signlen);
34 hmac_sha1 (
const void *key,
int keylen,
const void *buf,
int buflen)
36 void *signature = g_malloc0 (20);
40 hmac = g_hmac_new (G_CHECKSUM_SHA1, key, keylen);
41 g_hmac_update (hmac, buf, buflen);
42 g_hmac_get_digest (hmac, signature, &signlen);
48 hmac_sha256 (
const void *key,
int keylen,
const void *buf,
int buflen)
50 void *signature = g_malloc0 (32);
54 hmac = g_hmac_new (G_CHECKSUM_SHA256, key, keylen);
55 g_hmac_update (hmac, buf, buflen);
56 g_hmac_get_digest (hmac, signature, &signlen);
62 hmac_sha384 (
const void *key,
int keylen,
const void *buf,
int buflen)
68 if (!buf || buflen <= 0)
71 err = gcry_md_open (&hd, GCRY_MD_SHA384, key ? GCRY_MD_FLAG_HMAC : 0);
74 g_message (
"nasl_gcrypt_hash(): gcry_md_open failed: %s/%s",
75 gcry_strsource (err), gcry_strerror (err));
81 err = gcry_md_setkey (hd, key, keylen);
84 g_message (
"nasl_gcrypt_hash(): gcry_md_setkey failed: %s/%s",
85 gcry_strsource (err), gcry_strerror (err));
90 gcry_md_write (hd, buf, buflen);
91 ret = g_memdup2 (gcry_md_read (hd, 0), 48);
97 mac (
const char *key,
const size_t key_len,
const char *data,
98 const size_t data_len,
const char *iv,
const size_t iv_len,
int algo,
99 int flags,
char **out,
size_t *out_len)
102 gpg_err_code_t result = 0;
104 if (key == NULL || key_len < 1)
105 return GPG_ERR_MISSING_KEY;
106 if (data == NULL || data_len < 1)
107 return GPG_ERR_MISSING_VALUE;
110 return GPG_ERR_GENERAL;
112 if ((result = gcry_mac_open (&hd, algo, flags, NULL)))
114 if ((result = gcry_mac_setkey (hd, key, key_len)))
116 if (iv && (result = gcry_mac_setiv (hd, iv, iv_len)))
118 if ((result = gcry_mac_write (hd, data, data_len)))
121 *out_len = gcry_mac_get_algo_maclen (algo);
122 if ((*out = g_malloc0 (*out_len *
sizeof (*out))) == NULL)
124 result = GPG_ERR_ENOMEM;
127 if ((result = gcry_mac_read (hd, *out, out_len)))
136 smb_sign (
const int algo,
const char *key,
const size_t key_len,
char *buf,
137 const size_t buf_len,
const char *iv,
const size_t iv_len,
char **out)
139 gcry_error_t error = GPG_ERR_NO_ERROR;
140 char *signature = NULL;
141 size_t signature_len;
142 if (buf == NULL || buf_len < 64)
144 return GPG_ERR_NO_VALUE;
146 if (key == NULL || key_len < 16)
147 return GPG_ERR_NO_KEY;
148 memset ((
char *) buf + 48, 0, 16);
151 case GCRY_MAC_GMAC_AES:
152 if ((error =
mac (key, key_len, buf, buf_len, iv, iv_len, algo,
153 GCRY_MAC_FLAG_SECURE, &signature, &signature_len)))
156 case GCRY_MAC_CMAC_AES:
157 if ((error =
mac (key, key_len, buf, buf_len, NULL, 0, algo,
158 GCRY_MAC_FLAG_SECURE, &signature, &signature_len)))
161 case G_CHECKSUM_SHA256:
162 signature =
hmac_sha256 (key, key_len, buf, buf_len);
166 error = GPG_ERR_UNKNOWN_ALGORITHM;
170 *out = g_malloc0 (buf_len);
171 memcpy (*out, buf, buf_len);
172 memcpy (*out + 48, signature, 16);
181 char *key, *buf, *iv, *res;
182 int keylen, buflen, ivlen;
193 switch ((error =
smb_sign (algo, key, keylen, buf, buflen, iv, ivlen, &res)))
195 case GPG_ERR_NO_ERROR:
200 case GPG_ERR_MISSING_KEY:
201 case GPG_ERR_MISSING_VALUE:
202 nasl_perror (lexic,
"Syntax: nasl_mac: Missing key, or data argument");
205 nasl_perror (lexic,
"Internal: %s.", gcry_strerror (error));