Skip to content
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

NO_64BIT_MATH #689

Merged
merged 1 commit into from
Jan 23, 2024
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
14 changes: 2 additions & 12 deletions crypto/include/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,8 @@ void v128_left_shift(v128_t *x, size_t shift_index);
(z)->v32[2] = (x)->v32[2] ^ (y)->v32[2], \
(z)->v32[3] = (x)->v32[3] ^ (y)->v32[3])

/* ok for NO_64BIT_MATH if it can compare uint64_t's (even as structures) */
#ifdef NO_64BIT_MATH
#define v128_xor_eq(z, x) \
((z)->v32[0] ^= (x)->v32[0], (z)->v32[1] ^= (x)->v32[1], \
(z)->v32[2] ^= (x)->v32[2], (z)->v32[3] ^= (x)->v32[3])
#else
#define v128_xor_eq(z, x) \
((z)->v64[0] ^= (x)->v64[0], (z)->v64[1] ^= (x)->v64[1])
#endif

#endif /* defined(__SSE2__) */

Expand Down Expand Up @@ -224,14 +217,11 @@ static inline uint64_t be64_to_cpu(uint64_t v)
{
#if defined(__GNUC__)
v = __builtin_bswap64(v);
#elif defined(NO_64BIT_MATH)
/* use the make64 functions to do 64-bit math */
v = make64(htonl(low32(v)), htonl(high32(v)));
#else /* NO_64BIT_MATH */
#else
/* use the native 64-bit math */
v = (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) |
(((uint64_t)be32_to_cpu((uint32_t)v)) << 32));
#endif /* NO_64BIT_MATH */
#endif
return v;
}

Expand Down
10 changes: 1 addition & 9 deletions crypto/include/integers.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef unsigned long uint64_t;
#elif SIZEOF_UNSIGNED_LONG_LONG == 8
typedef unsigned long long uint64_t;
#else
#define NO_64BIT_MATH 1
#error "NO_64BIT_MATH"
#endif
#endif

Expand All @@ -96,14 +96,6 @@ typedef unsigned int uint32_t;
typedef int int32_t;
#endif

