Skip to content

Fix misuse of signed ints in the HAVEGE module #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/mbedtls/havege.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#endif

#include <stddef.h>
#include <stdint.h>

#define MBEDTLS_HAVEGE_COLLECT_SIZE 1024

Expand All @@ -43,9 +44,9 @@ extern "C" {
*/
typedef struct mbedtls_havege_state
{
int PT1, PT2, offset[2];
int pool[MBEDTLS_HAVEGE_COLLECT_SIZE];
int WALK[8192];
uint32_t PT1, PT2, offset[2];
uint32_t pool[MBEDTLS_HAVEGE_COLLECT_SIZE];
uint32_t WALK[8192];
}
mbedtls_havege_state;

Expand Down
22 changes: 12 additions & 10 deletions library/havege.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "mbedtls/timing.h"
#include "mbedtls/platform_util.h"

#include <stdint.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: Should we bother to include stdint.h again as we'd have already included it via including havege.h?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer to include headers for what they define, not for what headers they pull in. According to this style, since havege.c uses uint32_t, it includes stdint.h.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the convention we use in the rest of Mbed TLS as well? I'm happy to use whatever the library already does.

It's slightly more efficient to not #include a header if you know the types are already available, as they must since the declarations we use from havege.h require it, because the preprocessor doesn't need to read in that file at all. For large projects, and especially on slow filesystems (e.g. Windows, network drives), this can add up to a big savings in compile time. It's not uncommon to have a style that encourages efficiency. (C++ style guides recommending pre-increment operators over post-increment operators in for loops is one example of this.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember that we ever discussed this, and I don't think we're doing or not-doing this with any noticeable consistency.

All our header files have garden-variety include guards. Compilers typically optimize well for that case. In any case, compilation time is the least of our worries. This is not a 100000-file C++ project. Being a security project, we value security, readability and ease of maintenance rather more than runtime performance, let alone build-time performance. Also, if you want to optimize compilation time, the biggest thing by far would be to get rid of cmake!

#include <string.h>

/* ------------------------------------------------------------------------
Expand All @@ -54,7 +55,7 @@
* ------------------------------------------------------------------------
*/

#define SWAP(X,Y) { int *T = (X); (X) = (Y); (Y) = T; }
#define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; }

#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
Expand All @@ -77,7 +78,7 @@
PTX = (PT1 >> 18) & 7; \
PT1 &= 0x1FFF; \
PT2 &= 0x1FFF; \
CLK = (int) mbedtls_timing_hardclock(); \
CLK = (uint32_t) mbedtls_timing_hardclock(); \
\
i = 0; \
A = &WALK[PT1 ]; RES[i++] ^= *A; \
Expand All @@ -100,7 +101,7 @@
\
IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
*A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
*B = IN; CLK = (int) mbedtls_timing_hardclock(); \
*B = IN; CLK = (uint32_t) mbedtls_timing_hardclock(); \
*C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
*D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
\
Expand Down Expand Up @@ -158,10 +159,11 @@
*/
static void havege_fill( mbedtls_havege_state *hs )
{
int i, n = 0;
int U1, U2, *A, *B, *C, *D;
int PT1, PT2, *WALK, RES[16];
int PTX, PTY, CLK, PTEST, IN;
size_t n = 0;
size_t i;
uint32_t U1, U2, *A, *B, *C, *D;
uint32_t PT1, PT2, *WALK, RES[16];
uint32_t PTX, PTY, CLK, PTEST, IN;

WALK = hs->WALK;
PT1 = hs->PT1;
Expand Down Expand Up @@ -212,16 +214,16 @@ void mbedtls_havege_free( mbedtls_havege_state *hs )
*/
int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
{
int val;
uint32_t val;
size_t use_len;
mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
unsigned char *p = buf;

while( len > 0 )
{
use_len = len;
if( use_len > sizeof(int) )
use_len = sizeof(int);
if( use_len > sizeof( val ) )
use_len = sizeof( val );

if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
havege_fill( hs );
Expand Down