【问题标题】:error in IAR compiler for "#include" preprocessor command and "typedef" command“#include”预处理器命令和“typedef”命令的 IAR 编译器错误
【发布时间】:2015-11-09 20:18:54
【问题描述】:

我刚刚开始使用 IAR 编译器和 ARM 微控制器。在第一步中,我想使用我的 AT91SAM7S MCU 进行 RSA 加密(我知道这不是一个好的第一步!;))。

无论如何,在谷歌搜索后,我发现 this 站点包含两个名为 rsa.hrsa.c 的文件,它们为嵌入式设备实现了 RSA 算法。

所以我下载了这些文件并将它们放在我的程序目录中,(在同一目录中 main.c 是)。

现在,当我尝试构建和编译这个项目时,我遇到了以下错误:

构建配置:4rsa - 调试 正在更新构建树...

3  file(s) deleted. 
Updating build tree... 
main.c 
Error[Pe020]: identifier "uint64_t" is undefined C:\4rsa\rsa.h 22 
Error while running C/C++ Compiler 
rsa.c 
Fatal Error[Pe005]: could not open source file "cross_studio_io.h" C:\4rsa\rsa.c 22 
Error while running C/C++ Compiler 

Total number of errors: 2 
Total number of warnings: 0 

Total number of errors: 2 
Total number of warnings: 0 

看来我必须下载一些库并将其添加到我的程序中,但我不知道我需要哪些库以及在哪里可以下载它们。


仅供参考:

这是rsa.h的内容:

/**************************************************************************/
/*! 
    \file     rsa.h
    \author   Kyle Loudon
              modified: microBuilder.eu
    \date     4 January, 2010
    \version  1.0

    Basic RSA-encryption using 64-bit math (32-bit keys).

    Based on the examples from "Mastering Algorithms with C" by
    Kyle Loudon (O'Reilly, 1999).
*/
/**************************************************************************/
#include <stdlib.h>
#ifndef _RSA_H_
#define _RSA_H_

/* In a secure implementation, huge_t should be at least 400 decimal digits, *
 * instead of the 20 provided by a 64-bit value.  This means that key values *
 * can be no longer than 10 digits in length in the current implementation.  */
typedef uint64_t huge_t;

/* Structure for RSA public keys. */
typedef struct rsaPubKey_s
{
  huge_t e;
  huge_t n;
} 
rsaPubKey_t;

/* Define a structure for RSA private keys. */
typedef struct rsaPriKey_s
{
  huge_t d;
  huge_t n;
} 
rsaPriKey_t;

void rsaTest();
void rsaEncrypt(huge_t plaintext, huge_t *ciphertext, rsaPubKey_t pubkey);
void rsaDecrypt(huge_t ciphertext, huge_t *plaintext, rsaPriKey_t prikey);

#endif

这是rsa.c的内容:

/**************************************************************************/
/*! 
    \file     rsa.c
    \author   Kyle Loudon
              modified: microBuilder.eu
    \date     4 January, 2010
    \version  1.0

    Basic RSA-encryption using 64-bit math (32-bit keys).

    Based on the examples from "Mastering Algorithms with C" by
    Kyle Loudon (O'Reilly, 1999).

    Note: The rsaTest function uses debug_printf in Rowley Associate's
    Crossworks for ARM.  If you wish to use an alternative means to
    display the test results, cross_studio_io.h can be removed from the
    include list, and debug_printf can be renamed to a different
    output method.
*/
/**************************************************************************/

#include <cross_studio_io.h>

#include "rsa.h"

static huge_t modexp(huge_t a, huge_t b, huge_t n) 
{
  huge_t y;
  y = 1;

  /*  Compute pow(a, b) % n using the binary square and multiply method. */
  while (b != 0) 
  {
    /*  For each 1 in b, accumulate y. */
    if (b & 1)
    {
      y = (y * a) % n;
    }

    /* Square a for each bit in b. */
    a = (a * a) % n;

    /*  Prepare for the next bit in b. */
    b = b >> 1;
  }

  return y;
}

void rsaTest()
{
  huge_t      rsaOrig, rsaDecrypted, rsaEncrypted;
  rsaPubKey_t publicKey;
  rsaPriKey_t privateKey;
  int         i;

  debug_printf("Encrypting with RSA\n");

  // Values based on 64-bit math (huge_t = unsigned long long)
  // which will result in more secure encryption, but also
  // increases the size of the encrypted text
  publicKey.e = 21;
  publicKey.n = 16484947;
  privateKey.d = 15689981;
  privateKey.n = 16484947;

  // Alternative values with 32-bit math (huge_t = unsigned long)
  // or when smaller encrypted text is desired
  // publicKey.e = 17;
  // publicKey.n = 209;
  // privateKey.d = 53;
  // privateKey.n = 209;

  debug_printf("d=%lld, e=%lld, n=%lld\n", 
                privateKey.d, publicKey.e, publicKey.n);

  for (i = 0; i < 128; i++) 
  {  
     rsaOrig = i;
     rsaEncrypt(rsaOrig, &rsaEncrypted, publicKey);
     rsaDecrypt(rsaEncrypted, &rsaDecrypted, privateKey);

     if (rsaOrig == rsaDecrypted)
     {
        debug_printf("In=%5lld, Encrypted=%10lld, Out=%5lld (OK)\n", 
                      rsaOrig, rsaEncrypted, rsaDecrypted);
     }
     else
     {
        debug_printf("In=%5lld, Encrypted=%10lld, Out=%5lld (ERROR)\n", 
                      rsaOrig, rsaEncrypted, rsaDecrypted);
     }  
  }
}

void rsaEncrypt(huge_t plaintext, huge_t *ciphertext, rsaPubKey_t pubkey) 
{
  *ciphertext = modexp(plaintext, pubkey.e, pubkey.n);

  return;
}

void rsaDecrypt(huge_t ciphertext, huge_t *plaintext, rsaPriKey_t prikey) 
{
  *plaintext = modexp(ciphertext, prikey.d, prikey.n);

  return;
}

这是我的 IAR IDE 输出:

我该如何处理这些错误?

请帮助我开始使用此设备。 提前致谢。

【问题讨论】:

  • 对于第一个问题,您应该包含定义fixed-width integer types&lt;stdint.h&gt;。但是,如果编译器不支持,例如64 位类型,那么您将没有 unt64_t 类型。
  • 至于缺少的头文件,可能是在非标准位置?你试过搜索吗?

标签: c encryption arm iar


【解决方案1】:

rsa.h 写错了。它需要包含stdint.h。这与 IAR、ARM 或其他任何东西无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2012-03-19
    • 2016-01-10
    相关资源
    最近更新 更多