我刚刚开始使用 IAR 编译器和 ARM 微 Controller 。第一步,我想使用我的 AT91SAM7S MCU 进行 RSA 加密(我知道这不是一个好的第一步!;))。
无论如何,谷歌搜索后我发现 this站点包含两个名为 rsa.h
和 rsa.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 输出:
我该如何处理这些错误?
请帮助我开始使用此设备。 提前致谢。
最佳答案
rsa.h
写入不正确。它需要包含 stdint.h
。这与 IAR、ARM 或其他任何东西无关。
关于c - IAR 编译器中 "#include"预处理器命令和 "typedef"命令的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32046260/