aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/gowin
diff options
context:
space:
mode:
Diffstat (limited to 'techlibs/gowin')
0 files changed, 0 insertions, 0 deletions
5' href='#n45'>45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
// ===================================================================
// 
// Copyright (c) 2005, Intel Corp.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
//
//   * Redistributions of source code must retain the above copyright 
//     notice, this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above 
//     copyright notice, this list of conditions and the following 
//     disclaimer in the documentation and/or other materials provided 
//     with the distribution.
//   * Neither the name of Intel Corporation nor the names of its 
//     contributors may be used to endorse or promote products derived
//     from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// ===================================================================
// 
// vtsp.c
// 
//  Higher level interface to TCS for use in service.
//
// ==================================================================

#include <string.h>
#include "tcg.h"
#include "tcs.h"
#include "bsg.h"
#include "log.h"
#include "crypto.h"
#include "vtsp.h"
#include "buffer.h"

#define  RSA_KEY_SIZE 0x0800

/***********************************************************************************
 * GenerateAuth: Generate authorization info to be sent back to application
 *
 * Parameters: outParamDigestText  The concatenation of output parameters to be SHA1ed
 *    outParamDigestTextSize Size of inParamDigestText
 *    HMACkey     Key to be used for HMACing
 *          For OIAP use key.authUsage or PersistStore.ownerAuth
 *          For OSAP use shared secret
 *    pAuth     Authorization information from the application
 *
 * Return:  TPM_SUCCESS   Authorization data created
 *    TPM_AUTHFAIL   Invalid (NULL) HMACkey presented for OSAP
 *************************************************************************************/
TPM_RESULT GenerateAuth( /*[IN]*/ const BYTE *inParamDigestText,
			 /*[IN]*/ UINT32 inParamDigestTextSize,
			 /*[IN]*/ const TPM_SECRET *HMACkey,  
			 /*[IN,OUT]*/ TCS_AUTH *auth) {
    
  if (inParamDigestText == NULL || auth == NULL) 
    return (TPM_AUTHFAIL);
  else {
    
    //Generate new OddNonce
    Crypto_GetRandom(auth->NonceOdd.nonce, sizeof(TPM_NONCE));
    
    // Create SHA1 inParamDigest
    TPM_DIGEST inParamDigest;
    Crypto_SHA1Full(inParamDigestText, inParamDigestTextSize, (BYTE *) &inParamDigest);
    
    // Create HMAC text. (Concat inParamsDigest with inAuthSetupParams).
    BYTE hmacText[sizeof(TPM_DIGEST) + (2 * sizeof(TPM_NONCE)) + sizeof(BOOL)];
    
    BSG_PackList(   hmacText, 4, 
		    BSG_TPM_DIGEST, &inParamDigest,
		    BSG_TPM_NONCE, &(auth->NonceEven),
		    BSG_TPM_NONCE, &(auth->NonceOdd), 
		    BSG_TYPE_BOOL, &(auth->fContinueAuthSession) );
    
    Crypto_HMAC((BYTE *) hmacText, sizeof(hmacText), (BYTE *) HMACkey, sizeof(TPM_DIGEST), (BYTE *) &(auth->HMAC));
    
    return(TPM_SUCCESS);
    
  }
}

/***********************************************************************************
 * VerifyAuth: Verify the authdata for a command requiring authorization
 *
 * Parameters: inParamDigestText  The concatenation of parameters to be SHA1ed
 *    inParamDigestTextSize Size of inParamDigestText
 *    authDataUsage   AuthDataUsage for the Entity being used
 *          Key->authDataUsage or TPM_AUTH_OWNER
 *    HMACkey     Key to be used for HMACing
 *          For OIAP use key.authUsage or PersistStore.ownerAuth
 *          For OSAP use NULL (It will be aquired from the Auth Session)
 *          If unknown (default), assume OIAP
 *    sessionAuth    A TCS_AUTH info for the session
 *    pAuth     Authorization information from the application
 *              hContext        If specified, on failed Auth, VerifyAuth will
 *                                      generate a new OIAP session in place of themselves
 *                                      destroyed session.
 *
 * Return:  TPM_SUCCESS   Authorization Verified
 *    TPM_AUTHFAIL   Authorization Failed
 *    TPM_FAIL    Failure during SHA1 routines
 *************************************************************************************/
