cwc: use better random values (coverity)

This commit is contained in:
Jaroslav Kysela 2014-10-03 21:12:31 +02:00
parent ab8f4bf1f4
commit 11eedbbd1e
3 changed files with 14 additions and 17 deletions

View file

@ -327,21 +327,6 @@ des_key_spread(uint8_t *normal, uint8_t *spread)
des_key_parity_adjust(spread, 16);
}
/**
*
*/
static void
des_random_get(uint8_t *buffer, uint8_t len)
{
uint8_t idx = 0;
int randomNo = 0;
for (idx = 0; idx < len; idx++) {
if (!(idx % 3)) randomNo = rand();
buffer[idx] = (randomNo >> ((idx % 3) << 3)) & 0xff;
}
}
/**
*
*/
@ -356,11 +341,11 @@ des_encrypt(uint8_t *buffer, int len, cwc_t *cwc)
noPadBytes = (8 - ((len - 1) % 8)) % 8;
if (len + noPadBytes + 1 >= CWS_NETMSGSIZE-8) return -1;
des_random_get(padBytes, noPadBytes);
uuid_random(padBytes, noPadBytes);
for (i = 0; i < noPadBytes; i++) buffer[len++] = padBytes[i];
for (i = 2; i < len; i++) checksum ^= buffer[i];
buffer[len++] = checksum;
des_random_get((uint8_t *)ivec, 8);
uuid_random((uint8_t *)ivec, 8);
memcpy(buffer+len, ivec, 8);
for (i = 2; i < len; i += 8) {
DES_ncbc_encrypt(buffer+i, buffer+i, 8, &cwc->cwc_k1, &ivec, 1);

View file

@ -101,6 +101,15 @@ uuid_init ( void )
}
}
void
uuid_random ( uint8_t *buf, size_t bufsize )
{
if (read(fd, buf, bufsize) != bufsize) {
tvherror("uuid", "random failed: %s", strerror(errno));
exit(1);
}
}
/* Initialise binary */
int
uuid_init_bin ( tvh_uuid_t *u, const char *str )

View file

@ -36,6 +36,9 @@ typedef struct uuid {
/* Initialise subsystem */
void uuid_init ( void );
/* Random bytes */
void uuid_random ( uint8_t *buf, size_t bufsize );
/* Initialise binary */
int uuid_init_bin ( tvh_uuid_t *u, const char *str );