Libu2f-emu  0.0.0
Universal 2nd Factor (U2F) Emulation C Library
packet.h
Go to the documentation of this file.
1 #ifndef PACKET_H
2 #define PACKET_H
3 
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 
8 
9 /* bit macro */
10 #define BIT(x) (1 << (x))
11 
12 /* Packed macro */
13 #define __packed __attribute__((__packed__))
14 
15 /* Packet general size */
16 #define PACKET_SIZE 64
17 
18 /* Init packet specific sizes */
19 #define PACKET_INIT_HEADER_SIZE 7
20 #define PACKET_INIT_DATA_SIZE (PACKET_SIZE - PACKET_INIT_HEADER_SIZE)
21 
22 /* Cont packet specific sizes */
23 #define PACKET_CONT_HEADER_SIZE 5
24 #define PACKET_CONT_DATA_SIZE (PACKET_SIZE - PACKET_CONT_HEADER_SIZE)
25 #define PACKET_CONT_MAX_SEQ (BIT(7) - 1)
26 
27 /* Broadcast channels */
28 #define BROADCAST_CID 0xFFFFFFFF
29 
34 {
35  uint32_t cid;
36  uint8_t cmd;
37  uint8_t bcnth;
38  uint8_t bcntl;
40 } __packed;
41 
46 {
47  uint32_t cid;
48  uint8_t seq;
50 } __packed;
51 
58 static inline uint32_t packet_get_cid(const void *packet)
59 {
60  return *((uint32_t *)packet);
61 }
62 
69 static inline bool packet_is_init(const void *packet)
70 {
71  return ((uint8_t *)packet)[4] & (1 << 7);
72 }
73 
80 static inline uint16_t packet_init_get_bcnt(
81  const struct packet_init *init_packet)
82 {
83  uint16_t bcnt = 0;
84  bcnt |= init_packet->bcnth << 8;
85  bcnt |= init_packet->bcntl;
86 
87  return bcnt;
88 }
89 
96 static inline void packet_init_set_bcnt(
97  struct packet_init *init_packet, uint16_t bcnt)
98 {
99  /* High */
100  init_packet->bcnth = bcnt >> 8;
101 
102  /* Low */
103  init_packet->bcntl = bcnt & 0xFF;
104 }
105 
112 static inline void packet_init_add_bcnt(
113  struct packet_init *init_packet, uint16_t value)
114 {
115  /* Current bcnt */
116  uint16_t bcnt = packet_init_get_bcnt(init_packet);
117 
118  /* Update */
119  packet_init_set_bcnt(init_packet, bcnt + value);
120 }
121 
130 struct packet_init *packet_init_new(uint32_t cid, uint8_t cmd,
131  uint16_t bcnt);
132 
140 struct packet_cont *packet_cont_new(uint32_t cid, uint8_t seq);
141 
148 void *packet_copy(const void *packet);
149 
150 #endif
uint32_t cid
Definition: packet.h:41
#define PACKET_INIT_DATA_SIZE
Definition: packet.h:20
uint32_t cid
Definition: packet.h:47
U2FHID packet use for start messsaging during transaction.
Definition: packet.h:33
static void packet_init_set_bcnt(struct packet_init *init_packet, uint16_t bcnt)
Set the bcnt of an init packet.
Definition: packet.h:96
#define __packed
Definition: packet.h:13
uint8_t cmd
Definition: packet.h:36
U2FHID packet use for start messsaging during transaction.
Definition: packet.h:45
#define PACKET_CONT_DATA_SIZE
Definition: packet.h:24
uint8_t seq
Definition: packet.h:48
uint8_t data[(64-5)]
Definition: packet.h:49
static uint32_t packet_get_cid(const void *packet)
Get the channel id of a packet.
Definition: packet.h:58
static void packet_init_add_bcnt(struct packet_init *init_packet, uint16_t value)
Add a number to the current bcnt of an init packet.
Definition: packet.h:112
static bool packet_is_init(const void *packet)
Check if a packet is an init packet.
Definition: packet.h:69
struct packet_init * packet_init_new(uint32_t cid, uint8_t cmd, uint16_t bcnt)
Allocate and initialize a initialisation packet.
Definition: packet.c:8
uint8_t bcnth
Definition: packet.h:37
void * packet_copy(const void *packet)
Copy a packet.
Definition: packet.c:40
uint8_t bcntl
Definition: packet.h:38
uint8_t cmd
Definition: packet.h:42
uint8_t data[(64-7)]
Definition: packet.h:39
static uint16_t packet_init_get_bcnt(const struct packet_init *init_packet)
Get the bcnt of an init packet.
Definition: packet.h:80
struct packet_cont * packet_cont_new(uint32_t cid, uint8_t seq)
Allocate and initialize a initialisation packet.
Definition: packet.c:25
uint8_t seq
Definition: packet.h:42
uint32_t cid
Definition: packet.h:35