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

« back to all changes in this revision

Viewing changes to src/tspi/spi_hash.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-2006
8
 
 *
9
 
 */
10
 
 
11
 
 
12
 
#include <stdlib.h>
13
 
#include <stdio.h>
14
 
#include <string.h>
15
 
 
16
 
#include "trousers/tss.h"
17
 
#include "trousers/trousers.h"
18
 
#include "spi_internal_types.h"
19
 
#include "spi_utils.h"
20
 
#include "capabilities.h"
21
 
#include "tsplog.h"
22
 
#include "obj.h"
23
 
 
24
 
TSS_RESULT
25
 
Tspi_Hash_Sign(TSS_HHASH hHash,                 /* in */
26
 
               TSS_HKEY hKey,                   /* in */
27
 
               UINT32 * pulSignatureLength,     /* out */
28
 
               BYTE ** prgbSignature)           /* out */
29
 
{
30
 
        TPM_AUTH privAuth;
31
 
        TPM_AUTH *pPrivAuth = &privAuth;
32
 
        BYTE hashblob[512];
33
 
        UINT64 offset;
34
 
        TCPA_DIGEST digest;
35
 
        TCPA_RESULT result;
36
 
        TSS_HPOLICY hPolicy;
37
 
        TCS_KEY_HANDLE tcsKeyHandle;
38
 
        TSS_BOOL usesAuth;
39
 
        TSS_HCONTEXT tspContext;
40
 
        UINT32 ulDataLen;
41
 
        BYTE *data;
42
 
 
43
 
        if (pulSignatureLength == NULL || prgbSignature == NULL)
44
 
                return TSPERR(TSS_E_BAD_PARAMETER);
45
 
 
46
 
        if ((result = obj_hash_get_tsp_context(hHash, &tspContext)))
47
 
                return result;
48
 
 
49
 
        if ((result = obj_rsakey_get_policy(hKey, TSS_POLICY_USAGE, &hPolicy, &usesAuth)))
50
 
                return result;
51
 
 
52
 
        if ((result = obj_hash_get_value(hHash, &ulDataLen, &data)))
53
 
                return result;
54
 
 
55
 
        if ((result = obj_rsakey_get_tcs_handle(hKey, &tcsKeyHandle)))
56
 
                goto done;
57
 
 
58
 
        if (usesAuth) {
59
 
                offset = 0;
60
 
                Trspi_LoadBlob_UINT32(&offset, TPM_ORD_Sign, hashblob);
61
 
                Trspi_LoadBlob_UINT32(&offset, ulDataLen, hashblob);
62
 
                Trspi_LoadBlob(&offset, ulDataLen, hashblob, data);
63
 
                Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest);
64
 
                pPrivAuth = &privAuth;
65
 
 
66
 
                if ((result = secret_PerformAuth_OIAP(hKey, TPM_ORD_Sign,
67
 
                                                      hPolicy, &digest,
68
 
                                                      &privAuth)))
69
 
                        goto done;
70
 
        } else {
71
 
                pPrivAuth = NULL;
72
 
        }
73
 
 
74
 
        if ((result = TCSP_Sign(tspContext, tcsKeyHandle,
75
 
                               ulDataLen, data,
76
 
                               pPrivAuth, pulSignatureLength, prgbSignature)))
77
 
                goto done;
78
 
 
79
 
        if (usesAuth) {
80
 
                offset = 0;
81
 
                Trspi_LoadBlob_UINT32(&offset, result, hashblob);
82
 
                Trspi_LoadBlob_UINT32(&offset, TPM_ORD_Sign, hashblob);
83
 
                Trspi_LoadBlob_UINT32(&offset, *pulSignatureLength, hashblob);
84
 
                Trspi_LoadBlob(&offset, *pulSignatureLength, hashblob, *prgbSignature);
85
 
                Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest);
86
 
 
87
 
                if ((result = obj_policy_validate_auth_oiap(hPolicy, &digest, &privAuth))) {
88
 
                        free_tspi(tspContext, *prgbSignature);
89
 
                        goto done;
90
 
                }
91
 
        }
92
 
 
93
 
done:
94
 
        free_tspi(tspContext, data);
95
 
        return result;
96
 
}
97
 
 
98
 
