diff options
Diffstat (limited to 'src/ao_radio_cmac.c')
| -rw-r--r-- | src/ao_radio_cmac.c | 278 | 
1 files changed, 203 insertions, 75 deletions
diff --git a/src/ao_radio_cmac.c b/src/ao_radio_cmac.c index 7648a2f5..9694b5b3 100644 --- a/src/ao_radio_cmac.c +++ b/src/ao_radio_cmac.c @@ -17,27 +17,24 @@  #include "ao.h" -#define AO_CMAC_KEY_LEN		16 +#define AO_CMAC_KEY_LEN		AO_AES_LEN  #define AO_CMAC_MAX_LEN		(128 - AO_CMAC_KEY_LEN) -static __xdata uint8_t cmac_key[AO_CMAC_KEY_LEN]; +static __xdata uint8_t ao_radio_cmac_mutex;  static __xdata uint8_t cmac_data[AO_CMAC_MAX_LEN + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN];  static __pdata uint8_t ao_radio_cmac_len;  static uint8_t  getnibble(void)  { -	__pdata char	c; - -	c = getchar(); -	if ('0' <= c && c <= '9') -		return c - '0'; -	if ('a' <= c && c <= 'f') -		return c - ('a' - 10); -	if ('A' <= c && c <= 'F') -		return c - ('A' - 10); -	ao_cmd_status = ao_cmd_lex_error; -	return 0; +	int8_t	b; + +	b = ao_cmd_hexchar(getchar()); +	if (b < 0) { +		ao_cmd_status = ao_cmd_lex_error; +		return 0; +	} +	return (uint8_t) b;  }  static uint8_t @@ -49,20 +46,137 @@ getbyte(void)  	return b;  } +static uint8_t +round_len(uint8_t len) +{ +	uint8_t	rem; + +	/* Make sure we transfer at least one packet, and +	 * then make sure every packet is full. Note that +	 * there is no length encoded, and that the receiver +	 * must deal with any extra bytes in the packet +	 */ +	if (len < AO_CMAC_KEY_LEN) +		len = AO_CMAC_KEY_LEN; +	rem = len % AO_CMAC_KEY_LEN; +	if (rem != 0) +		len += (AO_CMAC_KEY_LEN - rem); +	return len; +} + +/* + * Sign and deliver the data sitting in the cmac buffer + */  static void -ao_radio_cmac_key(void) __reentrant +radio_cmac_send(uint8_t len) __reentrant  {  	uint8_t	i; -	for (i = 0; i < AO_CMAC_KEY_LEN; i++) { -		cmac_key[i] = getbyte(); -		if (ao_cmd_status != ao_cmd_success) -			return; +	len = round_len(len); +	/* Make sure the AES key is loaded */ +	ao_config_get(); + +#if HAS_MONITOR +	ao_set_monitor(0); +#endif + +	ao_mutex_get(&ao_aes_mutex); +	ao_aes_set_mode(ao_aes_mode_cbc_mac); +	ao_aes_set_key(ao_config.aes_key); +	ao_aes_zero_iv(); +	for (i = 0; i < len; i += AO_CMAC_KEY_LEN) { +		if (i + AO_CMAC_KEY_LEN < len) +			ao_aes_run(&cmac_data[i], NULL); +		else +			ao_aes_run(&cmac_data[i], &cmac_data[len]);  	} +	ao_mutex_put(&ao_aes_mutex); + +	ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN); +} + +/* + * Receive and validate an incoming packet + */ + +static int8_t +radio_cmac_recv(uint8_t len, uint16_t timeout) __reentrant +{ +	uint8_t	i; + +	len = round_len(len); +#if HAS_MONITOR +	ao_set_monitor(0); +#endif +	if (timeout) +		ao_alarm(timeout); + +	i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2); +	ao_clear_alarm(); + +	if (!i) +		return AO_RADIO_CMAC_TIMEOUT; + +	if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & PKT_APPEND_STATUS_1_CRC_OK)) +		return AO_RADIO_CMAC_CRC_ERROR; + +	ao_config_get(); + +	/* Compute the packet signature +	 */ +	ao_mutex_get(&ao_aes_mutex); +	ao_aes_set_mode(ao_aes_mode_cbc_mac); +	ao_aes_set_key(ao_config.aes_key); +	ao_aes_zero_iv(); +	for (i = 0; i < len; i += AO_CMAC_KEY_LEN) { +		if (i + AO_CMAC_KEY_LEN < len) +			ao_aes_run(&cmac_data[i], NULL); +		else +			ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]); +	} +	ao_mutex_put(&ao_aes_mutex); + +	/* Check the packet signature against the signature provided +	 * over the link +	 */ +	  +	if (memcmp(&cmac_data[len], +		   &cmac_data[len + AO_CMAC_KEY_LEN + 2], +		   AO_CMAC_KEY_LEN) != 0) { +		return AO_RADIO_CMAC_MAC_ERROR; +	} + +	return AO_RADIO_CMAC_OK; +} + +int8_t +ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant +{ +	if (len > AO_CMAC_MAX_LEN) +		return AO_RADIO_CMAC_LEN_ERROR; +	ao_mutex_get(&ao_radio_cmac_mutex); +	memcpy(cmac_data, packet, len); +	radio_cmac_send(len); +	ao_mutex_put(&ao_radio_cmac_mutex); +	return AO_RADIO_CMAC_OK; +} + +int8_t +ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant +{ +	uint8_t	i; +	if (len > AO_CMAC_MAX_LEN) +		return AO_RADIO_CMAC_LEN_ERROR; +	ao_mutex_get(&ao_radio_cmac_mutex); +	i = radio_cmac_recv(len, timeout); +	if (i == AO_RADIO_CMAC_OK) +		memcpy(packet, cmac_data, len); +	ao_mutex_put(&ao_radio_cmac_mutex); +	return i;  }  static void -ao_radio_cmac_send(void) __reentrant +radio_cmac_send_cmd(void) __reentrant  {  	uint8_t	i;  	uint8_t	len; @@ -70,43 +184,25 @@ ao_radio_cmac_send(void) __reentrant  	ao_cmd_decimal();  	if (ao_cmd_status != ao_cmd_success)  		return; -	if (ao_cmd_lex_i < AO_CMAC_KEY_LEN || -	    ao_cmd_lex_i > AO_CMAC_MAX_LEN || -	    ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0) -	{ +	len = ao_cmd_lex_i; +	if (len > AO_CMAC_MAX_LEN) {  		ao_cmd_status = ao_cmd_syntax_error;  		return;  	}  	flush(); +	ao_mutex_get(&ao_radio_cmac_mutex);  	len = ao_cmd_lex_i;  	for (i = 0; i < len; i++) {  		cmac_data[i] = getbyte();  		if (ao_cmd_status != ao_cmd_success)  			return;  	} -	ao_mutex_get(&ao_aes_mutex); -	ao_aes_set_mode(ao_aes_mode_cbc_mac); -	ao_aes_set_key(cmac_key); -	ao_aes_zero_iv(); -	for (i = 0; i < len; i += AO_CMAC_KEY_LEN) { -		if (i + AO_CMAC_KEY_LEN < len) -			ao_aes_run(&cmac_data[i], NULL); -		else -			ao_aes_run(&cmac_data[i], &cmac_data[len]); -	} -	ao_mutex_put(&ao_aes_mutex); -#if HAS_MONITOR -	ao_set_monitor(0); -#endif -	printf("send:"); -	for (i = 0; i < len + AO_CMAC_KEY_LEN; i++) -		printf(" %02x", cmac_data[i]); -	printf("\n"); flush(); -	ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN); +	radio_cmac_send(len); +	ao_mutex_put(&ao_radio_cmac_mutex);  }  static void -ao_radio_cmac_recv(void) __reentrant +radio_cmac_recv_cmd(void) __reentrant  {  	uint8_t		len, i;  	uint16_t	timeout; @@ -114,47 +210,79 @@ ao_radio_cmac_recv(void) __reentrant  	ao_cmd_decimal();  	if (ao_cmd_status != ao_cmd_success)  		return; -	if (ao_cmd_lex_i < AO_CMAC_KEY_LEN || -	    ao_cmd_lex_i > AO_CMAC_MAX_LEN || -	    ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0) -	{ -		ao_cmd_status = ao_cmd_syntax_error; -		return; -	}  	len = ao_cmd_lex_i;  	ao_cmd_decimal();  	if (ao_cmd_status != ao_cmd_success)  		return; -	timeout = ao_cmd_lex_i; -#if HAS_MONITOR -	ao_set_monitor(0); -#endif -	if (timeout) -		ao_alarm(timeout); -	if (!ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2)) { -		printf("timeout\n"); +	timeout = AO_MS_TO_TICKS(ao_cmd_lex_i); +	ao_mutex_get(&ao_radio_cmac_mutex); +	i = radio_cmac_recv(len, timeout); +	if (i == AO_RADIO_CMAC_OK) { +		printf ("PACKET "); +		for (i = 0; i < len; i++) +			printf("%02x", cmac_data[i]); +		printf ("\n"); +	} else +		printf ("ERROR %d\n", i); +	ao_mutex_put(&ao_radio_cmac_mutex); +} + +static void +launch_report_cmd(void) __reentrant +{ +	static __xdata struct ao_launch_command	command; +	static __xdata struct ao_launch_query	query; +	uint8_t		channel; +	uint16_t	serial; +	uint8_t		i; + +	ao_cmd_decimal(); +	serial = ao_cmd_lex_i; +	ao_cmd_decimal(); +	channel = ao_cmd_lex_i; +	if (ao_cmd_status != ao_cmd_success)  		return; +	flush(); +	for (i = 0; i < 10; i++) { +		printf ("."); flush(); +		command.tick = 0; +		command.serial = serial; +		command.cmd = AO_LAUNCH_QUERY; +		command.channel = channel; +		ao_radio_cmac_send(&command, sizeof (command)); +		switch (ao_radio_cmac_recv(&query, sizeof (query), AO_MS_TO_TICKS(500))) { +		case AO_RADIO_CMAC_OK: +			printf("\n"); +			switch (query.arm_status) { +			case ao_igniter_ready: +			case ao_igniter_active: +				printf ("Armed: "); +				switch (query.igniter_status) { +				default: +					printf("unknown status\n"); +					break; +				case ao_igniter_ready: +					printf("igniter good\n"); +					break; +				case ao_igniter_open: +					printf("igniter bad\n"); +					break; +				} +			default: +				printf("Disarmed\n"); +			} +			return; +		default: +			continue; +		}  	} -	ao_mutex_get(&ao_aes_mutex); -	ao_aes_set_mode(ao_aes_mode_cbc_mac); -	ao_aes_set_key(cmac_key); -	ao_aes_zero_iv(); -	for (i = 0; i < len; i += AO_CMAC_KEY_LEN) { -		if (i + AO_CMAC_KEY_LEN < len) -			ao_aes_run(&cmac_data[i], NULL); -		else -			ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]); -	} -	printf ("PACKET "); -	for (i = 0; i < len + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN; i++) -		printf("%02x", cmac_data[i]); -	printf ("\n"); +	printf ("Timeout\n");  }  static __code struct ao_cmds ao_radio_cmac_cmds[] = { -	{ ao_radio_cmac_key,	"k\0Set AES-CMAC key. 16 key bytes follow on next line" }, -	{ ao_radio_cmac_send,	"s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" }, -	{ ao_radio_cmac_recv,	"S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" }, +	{ radio_cmac_send_cmd,	"s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" }, +	{ radio_cmac_recv_cmd,	"S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" }, +	{ launch_report_cmd,    "l <serial> <channel>\0Get remote launch status" },  	{ 0, NULL },  };  | 
