arduino 中的 Ascon-128

问题描述 投票:0回答:1

我尝试在 Arduino 中实现 Ascon-128 加密算法https://ascon.iaik.tugraz.at。 编译器没有发现任何错误,但是当我运行代码时,监视器没有返回任何内容。甚至连

Serial.println(ciphertext[0]);
都没有打印出来。 你看到我的代码有什么错误吗? 谢谢你

#include <stdio.h>

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 };

void setup() {
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  main();
  delay(500);
}

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++) printf("%llx\n", state[i]);
}

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);
  // bijgevoegd
  state[3] ^= key[0];
  state[4] ^= key[1];
}

int main() {
  bit64 nonce[2] = { 2000 };
  bit64 key[2] = { 3000 };
  bit64 IV = 0x80400c060000000;
  
  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] = key[1];
  initialization(state, key);
  print_state(state);
  encrypt(state, 2, plaintext, ciphertext);
  Serial.println(ciphertext[0]);
  Serial.println(ciphertext[1]);
  
  finalization(state, key);
  Serial.println(state[3]);
  Serial.println(state[4]);
  return 0;
}
arduino arduino-uno arduino-ide arduino-c++
1个回答
0
投票

您能否将带有解密的完整项目发送给我?我正在尝试在我的学士项目中使用此代码,但我无法编写正确的解密方法

© www.soinside.com 2019 - 2024. All rights reserved.