10static const uint32_t
SIGVAL = 0xAA55AA55;
26 for (uint16_t i = 0; i < len; i++) {
29 Serial.print(buf[i], HEX);
47 while (v > 0 && pos >= 0) {
48 buf[pos--] =
'0' + (v % 10);
51 Serial.print(&buf[pos + 1]);
66 unsigned char digest[16]) {
75 uint16_t buf_len = 8 + 2;
80 uint8_t *buf = (uint8_t *)malloc(buf_len);
87 Serial.print(
"[DBG] counter = ");
92 for (
int i = 7; i >= 0; i--) {
93 buf[off + i] = tmp & 0xFF;
102 buf[off++] =
mm_id >> 8;
103 buf[off++] =
mm_id & 0xFF;
104 Serial.print(
"[DBG] CAN ID = 0x");
105 Serial.println(
mm_id, HEX);
112 Serial.print(
"[DBG] history_count = ");
116 Serial.print(
"[DBG] hist[");
118 Serial.print(
"] = ");
129 Serial.print(
"[DBG] current_data = ");
132 memcpy(buf + off, data, len);
144 Serial.print(
"[DBG] raw MD5 = ");
175 EEPROM.get(addr,
mm_hist[i].len);
176 addr +=
sizeof(
mm_hist[i].len);
179 EEPROM.get(addr,
mm_hist[i].data);
184 Serial.println(
"[DBG] load_state: loaded from EEPROM");
185 Serial.print(
" counter = ");
188 Serial.print(
" history_count = ");
212 EEPROM.put(addr,
mm_hist[i].len);
213 addr +=
sizeof(
mm_hist[i].len);
216 EEPROM.put(addr,
mm_hist[i].data);
221 Serial.println(
"[DBG] save_state: saved to EEPROM");
222 Serial.print(
" counter = ");
225 Serial.print(
" history_count = ");
243 Serial.begin(115200);
246 Serial.println(
"[DBG] minimac_init()");
257 Serial.println(
"[DBG] minimac_init: no EEPROM state, initialize fresh");
284 Serial.println(
"[DBG] minimac_sign()");
287 unsigned char digest[16];
291 Serial.print(
"[DBG] sign: tag = ");
300 Serial.println(
"[DBG] sign: history full, dropping oldest");
310 Serial.print(
"[DBG] sign: new history_count = ");
315 Serial.print(
"[DBG] sign: new counter = ");
339 const uint8_t *tag) {
341 Serial.println(
"[DBG] minimac_verify()");
344 unsigned char digest[16];
348 Serial.print(
"[DBG] verify: expected tag = ");
350 Serial.print(
"[DBG] verify: recv tag = ");
355 Serial.println(
"[DBG] verify: FAILED");
361 Serial.println(
"[DBG] verify: history full, dropping oldest");
371 Serial.print(
"[DBG] verify: new history_count = ");
376 Serial.print(
"[DBG] verify: new counter = ");
383 Serial.println(
"[DBG] verify: SUCCESS");
uint8_t minimac_sign(uint8_t *data, uint8_t payload_len)
송신할 메시지에 Mini-MAC 태그 생성 및 내부 상태 갱신
static void save_state(void)
Mini-MAC 상태를 EEPROM에 저장
static uint64_t mm_counter
64비트 메시지 카운터
static bool load_state(void)
EEPROM에서 Mini-MAC 상태 불러오기
static MiniMacHist mm_hist[5]
최근 λ개 메시지 히스토리
bool minimac_verify(const uint8_t *data, uint8_t payload_len, const uint8_t *tag)
수신된 메시지의 Mini-MAC 태그 검증 및 상태 동기화
static uint8_t mm_hist_cnt
히스토리 항목 수 (≤ λ)
static const int DATA_ADDR
static uint16_t mm_id
보호할 CAN ID, 그룹 키, 카운터, 메시지 히스토리
static const uint32_t SIGVAL
static const int SIG_ADDR
EEPROM 레이아웃: 시그니처 및 데이터 저장 시작 주소
void minimac_init(uint16_t can_id, const uint8_t *key)
Mini-MAC 초기화 및 EEPROM 동기화
static uint8_t mm_key[16]
공유 그룹 키
static void compute_digest(const uint8_t *data, uint8_t len, unsigned char digest[16])
Mini-MAC용 HMAC-MD5 다이제스트 계산
#define MINIMAC_HIST_LEN
메시지 히스토리 최대 개수 (λ = 5)
#define MINIMAC_TAG_LEN
Mini-MAC 다이제스트에서 사용할 태그 길이 (4바이트, 32비트)
#define MINIMAC_MAX_DATA
CAN 데이터 필드 최대 길이 (8바이트)
#define MINIMAC_KEY_LEN
Mini-MAC HMAC 키 길이 (16바이트, 128비트)
static bool load_state(void)
EEPROM에서 Mini-MAC 상태 불러오기
static void debug_print_hex(const uint8_t *buf, uint16_t len)
디버깅용: 바이트 배열을 16진수로 출력
static void print_u64(uint64_t v)
디버깅용: 64비트 부호 없는 정수를 10진수 문자열로 변환해 출력