TPM_RESULT VerifyAuth( /*[IN]*/ const BYTE *outParamDigestText,
		       /*[IN]*/ UINT32 outParamDigestTextSize,
		       /*[IN]*/ const TPM_SECRET *HMACkey,  
		       /*[IN,OUT]*/ TCS_AUTH *auth,
		       /*[IN]*/  TCS_CONTEXT_HANDLE hContext) {
  if (outParamDigestText == NULL || auth == NULL) 
    return (TPM_AUTHFAIL);
  
  
  // Create SHA1 inParamDigest
  TPM_DIGEST outParamDigest;
  Crypto_SHA1Full(outParamDigestText, outParamDigestTextSize, (BYTE *) &outParamDigest);
  
  // Create HMAC text. (Concat inParamsDigest with inAuthSetupParams).
  TPM_DIGEST hm;
  BYTE hmacText[sizeof(TPM_DIGEST) + (2 * sizeof(TPM_NONCE)) + sizeof(BOOL)];
  
  BSG_PackList(   hmacText, 4, 
		  BSG_TPM_DIGEST, &outParamDigest,
		  BSG_TPM_NONCE, &(auth->NonceEven),
		  BSG_TPM_NONCE, &(auth->NonceOdd), 
		  BSG_TYPE_BOOL, &(auth->fContinueAuthSession) );
  
  Crypto_HMAC((BYTE *) hmacText, sizeof(hmacText),
	      (BYTE *) HMACkey, sizeof(TPM_DIGEST), (BYTE *) &hm);
    
  // Compare correct HMAC with provided one.
  if (memcmp (&hm, &(auth->HMAC), sizeof(TPM_DIGEST)) == 0) { // 0 indicates equality
    if (!auth->fContinueAuthSession) 
      vtpmloginfo(VTPM_LOG_VTSP_DEEP, "Auth Session: 0x%x closed by TPM by fContinue=0.\n", auth->AuthHandle);
    
    return (TPM_SUCCESS);
  } else {
    // If specified, reconnect the OIAP session.
    // NOTE: This only works for TCS's that never have a 0 context. 
    if (hContext) {
      vtpmloginfo(VTPM_LOG_VTSP_DEEP, "Auth Session: 0x%x closed by TPM due to failure.\n", auth->AuthHandle);
      VTSP_OIAP( hContext, auth);
    }
    return (TPM_AUTHFAIL);
  }
}

TPM_RESULT VTSP_OIAP(const TCS_CONTEXT_HANDLE hContext,
		     TCS_AUTH *auth) {
  
  vtpmloginfo(VTPM_LOG_VTSP, "OIAP.\n");
  TPM_RESULT status = TPM_SUCCESS;                           
  TPMTRYRETURN( TCSP_OIAP(hContext,
			  &auth->AuthHandle,
			  &auth->NonceEven) );

  memset(&auth->HMAC, 0, sizeof(TPM_DIGEST));
  auth->fContinueAuthSession = FALSE;

  vtpmloginfo(VTPM_LOG_VTSP_DEEP, "Auth Session: 0x%x opened by TPM_OIAP.\n", auth->AuthHandle);
  goto egress;
  
 abort_egress:
  
 egress:
  
  return status;
}

TPM_RESULT VTSP_OSAP(const TCS_CONTEXT_HANDLE hContext,
		     const TPM_ENTITY_TYPE entityType,
		     const UINT32 entityValue,
		     const TPM_AUTHDATA *usageAuth,
		     TPM_SECRET *sharedSecret, 
		     TCS_AUTH *auth) {
  
  vtpmloginfo(VTPM_LOG_VTSP, "OSAP.\n");
  TPM_RESULT status = TPM_SUCCESS;
  TPM_NONCE nonceEvenOSAP, nonceOddOSAP;
  
  Crypto_GetRandom((BYTE *) &nonceOddOSAP, sizeof(TPM_NONCE) ); 
  
  TPMTRYRETURN( TCSP_OSAP(    hContext,
			      entityType,
			      entityValue, 
			      nonceOddOSAP,
			      &auth->AuthHandle, 
			      &auth->NonceEven, 
			      &nonceEvenOSAP) );
  
  // Calculating Session Secret
  BYTE sharedSecretText[TPM_DIGEST_SIZE * 2];
  
  BSG_PackList(  sharedSecretText, 2,
		 BSG_TPM_NONCE, &nonceEvenOSAP,
		 BSG_TPM_NONCE, &nonceOddOSAP);
  
  Crypto_HMAC(sharedSecretText, sizeof(sharedSecretText), (BYTE *) usageAuth, TPM_DIGEST_SIZE, (BYTE *) sharedSecret);       

  memset(&auth->HMAC, 0, sizeof(TPM_DIGEST));
  auth->fContinueAuthSession = FALSE;
   
  vtpmloginfo(VTPM_LOG_VTSP_DEEP, "Auth Session: 0x%x opened by TPM_OSAP.\n", auth->AuthHandle);

  goto egress;
  
 abort_egress:
  
 egress:
  
  return status;
}


