Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2015, Intel Corporation |
| 3 | // All rights reserved. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 6 | // modification, are permitted provided that the following conditions are met: |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 9 | // this list of conditions and the following disclaimer. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions and the following disclaimer in the documentation |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 13 | // and/or other materials provided with the distribution. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | //**********************************************************************; |
| 27 | |
Philip Tricca | dd3b03c | 2017-03-05 11:38:08 -0800 | [diff] [blame] | 28 | #include "sapi/tpm20.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 29 | #include "sample.h" |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 32 | #include "sysapi_util.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 33 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 34 | // |
| 35 | // This function is a helper function used to calculate cpHash and rpHash. |
| 36 | // |
| 37 | // NOTE: for calculating cpHash, set responseCode to TPM_RC_NO_RESPONSE; this |
| 38 | // tells the function to leave it out of the calculation. |
| 39 | // |
| 40 | TPM_RC TpmCalcPHash( TSS2_SYS_CONTEXT *sysContext, TPM_HANDLE handle1, TPM_HANDLE handle2, |
| 41 | TPMI_ALG_HASH authHash, TPM_RC responseCode, TPM2B_DIGEST *pHash ) |
| 42 | { |
| 43 | TPM_RC rval = TPM_RC_SUCCESS; |
| 44 | UINT32 i; |
| 45 | TPM2B_NAME name1; |
| 46 | TPM2B_NAME name2; |
| 47 | TPM2B_MAX_BUFFER hashInput; // Byte stream to be hashed to create pHash |
| 48 | UINT8 *hashInputPtr; |
| 49 | size_t parametersSize; |
| 50 | const uint8_t *startParams; |
| 51 | UINT8 cmdCode[4] = {0,0,0,0}; |
| 52 | UINT8 *cmdCodePtr = &cmdCode[0]; |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 53 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 54 | name1.b.size = name2.b.size = 0; |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 55 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 56 | // Calculate pHash |
| 57 | // |
| 58 | |
| 59 | // Only get names for commands |
| 60 | if( responseCode == TPM_RC_NO_RESPONSE ) |
| 61 | { |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 62 | if( handle1 == TPM_HT_NO_HANDLE ) |
| 63 | { |
| 64 | name1.t.size = 0; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | // Get names for the handles |
| 69 | rval = TpmHandleToName( handle1, &name1 ); |
| 70 | if( rval != TPM_RC_SUCCESS ) |
| 71 | return rval; |
| 72 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | #ifdef DEBUG |
Philip Tricca | b33221d | 2016-03-10 15:37:24 -0800 | [diff] [blame] | 76 | DebugPrintf( 0, "\n\nNAME1 = \n" ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 77 | PrintSizedBuffer( &(name1.b) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 78 | #endif |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 79 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 80 | // Only get names for commands |
| 81 | if( responseCode == TPM_RC_NO_RESPONSE ) |
| 82 | { |
| 83 | rval = Tss2_Sys_GetCpBuffer( sysContext, ¶metersSize, &startParams); |
| 84 | if( rval != TPM_RC_SUCCESS ) |
| 85 | return rval; |
| 86 | |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 87 | if( handle2 == TPM_HT_NO_HANDLE ) |
| 88 | { |
| 89 | name2.t.size = 0; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | rval = TpmHandleToName( handle2, &name2 ); |
| 94 | if( rval != TPM_RC_SUCCESS ) |
| 95 | return rval; |
| 96 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 97 | } |
| 98 | else |
| 99 | { |
| 100 | rval = Tss2_Sys_GetRpBuffer( sysContext, ¶metersSize, &startParams); |
| 101 | if( rval != TPM_RC_SUCCESS ) |
| 102 | return rval; |
| 103 | } |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 104 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 105 | #ifdef DEBUG |
Philip Tricca | b33221d | 2016-03-10 15:37:24 -0800 | [diff] [blame] | 106 | DebugPrintf( 0, "\n\nNAME2 = \n" ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 107 | PrintSizedBuffer( &(name2.b) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 108 | #endif |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 109 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 110 | // Create pHash input byte stream: first add response code, if any. |
| 111 | hashInput.b.size = 0; |
| 112 | if( responseCode != TPM_RC_NO_RESPONSE ) |
| 113 | { |
| 114 | hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] ); |
| 115 | *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( responseCode ); |
| 116 | hashInput.b.size += 4; |
| 117 | hashInputPtr += 4; |
| 118 | } |
| 119 | |
| 120 | // Create pHash input byte stream: now add command code. |
| 121 | rval = Tss2_Sys_GetCommandCode( sysContext, &cmdCode ); |
| 122 | if( rval != TPM_RC_SUCCESS ) |
| 123 | return rval; |
| 124 | |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 125 | hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 126 | *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( *(UINT32 *)cmdCodePtr ); |
| 127 | hashInput.t.size += 4; |
| 128 | |
| 129 | // Create pHash input byte stream: now add in names for the handles. |
| 130 | rval = ConcatSizedByteBuffer( &hashInput, &( name1.b ) ); |
| 131 | if( rval != TPM_RC_SUCCESS ) |
| 132 | return rval; |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 133 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 134 | rval = ConcatSizedByteBuffer( &hashInput, &( name2.b ) ); |
| 135 | if( rval != TPM_RC_SUCCESS ) |
| 136 | return rval; |
| 137 | |
| 138 | if( ( hashInput.t.size + parametersSize ) <= sizeof( hashInput.t.buffer ) ) |
| 139 | { |
| 140 | // Create pHash input byte stream: now add in parameters byte stream |
| 141 | for( i = 0; i < parametersSize; i++ ) |
| 142 | hashInput.t.buffer[hashInput.t.size + i ] = startParams[i]; |
| 143 | hashInput.t.size += (UINT16)parametersSize; |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | return( APPLICATION_ERROR( TSS2_BASE_RC_INSUFFICIENT_BUFFER ) ); |
| 148 | |
| 149 | } |
| 150 | #ifdef DEBUG |
Philip Tricca | b33221d | 2016-03-10 15:37:24 -0800 | [diff] [blame] | 151 | DebugPrintf( 0, "\n\nPHASH input bytes= \n" ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 152 | PrintSizedBuffer( &(hashInput.b) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 153 | #endif |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 154 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 155 | // Now hash the whole mess. |
| 156 | if( hashInput.t.size > sizeof( hashInput.t.buffer ) ) |
| 157 | { |
| 158 | rval = APPLICATION_ERROR( TSS2_BASE_RC_INSUFFICIENT_BUFFER ); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | rval = TpmHash( authHash, hashInput.t.size, &( hashInput.t.buffer[0] ), pHash ); |
| 163 | if( rval != TPM_RC_SUCCESS ) |
| 164 | return rval; |
| 165 | #ifdef DEBUG |
Philip Tricca | b33221d | 2016-03-10 15:37:24 -0800 | [diff] [blame] | 166 | DebugPrintf( 0, "\n\nPHASH = " ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 167 | PrintSizedBuffer( &(pHash->b) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 168 | #endif |
| 169 | } |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 170 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 171 | return rval; |
wcarthur | 098f8c0 | 2016-03-30 12:08:14 -0400 | [diff] [blame] | 172 | } |