在函数“ setup”中:Arduino-Uno板上的speck.ino未定义参考错误

时间:2019-02-05 16:38:12

标签: arduino cryptography arduino-uno

你好,我想在Arduino-Uno上实现一个斑点算法。我在ubuntu 18.04上将specklibrary上传到了arduino IDE。但是,当我编译代码时,会出现错误编译。这是错误的错误日志。

Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\AppData\Local\Temp\ccYOtIOV.ltrans0.ltrans.o: In function `setup':

D:\speck/speck.ino:47: undefined reference to `runEncryptionKeySchedule(unsigned char*, unsigned char*)'

D:\speck/speck.ino:51: undefined reference to `encrypt(unsigned char*, unsigned char*)'

D:\speck/speck.ino:66: undefined reference to `runDecryptionKeySchedule(unsigned char*, unsigned char*)'

D:\speck/speck.ino:69: undefined reference to `decrypt(unsigned char*, unsigned char*)'

Error compiling for board Arduino/Genuino Uno.

arduino代码。

#include <string.h>
#include "cipher.h"
#include "common.h"
#include "constants.h"
#include "data_types.h"
#include "primitives.h"
#include "rot8.h"
#include "rot16.h"
#include "rot32.h"



const char *name = "Speck-64-96-ECB";

uint8_t key[12] = { 
  0x13, 0x12, 0x11, 0x10, 0x0b, 0x0a, 0x09, 0x08,
  0x03, 0x02, 0x01, 0x00      };

uint8_t plaintext[BLOCK_SIZE] = {  
  0x74, 0x61, 0x46, 0x20, 0x73, 0x6e, 0x61, 0x65 };

uint8_t ciphertext[BLOCK_SIZE] = { 
  0x9f, 0x79, 0x52, 0xec, 0x41, 0x75, 0x94, 0x6c };

uint8_t state [BLOCK_SIZE] = {  } 
; 

uint8_t roundKeys [ ROUND_KEYS_SIZE ] = {    } 
;

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

  Serial.println();

  Serial.println("State Sizes:");
  Serial.println("Speck ... ");

  Serial.println("Speck Test Vectors:");

  Serial.print(name);
  Serial.print(" Encryption ... ");


  runEncryptionKeySchedule(key, roundKeys);



  encrypt(state, roundKeys);

  if (memcmp(state, ciphertext, BLOCK_SIZE) == 0)
    Serial.println("Passed");
  else
    Serial.println("Failed");

/*  if (!decryption)
    return;
*/

  Serial.print(name);
  Serial.print(" Decryption ... ");


  runDecryptionKeySchedule(key, roundKeys);


  decrypt(state, roundKeys);

  if (memcmp(state, plaintext, BLOCK_SIZE) == 0)
    Serial.println("Passed");
  else
    Serial.println("Failed");

}

void loop()
{

}

头文件:

#ifndef CIPHER_H
#define CIPHER_H


#ifdef AVR /* AVR */
#include <avr/pgmspace.h>
#endif /* AVR */


/*
 *
 * Run the encryption key schedule
 * ... key - the cipher key
 * ... roundKeys - the encryption round keys
 *
 */
 void runEncryptionKeySchedule(uint8_t *key, uint8_t *roundKeys);

/*
 *
 * Run the decryption key schedule
 * ... key - the cipher key
 * ... roundKeys - the decryption round keys
 *
 */
void runDecryptionKeySchedule(uint8_t *key, uint8_t *roundKeys);


/*
 *
 * Encrypt the given block using the given round keys
 * ... block - the block to encrypt
 * ... roundKeys - the round keys to be used during encryption
 *
 */
void encrypt(uint8_t *block, uint8_t *roundKeys);

/*
 *
 * Decrypt the given block using the given round keys
 * ... block - the block to decrypt
 * ... roundKeys - the round keys to be used during decryption
 *
 */
void decrypt(uint8_t *block, uint8_t *roundKeys);

/*
 *
 * Optimization levels
 * ... OPTIMIZATION_LEVEL_0 - O0
 * ... OPTIMIZATION_LEVEL_1 - O1
 * ... OPTIMIZATION_LEVEL_2 - O2
 * ... OPTIMIZATION_LEVEL_3 - O3 = defualt
 *
 */
#define OPTIMIZATION_LEVEL_0 __attribute__((optimize("O0")))
#define OPTIMIZATION_LEVEL_1 __attribute__((optimize("O1")))
#define OPTIMIZATION_LEVEL_2 __attribute__((optimize("O2")))
#define OPTIMIZATION_LEVEL_3 __attribute__((optimize("O3")))


/*
 * 
 * SCENARIO values:
 * ... SCENARIO_0 0 - cipher operation: encrypt & decrypt one data block
 * ... SCENARIO_1 1 - scenario 1: encrypt & decrypt data in CBC mode
 * ... SCENARIO_2 2 - scenario 2: encrypt & decrypt data in CTR mode
 *
 */
#define SCENARIO_0 0
#define SCENARIO_1 1
#define SCENARIO_2 2

#ifndef SCENARIO
#define SCENARIO SCENARIO_0
#endif


/*
 *
 * MEASURE_CYCLE_COUNT values:
 * ... MEASURE_CYCLE_COUNT_DISABLED 0 - measure cycle count is disabled
 * ... MEASURE_CYCLE_COUNT_ENABLED 1 - measure cycle count is enabled
 *
 */
#define MEASURE_CYCLE_COUNT_DISABLED 0
#define MEASURE_CYCLE_COUNT_ENABLED 1

#ifndef MEASURE_CYCLE_COUNT
#define MEASURE_CYCLE_COUNT MEASURE_CYCLE_COUNT_DISABLED
#endif


/*
 *
 * Align memory boundaries in bytes
 *
 */
#define ALIGN_PC_BOUNDRY 64
#define ALIGN_AVR_BOUNDRY 2
#define ALIGN_MSP_BOUNDRY 2
#define ALIGN_ARM_BOUNDRY 8

#if defined(PC) && !defined(ALIGNED) /* PC ALIGNED */
#define ALIGNED __attribute__ ((aligned(ALIGN_PC_BOUNDRY)))
#endif /* PC ALIGNED */

#if defined(AVR) && !defined(ALIGNED) /* AVR ALIGNED */
#define ALIGNED __attribute__ ((aligned(ALIGN_AVR_BOUNDRY)))
#endif /* AVR ALIGNED */

#if defined(MSP) && !defined(ALIGNED) /* MSP ALIGNED */
#define ALIGNED __attribute__ ((aligned(ALIGN_MSP_BOUNDRY)))
#endif /* MSP ALIGNED */

#if defined(ARM) && !defined(ALIGNED) /* ARM ALIGNED */
#define ALIGNED __attribute__ ((aligned(ALIGN_ARM_BOUNDRY)))
#endif /* ARM ALIGNED */


/* 
 *
 * RAM data types 
 *
 */
#define RAM_DATA_BYTE uint8_t ALIGNED
#define RAM_DATA_WORD uint16_t ALIGNED
#define RAM_DATA_DOUBLE_WORD uint32_t ALIGNED

#define READ_RAM_DATA_BYTE(x) x
#define READ_RAM_DATA_WORD(x) x
#define READ_RAM_DATA_DOUBLE_WORD(x) x


/* 
 *
 * Flash/ROM data types 
 *
 */
#if defined(AVR) /* AVR */
#define ROM_DATA_BYTE const uint8_t PROGMEM ALIGNED
#define ROM_DATA_WORD const uint16_t PROGMEM ALIGNED
#define ROM_DATA_DOUBLE_WORD const uint32_t PROGMEM ALIGNED

#define READ_ROM_DATA_BYTE(x) pgm_read_byte(&x)
#define READ_ROM_DATA_WORD(x) pgm_read_word(&x)
#define READ_ROM_DATA_DOUBLE_WORD(x) pgm_read_dword(&x)
#else /* AVR */
#define ROM_DATA_BYTE const uint8_t ALIGNED
#define ROM_DATA_WORD const uint16_t ALIGNED
#define ROM_DATA_DOUBLE_WORD const uint32_t ALIGNED

#define READ_ROM_DATA_BYTE(x) x
#define READ_ROM_DATA_WORD(x) x
#define READ_ROM_DATA_DOUBLE_WORD(x) x
#endif /* AVR */


/*
 *
 * Scenario 2 round keys are stored in Flash/ROM
 *
 */
#if defined(SCENARIO) && (SCENARIO_2 == SCENARIO)
#define READ_ROUND_KEY_BYTE(x) READ_ROM_DATA_BYTE(x)
#define READ_ROUND_KEY_WORD(x) READ_ROM_DATA_WORD(x)
#define READ_ROUND_KEY_DOUBLE_WORD(x) READ_ROM_DATA_DOUBLE_WORD(x)
#else
#define READ_ROUND_KEY_BYTE(x) READ_RAM_DATA_BYTE(x)
#define READ_ROUND_KEY_WORD(x) READ_RAM_DATA_WORD(x)
#define READ_ROUND_KEY_DOUBLE_WORD(x) READ_RAM_DATA_DOUBLE_WORD(x)
#endif



#endif /* CIPHER_H */

和c代码文件:

#include <stdint.h>

#include "cipher.h"
#include "rot8.h"
#include "constants.h"

void encrypt(uint8_t *block, uint8_t *roundKeys)
{
  uint8_t       *block8  = (uint8_t *)block;
  const uint8_t *rk       = (uint8_t *)roundKeys;

  uint8_t y = block8[0];
  uint8_t x = block8[1];

  uint8_t i;

  for (i = 0; i < NUMBER_OF_ROUNDS; ++i) {

    x = (rot8r8(x) + y) ^ READ_ROUND_KEY_DOUBLE_WORD(rk[i]);
    y = rot8l3(y) ^ x;

  }

  block8[0] = y;
  block8[1] = x;
}

`