TPM_RESULT VTSP_TerminateHandle(const TCS_CONTEXT_HANDLE hContext,
                                const TCS_AUTH *auth) {

  vtpmloginfo(VTPM_LOG_VTSP, "Terminate Handle.\n");
  TPM_RESULT status = TPM_SUCCESS;
  TPMTRYRETURN( TCSP_TerminateHandle(hContext, auth->AuthHandle) );

  vtpmloginfo(VTPM_LOG_VTSP_DEEP, "Auth Session: 0x%x closed by TPM_TerminateHandle.\n", auth->AuthHandle);
  goto egress;

 abort_egress:

 egress:

  return status;
}


TPM_RESULT VTSP_ReadPubek(   const TCS_CONTEXT_HANDLE hContext,
                             CRYPTO_INFO *crypto_info) {
  
  TPM_RESULT status;
  TPM_NONCE antiReplay;
  TPM_DIGEST   checksum;
  BYTE *pubEKtext;
  UINT32 pubEKtextsize;
  
  vtpmloginfo(VTPM_LOG_VTSP, "Reading Public EK.\n");
  
  // GenerateAuth new nonceOdd    
  Crypto_GetRandom(&antiReplay, sizeof(TPM_NONCE) );
  
  
  TPMTRYRETURN( TCSP_ReadPubek(  hContext,
				 antiReplay,
				 &pubEKtextsize,
				 &pubEKtext,
				 &checksum) );
  
  
  // Extract the remaining output parameters
  TPM_PUBKEY pubEK;
  
  BSG_Unpack(BSG_TPM_PUBKEY, pubEKtext, (BYTE *) &pubEK);
  
  // Build CryptoInfo for the bindingKey
  TPM_RSA_KEY_PARMS rsaKeyParms;
  
  BSG_Unpack(BSG_TPM_RSA_KEY_PARMS, 
	     pubEK.algorithmParms.parms, 
	     &rsaKeyParms);
  
  Crypto_RSABuildCryptoInfoPublic(rsaKeyParms.exponentSize, 
				  rsaKeyParms.exponent, 
				  pubEK.pubKey.keyLength, 
				  pubEK.pubKey.key, 
				  crypto_info);
    
  // Destroy rsaKeyParms
  BSG_Destroy(BSG_TPM_RSA_KEY_PARMS, &rsaKeyParms);

  // Set encryption scheme
  crypto_info->encScheme = CRYPTO_ES_RSAESOAEP_SHA1_MGF1;
  //crypto_info->encScheme = pubEK.algorithmParms.encScheme;
  crypto_info->algorithmID = pubEK.algorithmParms.algorithmID;
  
  goto egress;
  
 abort_egress:
  
 egress:
  
  return status;
}

