OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /* Test of two-stage locking using bGlobalLock and PP. | 6 /* Test of two-stage locking using bGlobalLock and PP. |
7 */ | 7 */ |
8 | 8 |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 | 12 |
13 #include "tlcl.h" | 13 #include "tlcl.h" |
| 14 #include "tlcl_tests.h" |
14 #include "utility.h" | 15 #include "utility.h" |
15 | 16 |
16 #define INDEX0 0xcafe | |
17 #define INDEX1 0xcaff | |
18 | |
19 int main(int argc, char** argv) { | 17 int main(int argc, char** argv) { |
20 uint32_t zero = 0; | 18 uint32_t zero = 0; |
21 uint32_t perm; | |
22 uint32_t result; | 19 uint32_t result; |
23 uint32_t x; | 20 uint32_t x; |
24 | 21 |
25 TlclLibInit(); | 22 TlclLibInit(); |
26 | 23 TPM_CHECK(TlclStartupIfNeeded()); |
27 TlclStartup(); | 24 TPM_CHECK(TlclSelfTestFull()); |
28 TlclSelfTestFull(); | 25 TPM_CHECK(TlclAssertPhysicalPresence()); |
29 | 26 TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); |
30 TlclAssertPhysicalPresence(); | 27 TPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &zero, sizeof(uint32_t))); |
31 | 28 TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); |
32 result = TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)); | 29 TPM_CHECK(TlclWrite(INDEX1, (uint8_t*) &zero, sizeof(uint32_t))); |
33 if (result == TPM_E_BADINDEX) { | 30 TPM_CHECK(TlclSetGlobalLock()); |
34 perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; | |
35 TlclDefineSpace(INDEX0, perm, sizeof(uint32_t)); | |
36 } | |
37 result = TlclWrite(INDEX0, (uint8_t*) &zero, sizeof(uint32_t)); | |
38 assert(result == TPM_SUCCESS); | |
39 | |
40 result = TlclRead(INDEX1, (uint8_t*) &x, sizeof(x)); | |
41 if (result == TPM_E_BADINDEX) { | |
42 perm = TPM_NV_PER_PPWRITE; | |
43 TlclDefineSpace(INDEX1, perm, sizeof(uint32_t)); | |
44 } | |
45 result = TlclWrite(INDEX1, (uint8_t*) &zero, sizeof(uint32_t)); | |
46 assert(result == TPM_SUCCESS); | |
47 | |
48 // Sets the global lock. | |
49 TlclSetGlobalLock(); | |
50 | 31 |
51 // Verifies that write to index0 fails. | 32 // Verifies that write to index0 fails. |
52 x = 1; | 33 x = 1; |
53 result = TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)); | 34 result = TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)); |
54 if (result != TPM_E_AREA_LOCKED) { | 35 assert(result == TPM_E_AREA_LOCKED); |
55 error("INDEX0 is not locked\n"); | 36 TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); |
56 exit(1); | 37 assert(x == 0); |
57 } | |
58 | 38 |
59 // Verifies that write to index1 is still possible. | 39 // Verifies that write to index1 is still possible. |
60 x = 2; | 40 x = 2; |
61 result = TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x)); | 41 TPM_CHECK(TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x))); |
62 if (result != TPM_SUCCESS) { | 42 TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); |
63 error("failure to write at INDEX1\n"); | 43 assert(x == 2); |
64 exit(2); | |
65 } | |
66 | 44 |
67 // Turns off PP. | 45 // Turns off PP. |
68 TlclLockPhysicalPresence(); | 46 TlclLockPhysicalPresence(); |
69 | 47 |
70 // Verifies that write to index1 fails. | 48 // Verifies that write to index1 fails. |
71 x = 3; | 49 x = 3; |
72 result = TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x)); | 50 result = TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x)); |
73 if (result != TPM_E_BAD_PRESENCE) { | 51 assert(result == TPM_E_BAD_PRESENCE); |
74 error("INDEX1 is not locked\n"); | 52 TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); |
75 exit(3); | 53 assert(x == 2); |
76 } | 54 printf("TEST SUCCEEDED\n"); |
77 | |
78 printf("Test completed successfully\n"); | |
79 exit(0); | 55 exit(0); |
80 } | 56 } |
OLD | NEW |