Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
cwc: use better random values (coverity)
  • Loading branch information
perexg committed Oct 3, 2014
1 parent ab8f4bf commit 11eedbb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/descrambler/cwc.c
Expand Up @@ -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;
}
}

/**
*
*/
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/uuid.c
Expand Up @@ -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 )
Expand Down
3 changes: 3 additions & 0 deletions src/uuid.h
Expand Up @@ -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 );

Expand Down

0 comments on commit 11eedbb

Please sign in to comment.