TPM_RESULT VTSP_TakeOwnership(   const TCS_CONTEXT_HANDLE hContext,
                                 const TPM_AUTHDATA *ownerAuth, 
                                 const TPM_AUTHDATA *srkAuth,
                                 CRYPTO_INFO *ek_cryptoInfo,
                                 TCS_AUTH *auth) {
  
  vtpmloginfo(VTPM_LOG_VTSP, "Taking Ownership of TPM.\n");
  
  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_TakeOwnership;
  TPM_PROTOCOL_ID proto_id = TPM_PID_OWNER;
  BYTE *new_srk;
  
  BYTE *paramText;        // Digest to make Auth.
  UINT32 paramTextSize;
  
  // vars for srkpubkey parameter
  TPM_KEY srkPub;
  TPM_KEY_PARMS srkKeyInfo = {TPM_ALG_RSA, TPM_ES_RSAESOAEP_SHA1_MGF1, TPM_SS_NONE, 12, 0};
  BYTE srkRSAkeyInfo[12] = { 0x00, 0x00, (RSA_KEY_SIZE >> 8), 0x00,   0x00, 0x00, 0x00, 0x02,   0x00, 0x00, 0x00, 0x00};
  srkKeyInfo.parms = (BYTE *) &srkRSAkeyInfo;
  
  struct pack_buf_t srkText;
  
  //These values are accurate for an enc(AuthData).
  struct pack_buf_t encOwnerAuth, encSrkAuth;
  
  encOwnerAuth.data = (BYTE *)malloc(sizeof(BYTE) * 256);
  encSrkAuth.data = (BYTE *)malloc(sizeof(BYTE) * 256);
  
  if (encOwnerAuth.data == NULL || encSrkAuth.data == NULL) {
    vtpmloginfo(VTPM_LOG_VTSP, "Could not malloc encrypted auths.\n");
    status = TPM_RESOURCES;
    goto abort_egress;
  }
  
  Crypto_RSAEnc(ek_cryptoInfo, sizeof(TPM_SECRET), (BYTE *) ownerAuth, &encOwnerAuth.size, encOwnerAuth.data);
  Crypto_RSAEnc(ek_cryptoInfo, sizeof(TPM_SECRET), (BYTE *) srkAuth, &encSrkAuth.size, encSrkAuth.data);
  
  
  // Build srk public key struct
  srkPub.ver = TPM_STRUCT_VER_1_1;
  srkPub.keyUsage = TPM_KEY_STORAGE;
  srkPub.keyFlags = 0x00;
  srkPub.authDataUsage = TPM_AUTH_ALWAYS;
  memcpy(&srkPub.algorithmParms, &srkKeyInfo, sizeof(TPM_KEY_PARMS));
  srkPub.PCRInfoSize = 0;
  srkPub.PCRInfo = 0;
  srkPub.pubKey.keyLength= 0;
  srkPub.encDataSize = 0;
  
  srkText.data = (BYTE *) malloc(sizeof(BYTE) * TCPA_MAX_BUFFER_LENGTH);
  srkText.size = BSG_Pack(BSG_TPM_KEY, (BYTE *) &srkPub, srkText.data);
  
  paramText = (BYTE *) malloc(sizeof(BYTE) *  TCPA_MAX_BUFFER_LENGTH);
  
  paramTextSize = BSG_PackList(paramText, 5,
			       BSG_TPM_COMMAND_CODE,&command,
			       BSG_TPM_PROTOCOL_ID, &proto_id,
			       BSG_TPM_SIZE32_DATA, &encOwnerAuth,
			       BSG_TPM_SIZE32_DATA, &encSrkAuth,
			       BSG_TPM_KEY, &srkPub);
  
  TPMTRYRETURN( GenerateAuth( paramText, paramTextSize, ownerAuth, auth) );
  
  new_srk = srkText.data;
  TPMTRYRETURN( TCSP_TakeOwnership ( hContext,
				     proto_id,
				     encOwnerAuth.size, 
				     encOwnerAuth.data,
				     encSrkAuth.size,
				     encSrkAuth.data,
				     &srkText.size,
				     &new_srk, 
				     auth ) );
  
  
  paramTextSize = BSG_PackList(paramText, 2, 
			       BSG_TPM_RESULT, &status,
			       BSG_TPM_COMMAND_CODE, &command);
  memcpy(paramText + paramTextSize, new_srk, srkText.size);
  paramTextSize += srkText.size;
  
  
  TPMTRYRETURN( VerifyAuth(  paramText, paramTextSize,
			     ownerAuth, auth, 
			     hContext) );
  
  goto egress;
  
 abort_egress:
  
 egress:
  
  free(srkText.data);
  free(encSrkAuth.data);
  free(encOwnerAuth.data);
  free(paramText);
  
  TCS_FreeMemory(hContext, new_srk);
  
  return status;
}

TPM_RESULT VTSP_DisablePubekRead( const TCS_CONTEXT_HANDLE    hContext,
                                  const TPM_AUTHDATA          *ownerAuth, 
                                  TCS_AUTH                    *auth) {
  
  vtpmloginfo(VTPM_LOG_VTSP, "Disabling Pubek Read.\n");
  
  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_DisablePubekRead;
  
  BYTE *paramText;        // Digest to make Auth.
  UINT32 paramTextSize;
    
  paramText = (BYTE *) malloc(sizeof(BYTE) * TCPA_MAX_BUFFER_LENGTH);
  
  paramTextSize = BSG_PackList(paramText, 1,
			       BSG_TPM_COMMAND_CODE, &command);
  
  TPMTRYRETURN( GenerateAuth( paramText, paramTextSize,
			      ownerAuth, auth) );
  
  // Call TCS
  TPMTRYRETURN( TCSP_DisablePubekRead ( hContext, // in
                                        auth) );
  
  // Verify Auth
  paramTextSize = BSG_PackList(paramText, 2,
			       BSG_TPM_RESULT, &status,
			       BSG_TPM_COMMAND_CODE, &command);
  
  TPMTRYRETURN( VerifyAuth( paramText, paramTextSize,
			    ownerAuth, auth, 
			    hContext) );
  goto egress;
  
 abort_egress:
 egress:
  free(paramText);
  return status;
}