#if defined(NO_64BIT_MATH) && defined(HAVE_CONFIG_H)
typedef double uint64_t;
/* assert that sizeof(double) == 8 */
extern uint64_t make64(uint32_t high, uint32_t low);
extern uint32_t high32(uint64_t value);
extern uint32_t low32(uint64_t value);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
28 changes: 2 additions & 26 deletions crypto/kernel/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,9 @@
srtp_err_status_t srtp_key_limit_set(srtp_key_limit_t key,
const srtp_xtd_seq_num_t s)
{
#ifdef NO_64BIT_MATH
if (high32(s) == 0 && low32(s) < soft_limit) {
return srtp_err_status_bad_param;
}
#else
if (s < soft_limit) {
return srtp_err_status_bad_param;
}
#endif
key->num_left = s;
key->state = srtp_key_state_normal;
return srtp_err_status_ok;
Expand All @@ -79,34 +73,16 @@ srtp_err_status_t srtp_key_limit_clone(srtp_key_limit_t original,

srtp_key_event_t srtp_key_limit_update(srtp_key_limit_t key)
{
#ifdef NO_64BIT_MATH
if (low32(key->num_left) == 0) {
// carry
key->num_left =
make64(high32(key->num_left) - 1, low32(key->num_left) - 1);
} else {
// no carry
key->num_left = make64(high32(key->num_left), low32(key->num_left) - 1);
}
if (high32(key->num_left) != 0 || low32(key->num_left) >= soft_limit) {
return srtp_key_event_normal; /* we're above the soft limit */
}
#else
key->num_left--;
if (key->num_left >= soft_limit) {
return srtp_key_event_normal; /* we're above the soft limit */
}
#endif
if (key->state == srtp_key_state_normal) {
/* we just passed the soft limit, so change the state */
key->state = srtp_key_state_past_soft_limit;
}
#ifdef NO_64BIT_MATH
if (low32(key->num_left) == 0 && high32(key->num_left == 0))
#else
if (key->num_left < 1)
#endif
{ /* we just hit the hard limit */
if (key->num_left < 1) {
/* we just hit the hard limit */
key->state = srtp_key_state_expired;
return srtp_key_event_hard_limit;
}
Expand Down
51 changes: 2 additions & 49 deletions crypto/replay/rdbx.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,12 @@

void srtp_index_init(srtp_xtd_seq_num_t *pi)
{
#ifdef NO_64BIT_MATH
*pi = make64(0, 0);
#else
*pi = 0;
#endif
}

void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s)
{
#ifdef NO_64BIT_MATH
/* a > ~b means a+b will generate a carry */
/* s is uint16 here */
*pi = make64(high32(*pi) + (s > ~low32(*pi) ? 1 : 0), low32(*pi) + s);
#else
*pi += s;
#endif
}

/*
Expand All @@ -123,13 +113,8 @@ ssize_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
srtp_xtd_seq_num_t *guess,
srtp_sequence_number_t s)
{
#ifdef NO_64BIT_MATH
uint32_t local_roc = ((high32(*local) << 16) | (low32(*local) >> 16));
uint16_t local_seq = (uint16_t)(low32(*local));
#else
uint32_t local_roc = (uint32_t)(*local >> 16);
uint16_t local_seq = (uint16_t)*local;
#endif
uint32_t guess_roc;
uint16_t guess_seq;
ssize_t difference;
Expand All @@ -153,12 +138,8 @@ ssize_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
}
guess_seq = s;

/* Note: guess_roc is 32 bits, so this generates a 48-bit result! */
#ifdef NO_64BIT_MATH
*guess = make64(guess_roc >> 16, (guess_roc << 16) | guess_seq);
#else
/* Note: guess_roc is 32 bits, so this generates a 48-bit result! */
*guess = (((uint64_t)guess_roc) << 16) | guess_seq;
#endif

return difference;
}
Expand Down Expand Up @@ -208,18 +189,13 @@ srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc)
{
bitvector_set_to_zero(&rdbx->bitmask);

#ifdef NO_64BIT_MATH
#error not yet implemented
#else

/* make sure that we're not moving backwards */
if (roc < (rdbx->index >> 16)) {
return srtp_err_status_replay_old;
}

rdbx->index &= 0xffff; /* retain lowest 16 bits */
rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
#endif

return srtp_err_status_ok;
}
Expand Down Expand Up @@ -313,27 +289,13 @@ ssize_t srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx,
* 0xffffffff)
*/

#ifdef NO_64BIT_MATH
/* seq_num_median = 0x8000 */
if (high32(rdbx->index) > 0 || low32(rdbx->index) > seq_num_median)
#else
if (rdbx->index > seq_num_median)
#endif
{
if (rdbx->index > seq_num_median) {
return srtp_index_guess(&rdbx->index, guess, s);
}

#ifdef NO_64BIT_MATH
*guess = make64(0, (uint32_t)s);
#else
*guess = s;
#endif

#ifdef NO_64BIT_MATH
return s - low32(rdbx->index);
#else
return s - rdbx->index;
#endif
}

/*
Expand All @@ -346,11 +308,7 @@ uint32_t srtp_rdbx_get_roc(const srtp_rdbx_t *rdbx)
{
uint32_t roc;

#ifdef NO_64BIT_MATH
roc = ((high32(rdbx->index) << 16) | (low32(rdbx->index) >> 16));
#else
roc = (uint32_t)(rdbx->index >> 16);
#endif

return roc;
}
Expand All @@ -366,18 +324,13 @@ srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx,
uint32_t roc,
uint16_t seq)
{
#ifdef NO_64BIT_MATH
#error not yet implemented
#else

/* make sure that we're not moving backwards */
if (roc < (rdbx->index >> 16)) {
return srtp_err_status_replay_old;
}

rdbx->index = seq;
rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
#endif

bitvector_set_to_zero(&rdbx->bitmask);

Expand Down
6 changes: 0 additions & 6 deletions crypto/test/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ int main(void)
printf("CPU set to ALTIVEC\t\t\t\t(CPU_ALTIVEC == 0)\n");
#endif

#ifndef NO_64BIT_MATH
printf("using native 64-bit type\t\t(NO_64_BIT_MATH == 0)\n");
#else
printf("using built-in 64-bit math\t\t(NO_64_BIT_MATH == 1)\n");
#endif

#ifdef ERR_REPORTING_STDOUT
printf("using stdout for error reporting\t(ERR_REPORTING_STDOUT == 1)\n");
#endif
Expand Down
Loading
Loading