//file system
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//openssl
#include<openssl/rsa.h>
#include<openssl/aes.h>
#include<openssl/hmac.h>
#include<openssl/pem.h>
#include<openssl/err.h>
//socket
#include<netinet/in.h> // sockaddr_in
#include<sys/types.h> // socket
#include<sys/socket.h> // socket
#include<stdio.h> // printf
#include<stdlib.h> // exit
#include<string.h> // bzero
//time
#include<time.h>
#include <stdio.h>
#include <sys/time.h>
//file path
#define PUBPARENT "parent_pub.key"
#define PRIPARENT "parent.key"
#define PRIMIG "mig.key"
#define PUBMIG "mig_pub.key"
#define NPPUBKEY "nparent_pub.key"
#define NPPRIKEY "nparent.key"
//socket parameter
#define SERVER_PORT 8000
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
#define NAME 32
//#define seed "seed"
#define npNameAlg "ecc"
//unsigned char* dupSensitive;
char *se;
char see[1024];
char* my_encrypt(char *str,char *path_key);//加密
char* my_decrypt(char *str,char *path_key);//解密
void aes_box_decrypt(unsigned char* source_string, unsigned char* des_string)
{
int iLoop = 0;
int iLen =0;
AES_KEY aes;
unsigned char key[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
if(NULL == source_string || NULL == des_string)
{
return;
}
//Generate own AES Key
for(iLoop = 0; iLoop < 16; iLoop++)
{
key[iLoop] = 32 + iLoop;
}
// Set encryption key
for (iLoop=0; iLoop<AES_BLOCK_SIZE; iLoop++)
{
iv[iLoop] = 0;
}
if (AES_set_decrypt_key(key, 128, &aes) < 0)
{
return ;
}
iLen = strlen(source_string)+1;
AES_cbc_encrypt(source_string, des_string, iLen, &aes, iv, AES_DECRYPT);
}
void aes_box_encrypt(unsigned char* source_string, unsigned char* des_string)
{
int iLoop = 0;
int iLen =0;
AES_KEY aes;
unsigned char key[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
if(NULL == source_string || NULL == des_string)
{
return;
}
//Generate own AES Key
for(iLoop = 0; iLoop < 16; iLoop++)
{
key[iLoop] = 32 + iLoop;
}
// Set encryption key
for (iLoop=0; iLoop<AES_BLOCK_SIZE; iLoop++)
{
iv[iLoop] = 0;
}
if (AES_set_encrypt_key(key, 128, &aes) < 0)
{
return ;
}
iLen = strlen(source_string)+1;
AES_cbc_encrypt(source_string, des_string, iLen, &aes, iv, AES_ENCRYPT);
}
void verify(char *source,char *hmac)
{
unsigned char* hmackey="123ecc";
unsigned char* algo,*outerHMAC;
unsigned int outerHMAClen;
int i;
HMAC_CTX ctx;
if(hmac!=NULL)
{
printf("target TPM:create hmac key......\n");
printf("target TPM:verify hamc start.......\n");
}
else
{
printf("target TPM:verify hash start......\n");
}
//进行HMAC
outerHMAC=(unsigned char*)malloc(EVP_MAX_MD_SIZE);
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,hmackey,strlen(hmackey),EVP_sha1(),NULL);
HMAC_Update(&ctx,source,strlen(source));
HMAC_Final(&ctx,outerHMAC,&outerHMAClen);
HMAC_CTX_cleanup(&ctx);
printf("target TPM:verify success!\n");
}
int my_client(int cases)
{
// 声明并初始化一个客户端的socket地址结构
struct sockaddr_in client_addr;
bzero(&client_addr, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htons(INADDR_ANY);
client_addr.sin_port = htons(0);
// 创建socket,若成功,返回socket描述符
int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(client_socket_fd < 0)
{
perror("target TPM:Create Socket Failed");
exit(1);
}
// 绑定客户端的socket和客户端的socket地址结构 非必需
if(-1 == (bind(client_socket_fd, (struct sockaddr*)&client_addr, sizeof(client_addr))))
{
perror("target TPM:Client Bind Failed");
exit(1);
}
// 声明一个服务器端的socket地址结构,并用服务器那边的IP地址及端口对其进行初始>化,用于后面的连接
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(inet_pton(AF_INET, "169.254.12.125", &server_addr.sin_addr) == 0)
{
perror("target TPM:Server IP Address Error:");
exit(1);
}
server_addr.sin_port = htons(SERVER_PORT);
socklen_t server_addr_length = sizeof(server_addr);
// 向服务器发起连接,连接成功后client_socket_fd代表了客户端和服务器的一个socket连接
if(connect(client_socket_fd, (struct sockaddr*)&server_addr, server_addr_length) < 0)
{
perror("target TPM:Can Not Connect To Server IP:");
exit(0);
}
else
{
printf("target TPM:connect source TPM success\n");
}
// 输入文件名 并放到缓冲区buffer中等待发送
char file_name[FILE_NAME_MAX_SIZE+1]="mig.key";
char buffer[BUFFER_SIZE];
char *seed,*dups,*outerhmac,*encryptionKeyout;
int length = 0;
char dups1[17]={0},sens[17]={0},du[17]={0},*keyout,privatekey[17]={0};
if(cases==2)
{
printf("target TPM:recieve migrate blob start\n");
bzero(buffer, BUFFER_SIZE);
strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));
// 向服务器发送buffer中的数据
if(send(client_socket_fd, buffer, BUFFER_SIZE, 0) < 0)
{
perror("target TPM:Send File Name Failed:");
exit(1);
}
// 打开文件,准备写入
FILE *fp = fopen(file_name, "w");
if(NULL == fp)
{
printf("target TPM:File\t%s Can Not Open To Write\n", file_name);
exit(1);
}
// 从服务器接收数据到buffer中
// 每接收一段数据,便将其写入文件中,循环直到文件接收完并写完为止
bzero(buffer, BUFFER_SIZE);
int length = 0;
while((length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0)) > 0)
{
if(fwrite(buffer, sizeof(char), length, fp) < length)
{
printf("target TPM:File\t%s Write Failed\n", file_name);
break;
}
bzero(buffer, BUFFER_SIZE);
}
}
if(cases==3)
{
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
se=my_decrypt(seed,NPPRIKEY);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get sensitive....\n");
printf("target TPM:decrypt success,get migkey's private part!\n");
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(sens,NPPUBKEY);
}
if(cases==4)
{
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
se=my_decrypt(seed,NPPRIKEY);
//printf("se==%s\n",se);
bzero(buffer, BUFFER_SIZE);
encryptionKeyout=buffer;
length = recv(client_socket_fd, encryptionKeyout, BUFFER_SIZE, 0);
//printf("encryptionKeyout==%s\n",encryptionKeyout);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
keyout=my_decrypt(encryptionKeyout,NPPRIKEY);
//printf("keyout==%s\n",keyout);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get encsensitive....\n");
aes_box_decrypt(sens,privatekey);
//printf("privatekey==%s\n",privatekey);
printf("target TPM:decrypt success,get migkey's private part!\n");
verify(privatekey,NULL);
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(privatekey,NPPUBKEY);
}
if(cases==5)
{
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0);
my_encrypt(buffer,NPPUBKEY);
}
if(cases==6)
{
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
se=my_decrypt(seed,NPPRIKEY);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get sensitive....\n");
printf("target TPM:decrypt success,get migkey's private part!\n");
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(sens,NPPUBKEY);
}
if(cases==7)
{
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
se=my_decrypt(seed,NPPRIKEY);
//printf("se==%s\n",se);
bzero(buffer, BUFFER_SIZE);
encryptionKeyout=buffer;
length = recv(client_socket_fd, encryptionKeyout, BUFFER_SIZE, 0);
//printf("encryptionKeyout==%s\n",encryptionKeyout);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
keyout=my_decrypt(encryptionKeyout,NPPRIKEY);
//printf("keyout==%s\n",keyout);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get encsensitive....\n");
aes_box_decrypt(sens,privatekey);
//printf("privatekey==%s\n",privatekey);
printf("target TPM:decrypt success,get migkey's private part!\n");
verify(privatekey,NULL);
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(privatekey,NPPUBKEY);
}
if(cases==8)
{
char temp[17]={0};
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0);
//my_encrypt(buffer,NPPUBKEY);
aes_box_encrypt(buffer,temp);
}
if(cases==9)
{
char temp9[17]={0},*stemp;
bzero(buffer, BUFFER_SIZE);
printf("target TPM: using migrate key's public key encrypt seed........\n");
stemp=my_encrypt("seed",PUBMIG);
printf("target TPM: transfer seed to source TPM ........\n");
send(client_socket_fd,stemp,BUFFER_SIZE,0);
printf("target TPM: receve data from source TPM ........\n");
//bzero(buffer, BUFFER_SIZE);
//seed=buffer;
//length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
//printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
//se=my_decrypt(seed,NPPRIKEY);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:using seed create encrypt key-ks and hmac key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get sensitive....\n");
printf("target TPM:decrypt success,get migkey's private part!\n");
printf("target TPM:using nparent key to procted migkey......\n");
aes_box_encrypt(sens,temp9);
}
if(cases==10)
{
char *temp10,*stemp10;
bzero(buffer, BUFFER_SIZE);
printf("target TPM: using migrate key's public key encrypt seed and encryptionKeyin........\n");
stemp10=my_encrypt("seed",PUBMIG);
temp10=my_encrypt("123456gg",PUBMIG);
printf("target TPM: transfer seed to source TPM ........\n");
send(client_socket_fd,stemp10,BUFFER_SIZE,0);
send(client_socket_fd,temp10,BUFFER_SIZE,0);
printf("target TPM: receve data from source TPM ........\n");
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:using seed create encrypt key-ks and hmac key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get encsensitive....\n");
aes_box_decrypt(sens,privatekey);
//printf("privatekey==%s\n",privatekey);
printf("target TPM:decrypt encsensitiven success,get migkey's private part!\n");
verify(privatekey,NULL);
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(privatekey,NPPUBKEY);
}
if(cases==11)
{
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0);
//my_encrypt(buffer,NPPUBKEY);
aes_box_encrypt(buffer,NULL);
}
if(cases==12)
{
//printf("target TPM: transfer seed to source TPM ........\n");
printf("target TPM: receve data from source TPM ........\n");
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
//se=my_decrypt(seed,NPPRIKEY);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get sensitive....\n");
printf("target TPM:decrypt success,get migkey's private part!\n");
printf("target TPM:using nparent key to procted migkey......\n");
aes_box_encrypt(sens,NULL);
}
if(cases==13)
{
printf("target TPM: receve data from source TPM ........\n");
bzero(buffer, BUFFER_SIZE);
seed=buffer;
length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
//printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
//se=my_decrypt(seed,NPPRIKEY);
//printf("se==%s\n",se);
bzero(buffer, BUFFER_SIZE);
encryptionKeyout=buffer;
length = recv(client_socket_fd, encryptionKeyout, BUFFER_SIZE, 0);
//printf("encryptionKeyout==%s\n",encryptionKeyout);
//printf("target TPM:using nparentkey's private key dencypto sysmetricsseed to get seed\n");
//keyout=my_decrypt(encryptionKeyout,NPPRIKEY);
//printf("keyout==%s\n",keyout);
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket_fd, du, BUFFER_SIZE, 0);
strcpy(dups1,du);
printf("target TPM:create encrypt key...\n");
aes_box_decrypt(du,sens);
bzero(buffer, BUFFER_SIZE);
outerhmac=buffer;
length = recv(client_socket_fd, outerhmac, BUFFER_SIZE, 0);
verify(dups1,outerhmac);
printf("target TPM:decrypt dupSensitive to get encsensitive....\n");
aes_box_decrypt(sens,privatekey);
//printf("privatekey==%s\n",privatekey);
printf("target TPM:decrypt success,get migkey's private part!\n");
verify(privatekey,NULL);
printf("target TPM:using nparent key to procted migkey......\n");
my_encrypt(privatekey,NPPUBKEY);
}
close(client_socket_fd);
return 0;
}
int my_createKey(int bits,char *prikey_path,char *pubkey_path)
{
//生成密钥对
RSA *r = RSA_new();
BIGNUM *e = BN_new();
BN_set_word(e, 65537);
RSA_generate_key_ex(r, bits, e, NULL);
//RSA_print_fp(stdout, r, 0);
BIO *pri,*pub;
pri= BIO_new_file(prikey_path,"w");
//这里生成的私钥没有加密,可选加密
int ret = PEM_write_bio_RSAPrivateKey(pri, r, NULL, NULL, 0, NULL, NULL);
//printf("writepri:%d\n",ret);
BIO_flush(pri);
BIO_free(pri);
pub = BIO_new_file(pubkey_path,"w");
ret = PEM_write_bio_RSAPublicKey(pub, r);
//printf("writepub:%d\n",ret);
BIO_flush(pub);
BIO_free(pub);
BN_free(e);
RSA_free(r);
return 1;
}
//char* my_encrypt(char *str,char *path_key);//加密
// char* my_decrypt(char *str,char *path_key);//解密
int main(void){
int c,timeuse;
unsigned char *prikey,*dups1;
unsigned char str[10]="migratekey";
FILE *migkeyfp;
char *seed1;
char source[202];
struct timeval start, end;
printf("please input which case you want to go:");
scanf("%d",&c);
int fixedTPM,fixedParent,FlagParentKey,FlagMigKey,innerwarp,outerwarp,encryptionkeyin,newParentHandle;
switch(c){
case 1:
fixedTPM=1;
fixedParent=1;
printf("target TPM:Going to case 1\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d\n",fixedTPM,fixedParent);
printf("target TPM:migration key end\n");
gettimeofday( &start, NULL );
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 2:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=0;
innerwarp=0;
outerwarp=0;
printf("target TPM:Going to case 2\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
//既不进行进行outerwarp也不进行outerwarp
my_client(2);
migkeyfp=fopen(PRIMIG,"r");
if(migkeyfp==NULL){
printf("target TPM:open file %s failed\n",PRIMIG);
return 0;
}
//把文件的位置指针移到文件尾
fseek(migkeyfp,0L,SEEK_END);
//获取文件长度
long length=ftell(migkeyfp);
//把文件的位置指针移到文件头
fseek(migkeyfp,0L,SEEK_SET);
fread(source,1,length,migkeyfp);
my_encrypt(source,NPPUBKEY);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 3:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=0;
innerwarp=0;
outerwarp=1;
printf("target TPM:Going to case 3\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
printf("target TPM:this case will only do outerwarp and the newparent and migrate key is asymmetric key\n");
printf("target TPM: receve data from source TPM start........\n");
gettimeofday( &start, NULL );
//接收数据
my_client(3);
printf("---------------target TPM:migration key finished--------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 4:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=0;
innerwarp=1;
outerwarp=1;
printf("target TPM:Going to case 4\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
my_client(4);
printf("---------------target TPM:migration key finished--------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 5:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=1;
innerwarp=0;
outerwarp=0;
printf("target TPM:Going to case 5\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
printf("target TPM: receve data from source TPM ........\n");
my_client(5);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 6:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=1;
innerwarp=0;
outerwarp=1;
printf("target TPM:Going to case 6\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
//接收数据
my_client(3);
printf("---------------target TPM:migration key finished--------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 7:
fixedTPM=0;
fixedParent=0;
FlagParentKey=0;
FlagMigKey=1;
innerwarp=1;
outerwarp=1;
printf("source TPM:Going to case 7\n");
printf("source TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------source TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
my_client(7);
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("source TPM:migration key total run time: %d us\n", timeuse);
break;
case 8:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=0;
innerwarp=0;
outerwarp=0;
printf("target TPM:Going to case 8\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
printf("target TPM: receve data from source TPM ........\n");
my_client(8);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 9:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=0;
innerwarp=0;
outerwarp=1;
printf("target TPM:Going to case 9\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
//printf("target TPM: receve data from source TPM ........\n");
my_client(9);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 10:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=0;
innerwarp=1;
outerwarp=1;
printf("target TPM:Going to case 10\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------source TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
my_client(10);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 11:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=1;
innerwarp=0;
outerwarp=0;
printf("target TPM:Going to case 11\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
printf("target TPM: receve data from source TPM ........\n");
my_client(11);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 12:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=1;
innerwarp=0;
outerwarp=1;
printf("target TPM:Going to case 12\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------target TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
my_client(12);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
case 13:
fixedTPM=0;
fixedParent=0;
FlagParentKey=1;
FlagMigKey=1;
innerwarp=1;
outerwarp=1;
printf("target TPM:Going to case 13\n");
printf("target TPM:fixedTPM=%d,fixedParent=%d,encryptionkeyin=%d,newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
printf("---------------source TPM:migration key start--------------\n");
gettimeofday( &start, NULL );
my_client(13);
printf("target TPM:use nparentkey protected migratekey finished....\n");
printf("target TPM:successfuly migrate key \n");
printf("-------------target TPM: migrate key finished----------------\n");
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("target TPM:migration key total run time: %d us\n", timeuse);
break;
default:
printf("target TPM:input error\n");
return -1;
}
return 0;
}
char *my_encrypt(char *str,char *path_key){
char *p_en;
RSA *p_rsa;
FILE *file;
int flen,rsa_len;
if((file=fopen(path_key,"r"))==NULL){
perror("source TPM: open key file error");
return NULL;
}
//if((p_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){
if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){ // 换成这句死活通不过,无论是否将公钥>分离源文件
ERR_print_errors_fp(stdout);
return NULL;
}
flen=strlen(str);
rsa_len=RSA_size(p_rsa);
p_en=(unsigned char *)malloc(rsa_len+1);
memset(p_en,0,rsa_len+1);
if(RSA_public_encrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_en,p_rsa,RSA_NO_PADDING)<0){
return NULL;
}
RSA_free(p_rsa);
fclose(file);
printf("target TPM:encrypt finished\n");
return p_en;
}
char *my_decrypt(char *str,char *path_key){
char *p_de;
RSA *p_rsa;
FILE *file;
int rsa_len;
if((file=fopen(path_key,"r"))==NULL){
perror("source TPM: open key file error");
return NULL;
}
if((p_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){
ERR_print_errors_fp(stdout);
return NULL;
}
rsa_len=RSA_size(p_rsa);
p_de=(unsigned char *)malloc(rsa_len+1);
memset(p_de,0,rsa_len+1);
if(RSA_private_decrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_de,p_rsa,RSA_NO_PADDING)<0){
return NULL;
}
RSA_free(p_rsa);
fclose(file);
return p_de;
}