You need to enable JavaScript to run this app. Arduino平台Ascon-128加密算法实现后串口无输出问题排查
优惠活动
大模型
产品
解决方案
定价
更多

Arduino平台Ascon-128加密算法实现后串口无输出问题排查

问题排查与修复方案

直接导致无输出的核心问题

  • Arduino框架内置了程序入口逻辑,会自动执行setup()后循环执行loop(),不允许用户自定义main()函数,你自行定义的main()会和Arduino内置的入口冲突,导致程序启动直接崩溃,根本跑不到串口打印的逻辑。
  • print_state函数中使用的printf默认不会输出到Arduino串口,没有做重定向的情况下没有任何作用,还可能引发输出异常。

其他影响加密正确性的问题

  • Ascon-128的标准IV值为0x80400c0600000000,你代码中写的0x80400c060000000少了一位,长度不符合要求。
  • 初始状态赋值错误:你把state[4]赋值为key[1],按照Ascon规范,state[3]和state[4]应该分别存放nonce的两个元素。
  • loop()中反复执行业务逻辑会导致状态重复初始化,如果只需要执行一次加密可以直接把逻辑放到setup()中。

修复后的可运行代码

typedef unsigned long long bit64;

bit64 constants[16] = {0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69
,0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f };

bit64 state[5] = {0}, t[5] = { 0 };

bit64 rotate(bit64 x, int l){
  bit64 temp;
  temp = (x >> l) ^ (x << (64 - l));
  return temp;
}

void print_state(bit64 state[5]){
    for (int i = 0; i < 5; i++) {
      Serial.println(state[i], HEX);
    }
}

void sbox (bit64 x[5]) {
  x[0] ^= x[4];   x[4] ^= x[3];   x[2] ^= x[1];
  t[0] = x[0];    t[1] = x[1];    t[2] = x[2];    t[3] = x[3];    t[4] = x[4];
  t[0] = ~t[0];   t[1] = ~t[1];   t[2] = ~t[2];   t[3] = ~t[3];   t[4] = ~t[4];
  t[0] &= x[1];   t[1] &= x[2];   t[2] &= x[3];   t[3] &= x[4];   t[4] &= x[0];
  x[0] ^= t[1];   x[1] ^= t[2];   x[2] ^= t[3];   x[3] ^= t[4];   x[4] ^= t[0];
  x[1] ^= x[0];   x[0] ^= x[4];   x[3] ^= x[2];   x[2] = ~x[2];
}

void linear(bit64 state[5]) {
  bit64 temp0, temp1;
  temp0 = rotate(state[0], 19);
  temp1 = rotate(state[0], 28);
  state[0] ^= temp0 ^temp1;
  temp0 = rotate(state[1], 61);
  temp1 = rotate(state[1], 39);
  state[1] ^= temp0 ^temp1;
  temp0 = rotate(state[2], 1);
  temp1 = rotate(state[2], 6);
  state[2] ^= temp0 ^temp1;
  temp0 = rotate(state[3], 10);
  temp1 = rotate(state[3], 17);
  state[3] ^= temp0 ^temp1;
  temp0 = rotate(state[4], 7);
  temp1 = rotate(state[4], 41);
  state[4] ^= temp0 ^temp1;
}

void add_constant(bit64 state[5], int i, int a) {
  state[2] = state[2] ^ constants[12- a + i]; 
}

void p(bit64 state[5], int a) {
  for (int i = 0; i < a; i++){
    add_constant(state, i, a);
      sbox(state);
      linear(state);
  }
}
    
void initialization(bit64 state[5], bit64 key[2]) {
  p(state, 12);
  state[3] ^= key[0];
  state[4] ^= key[1];
}

void encrypt(bit64 state[5], int length, bit64 plaintext[], bit64 ciphertext[]) {
  ciphertext[0] = plaintext[0] ^ state[0];
  for (int i = 1; i < length; i++){
    p(state, 6);
    ciphertext[i] = plaintext[i] ^ state[0];
    state[0] = ciphertext[i];
    }
}

void finalization(bit64 state[5], bit64 key[2]){
  state[3] ^= key[0];
  state[4] ^= key[1];
  p(state, 12);
  state[3] ^= key[0];
  state[4] ^= key[1];
}

void setup() {
  Serial.begin(9600);
  // 等待串口连接,适配USB转串口的开发板
  while (!Serial);

  bit64 nonce[2] = { 2000, 0 };
  bit64 key[2] = { 3000, 0 };
  // 修正IV值
  bit64 IV = 0x80400c0600000000;
  
  bit64 to_encode = 0x82187;
  bit64 plaintext [] = { 0x1234567890abcdef, to_encode }, ciphertext[10] = { 0 };
  state[0] = IV;
  state[1] = key[0];
  state[2] = key[1];
  state[3] = nonce[0];
  // 修正state[4]赋值为nonce[1]
  state[4] = nonce[1];
  initialization(state, key);
  print_state(state);
  encrypt(state, 2, plaintext, ciphertext);
  Serial.println(ciphertext[0], HEX);
  Serial.println(ciphertext[1], HEX);
  
  finalization(state, key);
  Serial.println(state[3], HEX);
  Serial.println(state[4], HEX);
}

void loop() {
  // 不需要反复执行可以留空
}

验证说明

烧录修复后的代码,串口监视器波特率设置为9600,即可看到加密输出的十六进制数据。如果需要和官方测试向量对齐,修改key、nonce、plaintext为对应测试值即可。

内容的提问来源于stack exchange,提问作者Hannes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 08:45:02