我使用c-capnproto序列化我的传感器数据。现在我需要向它添加一个 HMAC。它必须是一个capn_data
blob,但这在示例代码中没有处理。
当我尝试添加数据时,它不起作用。这是我的capnp
文件:
@0xe2d0214c41cb77af;
using C = import "c.capnp";
$C.fieldgetset;
struct Heartbeat @0x97002102216a8d67 {
temperature @0 :UInt32;
}
struct AuthenticatedHeartbeat @0xe2fa13efd2e3a9dc {
heartbeat @0 :Data;
hmac @1 :Data;
}
编译后,我使用它:
static uint8_t beat[1024];
static uint8_t hmac[32] = {0xc2, 0xf1, 0x53, 0x12, 0x4b, 0x0c, 0xa4, 0xd1,
0x22, 0xf1, 0x64, 0x22, 0x1b, 0xbc, 0xa5, 0xd2,
0xe2, 0xf2, 0x75, 0x32, 0x2b, 0x3c, 0xa6, 0xd3,
0x72, 0xf1, 0x86, 0x42, 0x6b, 0x7c, 0xa7, 0xd4};
static uint8_t auth[1024];
int main(void) {
int packed_size;
{
struct capn root;
capn_init_malloc(&root);
capn_ptr cr = capn_root(&root);
struct capn_segment *cs = cr.seg;
Heartbeat_ptr hb = new_Heartbeat(cs);
Heartbeat_set_temperature(hb, 0x25);
capn_setp(capn_root(&root), 0, hb.p);
packed_size = capn_write_mem(&root, beat, sizeof(beat), 1/*packed*/);
printf("BEAT: max_unpacked_size=%d, packed_size=%d\n", capn_size(&root), packed_size);
capn_free(&root);
}
print_buffer(beat, packed_size);
//HMAC_calc(beat, packed_size, hmac);
{
struct capn root;
capn_init_malloc(&root);
capn_ptr cr = capn_root(&root);
struct capn_segment *cs = cr.seg;
AuthenticatedHeartbeat_ptr ahb = new_AuthenticatedHeartbeat(cs);
AuthenticatedHeartbeat_set_heartbeat(ahb, buf_to_data(beat, packed_size));
AuthenticatedHeartbeat_set_hmac(ahb, buf_to_data(hmac, 32));
capn_setp(capn_root(&root), 0, ahb.p);
packed_size = capn_write_mem(&root, auth, sizeof(auth), 1/*packed*/);
printf("AUTH: max_unpacked_size=%d, packed_size=%d\n", capn_size(&root), packed_size);
capn_free(&root);
}
print_buffer(auth, packed_size);
}
我编写了一个辅助函数,类似于chars_to_text
示例中提供的函数
static capn_data buf_to_data(char *buf, const int len) {
return (capn_data) {{
.type = CAPN_NULL,
.has_ptr_tag = 0,
.is_list_member = 0,
.is_composite_list = 0,
.datasz = 0,
.ptrs = 0,
.len = len,
.data = buf,
.seg = NULL,
}};
}
但我想我错过了一些部分。结果数据
BEAT: max_unpacked_size=24, packed_size=6
[ 0] - 10021001 - ....
[ 1] - 01250000 - .%
AUTH: max_unpacked_size=32, packed_size=6
[ 0] - 10034002 - ..@.
[ 1] - 00010000 - ..
至少在经过验证的数据中不再包含温度 ( 0x25
)。HMAC也不存在,长度太短了。
您能否提示我如何处理capn_data
blob,如何填充它以及如何将其打包到根目录中。