From 1807a48fc19dd5c230711ee5cd1a4cff40a7dbae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20B=C3=BChler?= Date: Thu, 18 Jan 2024 21:00:55 +0100 Subject: [PATCH] NO_64BIT_MATH It is now required that there be 64bit integer support, this should not be a problem with stdint.h and C99. #686 --- crypto/include/datatypes.h | 14 +--- crypto/include/integers.h | 10 +-- crypto/kernel/key.c | 28 +------ crypto/replay/rdbx.c | 51 +----------- crypto/test/env.c | 6 -- srtp/srtp.c | 154 +++---------------------------------- 6 files changed, 18 insertions(+), 245 deletions(-) diff --git a/crypto/include/datatypes.h b/crypto/include/datatypes.h index 3cd35b2da..4510cc870 100644 --- a/crypto/include/datatypes.h +++ b/crypto/include/datatypes.h @@ -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__) */ @@ -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; } diff --git a/crypto/include/integers.h b/crypto/include/integers.h index 3968cd56b..01136d631 100644 --- a/crypto/include/integers.h +++ b/crypto/include/integers.h @@ -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 @@ -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 diff --git a/crypto/kernel/key.c b/crypto/kernel/key.c index 5e6a18872..937ea38de 100644 --- a/crypto/kernel/key.c +++ b/crypto/kernel/key.c @@ -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; @@ -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; } diff --git a/crypto/replay/rdbx.c b/crypto/replay/rdbx.c index 1d2d8285a..36d50ed46 100644 --- a/crypto/replay/rdbx.c +++ b/crypto/replay/rdbx.c @@ -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 } /* @@ -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; @@ -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; } @@ -208,10 +189,6 @@ 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; @@ -219,7 +196,6 @@ srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc) rdbx->index &= 0xffff; /* retain lowest 16 bits */ rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */ -#endif return srtp_err_status_ok; } @@ -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 } /* @@ -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; } @@ -366,10 +324,6 @@ 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; @@ -377,7 +331,6 @@ srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx, rdbx->index = seq; rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */ -#endif bitvector_set_to_zero(&rdbx->bitmask); diff --git a/crypto/test/env.c b/crypto/test/env.c index 46f8b8f5d..0dfa88f69 100644 --- a/crypto/test/env.c +++ b/crypto/test/env.c @@ -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 diff --git a/srtp/srtp.c b/srtp/srtp.c index a4d0818fc..e4d004b88 100644 --- a/srtp/srtp.c +++ b/srtp/srtp.c @@ -977,16 +977,8 @@ srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp, */ session_keys = &srtp->session_keys[current_mki_index]; -/* initialize key limit to maximum value */ -#ifdef NO_64BIT_MATH - { - uint64_t temp; - temp = make64(UINT_MAX, UINT_MAX); - srtp_key_limit_set(session_keys->limit, temp); - } -#else + /* initialize key limit to maximum value */ srtp_key_limit_set(session_keys->limit, 0xffffffffffffLL); -#endif if (master_key->mki_size != 0) { session_keys->mki_id = srtp_crypto_alloc(master_key->mki_size); @@ -1614,13 +1606,8 @@ static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys, v128_t in; v128_t salt; -#ifdef NO_64BIT_MATH - uint32_t local_roc = ((high32(*seq) << 16) | (low32(*seq) >> 16)); - uint16_t local_seq = (uint16_t)(low32(*seq)); -#else uint32_t local_roc = (uint32_t)(*seq >> 16); uint16_t local_seq = (uint16_t)*seq; -#endif memset(&in, 0, sizeof(v128_t)); memset(&salt, 0, sizeof(v128_t)); @@ -1694,69 +1681,19 @@ static srtp_err_status_t srtp_estimate_index(srtp_rdbx_t *rdbx, srtp_sequence_number_t seq, ssize_t *delta) { -#ifdef NO_64BIT_MATH - uint32_t internal_pkt_idx_reduced; - uint32_t external_pkt_idx_reduced; - uint32_t internal_roc; - uint32_t roc_difference; -#endif - -#ifdef NO_64BIT_MATH - *est = (srtp_xtd_seq_num_t)make64(roc >> 16, (roc << 16) | seq); - *delta = low32(est) - rdbx->index; -#else *est = (srtp_xtd_seq_num_t)(((uint64_t)roc) << 16) | seq; *delta = *est - rdbx->index; -#endif if (*est > rdbx->index) { -#ifdef NO_64BIT_MATH - internal_roc = (uint32_t)(rdbx->index >> 16); - roc_difference = roc - internal_roc; - if (roc_difference > 1) { - *delta = 0; - return srtp_err_status_pkt_idx_adv; - } - - internal_pkt_idx_reduced = (uint32_t)(rdbx->index & 0xFFFF); - external_pkt_idx_reduced = (uint32_t)((roc_difference << 16) | seq); - - if (external_pkt_idx_reduced - internal_pkt_idx_reduced > - seq_num_median) { - *delta = 0; - return srtp_err_status_pkt_idx_adv; - } -#else if (*est - rdbx->index > seq_num_median) { *delta = 0; return srtp_err_status_pkt_idx_adv; } -#endif } else if (*est < rdbx->index) { -#ifdef NO_64BIT_MATH - - internal_roc = (uint32_t)(rdbx->index >> 16); - roc_difference = internal_roc - roc; - if (roc_difference > 1) { - *delta = 0; - return srtp_err_status_pkt_idx_adv; - } - - internal_pkt_idx_reduced = - (uint32_t)((roc_difference << 16) | rdbx->index & 0xFFFF); - external_pkt_idx_reduced = (uint32_t)(seq); - - if (internal_pkt_idx_reduced - external_pkt_idx_reduced > - seq_num_median) { - *delta = 0; - return srtp_err_status_pkt_idx_old; - } -#else if (rdbx->index - *est > seq_num_median) { *delta = 0; return srtp_err_status_pkt_idx_old; } -#endif } return srtp_err_status_ok; @@ -1778,12 +1715,8 @@ static srtp_err_status_t srtp_get_est_pkt_index(const srtp_hdr_t *hdr, srtp_rdbx_estimate_index(&stream->rtp_rdbx, est, ntohs(hdr->seq)); } -#ifdef NO_64BIT_MATH - debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(*est), - low32(*est)); -#else debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, *est); -#endif + return result; } @@ -1877,24 +1810,14 @@ static srtp_err_status_t srtp_protect_aead(srtp_ctx_t *ctx, srtp_rdbx_add_index(&stream->rtp_rdbx, delta); } -#ifdef NO_64BIT_MATH - debug_print2(mod_srtp, "estimated packet index: %08x%08x", high32(est), - low32(est)); -#else debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est); -#endif /* * AEAD uses a new IV formation method */ srtp_calc_aead_iv(session_keys, &iv, &est, hdr); -/* shift est, put into network byte order */ -#ifdef NO_64BIT_MATH - est = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else + /* shift est, put into network byte order */ est = be64_to_cpu(est << 16); -#endif status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, srtp_direction_encrypt); @@ -1984,12 +1907,7 @@ static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx, debug_print0(mod_srtp, "function srtp_unprotect_aead"); -#ifdef NO_64BIT_MATH - debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), - low32(est)); -#else debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est); -#endif /* get tag length from stream */ tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); @@ -2003,12 +1921,7 @@ static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx, if (!status && session_keys->rtp_xtn_hdr_cipher) { iv.v32[0] = 0; iv.v32[1] = hdr->ssrc; -#ifdef NO_64BIT_MATH - iv.v64[1] = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else iv.v64[1] = be64_to_cpu(est << 16); -#endif status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, (uint8_t *)&iv, srtp_direction_encrypt); } @@ -2360,12 +2273,7 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx, srtp_rdbx_add_index(&stream->rtp_rdbx, delta); } -#ifdef NO_64BIT_MATH - debug_print2(mod_srtp, "estimated packet index: %08x%08x", high32(est), - low32(est)); -#else debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est); -#endif /* * if we're using rindael counter mode, set nonce and seq @@ -2377,12 +2285,7 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx, iv.v32[0] = 0; iv.v32[1] = hdr->ssrc; -#ifdef NO_64BIT_MATH - iv.v64[1] = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else iv.v64[1] = be64_to_cpu(est << 16); -#endif status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, srtp_direction_encrypt); if (!status && session_keys->rtp_xtn_hdr_cipher) { @@ -2392,13 +2295,8 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx, } else { v128_t iv; -/* otherwise, set the index to est */ -#ifdef NO_64BIT_MATH - iv.v32[0] = 0; - iv.v32[1] = 0; -#else + /* otherwise, set the index to est */ iv.v64[0] = 0; -#endif iv.v64[1] = be64_to_cpu(est); status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, srtp_direction_encrypt); @@ -2411,13 +2309,8 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx, return srtp_err_status_cipher_fail; } -/* shift est, put into network byte order */ -#ifdef NO_64BIT_MATH - est = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else + /* shift est, put into network byte order */ est = be64_to_cpu(est << 16); -#endif /* * if we're authenticating using a universal hash, put the keystream @@ -2555,17 +2448,12 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx, debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)", ntohl(hdr->ssrc)); -/* - * set estimated packet index to sequence number from header, - * and set delta equal to the same value - */ -#ifdef NO_64BIT_MATH - est = (srtp_xtd_seq_num_t)make64(0, ntohs(hdr->seq)); - delta = low32(est); -#else + /* + * set estimated packet index to sequence number from header, + * and set delta equal to the same value + */ est = (srtp_xtd_seq_num_t)ntohs(hdr->seq); delta = (int)est; -#endif } else { /* * no stream corresponding to SSRC found, and we don't do @@ -2595,12 +2483,7 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx, } } -#ifdef NO_64BIT_MATH - debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), - low32(est)); -#else debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est); -#endif /* Determine if MKI is being used and what session keys should be used */ if (use_mki) { @@ -2638,12 +2521,7 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx, /* aes counter mode */ iv.v32[0] = 0; iv.v32[1] = hdr->ssrc; /* still in network order */ -#ifdef NO_64BIT_MATH - iv.v64[1] = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else iv.v64[1] = be64_to_cpu(est << 16); -#endif status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, srtp_direction_decrypt); if (!status && session_keys->rtp_xtn_hdr_cipher) { @@ -2651,13 +2529,8 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx, (uint8_t *)&iv, srtp_direction_decrypt); } } else { -/* no particular format - set the iv to the pakcet index */ -#ifdef NO_64BIT_MATH - iv.v32[0] = 0; - iv.v32[1] = 0; -#else + /* no particular format - set the iv to the packet index */ iv.v64[0] = 0; -#endif iv.v64[1] = be64_to_cpu(est); status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, srtp_direction_decrypt); @@ -2670,13 +2543,8 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx, return srtp_err_status_cipher_fail; } -/* shift est, put into network byte order */ -#ifdef NO_64BIT_MATH - est = be64_to_cpu( - make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16)); -#else + /* shift est, put into network byte order */ est = be64_to_cpu(est << 16); -#endif /* * find starting point for decryption and length of data to be