TPM_RESULT VTSP_CreateWrapKey(  const TCS_CONTEXT_HANDLE hContext,
                                const TPM_KEY_USAGE      usage,
                                const TPM_AUTHDATA       *newKeyAuth,
                                const TCS_KEY_HANDLE     parentHandle, 
                                const TPM_AUTHDATA       *osapSharedSecret,
                                buffer_t                 *pubKeyBuf,
                                TCS_AUTH                 *auth) {
  
  int i;
  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_CreateWrapKey;
  
  vtpmloginfo(VTPM_LOG_VTSP, "Creating new key of type %d.\n", usage);
  
  // vars for Calculate encUsageAuth
  BYTE *paramText;      
  UINT32 paramTextSize;
  
  // vars for Calculate encUsageAuth
  BYTE XORbuffer[sizeof(TPM_SECRET) + sizeof(TPM_NONCE)];
  TPM_DIGEST XORKey1;
  UINT32 XORbufferSize;
  TPM_SECRET encUsageAuth, encMigrationAuth;
  
  // vars for Flatten newKey prototype
  BYTE *flatKey = (BYTE *) malloc(sizeof(BYTE) *  TCPA_MAX_BUFFER_LENGTH);
  UINT32 flatKeySize = TCPA_MAX_BUFFER_LENGTH;                                    
  struct pack_buf_t newKeyText;
  
  // Fill in newKey
  TPM_KEY newKey;
  
  BYTE RSAkeyInfo[12] = { 0x00, 0x00, (RSA_KEY_SIZE >> 8), 0x00,   0x00, 0x00, 0x00, 0x02,   0x00, 0x00, 0x00, 0x00};
  newKey.algorithmParms.algorithmID = TPM_ALG_RSA;
  newKey.algorithmParms.parms = (BYTE *) &RSAkeyInfo;
  newKey.algorithmParms.parmSize = 12;
  
  switch (usage) {
  case TPM_KEY_SIGNING:
    vtpmloginfo(VTPM_LOG_VTSP, "Creating Signing Key...\n");
    newKey.keyUsage = TPM_KEY_SIGNING;
    newKey.algorithmParms.encScheme = TPM_ES_NONE;
    newKey.algorithmParms.sigScheme = TPM_SS_RSASSAPKCS1v15_SHA1;
    break;
  case TPM_KEY_STORAGE:
    vtpmloginfo(VTPM_LOG_VTSP, "Creating Storage Key...\n");
    newKey.keyUsage = TPM_KEY_STORAGE;
    newKey.algorithmParms.encScheme = TPM_ES_RSAESOAEP_SHA1_MGF1;
    newKey.algorithmParms.sigScheme = TPM_SS_NONE;
    break;
  case TPM_KEY_BIND:
    vtpmloginfo(VTPM_LOG_VTSP, "Creating Binding Key...\n");
    newKey.keyUsage = TPM_KEY_BIND;
    newKey.algorithmParms.encScheme = TPM_ES_RSAESOAEP_SHA1_MGF1;
    newKey.algorithmParms.sigScheme = TPM_SS_NONE;
    break;
  default:
    vtpmloginfo(VTPM_LOG_VTSP, "Cannot create key. Invalid Key Type.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  
  newKey.ver = TPM_STRUCT_VER_1_1;
  
  newKey.keyFlags = 0;
  newKey.authDataUsage = TPM_AUTH_ALWAYS;
  newKey.pubKey.keyLength= 0;
  newKey.encDataSize = 0;
  newKey.encData = NULL;
  
  // FIXME: Support PCR bindings
  newKey.PCRInfoSize = 0;
  newKey.PCRInfo = NULL;
  
  // Calculate encUsageAuth                                    
  XORbufferSize = BSG_PackList(  XORbuffer, 2, 
				 BSG_TPM_SECRET, osapSharedSecret,
				 BSG_TPM_NONCE, &auth->NonceEven);
  Crypto_SHA1Full(XORbuffer, XORbufferSize, (BYTE *) &XORKey1);
  
  // FIXME: No support for migratable keys.
  for (i=0; i < TPM_DIGEST_SIZE; i++) 
    ((BYTE *) &encUsageAuth)[i] = ((BYTE *) &XORKey1)[i] ^ ((BYTE *) newKeyAuth)[i];
  
  // Flatten newKey prototype
  flatKeySize = BSG_Pack(BSG_TPM_KEY, (BYTE *) &newKey, flatKey);
  newKeyText.data = flatKey;
  newKeyText.size = flatKeySize;
  
  // Generate HMAC
  paramText = (BYTE *) malloc(sizeof(BYTE) * TCPA_MAX_BUFFER_LENGTH);
  
  paramTextSize = BSG_PackList(paramText, 3,
			       BSG_TPM_COMMAND_CODE, &command,
			       BSG_TPM_AUTHDATA, &encUsageAuth,
			       BSG_TPM_AUTHDATA, &encMigrationAuth);
  memcpy(paramText + paramTextSize, newKeyText.data, newKeyText.size);
  paramTextSize += newKeyText.size;
  
  
  TPMTRYRETURN( GenerateAuth( paramText, paramTextSize,
			      osapSharedSecret, auth) );
  
  // Call TCS
  TPMTRYRETURN( TCSP_CreateWrapKey(  hContext, 
				     parentHandle,
				     encUsageAuth,
				     encMigrationAuth,
				     &newKeyText.size,
				     &newKeyText.data,
				     auth) );
  
  // Verify Auth
  paramTextSize = BSG_PackList(paramText, 2,
			       BSG_TPM_RESULT, &status,
			       BSG_TPM_COMMAND_CODE, &command);
  memcpy(paramText + paramTextSize, newKeyText.data, newKeyText.size);
  paramTextSize += newKeyText.size;
  
  TPMTRYRETURN( VerifyAuth( paramText, paramTextSize,
			    osapSharedSecret, auth, 0) );
  
  // Unpack/return key structure
  TPMTRYRETURN(buffer_init(pubKeyBuf, 0, 0) );
  TPMTRYRETURN(buffer_append_raw(pubKeyBuf, newKeyText.size, newKeyText.data) );
  
  goto egress;
  
 abort_egress:
  
 egress:
  
  free(flatKey);
  free(paramText);
  TCS_FreeMemory(hContext, newKeyText.data);
  
  return status;
}

TPM_RESULT VTSP_LoadKey(const TCS_CONTEXT_HANDLE    hContext,
                        const TCS_KEY_HANDLE        hUnwrappingKey,
                        const buffer_t              *rgbWrappedKeyBlob,
                        const TPM_AUTHDATA          *parentAuth,
                        TPM_HANDLE                  *newKeyHandle,
                        TCS_AUTH                    *auth,
                        CRYPTO_INFO                 *cryptoinfo,
                        const BOOL                  skipTPMLoad) { 
  
  
  vtpmloginfo(VTPM_LOG_VTSP, "Loading Key %s.\n", (!skipTPMLoad ? "into TPM" : "only into memory"));
  
  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_LoadKey;

  BYTE *paramText=NULL;        // Digest to make Auth.
  UINT32 paramTextSize;

  // SkipTPMLoad stops key from being loaded into TPM, but still generates CRYPTO_INFO for it
  if (! skipTPMLoad) { 
  
    if ((rgbWrappedKeyBlob == NULL) || (parentAuth == NULL) || 
        (newKeyHandle==NULL) || (auth==NULL)) {
      status = TPM_BAD_PARAMETER;
      goto abort_egress;
    }
  
    // Generate Extra TCS Parameters
    TPM_HANDLE phKeyHMAC;
  
    paramText = (BYTE *) malloc(sizeof(BYTE) *  TCPA_MAX_BUFFER_LENGTH);
  
    paramTextSize = BSG_PackList(paramText, 1,
  			         BSG_TPM_COMMAND_CODE, &command);
  
    memcpy(paramText + paramTextSize, rgbWrappedKeyBlob->bytes, buffer_len(rgbWrappedKeyBlob));
    paramTextSize += buffer_len(rgbWrappedKeyBlob);
  
    TPMTRYRETURN( GenerateAuth( paramText, paramTextSize,
			      parentAuth, auth) );
  
    // Call TCS
    TPMTRYRETURN( TCSP_LoadKeyByBlob(  hContext,
				       hUnwrappingKey,
				       buffer_len(rgbWrappedKeyBlob),
				       rgbWrappedKeyBlob->bytes,
				       auth,
				       newKeyHandle,
				       &phKeyHMAC) );
  
    // Verify Auth
    paramTextSize = BSG_PackList(paramText, 3,
			         BSG_TPM_RESULT, &status,
			         BSG_TPM_COMMAND_CODE, &command,
			         BSG_TPM_HANDLE, newKeyHandle);
  
    TPMTRYRETURN( VerifyAuth( paramText, paramTextSize,
			      parentAuth, auth, 
			      hContext) );
  } 
  
  // Build cryptoinfo structure for software crypto function. 
  if (cryptoinfo != NULL) {
    TPM_KEY newKey;
    
    // Unpack/return key structure
    BSG_Unpack(BSG_TPM_KEY, rgbWrappedKeyBlob->bytes , &newKey);
    TPM_RSA_KEY_PARMS rsaKeyParms;
    
    BSG_Unpack(BSG_TPM_RSA_KEY_PARMS, 
	       newKey.algorithmParms.parms, 
	       &rsaKeyParms);
    
    Crypto_RSABuildCryptoInfoPublic(rsaKeyParms.exponentSize, 
				    rsaKeyParms.exponent, 
				    newKey.pubKey.keyLength, 
				    newKey.pubKey.key, 
				    cryptoinfo);
    
    // Destroy rsaKeyParms
    BSG_Destroy(BSG_TPM_RSA_KEY_PARMS, &rsaKeyParms);
    
    // Set encryption scheme
    cryptoinfo->encScheme = CRYPTO_ES_RSAESOAEP_SHA1_MGF1;
  }
  
  goto egress;
  
 abort_egress:
  
 egress:
  
  free(paramText);
  return status;
}

TPM_RESULT VTSP_Unbind( const TCS_CONTEXT_HANDLE    hContext,
                        const TPM_KEY_HANDLE        key_handle,
                        const buffer_t              *bound_data,
                        const TPM_AUTHDATA          *usage_auth,
                        buffer_t                    *clear_data,
                        TCS_AUTH                    *auth) {
  
  vtpmloginfo(VTPM_LOG_VTSP, "Unbinding %d bytes of data.\n", buffer_len(bound_data));
  
  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_UnBind;
  
  BYTE *paramText;        // Digest to make Auth.
  UINT32 paramTextSize;
  
  // Generate Extra TCS Parameters
  struct pack_buf_t clear_data32;
  BYTE *clear_data_text;
  UINT32 clear_data_size;
  
  struct pack_buf_t bound_data32 = {bound_data->size, bound_data->bytes};
  
  paramText = (BYTE *) malloc(sizeof(BYTE) * TCPA_MAX_BUFFER_LENGTH);
  
  paramTextSize = BSG_PackList(paramText, 2,
			       BSG_TPM_COMMAND_CODE, &command,
			       BSG_TPM_SIZE32_DATA, &bound_data32);
  
  TPMTRYRETURN( GenerateAuth( paramText, paramTextSize,
			      usage_auth, auth) );
  
  // Call TCS
  TPMTRYRETURN( TCSP_UnBind( hContext,
			     key_handle,
			     buffer_len(bound_data),
			     bound_data->bytes,
			     auth,
			     &clear_data_size,
			     &clear_data_text) );
  
  
  // Verify Auth
  clear_data32.size = clear_data_size;
  clear_data32.data = clear_data_text;
  paramTextSize = BSG_PackList(paramText, 3,
			       BSG_TPM_RESULT, &status,
			       BSG_TPM_COMMAND_CODE, &command,
			       BSG_TPM_SIZE32_DATA, &clear_data32);
  
  TPMTRYRETURN( VerifyAuth( paramText, paramTextSize,
			    usage_auth, auth, 
			    hContext) );
  
  // Unpack/return key structure
  TPMTRYRETURN(buffer_init(clear_data, 0, 0));
  TPMTRYRETURN(buffer_append_raw (clear_data, clear_data_size, clear_data_text) );
  
  goto egress;
  
 abort_egress:
  
 egress:
  
  free(paramText);
  TCS_FreeMemory(hContext, clear_data_text);
  
  return status;
}

TPM_RESULT VTSP_Bind(   CRYPTO_INFO *cryptoInfo, 
			const buffer_t *inData, 
			buffer_t *outData)               
{
  vtpmloginfo(VTPM_LOG_VTSP, "Binding %d bytes of data.\n", buffer_len(inData));
  TPM_RESULT status = TPM_SUCCESS;
  TPM_BOUND_DATA boundData;
  UINT32 i;
  
  // Fill boundData's accessory information
  boundData.ver = TPM_STRUCT_VER_1_1;
  boundData.payload = TPM_PT_BIND;
  boundData.payloadData = inData->bytes;
  
  // Pack boundData before encryption
  BYTE* flatBoundData = (BYTE *)malloc(sizeof(BYTE) * 
				       (sizeof(TPM_VERSION) +
					sizeof(TPM_PAYLOAD_TYPE) +
					buffer_len(inData)));
  if (flatBoundData == NULL) {
    return TPM_NOSPACE;
  }
  UINT32 flatBoundDataSize = 0;
  flatBoundDataSize = BSG_PackList(  flatBoundData, 2, 
				     BSG_TPM_VERSION, &boundData.ver, 
				     BSG_TYPE_BYTE, &boundData.payload);
  
  memcpy(flatBoundData+flatBoundDataSize, inData->bytes, buffer_len(inData));
  flatBoundDataSize += buffer_len(inData);
  
  BYTE out_tmp[RSA_KEY_SIZE/8]; // RSAEnc does not do blocking, So this is what will come out.
  UINT32 out_tmp_size;
  
  // Encrypt flatBoundData
  TPMTRY(TPM_ENCRYPT_ERROR, Crypto_RSAEnc( cryptoInfo, 
                                           flatBoundDataSize, 
                                           flatBoundData, 
                                           &out_tmp_size, 
                                           out_tmp) );
  
  if (out_tmp_size > RSA_KEY_SIZE/8) {
    // The result of RSAEnc should be a fixed size based on key size.
    vtpmlogerror(VTPM_LOG_VTSP, "Enc buffer just overflowed.\n");
  }
  
  buffer_init(outData, 0, NULL);
  buffer_append_raw(outData, out_tmp_size, out_tmp);
  
  vtpmloginfo(VTPM_LOG_TXDATA, "Bind Generated[%d] = 0x", out_tmp_size);
  for(i = 0 ; i < out_tmp_size ; i++) {
    vtpmloginfomore(VTPM_LOG_TXDATA, "%2.2x ", out_tmp[i]);
  }
  vtpmloginfomore(VTPM_LOG_TXDATA, "\n");

  goto egress;
  abort_egress: 
  egress:
 
  // Free flatBoundData
  free(flatBoundData);
  
  return TPM_SUCCESS;
}

TPM_RESULT VTSP_Seal(const TCS_CONTEXT_HANDLE    hContext,
                     const TPM_KEY_HANDLE        keyHandle,
                     const TPM_AUTHDATA          *sealDataAuth,
                     const TPM_PCR_COMPOSITE     *pcrComp,
                     const buffer_t              *inData,
                     TPM_STORED_DATA             *sealedData,                                   
                     const TPM_SECRET            *osapSharedSecret,
                     TCS_AUTH                    *auth) {

  TPM_RESULT status = TPM_SUCCESS;
  TPM_COMMAND_CODE command = TPM_ORD_Seal;

  BYTE *paramText;        // Digest to make Auth.
  UINT32 paramTextSize;

  // Generate PCR_Info Struct from Comp
  TPM_PCR_INFO pcrInfo;
  UINT32 pcrInfoSize, flatpcrSize;
  BYTE flatpcr[3 +                          // PCR_Select = 3 1 byte banks
               sizeof(UINT16) +             //              2 byte UINT16
               sizeof(UINT32) +             // PCR_Comp   = 4 byte UINT32
               24 * sizeof(TPM_PCRVALUE) ]; //              up to 24 PCRs

  if (pcrComp != NULL) {
      //printf("\n\tBinding to PCRs: ");
      //for(int i = 0 ; i < pcrComp->select.sizeOfSelect ; i++)
      //printf("%2.2x", pcrComp->select.pcrSelect[i]);

      memcpy(&pcrInfo.pcrSelection, &pcrComp->select, sizeof(TPM_PCR_SELECTION));

      flatpcrSize = BSG_Pack(BSG_TPM_PCR_COMPOSITE, (BYTE *) pcrComp, flatpcr);
      Crypto_SHA1Full((BYTE *) flatpcr, flatpcrSize, (BYTE *) &(pcrInfo.digestAtRelease));
      memset(&(pcrInfo.digestAtCreation), 0, sizeof(TPM_DIGEST));
      pcrInfoSize = BSG_Pack(BSG_TPM_PCR_INFO, (BYTE *) &pcrInfo, flatpcr);
  } else {
      //printf("\n\tBinding to no PCRS.");
      pcrInfoSize = 0;
  }

  // Calculate encUsageAuth
  BYTE XORbuffer[sizeof(TPM_SECRET) + sizeof(TPM_NONCE)];
  UINT32 XORbufferSize = sizeof(XORbuffer);
  TPM_DIGEST XORKey;
  TPM_ENCAUTH encAuth;

  BSG_PackList( XORbuffer, 2,
                BSG_TPM_SECRET, osapSharedSecret,
                BSG_TPM_NONCE, &auth->NonceEven );

  Crypto_SHA1Full(XORbuffer, XORbufferSize, (BYTE *) &XORKey);

  int i;
  for (i=0; i < TPM_DIGEST_SIZE; i++)
    ((BYTE *) &encAuth)[i] = ((BYTE *) &XORKey)[i] ^ ((BYTE *) sealDataAuth)[i];

  // Generate Extra TCS Parameters
  UINT32 inDataSize = buffer_len(inData);
  struct pack_buf_t inData_pack = {inDataSize, inData->bytes};
  struct pack_buf_t pcrInfo_pack = {pcrInfoSize, flatpcr};

  UINT32 sealedDataSize;
  BYTE *flatSealedData=NULL;

  paramText = (BYTE *) malloc(sizeof(BYTE) *  TCPA_MAX_BUFFER_LENGTH);