WSUTIL: create phtole32 and 64 functions

Change-Id: I15c3c40665ccab1e60057837ffce5bae50d1b52c
Reviewed-on: https://code.wireshark.org/review/28567
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Julien Staub 2018-07-02 14:34:28 +02:00 committed by Peter Wu
parent 53e04b621c
commit 4572b77148
3 changed files with 28 additions and 24 deletions

View File

@ -21,6 +21,7 @@
#include <epan/prefs.h>
#include <epan/uat.h>
#include <wsutil/bits_ctz.h>
#include <wsutil/pint.h>
#include "packet-zbee.h"
#include "packet-zbee-nwk.h"
#include "packet-zbee-security.h"
@ -1564,19 +1565,11 @@ zbee_gp_make_nonce(zbee_nwk_green_power_packet *packet, gchar *nonce)
{
memset(nonce, 0, ZBEE_SEC_CONST_NONCE_LEN);
if (packet->direction == ZBEE_NWK_GP_FC_EXT_DIRECTION_FROM_ZGPD) {
nonce[0] = (guint8)((packet->source_id) & 0xff);
nonce[1] = (guint8)((packet->source_id) >> 8 & 0xff);
nonce[2] = (guint8)((packet->source_id) >> 16 & 0xff);
nonce[3] = (guint8)((packet->source_id) >> 24 & 0xff);
phtole32(nonce, packet->source_id);
}
nonce[4] = (guint8)((packet->source_id) & 0xff);
nonce[5] = (guint8)((packet->source_id) >> 8 & 0xff);
nonce[6] = (guint8)((packet->source_id) >> 16 & 0xff);
nonce[7] = (guint8)((packet->source_id) >> 24 & 0xff);
nonce[8] = (guint8)((packet->security_frame_counter) & 0xff);
nonce[9] = (guint8)((packet->security_frame_counter) >> 8 & 0xff);
nonce[10] = (guint8)((packet->security_frame_counter) >> 16 & 0xff);
nonce[11] = (guint8)((packet->security_frame_counter) >> 24 & 0xff);
phtole32(nonce+4, packet->source_id);
phtole32(nonce+8, packet->security_frame_counter);
if ((packet->application_id == ZBEE_NWK_GP_APP_ID_ZGP) && (packet->direction !=
ZBEE_NWK_GP_FC_EXT_DIRECTION_FROM_ZGPD)) {
nonce[12] = (gchar)0xa3;

View File

@ -26,6 +26,7 @@
* we can do is parse the security header and give up.
*/
#include <wsutil/wsgcrypt.h>
#include <wsutil/pint.h>
#include "packet-ieee802154.h"
#include "packet-zbee.h"
@ -825,19 +826,11 @@ static void
zbee_sec_make_nonce(zbee_security_packet *packet, guint8 *nonce)
{
/* First 8 bytes are the extended source address (little endian). */
*(nonce++) = (guint8)((packet->src64)>>0 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>8 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>16 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>24 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>32 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>40 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>48 & 0xff);
*(nonce++) = (guint8)((packet->src64)>>56 & 0xff);
phtole64(nonce, packet->src64);
nonce += 8;
/* Next 4 bytes are the frame counter (little endian). */
*(nonce++) = (guint8)((packet->counter)>>0 & 0xff);
*(nonce++) = (guint8)((packet->counter)>>8 & 0xff);
*(nonce++) = (guint8)((packet->counter)>>16 & 0xff);
*(nonce++) = (guint8)((packet->counter)>>24 & 0xff);
phtole32(nonce, packet->counter);
nonce += 4;
/* Next byte is the security control field. */
*(nonce) = packet->control;
} /* zbee_sec_make_nonce */

View File

@ -135,6 +135,24 @@ static inline void phton64(guint8 *p, guint64 v) {
p[7] = (guint8)(v >> 0);
}
static inline void phtole32(guint8 *p, guint32 v) {
p[0] = (guint8)(v >> 0);
p[1] = (guint8)(v >> 8);
p[2] = (guint8)(v >> 16);
p[3] = (guint8)(v >> 24);
}
static inline void phtole64(guint8 *p, guint64 v) {
p[0] = (guint8)(v >> 0);
p[1] = (guint8)(v >> 8);
p[2] = (guint8)(v >> 16);
p[3] = (guint8)(v >> 24);
p[4] = (guint8)(v >> 32);
p[5] = (guint8)(v >> 40);
p[6] = (guint8)(v >> 48);
p[7] = (guint8)(v >> 56);
}
/* Subtract two guint32s with respect to wraparound */
#define guint32_wraparound_diff(higher, lower) ((higher>lower)?(higher-lower):(higher+0xffffffff-lower+1))