思路.按照.net的方式新建一个供C#调用的C接口,然后通过C接口调用LibTom的加解密算法。
1、C#调用C方法,请参考.net micro framework提供的资料进行
2、修改工程的dotNetMF.proj文件,然后把LibTom的必要文件加上,要添加那些文件可以,参考使用Libtomcrypt RSA/ECC等加密算法到项目中
3、然后重新编译固件,此时你会发现好多问题.
我遇到的问题及相关解决方式如下:
EXEC : error : L6915E: Library reports error: __use_no_semihosting_swi was requested, but _sys_open was referenced [C:\MF\PK\Solutions\NetduinoPlus2\TinyCLR\TinyCLR.proj]
EXEC : error : L6915E: Library reports error: __use_no_semihosting_swi was requested, but clock was referenced [C:\MF\PK\Solutions\NetduinoPlus2\TinyCLR\TinyCLR.proj]
A、在tomcrypt_custom.h中打开以下宏定义,主要不使用printf or getline,fprintf, assert, fopen, fclose这些函数,参考
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka11357.html
/* clean the stack of functions which put private information on stack */
#define LTC_CLEAN_STACK
/* disable all file related functions */
#define LTC_NO_FILE
/* disable all forms of ASM */
#define LTC_NO_ASM
/* disable FAST mode */
/* #define LTC_NO_FAST */
/* disable BSWAP on x86 */
#define LTC_NO_BSWAP/
B、在tomcrypt_cfg.h中修改ARGTYPE的定义
/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
#ifndef ARGTYPE
#define ARGTYPE 4
#endif
解决_sys_open这个Bug。
B、rng_get_bytes.c文件中取消这个函数
/* on ANSI C platforms with 100 < CLOCKS_PER_SEC < 10000 */
#if 0//defined(CLOCKS_PER_SEC) && !defined(WINCE)
#define ANSI_RNG
static unsigned long rng_ansic(unsigned char *buf, unsigned long len,
void (*callback)(void))
{
clock_t t1;
int l, acc, bits, a, b;
if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) {
return 0;
}
l = len;
bits = 8;
acc = a = b = 0;
while (len--) {
if (callback != NULL) callback();
while (bits--) {
do {
t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1;
t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1;
} while (a == b);
acc = (acc << 1) | a;
}
*buf++ = acc;
acc = 0;
bits = 8;
}
acc = bits = a = b = 0;
return l;
}
#endif