我搜索了所有来源。他们说了所有的话,但是,我找不到原因。非常感谢您的帮助...

1 个答案:

答案 0 :(得分:0)

非常感谢您的帮助。这时,这里的帮助只是写作。我找到了解决问题的方法。

对于arduino.ino中使用的所有类文件,我们应该在下面添加页眉和页脚。

#ifndef XXX_H_
#define XXX_H_

#ifdef __cplusplus
 extern "C" {
#endif

#include "cipher.h"
#include "rot8.h"
#include "constants.h"

void encrypt(uint8_t *block, uint8_t *roundKeys);

#ifdef __cplusplus
    }
#endif

#endif

对于我的班级,这是解决方案;

#ifdef __cplusplus
 extern "C" {
#endif

#include <stdint.h>

#include "cipher.h"
#include "rot8.h"
#include "constants.h"

void encrypt(uint8_t *block, uint8_t *roundKeys)
{
  uint8_t       *block8  = (uint8_t *)block;
  const uint8_t *rk       = (uint8_t *)roundKeys;

  uint8_t y = block8[0];
  uint8_t x = block8[1];

  uint8_t i;

  for (i = 0; i < NUMBER_OF_ROUNDS; ++i) {

    x = (rot8r8(x) + y) ^ READ_ROUND_KEY_DOUBLE_WORD(rk[i]);
    y = rot8l3(y) ^ x;

  }

  block8[0] = y;
  block8[1] = x;
}
#ifdef __cplusplus
    }
#endif