| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Licensed Materials - Property of IBM | 3 * Licensed Materials - Property of IBM |
| 4 * | 4 * |
| 5 * trousers - An open source TCG Software Stack | 5 * trousers - An open source TCG Software Stack |
| 6 * | 6 * |
| 7 * (C) Copyright International Business Machines Corp. 2004 | 7 * (C) Copyright International Business Machines Corp. 2004 |
| 8 * | 8 * |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 * According to the TPM compliance work posted here: http://www.prosec.rub.de/tp
mcompliance.html, | 33 * According to the TPM compliance work posted here: http://www.prosec.rub.de/tp
mcompliance.html, |
| 34 * some TPMs return as little as 132 bytes per query, which would require about
30 loops to get 4K. | 34 * some TPMs return as little as 132 bytes per query, which would require about
30 loops to get 4K. |
| 35 * We'll be extremely conservative here and loop 50 times, since it won't affect
performance on | 35 * We'll be extremely conservative here and loop 50 times, since it won't affect
performance on |
| 36 * TPMs that return more bytes. | 36 * TPMs that return more bytes. |
| 37 */ | 37 */ |
| 38 TSS_RESULT | 38 TSS_RESULT |
| 39 TCSP_GetRandom_Internal(TCS_CONTEXT_HANDLE hContext, /* in */ | 39 TCSP_GetRandom_Internal(TCS_CONTEXT_HANDLE hContext, /* in */ |
| 40 UINT32 * bytesRequested, /* in, out */ | 40 UINT32 * bytesRequested, /* in, out */ |
| 41 BYTE ** randomBytes) /* out */ | 41 BYTE ** randomBytes) /* out */ |
| 42 { | 42 { |
| 43 » UINT64 offset = 0; | 43 » UINT64 offset; |
| 44 TSS_RESULT result; | 44 TSS_RESULT result; |
| 45 UINT32 paramSize, totalReturned = 0, bytesReturned, retries = 50; | 45 UINT32 paramSize, totalReturned = 0, bytesReturned, retries = 50; |
| 46 » BYTE txBlob[TSS_TPM_TXBLOB_SIZE], *rnd_tmp = NULL; | 46 » BYTE txBlob[TSS_TPM_TXBLOB_SIZE], *rnd_tmp = NULL, *rnd_tmp2 = NULL; |
| 47 | 47 |
| 48 LogDebugFn("%u bytes", *bytesRequested); | 48 LogDebugFn("%u bytes", *bytesRequested); |
| 49 | 49 |
| 50 if ((result = ctx_verify_context(hContext))) | 50 if ((result = ctx_verify_context(hContext))) |
| 51 return result; | 51 return result; |
| 52 | 52 |
| 53 do { | 53 do { |
| 54 offset = 0; |
| 54 if ((result = tpm_rqu_build(TPM_ORD_GetRandom, &offset, txBlob, | 55 if ((result = tpm_rqu_build(TPM_ORD_GetRandom, &offset, txBlob, |
| 55 *bytesRequested - totalReturned, NUL
L))) | 56 *bytesRequested - totalReturned, NUL
L))) |
| 56 break; | 57 break; |
| 57 | 58 |
| 58 if ((result = req_mgr_submit_req(txBlob))) | 59 if ((result = req_mgr_submit_req(txBlob))) |
| 59 break;; | 60 break;; |
| 60 | 61 |
| 61 result = UnloadBlob_Header(txBlob, ¶mSize); | 62 result = UnloadBlob_Header(txBlob, ¶mSize); |
| 62 if (!result) { | 63 if (!result) { |
| 63 #if 0 | 64 #if 0 |
| 64 offset = 10; | 65 offset = 10; |
| 65 UnloadBlob_UINT32(&offset, &bytesReturned, txBlob); | 66 UnloadBlob_UINT32(&offset, &bytesReturned, txBlob); |
| 66 | 67 |
| 67 LogDebugFn("received %u bytes from the TPM", bytesReturn
ed); | 68 LogDebugFn("received %u bytes from the TPM", bytesReturn
ed); |
| 68 | 69 |
| 69 rnd_tmp = realloc(rnd_tmp, totalReturned + bytesReturned
); | 70 rnd_tmp = realloc(rnd_tmp, totalReturned + bytesReturned
); |
| 70 if (rnd_tmp == NULL) { | 71 if (rnd_tmp == NULL) { |
| 71 LogError("malloc of %u bytes failed.", bytesRetu
rned); | 72 LogError("malloc of %u bytes failed.", bytesRetu
rned); |
| 72 return TCSERR(TSS_E_OUTOFMEMORY); | 73 return TCSERR(TSS_E_OUTOFMEMORY); |
| 73 } | 74 } |
| 74 UnloadBlob(&offset, bytesReturned, txBlob, &rnd_tmp[tota
lReturned]); | 75 UnloadBlob(&offset, bytesReturned, txBlob, &rnd_tmp[tota
lReturned]); |
| 75 #else | 76 #else |
| 76 /* XXX */ | 77 /* XXX */ |
| 77 if ((result = tpm_rsp_parse(TPM_ORD_GetRandom, txBlob, p
aramSize, | 78 if ((result = tpm_rsp_parse(TPM_ORD_GetRandom, txBlob, p
aramSize, |
| 78 &bytesReturned, &rnd_tmp, NU
LL, NULL))) | 79 &bytesReturned, &rnd_tmp, NU
LL, NULL))) |
| 79 break; | 80 break; |
| 80 | 81 |
| 81 » » » *randomBytes = realloc(*randomBytes, totalReturned + byt
esReturned); | 82 » » » rnd_tmp2 = realloc(*randomBytes, totalReturned + bytesRe
turned); |
| 82 » » » if (*randomBytes == NULL) { | 83 » » » if (rnd_tmp2 == NULL) { |
| 83 free(rnd_tmp); | 84 free(rnd_tmp); |
| 84 rnd_tmp = NULL; | 85 rnd_tmp = NULL; |
| 85 LogError("malloc of %u bytes failed.", bytesRetu
rned); | 86 LogError("malloc of %u bytes failed.", bytesRetu
rned); |
| 86 result = TCSERR(TSS_E_OUTOFMEMORY); | 87 result = TCSERR(TSS_E_OUTOFMEMORY); |
| 87 break; | 88 break; |
| 88 } | 89 } |
| 89 » » » memcpy(*randomBytes, rnd_tmp, bytesReturned); | 90 » » » *randomBytes = rnd_tmp2; |
| 91 » » » memcpy(*randomBytes + totalReturned, rnd_tmp, bytesRetur
ned); |
| 90 free(rnd_tmp); | 92 free(rnd_tmp); |
| 91 rnd_tmp = NULL; | 93 rnd_tmp = NULL; |
| 92 #endif | 94 #endif |
| 93 totalReturned += bytesReturned; | 95 totalReturned += bytesReturned; |
| 94 } else { | 96 } else { |
| 95 free(rnd_tmp); | 97 free(rnd_tmp); |
| 96 return result; | 98 return result; |
| 97 } | 99 } |
| 98 } while (totalReturned < *bytesRequested && retries--); | 100 } while (totalReturned < *bytesRequested && retries--); |
| 99 | 101 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return result; | 138 return result; |
| 137 | 139 |
| 138 if ((result = req_mgr_submit_req(txBlob))) | 140 if ((result = req_mgr_submit_req(txBlob))) |
| 139 return result; | 141 return result; |
| 140 | 142 |
| 141 result = UnloadBlob_Header(txBlob, ¶mSize); | 143 result = UnloadBlob_Header(txBlob, ¶mSize); |
| 142 LogResult("Stir random", result); | 144 LogResult("Stir random", result); |
| 143 return result; | 145 return result; |
| 144 } | 146 } |
| 145 | 147 |
| OLD | NEW |