~ubuntu-branches/ubuntu/hardy/trousers/hardy-proposed

« back to all changes in this revision

Viewing changes to src/tcs/tcsi_random.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-01-23 22:03:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123220300-fhtqja3c0oq0gp6z
Tags: 0.3.1-4
* Added patch from Aaron M. Ucko <ucko@debian.org> to allow trousers to
  build successfully on amd64, and presumably also other 64-bit
  architectures (Closes: #457400).
* Including udev rule for /dev/tpm from William Lima
  <wlima.amadeus@gmail.com> as suggested by David Smith <dds@google.com>
  (Closes: #459682).
* Added lintian overrides.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Licensed Materials - Property of IBM
 
4
 *
 
5
 * trousers - An open source TCG Software Stack
 
6
 *
 
7
 * (C) Copyright International Business Machines Corp. 2004
 
8
 *
 
9
 */
 
10
 
 
11
 
 
12
#include <stdlib.h>
 
13
#include <stdio.h>
 
14
#include <string.h>
 
15
#include <inttypes.h>
 
16
 
 
17
#include "trousers/tss.h"
 
18
#include "trousers_types.h"
 
19
#include "tcs_tsp.h"
 
20
#include "tcsps.h"
 
21
#include "tcs_utils.h"
 
22
#include "tcs_int_literals.h"
 
23
#include "capabilities.h"
 
24
#include "tcslog.h"
 
25
#include "req_mgr.h"
 
26
#include "tcsd_wrap.h"
 
27
#include "tcsd.h"
 
28
 
 
29
 
 
30
/*
 
31
 * Get a random number generated by the TPM.  Most (all?) TPMs return a maximum number of random
 
32
 * bytes that's less than the max allowed to be returned at the TSP level, which is 4K bytes.
 
33
 * According to the TPM compliance work posted here: http://www.prosec.rub.de/tpmcompliance.html,
 
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
 
36
 * TPMs that return more bytes.
 
37
 */
 
38
TSS_RESULT
 
39
TCSP_GetRandom_Internal(TCS_CONTEXT_HANDLE hContext,    /* in */
 
40
                        UINT32 * bytesRequested,        /* in, out */
 
41
                        BYTE ** randomBytes)    /* out */
 
42
{
 
43
        UINT64 offset = 0;
 
44
        TSS_RESULT result;
 
45
        UINT32 paramSize, totalReturned = 0, bytesReturned, retries = 50;
 
46
        BYTE txBlob[TSS_TPM_TXBLOB_SIZE], *rnd_tmp = NULL;
 
47
 
 
48
        LogDebugFn("%u bytes", *bytesRequested);
 
49
 
 
50
        if ((result = ctx_verify_context(hContext)))
 
51
                return result;
 
52
 
 
53
        do {
 
54
                if ((result = tpm_rqu_build(TPM_ORD_GetRandom, &offset, txBlob,
 
55
                                            *bytesRequested - totalReturned, NULL)))
 
56
                        break;
 
57
 
 
58
                if ((result = req_mgr_submit_req(txBlob)))
 
59
                        break;;
 
60
 
 
61
                result = UnloadBlob_Header(txBlob, &paramSize);
 
62
                if (!result) {
 
63
#if 0
 
64
                        offset = 10;
 
65
                        UnloadBlob_UINT32(&offset, &bytesReturned, txBlob);
 
66
 
 
67
                        LogDebugFn("received %u bytes from the TPM", bytesReturned);
 
68
 
 
69
                        rnd_tmp = realloc(rnd_tmp, totalReturned + bytesReturned);
 
70
                        if (rnd_tmp == NULL) {
 
71
                                LogError("malloc of %u bytes failed.", bytesReturned);
 
72
                                return TCSERR(TSS_E_OUTOFMEMORY);
 
73
                        }
 
74
                        UnloadBlob(&offset, bytesReturned, txBlob, &rnd_tmp[totalReturned]);
 
75
#else
 
76
                        /* XXX */
 
77
                        if ((result = tpm_rsp_parse(TPM_ORD_GetRandom, txBlob, paramSize,
 
78
                                                    &bytesReturned, &rnd_tmp, NULL, NULL)))
 
79
                                break;
 
80
 
 
81
                        *randomBytes = realloc(*randomBytes, totalReturned + bytesReturned);
 
82
                        if (*randomBytes == NULL) {
 
83
                                free(rnd_tmp);
 
84
                                rnd_tmp = NULL;
 
85
                                LogError("malloc of %u bytes failed.", bytesReturned);
 
86
                                result = TCSERR(TSS_E_OUTOFMEMORY);
 
87
                                break;
 
88
                        }
 
89
                        memcpy(*randomBytes, rnd_tmp, bytesReturned);
 
90
                        free(rnd_tmp);
 
91
                        rnd_tmp = NULL;
 
92
#endif
 
93
                        totalReturned += bytesReturned;
 
94
                } else {
 
95
                        free(rnd_tmp);
 
96
                        return result;
 
97
                }
 
98
        } while (totalReturned < *bytesRequested && retries--);
 
99
 
 
100
        if (totalReturned != *bytesRequested) {
 
101
                LogDebugFn("Only %u random bytes recieved from TPM.", totalReturned);
 
102
                free(rnd_tmp);
 
103
                result = TCSERR(TSS_E_FAIL);
 
104
#if 0
 
105
        } else
 
106
                *randomBytes = rnd_tmp;
 
107
#else
 
108
        }
 
109
#endif
 
110
 
 
111
        return result;
 
112
}
 
113
 
 
114
TSS_RESULT
 
115
TCSP_StirRandom_Internal(TCS_CONTEXT_HANDLE hContext,   /* in */
 
116
                         UINT32 inDataSize,     /* in */
 
117
                         BYTE * inData) /* in */
 
118
{
 
119
        UINT64 offset = 0;
 
120
        UINT32 paramSize;
 
121
        TSS_RESULT result;
 
122
        BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
 
123
 
 
124
        LogDebug("Entering stir random");
 
125
 
 
126
        if (inDataSize > 255) {
 
127
                LogDebugFn("inData is too large! (%u bytes)", inDataSize);
 
128
                return TCSERR(TSS_E_BAD_PARAMETER);
 
129
        }
 
130
 
 
131
        if ((result = ctx_verify_context(hContext)))
 
132
                return result;
 
133
 
 
134
        if ((result = tpm_rqu_build(TPM_ORD_StirRandom, &offset, txBlob, inDataSize, inDataSize,
 
135
                                    inData, NULL, NULL)))
 
136
                return result;
 
137
 
 
138
        if ((result = req_mgr_submit_req(txBlob)))
 
139
                return result;
 
140
 
 
141
        result = UnloadBlob_Header(txBlob, &paramSize);
 
142
        LogResult("Stir random", result);
 
143
        return result;
 
144
}
 
145