文档章节

实验二 基于DA之DA.c

 宋小八
发布于 2018/01/22 15:52
字数 3942
阅读 6
收藏 0

//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"
#define PUBDA "DA_pub.key"
#define PRIDA "DA.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  hmac 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("DA:Can Not Connect To source TPM IP:");
    exit(0);
  }
  else
  {
    printf("DA:connect source TPM success\n");
  }
   // 声明并初始化一个客户端的socket地址结构
  struct sockaddr_in client_addr2;
  bzero(&client_addr2, sizeof(client_addr2));
  client_addr2.sin_family = AF_INET;
  client_addr2.sin_addr.s_addr = htons(INADDR_ANY);
  client_addr2.sin_port = htons(0);
  // 创建socket,若成功,返回socket描述符
  int client_socket_fd2 = socket(AF_INET, SOCK_STREAM, 0);
  if(client_socket_fd2 < 0)
  {
    perror("target TPM:Create Socket Failed");
    exit(1);
  }

  // 绑定客户端的socket和客户端的socket地址结构 非必需
  if(-1 == (bind(client_socket_fd2, (struct sockaddr*)&client_addr2, sizeof(client_addr2))))
  {
    perror("target TPM:Client Bind Failed");
    exit(1);
  }

  // 声明一个服务器端的socket地址结构,并用服务器那边的IP地址及端口对其进行初始>化,用于后面的连接
  struct sockaddr_in server_addr2;
  bzero(&server_addr2, sizeof(server_addr2));
  server_addr2.sin_family = AF_INET;
  if(inet_pton(AF_INET, "169.254.12.45", &server_addr2.sin_addr) == 0)
  {
    perror("target TPM:Server IP Address Error:");
    exit(1);
}
  server_addr2.sin_port = htons(SERVER_PORT);
  socklen_t server_addr_length2 = sizeof(server_addr2);
   // 向服务器发起连接,连接成功后client_socket_fd代表了客户端和服务器的一个socket连接
  if(connect(client_socket_fd2, (struct sockaddr*)&server_addr2, server_addr_length2) < 0)
  {
    perror("DA:Can Not Connect To target TPM IP:");
    exit(0);
  }
  else
  {
    printf("DA:connect target 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};
  int RA=0;
  int Ntpm=1;
  char *Nda0="2";
  if(cases==1)
  {
    printf("------------DA:migrate key start-----------\n");
    bzero(buffer, BUFFER_SIZE);
    seed=buffer;
    aes_box_encrypt("EndEcho",buffer);
    aes_box_encrypt("EndEcho",seed);
    printf("DA:send data to source and target TPM....\n");
    send(client_socket_fd,buffer,BUFFER_SIZE,0);
    send(client_socket_fd2,seed,BUFFER_SIZE,0);
    printf("------------DA:migrate key end------------\n");
  }  

  if(cases==2)
  {
    printf("target TPM:recieve data from source TPM...\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);
    }
    //printf("target TPM:recieve data from source TPM success\n");
  }
  if(cases==3)
  {
    bzero(buffer, BUFFER_SIZE);
    seed=buffer;
    length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
    printf("target TPM:using nparentkey dencrypt 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 ks...\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 migratekey's private part!\n");
    printf("target TPM:using nparentkey to procted migratekey......\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 dencrypt 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 dencrypt to get encryptionKeyin\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 ks...\n");
    printf("target TPM:decrypt dupSensitive to get encsensitive....\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 encsensitive to get sensitiveArea....\n");
    aes_box_decrypt(sens,privatekey);
    //printf("privatekey==%s\n",privatekey);
    printf("target TPM:decrypt success,get migratekey's private part!\n");
    verify(privatekey,NULL);
    printf("target TPM:using nparentkey to procted migratekey......\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 dencrypto  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 ks...\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 migratekey's private part!\n");
    printf("target TPM:using nparentkey to procted migratekey......\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 dencrypt 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 dencrypt to get encryptionKeyin\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 ks...\n");
    printf("target TPM:decrypt dupSensitive to get encsensitive....\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 encsensitive to get sensitiveArea....\n");
    aes_box_decrypt(sens,privatekey);
    //printf("privatekey==%s\n",privatekey);
    printf("target TPM:decrypt success,get migratekey's private part!\n");
    verify(privatekey,NULL);
    printf("target TPM:using nparentkey to procted migratekey......\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 migratekey 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:recieve 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");
    printf("target TPM:decrypt dupSensitive to get sensitive....\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 sensitive to get sensitiveArea....\n");
    printf("target TPM:decrypt success,get migratekey's private part!\n");
    printf("target TPM:using nparentkey to procted migratekey......\n");
    aes_box_encrypt(sens,temp9);
  }
  if(cases==10)
  {
    char *temp10,*stemp10;
    bzero(buffer, BUFFER_SIZE);
    printf("target TPM:using migratekey encrypt seed and encryptionKeyin........\n");
    stemp10=my_encrypt("seed",PUBMIG);
    temp10=my_encrypt("123456gg",PUBMIG);
    printf("target TPM:transfer seed and encryptionKeyin to source TPM ........\n");
    send(client_socket_fd,stemp10,BUFFER_SIZE,0);
    send(client_socket_fd,temp10,BUFFER_SIZE,0);
    printf("target TPM:recieve 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");
    printf("target TPM:decrypt dupSensitive to get encsensitive....\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 encsensitive to get sensitiveArea....\n");
    aes_box_decrypt(sens,privatekey);
    //printf("privatekey==%s\n",privatekey);
    printf("target TPM:decrypt success,get migratekey's private part!\n");
    verify(privatekey,NULL);
    printf("target TPM:using nparentkey to procted migratekey......\n");
    my_encrypt(privatekey,NPPUBKEY);

  }
  if(cases==11)
  {
     printf("target TPM:recieve data from source TPM ........\n");
    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:recieve data from source TPM ........\n");
    bzero(buffer, BUFFER_SIZE);
    seed=buffer;
    length = recv(client_socket_fd, seed, BUFFER_SIZE, 0);
    //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 ks...\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 migratekey's private part!\n");
    printf("target TPM:using nparentkey to procted migratekey......\n");
    aes_box_encrypt(sens,NULL);

  }
  if(cases==13)
  {
    printf("target TPM:recieve 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 ks...\n");
    printf("target TPM:decrypt dupSensitive to get encsensitive....\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 encsensitive to get sensitiveArea....\n");
    aes_box_decrypt(sens,privatekey);
    //printf("privatekey==%s\n",privatekey);
    printf("target TPM:decrypt success,get migratekey's private part!\n");
    verify(privatekey,NULL);
    printf("target TPM:using nparentkey to procted migratekey......\n");
    my_encrypt(privatekey,NPPUBKEY);

  }
  close(client_socket_fd);
   //printf("-----------------Pdes TPM:migrate key finished------------\n\n");


   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("DA:Going to case 1\n");
                printf("DA:fixedTPM=%d,fixedParent=%d\n",fixedTPM,fixedParent);
                //printf("target TPM:migration key end\n");
                gettimeofday( &start, NULL );
                my_client(1);
                gettimeofday( &end, NULL );
                timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
                printf("DA: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\n           fixedParent=%d\n           encryptionkeyin=%d\n           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\n           fixedParent=%d\n           encryptionkeyin=%d\n           newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------target TPM:migration key start--------------\n");
                //printf("target TPM:this case will only do outerwap and the newparent and migrate key is asymmetric key\n");
        printf("target TPM:recieve data from source TPM ........\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\n           fixedParent=%d\n           encryptionkeyin=%d\n           newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------target TPM:migration key start--------------\n");
        printf("target TPM:recieve data from source TPM ........\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\n           fixedParent=%d\n           encryptionkeyin=%d\n          newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------target TPM:migration key start--------------\n");

                gettimeofday( &start, NULL );
                printf("target TPM:recieve 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\n           fixedParent=%d\n           encryptionkeyin=%d\n           newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------target TPM:migration key start--------------\n");
                printf("target TPM:recieve data from source TPM ........\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\n           fixedParent=%d\n           encryptionkeyin=%d\n           newParentHandle=!TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------source TPM:migration key start--------------\n");
                printf("target TPM:recieve data from source TPM ........\n");
                gettimeofday( &start, NULL );
        my_client(7);
                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("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("adversary TPM:Going to case 8\n");
                printf("adversary TPM:fixedTPM=%d\n             fixedParent=%d\n             encryptionkeyin=%d\n             newParentHandle=TPM_RH_NULL\n",fixedTPM,fixedParent,innerwarp);
                printf("---------------adversary TPM:migration key start--------------\n");

                gettimeofday( &start, NULL );
                printf("adversary TPM:recieve data from source TPM ........\n");
                my_client(8);
                printf("adversary TPM:use nparentkey protected  migratekey finished....\n");
                printf("adversary TPM:successfuly migrate key \n");
                printf("-------------adversary TPM: migrate key finished----------------\n");
                gettimeofday( &end, NULL );
                timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
                printf("adversary 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\n           fixedParent=%d\n           encryptionkeyin=%d\n           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\n          fixedParent=%d\n          encryptionkeyin=%d\n          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\n           fixedParent=%d\n           encryptionkeyin=%d\n           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\n           fixedParent=%d\n           encryptionkeyin=%d\n           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\n           fixedParent=%d\n           encryptionkeyin=%d\n           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;
 }

© 著作权归作者所有

粉丝 2
博文 19
码字总数 37798
作品 0
成都
私信 提问
【图像复原】SSDA论文详解(Image Denoising and Inpainting with Deep Neural Networks )

论文原文:http://papers.nips.cc/paper/4686-image-denoising-and-inpainting-with-deep-neural-networks.pdf 一、简介 论文主要介绍了一种解决盲图像去噪和图像复原问题的新方法SSDA(叠加...

李文豪
2018/11/03
0
0
PIC16F883和TLC5615(DA)数模转化实验,呼吸灯

今天上课做了数模转换的实验。感觉很好玩,在这里写下。 器材:PIC16F883控制芯片、TLC5615、LED灯一个、电阻一个(1K)、PICkit2下载器 实验说明,本实验采用I/O模拟方法实现数模转换,转换...

0101
2012/07/06
535
0
Pathomx 2.5.0 发布,代谢路径分析工具

Pathomx 2.5.0 发布,Pathomx 是一个基于工作流的工具用于分析代谢路径以及对相关实验数据进行可视化展示。 主要改进内容包括: Annotations for the workflow editor allow you to mark ar...

oschina
2014/05/02
582
0
Linux平台代码覆盖率测试工具GCOV的前端工具LCOV简介

本博客 http://blog.csdn.net/livelylittlefish 贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正! Content 1. Lcov是什么? 2. 如何在Linux平台安装Lcov? 3. 如何...

晨曦之光
2012/03/09
662
0
各种作业的整理目录

机械学课程设计 六自由度的多关节坐标测量仪(报告) 单片机实验(程序) 双机通信 8255 AD DA 矩阵键盘 LCD显示 PC-51通信 定时器 数码管 电话拨码开关 花样流水灯 简单流水灯 秒表 抢答器 ...

hit2015spring
2017/06/21
0
0

没有更多内容

加载失败,请刷新页面

加载更多

nginx学习笔记

中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯。 是连接两个独立应用程序或独立系统的软件。 web请求通过中间件可以直接调用操作系统,也可以经过中间件把请求分发到多...

码农实战
58分钟前
5
0
Spring Security 实战干货:玩转自定义登录

1. 前言 前面的关于 Spring Security 相关的文章只是一个预热。为了接下来更好的实战,如果你错过了请从 Spring Security 实战系列 开始。安全访问的第一步就是认证(Authentication),认证...

码农小胖哥
今天
9
0
JAVA 实现雪花算法生成唯一订单号工具类

import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import java.util.Calendar;/** * Default distributed primary key generator. * * <p> * Use snowflake......

huangkejie
昨天
11
0
PhotoShop 色调:RGB/CMYK 颜色模式

一·、 RGB : 三原色:红绿蓝 1.通道:通道中的红绿蓝通道分别对应的是红绿蓝三种原色(RGB)的显示范围 1.差值模式能模拟三种原色叠加之后的效果 2.添加-颜色曲线:调整图像RGB颜色----R色增强...

东方墨天
昨天
10
1
将博客搬至CSDN

将博客搬至CSDN

算法与编程之美
昨天
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部