/src/libtpms/src/tpm12/tpm_secret.c
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************************/ |
2 | | /* */ |
3 | | /* Secret Data Handler */ |
4 | | /* Written by Ken Goldman */ |
5 | | /* IBM Thomas J. Watson Research Center */ |
6 | | /* $Id: tpm_secret.c 4071 2010-04-29 19:26:45Z kgoldman $ */ |
7 | | /* */ |
8 | | /* (c) Copyright IBM Corporation 2006, 2010. */ |
9 | | /* */ |
10 | | /* All rights reserved. */ |
11 | | /* */ |
12 | | /* Redistribution and use in source and binary forms, with or without */ |
13 | | /* modification, are permitted provided that the following conditions are */ |
14 | | /* met: */ |
15 | | /* */ |
16 | | /* Redistributions of source code must retain the above copyright notice, */ |
17 | | /* this list of conditions and the following disclaimer. */ |
18 | | /* */ |
19 | | /* Redistributions in binary form must reproduce the above copyright */ |
20 | | /* notice, this list of conditions and the following disclaimer in the */ |
21 | | /* documentation and/or other materials provided with the distribution. */ |
22 | | /* */ |
23 | | /* Neither the names of the IBM Corporation nor the names of its */ |
24 | | /* contributors may be used to endorse or promote products derived from */ |
25 | | /* this software without specific prior written permission. */ |
26 | | /* */ |
27 | | /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ |
28 | | /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ |
29 | | /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */ |
30 | | /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ |
31 | | /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ |
32 | | /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ |
33 | | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ |
34 | | /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */ |
35 | | /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ |
36 | | /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */ |
37 | | /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
38 | | /********************************************************************************/ |
39 | | |
40 | | #include <stdio.h> |
41 | | #include <string.h> |
42 | | |
43 | | #include "tpm_crypto.h" |
44 | | #include "tpm_debug.h" |
45 | | #include "tpm_error.h" |
46 | | #include "tpm_store.h" |
47 | | |
48 | | #include "tpm_secret.h" |
49 | | |
50 | | void TPM_Secret_Init(TPM_SECRET tpm_secret) |
51 | 0 | { |
52 | 0 | printf(" TPM_Secret_Init:\n"); |
53 | 0 | memset(tpm_secret, 0, TPM_SECRET_SIZE); |
54 | 0 | return; |
55 | 0 | } |
56 | | |
57 | | /* TPM_Secret_Load() |
58 | | |
59 | | deserialize the structure from a 'stream' |
60 | | 'stream_size' is checked for sufficient data |
61 | | returns 0 or error codes |
62 | | |
63 | | After use, call TPM_Secret_Delete() to free memory |
64 | | */ |
65 | | |
66 | | TPM_RESULT TPM_Secret_Load(TPM_SECRET tpm_secret, |
67 | | unsigned char **stream, |
68 | | uint32_t *stream_size) |
69 | 0 | { |
70 | 0 | TPM_RESULT rc = 0; |
71 | |
|
72 | 0 | printf(" TPM_Secret_Load:\n"); |
73 | 0 | rc = TPM_Loadn(tpm_secret, TPM_SECRET_SIZE, stream, stream_size); |
74 | 0 | return rc; |
75 | 0 | } |
76 | | |
77 | | /* TPM_Secret_Store() |
78 | | |
79 | | serialize the structure to a stream contained in 'sbuffer' |
80 | | returns 0 or error codes |
81 | | |
82 | | After use, call TPM_Sbuffer_Delete() to free memory |
83 | | */ |
84 | | |
85 | | TPM_RESULT TPM_Secret_Store(TPM_STORE_BUFFER *sbuffer, |
86 | | const TPM_SECRET tpm_secret) |
87 | 0 | { |
88 | 0 | TPM_RESULT rc = 0; |
89 | |
|
90 | 0 | printf(" TPM_Secret_Store:\n"); |
91 | 0 | rc = TPM_Sbuffer_Append(sbuffer, tpm_secret, TPM_SECRET_SIZE); |
92 | 0 | return rc; |
93 | 0 | } |
94 | | |
95 | | /* TPM_Secret_Delete() |
96 | | |
97 | | No-OP if the parameter is NULL, else: |
98 | | frees memory allocated for the secret |
99 | | sets pointers to NULL |
100 | | calls TPM_Secret_Init to set members back to default values |
101 | | The secret itself is not freed |
102 | | returns 0 or error codes |
103 | | */ |
104 | | |
105 | | void TPM_Secret_Delete(TPM_SECRET tpm_secret) |
106 | 0 | { |
107 | 0 | printf(" TPM_Secret_Delete:\n"); |
108 | 0 | if (tpm_secret != NULL) { |
109 | 0 | TPM_Secret_Init(tpm_secret); |
110 | 0 | } |
111 | 0 | return; |
112 | 0 | } |
113 | | |
114 | | /* TPM_Secret_Copy() copies the source to the destination |
115 | | */ |
116 | | |
117 | | void TPM_Secret_Copy(TPM_SECRET destination, const TPM_SECRET source) |
118 | 0 | { |
119 | 0 | printf(" TPM_Secret_Copy:\n"); |
120 | 0 | memcpy(destination, source, TPM_SECRET_SIZE); |
121 | 0 | return; |
122 | 0 | } |
123 | | |
124 | | /* TPM_Secret_Compare() compares the source to the destination. |
125 | | |
126 | | Returns TPM_AUTHFAIL if the nonces are not equal |
127 | | */ |
128 | | |
129 | | TPM_RESULT TPM_Secret_Compare(TPM_SECRET expect, const TPM_SECRET actual) |
130 | 0 | { |
131 | 0 | TPM_RESULT rc = 0; |
132 | |
|
133 | 0 | printf(" TPM_Secret_Compare:\n"); |
134 | 0 | rc = memcmp(expect, actual, TPM_SECRET_SIZE); |
135 | 0 | if (rc != 0) { |
136 | 0 | printf("TPM_Secret_Compare: Error comparing secret\n"); |
137 | 0 | rc = TPM_AUTHFAIL; |
138 | 0 | } |
139 | 0 | return rc; |
140 | 0 | } |
141 | | |
142 | | /* TPM_Secret_Generate() generates a new TPM_SECRET from the random number generator |
143 | | */ |
144 | | |
145 | | TPM_RESULT TPM_Secret_Generate(TPM_SECRET tpm_secret) |
146 | 0 | { |
147 | 0 | TPM_RESULT rc = 0; |
148 | |
|
149 | 0 | printf(" TPM_Secret_Generate:\n"); |
150 | 0 | rc = TPM_Random(tpm_secret, TPM_SECRET_SIZE); |
151 | 0 | return rc; |
152 | 0 | } |
153 | | |
154 | | /* TPM_Secret_XOR() XOR's the source and the destination, and returns the result on output. |
155 | | */ |
156 | | |
157 | | void TPM_Secret_XOR(TPM_SECRET output, TPM_SECRET input1, TPM_SECRET input2) |
158 | 0 | { |
159 | 0 | size_t i; |
160 | |
|
161 | 0 | printf(" TPM_Secret_XOR:\n"); |
162 | 0 | for (i = 0 ; i < TPM_SECRET_SIZE ; i++) { |
163 | 0 | output[i] = input1[i] ^ input2[i]; |
164 | 0 | } |
165 | 0 | return; |
166 | 0 | } |