libtom 用法
下载:
https://github.com/libtom
编译:
1 make install DESTDIR=../install
echo "hello" > crypt.ind latex crypt > /dev/null /bin/sh: 1: latex: not found make: *** [docs] Error 127
解决办法:1 sudo apt-get install texmaker
2 vi makefile
文件开头添加: NODOCS=1
3 make again.
xx@ubuntu:~/Source/libtom/libtomcrypt-develop$ make install DESTDIR=../install install -d ../install/usr/lib install -d ../install/usr/include install -d ../install/usr/share/doc/libtomcrypt/pdf install -m 644 libtomcrypt.a ../install/usr/lib install -m 644 src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h src/headers/tomcrypt_cipher.h src/headers/tomcrypt_custom.h src/headers/tomcrypt.h src/headers/tomcrypt_hash.h src/headers/tomcrypt_mac.h src/headers/tomcrypt_macros.h src/headers/tomcrypt_math.h src/headers/tomcrypt_misc.h src/headers/tomcrypt_pkcs.h src/headers/tomcrypt_pk.h src/headers/tomcrypt_prng.h testprof/tomcrypt_test.h ../install/usr/include
4 libtommath用同样的方法安装。
5 测试程序:
算一串数据的hash256值:
xx@ubuntu:~/Source/libtom/install/usr/lib$ cat test.c #include <stdio.h> #include <tomcrypt.h> #define DUMP(data, len) \ do { \ int i, l = (len); \ for (i = 0; i < l; i++) \ printf(" 0x%02x", *((data) + i)); \ printf("\n"); \ } while (0) int main() { unsigned char data[]={0x0,0x1,0x2,0x3,0x4}; int x; #ifdef USE_LTM ltc_mp = ltm_desc; #elif defined(USE_TFM) ltc_mp = tfm_desc; #elif defined(USE_GMP) ltc_mp = gmp_desc; #else //extern ltc_math_descriptor EXT_MATH_LIB; //ltc_mp = EXT_MATH_LIB; #endif int hash_idx = register_hash(&sha256_desc); unsigned char hash[]={0}; hash_state md; sha256_init(&md); sha256_process(&md, data, sizeof(data)); sha256_done(&md,hash); DUMP(hash,32); return 0; } xx@ubuntu:~/Source/libtom/install/usr/lib$ gcc test.c -I ../include libtomcrypt.a libtommath.a xx@ubuntu:~/Source/libtom/install/usr/lib$ ./a.out 0x08 0xbb 0x5e 0x5d 0x6e 0xaa 0xc1 0x04 0x9e 0xde 0x08 0x93 0xd3 0x0e 0xd0 0x22 0xb1 0xa4 0xd9 0xb5 0xb4 0x8d 0xb4 0x14 0x87 0x1f 0x51 0xc9 0xcb 0x35 0x28 0x3d xx@ubuntu:~/Source/libtom/install/usr/lib$
编译错误备注:
1
make Error::/usr/include/features.h:324:26: fatal error: bits/predefs.h: No such file or directory
sudo apt-get install lib32ncurses5-dev
sudo apt-get install libc6-dev-i386
2
gcc test.c -I ../include libtomcrypt.a libtommath.a
编译的时候可能会出现编译错误。请实时调整头文件。
比如:tomcrypt.h tomcrypt_customer.h tomcrypt_cfg.h
#include <stdio.h> #include <tomcrypt_test.h> #define DUMP(data, len) \ do { \ int i, l = (len); \ for (i = 0; i < l; i++){ \ printf("0x%02x ", *(unsigned char*)(data + i)); \ if((i+1)%16 == 0) \ { \ printf("\n"); \ }\ }\ printf("\n"); \ } while (0) int transfer_rsakey_der(rsa_key * key, unsigned char *key_output) { printf("transfer key...\n"); unsigned char buf[4096] = {0}; unsigned char header1[6] = {0x30,0x82,0x04,0xa3,0x02,0x01}; unsigned char header2[5] = {0x02,0x82,0x01,0x01,0x00}; //leading zero unsigned char header3[4] = {0x02,0x82,0x01,0x00}; unsigned char header4[4] = {0x02,0x81,0x81,0x00}; unsigned char header5[3] = {0x02,0x81,0x80}; unsigned char e_65537[5] = {0x02,0x03,0x01,0x00,0x01}; unsigned char e_3[3] = {0x02,0x01,0x03}; unsigned char e_17[3] = {0x02,0x01,0x11}; /****header***/ int pos = 0; memcpy(buf+pos, header1, sizeof(header1)); pos += sizeof(header1); /****N***/ if((*(unsigned char *)(key->N)& 0x80) != 0x0){ memcpy(buf+pos, header2, sizeof(header2)); pos += sizeof(header2); } else { memcpy(buf+pos, header3, sizeof(header3)); pos += sizeof(header3); } memcpy(buf+pos, key->N, 0x100); pos += 0x100; /****e***/ memcpy(buf+pos, e_65537, sizeof(e_65537)); pos += sizeof(e_65537); /****p***/ if(*(unsigned char *)key->p & 0x80 != 0x0){ memcpy(buf+pos, header4, sizeof(header4)); pos += sizeof(header4); }else { memcpy(buf+pos, header5, sizeof(header5)); pos += sizeof(header5); } memcpy(buf+pos, key->p, 0x80); pos += 0x80; /****q***/ if(*(unsigned char *)key->q & 0x80 != 0x0){ memcpy(buf+pos, header4, sizeof(header4)); pos += sizeof(header4); }else { memcpy(buf+pos, header5, sizeof(header5)); pos += sizeof(header5); } memcpy(buf+pos, key->q, 0x80); pos += 0x80; /****d mod (p-1)***/ if((*(unsigned char *)key->dP & 0x80) != 0x0){ memcpy(buf+pos, header4, sizeof(header4)); pos += sizeof(header4); }else { memcpy(buf+pos, header5, sizeof(header5)); pos += sizeof(header5); } memcpy(buf+pos, key->dP, 0x80); pos += 0x80; /****d mod (q-1)***/ if(*(unsigned char *)key->dQ & 0x80 != 0x0){ memcpy(buf+pos, header4, sizeof(header4)); pos += sizeof(header4); }else { memcpy(buf+pos, header5, sizeof(header5)); pos += sizeof(header5); } memcpy(buf+pos, key->dQ, 0x80); pos += 0x80; /****inverse q mod p***/ if(*(unsigned char *)key->qP & 0x80 != 0x0){ memcpy(buf+pos, header4, sizeof(header4)); pos += sizeof(header4); }else { memcpy(buf+pos, header5, sizeof(header5)); pos += sizeof(header5); } memcpy(buf+pos, key->qP, 0x80); pos += 0x80; printf("pos: 0x%x \n", pos); DUMP(buf, pos); return 0; } prng_state yarrow_prng; int main() { unsigned char data[]={0x0,0x1,0x2,0x3,0x4}; int x; #ifdef USE_LTM ltc_mp = ltm_desc; #elif defined(USE_TFM) ltc_mp = tfm_desc; #elif defined(USE_GMP) ltc_mp = gmp_desc; #else //extern ltc_math_descriptor EXT_MATH_LIB; //ltc_mp = EXT_MATH_LIB; #endif int hash_idx = register_hash(&sha256_desc); unsigned char hash[128]={0}; hash_state md; sha256_init(&md); sha256_process(&md, data, sizeof(data)); sha256_done(&md,hash); DUMP(hash,32); rsa_key key; int prng_idx = register_prng(&yarrow_desc); printf("prng_idx:0x%x\n",prng_idx); prng_idx = find_prng("yarrow"); if (prng_idx == -1) { fprintf(stderr, "rsa_test requires and yarrow"); return 1; } printf("prng_idx:0x%x\n",prng_idx); int ret; if ((ret = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) { fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(ret)); exit(EXIT_FAILURE); } ret = rsa_make_key(&yarrow_prng, prng_idx, 2048/8, 65537, &key); if(ret != CRYPT_OK){ printf("rsa_make_key failed ret value: 0x%x \n", ret); return -1; } printf("len:0x%x\n",mp_count_bits(key.N)); unsigned char derkey[4096] = {0}; unsigned int len = sizeof(derkey); unsigned int lenkey = 256; //DUMP(&key, lenkey); printf("transfer_rsakey_der.........\n"); transfer_rsakey_der(&key, &derkey[0]); printf("return \n"); return 0; }

浙公网安备 33010602011771号