wsutil: Convert our cryptograpy code to C99 types

Convert adler32 as well, which was missed in the checksum code
conversion.
This commit is contained in:
Gerald Combs 2023-09-15 12:15:04 -07:00 committed by AndersBroman
parent 684f3bf979
commit a93d49c33a
11 changed files with 87 additions and 87 deletions

View File

@ -17,10 +17,10 @@
#define BASE 65521 /* largest prime smaller than 65536 */
/*--- update_adler32 --------------------------------------------------------*/
guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len)
uint32_t update_adler32(uint32_t adler, const uint8_t *buf, size_t len)
{
guint32 s1 = adler & 0xffff;
guint32 s2 = (adler >> 16) & 0xffff;
uint32_t s1 = adler & 0xffff;
uint32_t s2 = (adler >> 16) & 0xffff;
size_t n;
for (n = 0; n < len; n++) {
@ -31,15 +31,15 @@ guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len)
}
/*--- adler32 ---------------------------------------------------------------*/
guint32 adler32_bytes(const guint8 *buf, size_t len)
uint32_t adler32_bytes(const uint8_t *buf, size_t len)
{
return update_adler32(1, buf, len);
}
/*--- adler32_str -----------------------------------------------------------*/
guint32 adler32_str(const char *buf)
uint32_t adler32_str(const char *buf)
{
return update_adler32(1, (const guint8*)buf, strlen(buf));
return update_adler32(1, (const uint8_t*)buf, strlen(buf));
}
/*---------------------------------------------------------------------------*/

View File

@ -18,9 +18,9 @@
extern "C"{
#endif
WS_DLL_PUBLIC guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len);
WS_DLL_PUBLIC guint32 adler32_bytes(const guint8 *buf, size_t len);
WS_DLL_PUBLIC guint32 adler32_str(const char *buf);
WS_DLL_PUBLIC uint32_t update_adler32(uint32_t adler, const uint8_t *buf, size_t len);
WS_DLL_PUBLIC uint32_t adler32_bytes(const uint8_t *buf, size_t len);
WS_DLL_PUBLIC uint32_t adler32_str(const char *buf);
#ifdef __cplusplus
}

View File

