文档章节

使用OPENSSL模拟TPM2.0密钥迁移实验一之target TPM源码

 宋小八
发布于 2017/12/14 20:58
字数 3567
阅读 48
收藏 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"
//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;
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

© 著作权归作者所有

粉丝 2
博文 19
码字总数 37798
作品 0
成都
私信 提问
使用Openssl进行PKI实践(含多级证书详细步骤)

Openssl是Linux下的基础安全工具 先简要介绍一些基础知识: Openssl功能主要有:对称加密(DES、3DES、AES等),非对称加密(RSA),散列(MD5、SHA1等)以及证书的相关操作(创建、申请、颁...

niceguy_php
2014/04/02
365
0
自建证书实践(OpenSSL)

常用排序之快速排序 问题描述 原材料 加工过程 --- 问题描述 证书要钱还贵,但金融系统报文必须要进行安全传输,怎么办,自己生成证书吧,这篇博客记录的背景是: 生成一对 RSA 钥匙,公钥采用...

范老司
2015/06/05
2.2K
3
CentOS升级OpenSSL至OpenSSL 1.1.0f版本<其中有遇到libcrypto.so

采用源码安装,然后再做相关的链接指向. 一、检查当前环境 1. 查看当前版本 openssl version 或者使用 yum info openssl 2. 在升级之前检查一下openssl的路径 which openssl 因为需要在升级o...

sailikung
2018/05/16
0
0
qt5.8_for_vs2015 and openssl静态编译

关于qt5.8版本的通用编译参考<>,现假定已经静态编译了qt5.8版本,需要追加对openssl的静态编译。 就网络network模块进行编译举例: 1)qt5.8使用的openssl版本为1.0.2,其他平台可以去http:...

py_tty_0529
2018/07/31
0
0
Linux openssh openssl

Linux openssh openssl 笔记日期20180524 目录 openssh 配置ssh基于密钥的方式认证 服务器端配置文件 ssh服务的最佳实践 OpenSSL 三个组件 PKI: Public Key Infrastructure 建立私有CA 证书申...

Winthcloud
2018/06/29
0
0

没有更多内容

加载失败,请刷新页面

加载更多

代理模式之JDK动态代理 — “JDK Dynamic Proxy“

动态代理的原理是什么? 所谓的动态代理,他是一个代理机制,代理机制可以看作是对调用目标的一个包装,这样我们对目标代码的调用不是直接发生的,而是通过代理完成,通过代理可以有效的让调...

code-ortaerc
18分钟前
2
0
学习记录(day05-标签操作、属性绑定、语句控制、数据绑定、事件绑定、案例用户登录)

[TOC] 1.1.1标签操作v-text&v-html v-text:会把data中绑定的数据值原样输出。 v-html:会把data中值输出,且会自动解析html代码 <!--可以将指定的内容显示到标签体中--><标签 v-text=""></......

庭前云落
50分钟前
7
0
VMware vSphere的两种RDM磁盘

在VMware vSphere vCenter中创建虚拟机时,可以添加一种叫RDM的磁盘。 RDM - Raw Device Mapping,原始设备映射,那么,RDM磁盘是不是就可以称作为“原始设备映射磁盘”呢?这也是一种可以热...

大别阿郎
今天
10
0
【AngularJS学习笔记】02 小杂烩及学习总结

本文转载于:专业的前端网站☞【AngularJS学习笔记】02 小杂烩及学习总结 表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names | orderBy ......

前端老手
昨天
14
0
Linux 内核的五大创新

在科技行业,创新这个词几乎和革命一样到处泛滥,所以很难将那些夸张的东西与真正令人振奋的东西区分开来。Linux内核被称为创新,但它又被称为现代计算中最大的奇迹,一个微观世界中的庞然大...

阮鹏
昨天
18
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部