1

I am using the Oscilloscope component to read sensor data. I'm sending this data to a basestation mote which is sending the data to a laptop.

I want to implement AES encryption on the sensor data using the components provided here: http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x-contrib/crypto/index.html

My plan is to reduce the data buffer from 10 to 8 in Oscilloscope.h. This changes the total data size to 128 bits (the same as a block size in AES). Now the only problem is each array element in the AES block is a unit_8, but the array elements of the data buffer are uint_16.

Is there anyway I can 'split' a uint_16 value to the upper and lower bits? I would then store them as 2 elements in my AES block array.

To clarify, I have a uint_16 which corresponds to some data read by the sensor. I want to be able to store this value in 2 uint_8 values. I will then encrypt the data block, send the packet to the basestation, and the basestation will decrypt and combine the values to the original uint_16.

user1754045
  • 175
  • 1
  • 6
  • 15

1 Answers1

3

Sure, just store the least significant eight bits in one variable, and the most significant eight in the other

unit8_t low = value & 0xFF;
uint8_t high = (value >> 8) & 0xFF;

(the bitwise and is superfluous in both cases since the conversion to uint8_t is specified to do the same).

To retrieve the uint16_t value from the parts, you need a shift and a bitwise or,

uint16_t reassembled = ((uint16_t)high << 8) | low;

Since the integer promotions are performed on the arguments of the shift operator, the cast to uint16_t is not necessary if int is more than 16 bits wide, but if int is exactly 16 bits wide, the shift can lead to undefined behaviour because the value high*256 need not be representable as an int then, so to be safe, a cast is needed before the shift. If int is 16 bits wide, the uint16_t obtained from the cast will then be converted to an unsigned int - unless you have a perverse implementation where the width of unsigned int is smaller than that of int - otherwise to int, but that's wide enough then.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • To combine them would I just reverse the operations? I would do a left shift on high then bitwise and both values? – user1754045 Dec 01 '12 at 14:47