@ -16,31 +16,31 @@
/************************************************************************/
/* Note: copied from net80211/ieee80211_airpdcap_tkip.c */
#define S_SWAP(a,b) { guint8 t = S[a]; S[a] = S[b]; S[b] = t; }
#define S_SWAP(a,b) { uint8_t t = S[a]; S[a] = S[b]; S[b] = t; }
/* Note: copied from FreeBSD source code, RELENG 6, */
/* sys/net80211/ieee80211_crypto_wep.c, 391 */
int Dot11DecryptWepDecrypt(
const guchar *seed,
const unsigned char *seed,
const size_t seed_len,
guchar *cypher_text,
unsigned char *cypher_text,
const size_t data_len)
{
guint32 i, j, k, crc;
guint8 S[256];
guint8 icv[4];
uint32_t i, j, k, crc;
uint8_t S[256];
uint8_t icv[4];
size_t buflen;
/* Generate key stream (RC4 Pseudo-Random Number Generator) */
for (i = 0; i < 256; i++)
S[i] = (guint8)i;
S[i] = (uint8_t)i;
for (j = i = 0; i < 256; i++) {
j = (j + S[i] + seed[i % seed_len]) & 0xff;
S_SWAP(i, j);
}
/* Apply RC4 to data and compute CRC32 over decrypted data */
crc = ~(guint32)0;
crc = ~(uint32_t)0;
buflen = data_len;
for (i = j = k = 0; k < buflen; k++) {
@ -55,10 +55,10 @@ int Dot11DecryptWepDecrypt(
crc = ~crc;
/* Encrypt little-endian CRC32 and verify that it matches with the received ICV */
icv[0] = (guint8)crc;
icv[1] = (guint8)(crc >> 8);
icv[2] = (guint8)(crc >> 16);
icv[3] = (guint8)(crc >> 24);
icv[0] = (uint8_t)crc;
icv[1] = (uint8_t)(crc >> 8);
icv[2] = (uint8_t)(crc >> 16);
icv[3] = (uint8_t)(crc >> 24);
for (k = 0; k < 4; k++) {
i = (i + 1) & 0xff;
j = (j + S[i]) & 0xff;

View File

@ -16,9 +16,9 @@
#include <gcrypt.h>
typedef struct {
guint8 L[EAX_SIZEOF_KEY];
guint8 D[EAX_SIZEOF_KEY];
guint8 Q[EAX_SIZEOF_KEY];
uint8_t L[EAX_SIZEOF_KEY];
uint8_t D[EAX_SIZEOF_KEY];
uint8_t Q[EAX_SIZEOF_KEY];
} eax_s;
static eax_s instance;
@ -26,10 +26,10 @@ static eax_s instance;
/* these are defined as macros so they'll be easy to redo in assembly if desired */
#define BLK_CPY(dst, src) { memcpy(dst, src, EAX_SIZEOF_KEY); }
#define BLK_XOR(dst, src) { int z; for (z=0; z < EAX_SIZEOF_KEY; z++) dst[z] ^= src[z]; }
static void Dbl(guint8 *out, const guint8 *in);
static void CTR(const guint8 *ws, guint8 *pK, guint8 *pN, guint16 SizeN);
static void CMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN);
static void dCMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN, const guint8 *pC, guint16 SizeC);
static void Dbl(uint8_t *out, const uint8_t *in);
static void CTR(const uint8_t *ws, uint8_t *pK, uint8_t *pN, uint16_t SizeN);
static void CMAC(uint8_t *pK, uint8_t *ws, const uint8_t *pN, uint16_t SizeN);
static void dCMAC(uint8_t *pK, uint8_t *ws, const uint8_t *pN, uint16_t SizeN, const uint8_t *pC, uint16_t SizeC);
void AesEncrypt(unsigned char msg[EAX_SIZEOF_KEY], unsigned char key[EAX_SIZEOF_KEY]);
/*!
@ -43,20 +43,20 @@ void AesEncrypt(unsigned char msg[EAX_SIZEOF_KEY], unsigned char key[EAX_SIZEOF_
@param[in] SizeC byte length of ciphertext (pC) buffer
@param[in] pMac four-byte Message Authentication Code
@param[in] Mode EAX_MODE_CLEARTEXT_AUTH or EAX_MODE_CIPHERTEXT_AUTH
@return TRUE if message has been authenticated; FALSE if not
@return true if message has been authenticated; false if not
authenticated, invalid Mode or error
*/
gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
guint32 SizeN, guint32 SizeK, guint32 SizeC, MAC_T *pMac,
guint8 Mode)
bool Eax_Decrypt(uint8_t *pN, uint8_t *pK, uint8_t *pC,
uint32_t SizeN, uint32_t SizeK, uint32_t SizeC, MAC_T *pMac,
uint8_t Mode)
{
guint8 wsn[EAX_SIZEOF_KEY];
guint8 wsc[EAX_SIZEOF_KEY];
uint8_t wsn[EAX_SIZEOF_KEY];
uint8_t wsc[EAX_SIZEOF_KEY];
int i;
/* key size must match this implementation */
if (SizeK != EAX_SIZEOF_KEY)
return FALSE;
return false;
/* the key is new */
for (i = 0; i < EAX_SIZEOF_KEY; i++)
@ -78,7 +78,7 @@ gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
*/
if (Mode == EAX_MODE_CLEARTEXT_AUTH)
{
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? FALSE : TRUE);
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? false : true);
}
@ -89,7 +89,7 @@ gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
else if (Mode == EAX_MODE_CIPHERTEXT_AUTH)
{
if (SizeC == 0)
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? FALSE : TRUE);
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? false : true);
{
/* first copy the nonce into our working space */
BLK_CPY(wsc, instance.Q);
@ -99,17 +99,17 @@ gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
if (memcmp(pMac, &wsc[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) == 0)
{
CTR(wsn, pK, pC, SizeC);
return TRUE;
return true;
}
}
return FALSE;
return false;
}
/* set up D or Q from L */
static void Dbl(guint8 *out, const guint8 *in)
static void Dbl(uint8_t *out, const uint8_t *in)
{
int i;
guint8 carry = 0;
uint8_t carry = 0;
/* this might be a lot more efficient in assembly language */
for (i=0; i < EAX_SIZEOF_KEY; i++)
@ -121,24 +121,24 @@ static void Dbl(guint8 *out, const guint8 *in)
out[0] ^= 0x87;
}
static void CMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN)
static void CMAC(uint8_t *pK, uint8_t *ws, const uint8_t *pN, uint16_t SizeN)
{
dCMAC(pK, ws, pN, SizeN, NULL, 0);
}
static void dCMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN, const guint8 *pC, guint16 SizeC)
static void dCMAC(uint8_t *pK, uint8_t *ws, const uint8_t *pN, uint16_t SizeN, const uint8_t *pC, uint16_t SizeC)
{
gcry_cipher_hd_t cipher_hd;
guint8 *work;
guint8 *ptr;
guint16 SizeT = SizeN + SizeC;
guint16 worksize = SizeT;
uint8_t *work;
uint8_t *ptr;
uint16_t SizeT = SizeN + SizeC;
uint16_t worksize = SizeT;
/* worksize must be an integral multiple of 16 */
if (SizeT & 0xf) {
worksize += 0x10 - (worksize & 0xf);
}
work = (guint8 *)g_malloc(worksize);
work = (uint8_t *)g_malloc(worksize);
if (work == NULL) {
return;
}
@ -187,10 +187,10 @@ static void dCMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN, const
return;
}
static void CTR(const guint8 *ws, guint8 *pK, guint8 *pN, guint16 SizeN)
static void CTR(const uint8_t *ws, uint8_t *pK, uint8_t *pN, uint16_t SizeN)
{
gcry_cipher_hd_t cipher_hd;
guint8 ctr[EAX_SIZEOF_KEY];
uint8_t ctr[EAX_SIZEOF_KEY];
BLK_CPY(ctr, ws);
ctr[12] &= 0x7f;

View File

@ -16,7 +16,7 @@
typedef struct tagMAC_T
{
guint8 Mac[4];
uint8_t Mac[4];
} MAC_T;
#define EAX_MODE_CLEARTEXT_AUTH 1
@ -35,12 +35,12 @@ typedef struct tagMAC_T
@param[in] SizeC byte length of ciphertext (pC) buffer
@param[in] pMac four-byte Message Authentication Code
@param[in] Mode EAX_MODE_CLEARTEXT_AUTH or EAX_MODE_CIPHERTEXT_AUTH
@return TRUE if message has been authenticated; FALSE if not
@return true if message has been authenticated; false if not
authenticated, invalid Mode or error
*/
WS_DLL_PUBLIC
gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
guint32 SizeN, guint32 SizeK, guint32 SizeC, MAC_T *pMac,
guint8 Mode);
bool Eax_Decrypt(uint8_t *pN, uint8_t *pK, uint8_t *pC,
uint32_t SizeN, uint32_t SizeK, uint32_t SizeC, MAC_T *pMac,
uint8_t Mode);
#endif

