10static const uint32_t
SIGVAL = 0xAA55AA55;
27 for (uint16_t i = 0; i < len; i++) {
28 if (buf[i] < 0x10) Serial.print(
'0');
29 Serial.print(buf[i], HEX);
48 while (v > 0 && pos >= 0) {
49 buf[pos--] =
'0' + (v % 10);
52 Serial.print(&buf[pos + 1]);
66static void compute_digest(
const uint8_t *data, uint8_t len,
unsigned char digest[16])
76 uint16_t buf_len = 8 + 2;
81 uint8_t *buf = (uint8_t *)malloc(buf_len);
88 Serial.print(
"[DBG] counter = ");
93 for (
int i = 7; i >= 0; i--) {
94 buf[off + i] = tmp & 0xFF;
103 buf[off++] =
mm_id >> 8;
104 buf[off++] =
mm_id & 0xFF;
105 Serial.print(
"[DBG] CAN ID = 0x");
106 Serial.println(
mm_id, HEX);
113 Serial.print(
"[DBG] history_count = ");
117 Serial.print(
"[DBG] hist[");
119 Serial.print(
"] = ");
130 Serial.print(
"[DBG] current_data = ");
133 memcpy(buf + off, data, len);
145 Serial.print(
"[DBG] raw MD5 = ");
178 EEPROM.get(addr,
mm_hist[i].len);
179 addr +=
sizeof(
mm_hist[i].len);
182 EEPROM.get(addr,
mm_hist[i].data);
187 Serial.println(
"[DBG] load_state: loaded from EEPROM");
188 Serial.print(
" counter = ");
191 Serial.print(
" history_count = ");
217 EEPROM.put(addr,
mm_hist[i].len);
218 addr +=
sizeof(
mm_hist[i].len);
221 EEPROM.put(addr,
mm_hist[i].data);
226 Serial.println(
"[DBG] save_state: saved to EEPROM");
227 Serial.print(
" counter = ");
230 Serial.print(
" history_count = ");
249 Serial.begin(115200);
252 Serial.println(
"[DBG] minimac_init()");
263 Serial.println(
"[DBG] minimac_init: no EEPROM state, initialize fresh");
289 Serial.println(
"[DBG] minimac_sign()");
292 unsigned char digest[16];
296 Serial.print(
"[DBG] sign: tag = ");
305 Serial.println(
"[DBG] sign: history full, dropping oldest");
315 Serial.print(
"[DBG] sign: new history_count = ");
320 Serial.print(
"[DBG] sign: new counter = ");
346 Serial.println(
"[DBG] minimac_verify()");
349 unsigned char digest[16];
353 Serial.print(
"[DBG] verify: expected tag = ");
355 Serial.print(
"[DBG] verify: recv tag = ");
360 Serial.println(
"[DBG] verify: FAILED");
366 Serial.println(
"[DBG] verify: history full, dropping oldest");
376 Serial.print(
"[DBG] verify: new history_count = ");
381 Serial.print(
"[DBG] verify: new counter = ");
388 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 MiniMacHist mm_hist[5]
최근 λ개 메시지 히스토리
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 레이아웃: 시그니처 및 데이터 저장 시작 주소
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 상태 불러오기
bool minimac_verify(const uint8_t *data, uint8_t payload_len, const uint8_t *tag)
수신된 메시지의 Mini-MAC 태그 검증 및 상태 동기화
static void debug_print_hex(const uint8_t *buf, uint16_t len)
디버깅용: 바이트 배열을 16진수로 출력
static void print_u64(uint64_t v)
디버깅용: 64비트 부호 없는 정수를 10진수 문자열로 변환해 출력
void minimac_init(uint16_t can_id, const uint8_t *key)
Mini-MAC 초기화 및 EEPROM 동기화
static void compute_digest(const uint8_t *data, uint8_t len, unsigned char digest[16])
Mini-MAC용 HMAC-MD5 다이제스트 계산