TSS_RESULT
99
 
Tspi_Hash_VerifySignature(TSS_HHASH hHash,              /* in */
100
 
                          TSS_HKEY hKey,                /* in */
101
 
                          UINT32 ulSignatureLength,     /* in */
102
 
                          BYTE * rgbSignature)          /* in */
103
 
{
104
 
        TCPA_RESULT result;
105
 
        BYTE *pubKey = NULL;
106
 
        UINT32 pubKeySize;
107
 
        BYTE *hashData = NULL;
108
 
        UINT32 hashDataSize;
109
 
        UINT32 sigScheme;
110
 
        TSS_HCONTEXT tspContext;
111
 
 
112
 
        if (ulSignatureLength > 0 && rgbSignature == NULL)
113
 
                return TSPERR(TSS_E_BAD_PARAMETER);
114
 
 
115
 
        if ((result = obj_rsakey_get_tsp_context(hKey, &tspContext)))
116
 
                return result;
117
 
 
118
 
        if ((result = obj_rsakey_get_modulus(hKey, &pubKeySize, &pubKey)))
119
 
                return result;
120
 
 
121
 
        if ((result = obj_rsakey_get_ss(hKey, &sigScheme))) {
122
 
                free_tspi(tspContext, pubKey);
123
 
                return result;
124
 
        }
125
 
 
126
 
        if ((result = obj_hash_get_value(hHash, &hashDataSize, &hashData))) {
127
 
                free_tspi(tspContext, pubKey);
128
 
                return result;
129
 
        }
130
 
 
131
 
        if (sigScheme == TSS_SS_RSASSAPKCS1V15_SHA1) {
132
 
                result = Trspi_Verify(TSS_HASH_SHA1, hashData, hashDataSize, pubKey, pubKeySize,
133
 
                                      rgbSignature, ulSignatureLength);
134
 
        } else if (sigScheme == TSS_SS_RSASSAPKCS1V15_DER) {
135
 
                result = Trspi_Verify(TSS_HASH_OTHER, hashData, hashDataSize, pubKey, pubKeySize,
136
 
                                      rgbSignature, ulSignatureLength);
137
 
        } else {
138
 
                result = TSPERR(TSS_E_INVALID_SIGSCHEME);
139
 
        }
140
 
 
141
 
        free_tspi(tspContext, pubKey);
142
 
        free_tspi(tspContext, hashData);
143
 
 
144
 
        return result;
145
 
}
146
 
 
147
 
TSS_RESULT
148
 
Tspi_Hash_SetHashValue(TSS_HHASH hHash,                 /* in */
149
 
                       UINT32 ulHashValueLength,        /* in */
150
 
                       BYTE * rgbHashValue)             /* in */
151
 
{
152
 
        if (ulHashValueLength == 0 || rgbHashValue == NULL)
153
 
                return TSPERR(TSS_E_BAD_PARAMETER);
154
 
 
155
 
        return obj_hash_set_value(hHash, ulHashValueLength, rgbHashValue);
156
 
}
157
 
 
158
 
TSS_RESULT
159
 
Tspi_Hash_GetHashValue(TSS_HHASH hHash,                 /* in */
160
 
                       UINT32 * pulHashValueLength,     /* out */
161
 
                       BYTE ** prgbHashValue)           /* out */
162
 
{
163
 
        if (pulHashValueLength == NULL || prgbHashValue == NULL)
164
 
                return TSPERR(TSS_E_BAD_PARAMETER);
165
 
 
166
 
        return obj_hash_get_value(hHash, pulHashValueLength, prgbHashValue);
167
 
}
168
 
 
169
 
TSS_RESULT
170
 
Tspi_Hash_UpdateHashValue(TSS_HHASH hHash,      /* in */
171
 
                          UINT32 ulDataLength,  /* in */
172
 
                          BYTE *rgbData)        /* in */
173
 
{
174
 
        if (rgbData == NULL && ulDataLength != 0)
175
 
                return TSPERR(TSS_E_BAD_PARAMETER);
176
 
 
177
 
        if (ulDataLength == 0)
178
 
                return TSS_SUCCESS;
179
 
 
180
 
        return obj_hash_update_value(hHash, ulDataLength, rgbData);
181
 
}