/src/libtpms/src/tpm12/tpm_nonce.c
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************************/ |
2 | | /* */ |
3 | | /* Nonce Handler */ |
4 | | /* Written by Ken Goldman */ |
5 | | /* IBM Thomas J. Watson Research Center */ |
6 | | /* $Id: tpm_nonce.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_structures.h" |
47 | | |
48 | | #include "tpm_nonce.h" |
49 | | |
50 | | /* TPM_Nonce_Init resets a nonce structure to zeros */ |
51 | | |
52 | | void TPM_Nonce_Init(TPM_NONCE tpm_nonce) |
53 | 0 | { |
54 | 0 | size_t i; |
55 | |
|
56 | 0 | printf(" TPM_Nonce_Init:\n"); |
57 | 0 | for (i = 0 ; i < TPM_NONCE_SIZE ; i++) { |
58 | 0 | tpm_nonce[i] = 0; |
59 | 0 | } |
60 | 0 | return; |
61 | 0 | } |
62 | | |
63 | | /* TPM_Nonce_Load() |
64 | | |
65 | | deserialize the structure from a 'stream' |
66 | | 'stream_size' is checked for sufficient data |
67 | | returns 0 or error codes |
68 | | */ |
69 | | |
70 | | |
71 | | TPM_RESULT TPM_Nonce_Load(TPM_NONCE tpm_nonce, |
72 | | unsigned char **stream, |
73 | | uint32_t *stream_size) |
74 | 0 | { |
75 | 0 | TPM_RESULT rc = 0; |
76 | | |
77 | 0 | printf(" TPM_Nonce_Load:\n"); |
78 | 0 | rc = TPM_Loadn(tpm_nonce, TPM_NONCE_SIZE, stream, stream_size); |
79 | 0 | return rc; |
80 | 0 | } |
81 | | |
82 | | /* TPM_Nonce_Store() |
83 | | |
84 | | serialize the structure to a stream contained in 'sbuffer' |
85 | | returns 0 or error codes |
86 | | |
87 | | After use, call TPM_Sbuffer_Delete() to free memory |
88 | | */ |
89 | | |
90 | | TPM_RESULT TPM_Nonce_Store(TPM_STORE_BUFFER *sbuffer, |
91 | | const TPM_NONCE tpm_nonce) |
92 | 0 | { |
93 | 0 | TPM_RESULT rc = 0; |
94 | |
|
95 | 0 | printf(" TPM_Nonce_Store:\n"); |
96 | 0 | rc = TPM_Sbuffer_Append(sbuffer, tpm_nonce, TPM_NONCE_SIZE); |
97 | 0 | return rc; |
98 | 0 | } |
99 | | |
100 | | /* TPM_Nonce_Copy() copies the source to the destination |
101 | | */ |
102 | | |
103 | | void TPM_Nonce_Copy(TPM_NONCE destination, const TPM_NONCE source) |
104 | 0 | { |
105 | 0 | printf(" TPM_Nonce_Copy:\n"); |
106 | 0 | memcpy(destination, source, TPM_NONCE_SIZE); |
107 | 0 | return; |
108 | 0 | } |
109 | | |
110 | | /* TPM_Nonce_Compare() compares the source to the destination. |
111 | | |
112 | | Returns TPM_AUTHFAIL if the nonces are not equal |
113 | | */ |
114 | | |
115 | | TPM_RESULT TPM_Nonce_Compare(TPM_NONCE expect, const TPM_NONCE actual) |
116 | 0 | { |
117 | 0 | TPM_RESULT rc = 0; |
118 | |
|
119 | 0 | printf(" TPM_Nonce_Compare:\n"); |
120 | 0 | rc = memcmp(expect, actual, TPM_NONCE_SIZE); |
121 | 0 | if (rc != 0) { |
122 | 0 | printf("TPM_Nonce_Compare: Error comparing nonce\n"); |
123 | 0 | TPM_PrintFour(" TPM_Nonce_Compare: Expect", expect); |
124 | 0 | TPM_PrintFour(" TPM_Nonce_Compare: Actual", actual); |
125 | 0 | rc = TPM_AUTHFAIL; |
126 | 0 | } |
127 | 0 | return rc; |
128 | 0 | } |
129 | | |
130 | | /* TPM_Nonce_Generate() generates a new nonce from the random number generator |
131 | | */ |
132 | | |
133 | | TPM_RESULT TPM_Nonce_Generate(TPM_NONCE tpm_nonce) |
134 | 0 | { |
135 | 0 | TPM_RESULT rc = 0; |
136 | |
|
137 | 0 | printf(" TPM_Nonce_Generate:\n"); |
138 | 0 | rc = TPM_Random(tpm_nonce, TPM_NONCE_SIZE); |
139 | 0 | return rc; |
140 | 0 | } |
141 | | |
142 | | /* TPM_Nonce_IsZero() returns 'isZero' TRUE is all bytes 'tpm_nonce' are 0x00 |
143 | | */ |
144 | | |
145 | | void TPM_Nonce_IsZero(TPM_BOOL *isZero, TPM_NONCE tpm_nonce) |
146 | 0 | { |
147 | 0 | size_t i; |
148 | |
|
149 | 0 | printf(" TPM_Nonce_IsZero:\n"); |
150 | 0 | for (i = 0, *isZero = TRUE ; (i < TPM_NONCE_SIZE) && *isZero ; i++) { |
151 | 0 | if (tpm_nonce[i] != 0) { |
152 | 0 | *isZero = FALSE; |
153 | 0 | } |
154 | 0 | } |
155 | 0 | return; |
156 | 0 | } |
157 | | |