|
1 |
| -/** |
2 |
| - * MIT License |
3 |
| - * |
4 |
| - * Copyright (c) 2018 Infineon Technologies AG |
5 |
| - * |
6 |
| - * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 |
| - * of this software and associated documentation files (the "Software"), to deal |
8 |
| - * in the Software without restriction, including without limitation the rights |
9 |
| - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 |
| - * copies of the Software, and to permit persons to whom the Software is |
11 |
| - * furnished to do so, subject to the following conditions: |
12 |
| - * |
13 |
| - * The above copyright notice and this permission notice shall be included in all |
14 |
| - * copies or substantial portions of the Software. |
15 |
| - * |
16 |
| - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 |
| - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 |
| - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 |
| - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 |
| - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 |
| - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 |
| - * SOFTWARE |
23 |
| - * |
24 |
| - * Demonstrates use of the |
25 |
| - * Infineon Technologies AG OPTIGA™ Trust X Arduino library |
26 |
| - */ |
27 |
| - |
28 |
| -#include "OPTIGATrustX.h" |
29 |
| - |
30 |
| -#define MAXCMD_LEN 255 |
31 |
| -#define CERT_LENGTH 512 |
32 |
| -#define RND_LENGTH 64 |
33 |
| -#define HASH_LENGTH 32 |
34 |
| -#define SIGN_LENGTH 80 |
35 |
| -#define PUBKEY_LENGTH 70 |
36 |
| -#define UID_LENGTH 27 |
37 |
| - |
38 |
| -#define SUPPRESSCOLLORS |
39 |
| -#include "fprint.h" |
40 |
| - |
41 |
| -#define ASSERT(err) if (ret) { printlnRed("Failed"); while (true); } |
42 |
| - |
43 |
| -/* |
44 |
| - * Allocating buffers for further use in loop() |
45 |
| - */ |
46 |
| -uint8_t *cert = new uint8_t[CERT_LENGTH]; |
47 |
| -uint16_t certLen = CERT_LENGTH; |
48 |
| -uint8_t *rnd = new uint8_t[RND_LENGTH]; |
49 |
| -uint16_t rndLen = RND_LENGTH; |
50 |
| -uint8_t *hash = new uint8_t[HASH_LENGTH]; |
51 |
| -uint16_t hashLen = HASH_LENGTH; |
52 |
| -uint8_t *rawSign = new uint8_t[SIGN_LENGTH]; |
53 |
| -uint8_t *formSign = new uint8_t[SIGN_LENGTH]; |
54 |
| -uint16_t signLen = SIGN_LENGTH; |
55 |
| -uint8_t *pubKey = new uint8_t[PUBKEY_LENGTH]; |
56 |
| -uint16_t pubKeyLen = PUBKEY_LENGTH; |
57 |
| -uint8_t *uid = new uint8_t[UID_LENGTH]; |
58 |
| - |
59 |
| - |
60 |
| - |
61 |
| - |
62 |
| -static void output_result(char* tag, uint8_t* in, uint16_t in_len) |
63 |
| -{ |
64 |
| - printlnGreen("OK"); |
65 |
| - printMagenta(tag); |
66 |
| - printMagenta(" Length: "); |
67 |
| - Serial.println(in_len); |
68 |
| - printMagenta(tag); |
69 |
| - printlnMagenta(":"); |
70 |
| - HEXDUMP(in, in_len); |
71 |
| -} |
72 |
| - |
73 |
| -void loop() |
74 |
| -{ |
75 |
| - uint32_t ret = 0; |
76 |
| - uint8_t cntr = 10; |
77 |
| - uint8_t ifxPublicKey[68]; |
78 |
| - |
79 |
| - /* |
80 |
| - * Getting co-processor Unique ID |
81 |
| - */ |
82 |
| - printGreen("Get co-processor UID ... "); |
83 |
| - uint16_t uidLength = UID_LENGTH; |
84 |
| - ret = trustX.getUniqueID(uid, uidLength); |
85 |
| - ASSERT(ret); |
86 |
| - output_result("Co-processor UID", uid, uidLength); |
87 |
| - |
88 |
| - /* |
89 |
| - * Getting primary certificate |
90 |
| - */ |
91 |
| - printGreen("Reading cert ... "); |
92 |
| - ret = trustX.getCertificate(cert, certLen); |
93 |
| - ASSERT(ret); |
94 |
| - output_result("Certificate", cert, certLen); |
95 |
| - |
96 |
| - /* |
97 |
| - * Generate a Keypair |
98 |
| - */ |
99 |
| - printGreen("Generate Key Pair ... "); |
100 |
| - uint16_t ctx = 0; |
101 |
| - ret = trustX.generateKeypair(pubKey, pubKeyLen, ctx); |
102 |
| - ASSERT(ret); |
103 |
| - output_result("Public key", pubKey, pubKeyLen); |
104 |
| - |
105 |
| - /* |
106 |
| - * Get random value of RND_LENGTH length |
107 |
| - */ |
108 |
| - printGreen("Get random value ... "); |
109 |
| - ret = trustX.getRandom(RND_LENGTH, rnd); |
110 |
| - ASSERT(ret); |
111 |
| - output_result("Random", rnd, RND_LENGTH); |
112 |
| - |
113 |
| - /* |
114 |
| - * Calculate SHA256 value |
115 |
| - */ |
116 |
| - printGreen("Calculate Hash ... "); |
117 |
| - ret = trustX.sha256(rnd, RND_LENGTH, hash); |
118 |
| - hashLen = 32; |
119 |
| - ASSERT(ret); |
120 |
| - output_result("SHA256", hash, hashLen); |
121 |
| - |
122 |
| - /* |
123 |
| - * Generate a signature NIST-P256 |
124 |
| - */ |
125 |
| - printGreen("Generate Signature ... "); |
126 |
| - ret = trustX.calculateSignature(hash, hashLen, formSign, signLen); |
127 |
| - ASSERT(ret); |
128 |
| - output_result("Signature", formSign, signLen); |
129 |
| - |
130 |
| - /* |
131 |
| - * Verify just geberated signature |
132 |
| - */ |
133 |
| - getPublicKey(ifxPublicKey); |
134 |
| - |
135 |
| - printGreen("Verify Signature ... "); |
136 |
| - ret = trustX.verifySignature(hash, hashLen, formSign, signLen, ifxPublicKey, |
137 |
| - sizeof(ifxPublicKey) / sizeof(ifxPublicKey[0])); |
138 |
| - ASSERT(ret); |
139 |
| - printlnGreen("OK"); |
140 |
| - |
141 |
| - /* |
142 |
| - * Count down 10 seconds and restart the application |
143 |
| - */ |
144 |
| - while(cntr) { |
145 |
| - Serial.print(cntr); |
146 |
| - Serial.println(" seconds untill restart."); |
147 |
| - delay(1000); |
148 |
| - cntr--; |
149 |
| - } |
150 |
| -} |
151 |
| - |
152 |
| -void setup() |
153 |
| -{ |
154 |
| - uint32_t ret = 0; |
155 |
| - |
156 |
| - /* |
157 |
| - * Initialise serial output |
158 |
| - */ |
159 |
| - Serial.begin(38400); |
160 |
| - Serial.println("Initializing ... "); |
161 |
| - |
162 |
| - /* |
163 |
| - * Initialise OPTIGA™ Trust X |
164 |
| - */ |
165 |
| - printGreen("Begin Trust ... "); |
166 |
| - ret = trustX.begin(); |
167 |
| - ASSERT(ret); |
168 |
| - printlnGreen("OK"); |
169 |
| - |
170 |
| - /* |
171 |
| - * Speed up the chip (min is 6ma, maximum is 15ma) |
172 |
| - */ |
173 |
| - printGreen("Setting Current Limit... "); |
174 |
| - ret = trustX.setCurrentLimit(15); |
175 |
| - ASSERT(ret); |
176 |
| - printlnGreen("OK"); |
177 |
| - |
178 |
| - /* |
179 |
| - * Check the return value which we just set |
180 |
| - */ |
181 |
| - printGreen("Checking Power Limit... "); |
182 |
| - uint8_t current_lim = 0; |
183 |
| - ret = trustX.getCurrentLimit(current_lim); |
184 |
| - ASSERT(ret); |
185 |
| - if (current_lim == 15) { |
186 |
| - printlnGreen("OK"); |
187 |
| - } else { |
188 |
| - printlnRed("Failed"); |
189 |
| - while(1); |
190 |
| - } |
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Infineon Technologies AG |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE |
| 23 | + * |
| 24 | + * Demonstrates use of the |
| 25 | + * Infineon Technologies AG OPTIGA™ Trust X Arduino library |
| 26 | + */ |
| 27 | + |
| 28 | +#include "OPTIGATrustX.h" |
| 29 | + |
| 30 | +#define MAXCMD_LEN 255 |
| 31 | +#define CERT_LENGTH 512 |
| 32 | +#define RND_LENGTH 64 |
| 33 | +#define HASH_LENGTH 32 |
| 34 | +#define SIGN_LENGTH 80 |
| 35 | +#define PUBKEY_LENGTH 70 |
| 36 | +#define UID_LENGTH 27 |
| 37 | + |
| 38 | +#define SUPPRESSCOLLORS |
| 39 | +#include "fprint.h" |
| 40 | + |
| 41 | +#define ASSERT(err) if (ret) { printlnRed("Failed"); while (true); } |
| 42 | + |
| 43 | +/* |
| 44 | + * Allocating buffers for further use in loop() |
| 45 | + */ |
| 46 | +uint8_t *cert = new uint8_t[CERT_LENGTH]; |
| 47 | +uint16_t certLen = CERT_LENGTH; |
| 48 | +uint8_t *rnd = new uint8_t[RND_LENGTH]; |
| 49 | +uint16_t rndLen = RND_LENGTH; |
| 50 | +uint8_t *hash = new uint8_t[HASH_LENGTH]; |
| 51 | +uint16_t hashLen = HASH_LENGTH; |
| 52 | +uint8_t *rawSign = new uint8_t[SIGN_LENGTH]; |
| 53 | +uint8_t *formSign = new uint8_t[SIGN_LENGTH]; |
| 54 | +uint16_t signLen = SIGN_LENGTH; |
| 55 | +uint8_t *pubKey = new uint8_t[PUBKEY_LENGTH]; |
| 56 | +uint16_t pubKeyLen = PUBKEY_LENGTH; |
| 57 | +uint8_t *uid = new uint8_t[UID_LENGTH]; |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +static void output_result(char* tag, uint8_t* in, uint16_t in_len) |
| 63 | +{ |
| 64 | + printlnGreen("OK"); |
| 65 | + printMagenta(tag); |
| 66 | + printMagenta(" Length: "); |
| 67 | + Serial.println(in_len); |
| 68 | + printMagenta(tag); |
| 69 | + printlnMagenta(":"); |
| 70 | + HEXDUMP(in, in_len); |
| 71 | +} |
| 72 | + |
| 73 | +void loop() |
| 74 | +{ |
| 75 | + uint32_t ret = 0; |
| 76 | + uint8_t cntr = 10; |
| 77 | + uint8_t ifxPublicKey[68]; |
| 78 | + |
| 79 | + /* |
| 80 | + * Getting co-processor Unique ID |
| 81 | + */ |
| 82 | + printGreen("Get co-processor UID ... "); |
| 83 | + uint16_t uidLength = UID_LENGTH; |
| 84 | + ret = trustX.getUniqueID(uid, uidLength); |
| 85 | + ASSERT(ret); |
| 86 | + output_result("Co-processor UID", uid, uidLength); |
| 87 | + |
| 88 | + /* |
| 89 | + * Getting primary certificate |
| 90 | + */ |
| 91 | + printGreen("Reading cert ... "); |
| 92 | + ret = trustX.getCertificate(cert, certLen); |
| 93 | + ASSERT(ret); |
| 94 | + output_result("Certificate", cert, certLen); |
| 95 | + |
| 96 | + /* |
| 97 | + * Generate a Keypair |
| 98 | + */ |
| 99 | + printGreen("Generate Key Pair ... "); |
| 100 | + uint16_t ctx = 0; |
| 101 | + ret = trustX.generateKeypair(pubKey, pubKeyLen, ctx); |
| 102 | + ASSERT(ret); |
| 103 | + output_result("Public key", pubKey, pubKeyLen); |
| 104 | + |
| 105 | + /* |
| 106 | + * Get random value of RND_LENGTH length |
| 107 | + */ |
| 108 | + printGreen("Get random value ... "); |
| 109 | + ret = trustX.getRandom(RND_LENGTH, rnd); |
| 110 | + ASSERT(ret); |
| 111 | + output_result("Random", rnd, RND_LENGTH); |
| 112 | + |
| 113 | + /* |
| 114 | + * Calculate SHA256 value |
| 115 | + */ |
| 116 | + printGreen("Calculate Hash ... "); |
| 117 | + ret = trustX.sha256(rnd, RND_LENGTH, hash); |
| 118 | + hashLen = 32; |
| 119 | + ASSERT(ret); |
| 120 | + output_result("SHA256", hash, hashLen); |
| 121 | + |
| 122 | + /* |
| 123 | + * Generate a signature NIST-P256 |
| 124 | + */ |
| 125 | + printGreen("Generate Signature ... "); |
| 126 | + ret = trustX.calculateSignature(hash, hashLen, formSign, signLen); |
| 127 | + ASSERT(ret); |
| 128 | + output_result("Signature", formSign, signLen); |
| 129 | + |
| 130 | + /* |
| 131 | + * Verify just geberated signature |
| 132 | + */ |
| 133 | + trustX.getPublicKey(ifxPublicKey); |
| 134 | + |
| 135 | + printGreen("Verify Signature ... "); |
| 136 | + ret = trustX.verifySignature(hash, hashLen, formSign, signLen, ifxPublicKey, |
| 137 | + sizeof(ifxPublicKey) / sizeof(ifxPublicKey[0])); |
| 138 | + ASSERT(ret); |
| 139 | + printlnGreen("OK"); |
| 140 | + |
| 141 | + /* |
| 142 | + * Count down 10 seconds and restart the application |
| 143 | + */ |
| 144 | + while(cntr) { |
| 145 | + Serial.print(cntr); |
| 146 | + Serial.println(" seconds untill restart."); |
| 147 | + delay(1000); |
| 148 | + cntr--; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void setup() |
| 153 | +{ |
| 154 | + uint32_t ret = 0; |
| 155 | + |
| 156 | + /* |
| 157 | + * Initialise serial output |
| 158 | + */ |
| 159 | + Serial.begin(38400); |
| 160 | + Serial.println("Initializing ... "); |
| 161 | + |
| 162 | + /* |
| 163 | + * Initialise OPTIGA™ Trust X |
| 164 | + */ |
| 165 | + printGreen("Begin Trust ... "); |
| 166 | + ret = trustX.begin(); |
| 167 | + ASSERT(ret); |
| 168 | + printlnGreen("OK"); |
| 169 | + |
| 170 | + /* |
| 171 | + * Speed up the chip (min is 6ma, maximum is 15ma) |
| 172 | + */ |
| 173 | + printGreen("Setting Current Limit... "); |
| 174 | + ret = trustX.setCurrentLimit(15); |
| 175 | + ASSERT(ret); |
| 176 | + printlnGreen("OK"); |
| 177 | + |
| 178 | + /* |
| 179 | + * Check the return value which we just set |
| 180 | + */ |
| 181 | + printGreen("Checking Power Limit... "); |
| 182 | + uint8_t current_lim = 0; |
| 183 | + ret = trustX.getCurrentLimit(current_lim); |
| 184 | + ASSERT(ret); |
| 185 | + if (current_lim == 15) { |
| 186 | + printlnGreen("OK"); |
| 187 | + } else { |
| 188 | + printlnRed("Failed"); |
| 189 | + while(1); |
| 190 | + } |
191 | 191 | }
|
0 commit comments