View File

@ -33,7 +33,7 @@ rsa_privkey_to_sexp(gnutls_x509_privkey_t priv_key, char **err)
size_t tmp_size;
gcry_error_t gret;
gcry_sexp_t rsa_priv_key = NULL;
gint i;
int i;
gcry_mpi_t rsa_params[RSA_PARS];
*err = NULL;
@ -95,8 +95,8 @@ rsa_load_pem_key(FILE *fp, char **err)
gnutls_x509_privkey_t priv_key;
gnutls_datum_t key;
ws_statb64 statbuf;
gint ret;
guint bytes;
int ret;
unsigned bytes;
*err = NULL;
if (ws_fstat64(ws_fileno(fp), &statbuf) == -1) {
@ -122,7 +122,7 @@ rsa_load_pem_key(FILE *fp, char **err)
/* load all file contents into a datum buffer*/
key.data = (unsigned char *)g_malloc((size_t)statbuf.st_size);
key.size = (int)statbuf.st_size;
bytes = (guint) fread(key.data, 1, key.size, fp);
bytes = (unsigned) fread(key.data, 1, key.size, fp);
if (bytes < key.size) {
if (bytes == 0 && ferror(fp)) {
*err = ws_strdup_printf("can't read from file %d bytes, got error %s",
@ -173,7 +173,7 @@ BAGTYPE(gnutls_pkcs12_bag_type_t x) {
}
gnutls_x509_privkey_t
rsa_load_pkcs12(FILE *fp, const gchar *cert_passwd, char **err)
rsa_load_pkcs12(FILE *fp, const char *cert_passwd, char **err)
{
int i, j, ret;
int rest;
@ -348,7 +348,7 @@ done:
}
void
rsa_private_key_free(gpointer key)
rsa_private_key_free(void * key)
{
gcry_sexp_release((gcry_sexp_t) key);
}
@ -356,7 +356,7 @@ rsa_private_key_free(gpointer key)
#else /* ! defined(HAVE_LIBGNUTLS) */
void
rsa_private_key_free(gpointer key _U_)
rsa_private_key_free(void * key _U_)
{
}

View File

@ -38,7 +38,7 @@ WS_DLL_PUBLIC gnutls_x509_privkey_t rsa_load_pem_key(FILE* fp, char **err);
WS_DLL_PUBLIC gnutls_x509_privkey_t rsa_load_pkcs12(FILE* fp, const char *cert_passwd, char** err);
#endif
WS_DLL_PUBLIC void rsa_private_key_free(gpointer key);
WS_DLL_PUBLIC void rsa_private_key_free(void * key);
#endif /* __RSA_H__ */

View File

@ -48,9 +48,9 @@ gcry_error_t ws_cmac_buffer(int algo, void *digest, const void *buffer, size_t l
return result;
}
void crypt_des_ecb(guint8 *output, const guint8 *buffer, const guint8 *key56)
void crypt_des_ecb(uint8_t *output, const uint8_t *buffer, const uint8_t *key56)
{
guint8 key64[8];
uint8_t key64[8];
gcry_cipher_hd_t handle;
memset(output, 0x00, 8);
@ -76,9 +76,9 @@ void crypt_des_ecb(guint8 *output, const guint8 *buffer, const guint8 *key56)
gcry_cipher_close(handle);
}
size_t rsa_decrypt_inplace(const guint len, guchar* data, gcry_sexp_t pk, gboolean pkcs1_padding, char **err)
size_t rsa_decrypt_inplace(const unsigned len, unsigned char* data, gcry_sexp_t pk, bool pkcs1_padding, char **err)
{
gint rc = 0;
int rc = 0;
size_t decr_len = 0, i = 0;
gcry_sexp_t s_data = NULL, s_plain = NULL;
gcry_mpi_t encr_mpi = NULL, text = NULL;
@ -145,7 +145,7 @@ size_t rsa_decrypt_inplace(const guint len, guchar* data, gcry_sexp_t pk, gboole
rc = 0;
for (i = 1; i < decr_len; i++) {
if (data[i] == 0) {
rc = (gint) i+1;
rc = (int) i+1;
break;
}
}
@ -163,14 +163,14 @@ out:
}
gcry_error_t
hkdf_expand(int hashalgo, const guint8 *prk, guint prk_len, const guint8 *info, guint info_len,
guint8 *out, guint out_len)
hkdf_expand(int hashalgo, const uint8_t *prk, unsigned prk_len, const uint8_t *info, unsigned info_len,
uint8_t *out, unsigned out_len)
{
// Current maximum hash output size: 48 bytes for SHA-384.
guchar lastoutput[48];
unsigned char lastoutput[48];
gcry_md_hd_t h;
gcry_error_t err;
const guint hash_len = gcry_md_get_algo_dlen(hashalgo);
const unsigned hash_len = gcry_md_get_algo_dlen(hashalgo);
/* Some sanity checks */
if (!(out_len > 0 && out_len <= 255 * hash_len) ||
@ -183,14 +183,14 @@ hkdf_expand(int hashalgo, const guint8 *prk, guint prk_len, const guint8 *info,
return err;
}
for (guint offset = 0; offset < out_len; offset += hash_len) {
for (unsigned offset = 0; offset < out_len; offset += hash_len) {
gcry_md_reset(h);
gcry_md_setkey(h, prk, prk_len); /* Set PRK */
if (offset > 0) {
gcry_md_write(h, lastoutput, hash_len); /* T(1..N) */
}
gcry_md_write(h, info, info_len); /* info */
gcry_md_putc(h, (guint8) (offset / hash_len + 1)); /* constant 0x01..N */
gcry_md_putc(h, (uint8_t) (offset / hash_len + 1)); /* constant 0x01..N */
memcpy(lastoutput, gcry_md_read(h, hashalgo), hash_len);
memcpy(out + offset, lastoutput, MIN(hash_len, out_len - offset));

View File

@ -36,10 +36,10 @@ WS_DLL_PUBLIC gcry_error_t ws_cmac_buffer(int algo, void *digest, const void *bu
/* Convenience function to encrypt 8 bytes in BUFFER with DES using the 56 bits KEY expanded to
64 bits as key, encrypted data is returned in OUTPUT which must be at least 8 bytes large */
WS_DLL_PUBLIC void crypt_des_ecb(guint8 *output, const guint8 *buffer, const guint8 *key56);
WS_DLL_PUBLIC void crypt_des_ecb(uint8_t *output, const uint8_t *buffer, const uint8_t *key56);
/* Convenience function for RSA decryption. Returns decrypted length on success, 0 on failure */
WS_DLL_PUBLIC size_t rsa_decrypt_inplace(const guint len, guchar* data, gcry_sexp_t pk, gboolean pkcs1_padding, char **err);
WS_DLL_PUBLIC size_t rsa_decrypt_inplace(const unsigned len, unsigned char* data, gcry_sexp_t pk, bool pkcs1_padding, char **err);
/**
* RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF):
@ -55,8 +55,8 @@ WS_DLL_PUBLIC size_t rsa_decrypt_inplace(const guint len, guchar* data, gcry_sex
* @return 0 on success and an error code otherwise.
*/
WS_DLL_PUBLIC gcry_error_t
hkdf_expand(int hashalgo, const guint8 *prk, guint prk_len, const guint8 *info, guint info_len,
guint8 *out, guint out_len);
hkdf_expand(int hashalgo, const uint8_t *prk, unsigned prk_len, const uint8_t *info, unsigned info_len,
uint8_t *out, unsigned out_len);
/*
* Calculate HKDF-Extract(salt, IKM) -> PRK according to RFC 5869.
@ -64,7 +64,7 @@ hkdf_expand(int hashalgo, const guint8 *prk, guint prk_len, const guint8 *info,
* algorithm 'hashalgo' (e.g. 32 bytes for SHA-256).
*/
static inline gcry_error_t
hkdf_extract(int hashalgo, const guint8 *salt, size_t salt_len, const guint8 *ikm, size_t ikm_len, guint8 *prk)
hkdf_extract(int hashalgo, const uint8_t *salt, size_t salt_len, const uint8_t *ikm, size_t ikm_len, uint8_t *prk)
{
/* PRK = HMAC-Hash(salt, IKM) where salt is key, and IKM is input. */
return ws_hmac_buffer(hashalgo, prk, ikm, ikm_len, salt, salt_len);

View File

@ -16,10 +16,10 @@
#include "pint.h"
void decrypt_xtea_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds)
void decrypt_xtea_ecb(uint8_t plaintext[8], const uint8_t ciphertext[8], const uint32_t key[4], unsigned num_rounds)
{
guint i;
guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
unsigned i;
uint32_t v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
v[0] = pntoh32(&ciphertext[0]);
v[1] = pntoh32(&ciphertext[4]);
@ -36,10 +36,10 @@ void decrypt_xtea_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const gui
memcpy(plaintext, v, sizeof v);
}
void decrypt_xtea_le_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds)
void decrypt_xtea_le_ecb(uint8_t plaintext[8], const uint8_t ciphertext[8], const uint32_t key[4], unsigned num_rounds)
{
guint i;
guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
unsigned i;
uint32_t v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
v[0] = pletoh32(&ciphertext[0]);
v[1] = pletoh32(&ciphertext[4]);

View File

@ -19,9 +19,9 @@
*/
#include "wireshark.h"
WS_DLL_PUBLIC void decrypt_xtea_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds);
WS_DLL_PUBLIC void decrypt_xtea_ecb(uint8_t plaintext[8], const uint8_t ciphertext[8], const uint32_t key[4], unsigned num_rounds);
WS_DLL_PUBLIC void decrypt_xtea_le_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds);
WS_DLL_PUBLIC void decrypt_xtea_le_ecb(uint8_t plaintext[8], const uint8_t ciphertext[8], const uint32_t key[4], unsigned num_rounds);
#endif /* __XTEA_H__ */