Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 88ee23b

Browse files
committed
Fix Signature output for testFullAPI example
1 parent 1c31a1b commit 88ee23b

File tree

2 files changed

+192
-25
lines changed

2 files changed

+192
-25
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#ifndef FPRINT_H
2+
#define FPRINT_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include "Arduino.h"
9+
10+
#ifndef SUPPRESSCOLLORS
11+
#define ANSI_COLOR_RED "\x1b[31m"
12+
#define ANSI_COLOR_GREEN "\x1b[32m"
13+
#define ANSI_COLOR_YELLOW "\x1b[33m"
14+
#define ANSI_COLOR_BLUE "\x1b[34m"
15+
#define ANSI_COLOR_MAGENTA "\x1b[35m"
16+
#define ANSI_COLOR_CYAN "\x1b[36m"
17+
#define ANSI_COLOR_RESET "\x1b[0m"
18+
#else
19+
#define ANSI_COLOR_RED ""
20+
#define ANSI_COLOR_GREEN ""
21+
#define ANSI_COLOR_YELLOW ""
22+
#define ANSI_COLOR_BLUE ""
23+
#define ANSI_COLOR_MAGENTA ""
24+
#define ANSI_COLOR_CYAN ""
25+
#define ANSI_COLOR_RESET ""
26+
#endif
27+
28+
#define MAXCMD_LEN 255
29+
#define HEXDUMP_COLS 16
30+
31+
32+
#ifndef SUPPRESSHEXDUMP
33+
#define SUPPRESSHEXDUMP 0
34+
#endif
35+
#define HEXDUMP(a, b) (SUPPRESSHEXDUMP==0) ? __hexdump__(a,b) : (void) 0;
36+
/**
37+
*
38+
* This function prints a given input in green
39+
* color with a \r\n signs at the end
40+
*
41+
* @retval None
42+
*/
43+
inline void printlnGreen(const char c[]) {
44+
char tmp[100];
45+
sprintf(tmp, "%s%s%s", ANSI_COLOR_GREEN, c, ANSI_COLOR_RESET);
46+
Serial.println(tmp);
47+
}
48+
49+
/**
50+
*
51+
* This function prints a given input in red
52+
* color with a \r\n signs at the end
53+
*
54+
* @retval None
55+
*/
56+
inline void printlnRed(const char c[]) {
57+
char tmp[100];
58+
sprintf(tmp, "%s%s%s", ANSI_COLOR_RED, c, ANSI_COLOR_RESET);
59+
Serial.println(tmp);
60+
}
61+
62+
/**
63+
*
64+
* This function prints a given input in magenta
65+
* color with a \r\n signs at the end
66+
*
67+
* @retval None
68+
*/
69+
inline void printlnMagenta(const char c[]) {
70+
char tmp[100];
71+
sprintf(tmp, "%s%s%s", ANSI_COLOR_MAGENTA, c, ANSI_COLOR_RESET);
72+
Serial.println(tmp);
73+
}
74+
75+
/**
76+
*
77+
* This function prints a given input in magenta
78+
* color without a \r\n signs at the end
79+
*
80+
* @retval None
81+
*/
82+
inline void printMagenta(const char c[]) {
83+
char tmp[100];
84+
sprintf(tmp, "%s%s%s", ANSI_COLOR_MAGENTA, c, ANSI_COLOR_RESET);
85+
Serial.print(tmp);
86+
}
87+
88+
/**
89+
*
90+
* This function prints a given input in green
91+
* color without a \r\n signs at the end
92+
*
93+
* @retval None
94+
*/
95+
inline void printGreen(const char c[]) {
96+
char tmp[100];
97+
sprintf(tmp, "%s%s%s", ANSI_COLOR_GREEN, c, ANSI_COLOR_RESET);
98+
Serial.print(tmp);
99+
}
100+
101+
/**
102+
*
103+
* Printout data in a standard hex view
104+
*
105+
* @param[in] p_buf Pointer to data which should be printed out.
106+
* @param[in] l_len Length of a data
107+
*
108+
* @retval None
109+
* @example
110+
0x000000: 2e 2f 68 65 78 64 75 6d ./hexdum
111+
0x000008: 70 00 53 53 48 5f 41 47 p.SSH_AG
112+
0x000010: 45 4e 54 5f ENT_
113+
*/
114+
inline void __hexdump__(const void* p_buf, uint32_t l_len) {
115+
unsigned int i, j;
116+
char str[MAXCMD_LEN];
117+
for (i = 0; i < l_len + ((l_len % HEXDUMP_COLS) ?
118+
( HEXDUMP_COLS - l_len % HEXDUMP_COLS) : 0);
119+
i++) {
120+
/* print offset */
121+
if (i % HEXDUMP_COLS == 0) {
122+
sprintf(str, "0x%06x: ", i);
123+
Serial.print(str);
124+
}
125+
126+
/* print hex data */
127+
if (i < l_len) {
128+
sprintf(str, "%02x ", 0xFF & ((char*) p_buf)[i]);
129+
Serial.print(str);
130+
} else /* end of block, just aligning for ASCII dump */
131+
{
132+
sprintf(str, " ");
133+
Serial.print(str);
134+
}
135+
136+
/* print ASCII dump */
137+
if (i % HEXDUMP_COLS == ( HEXDUMP_COLS - 1)) {
138+
for (j = i - ( HEXDUMP_COLS - 1); j <= i; j++) {
139+
if (j >= l_len) /* end of block, not really printing */
140+
{
141+
Serial.print(' ');
142+
} else if (isprint((int) ((char*) p_buf)[j])) /* printable char */
143+
{
144+
Serial.print(((char*) p_buf)[j]);
145+
} else /* other char */
146+
{
147+
Serial.print('.');
148+
}
149+
}
150+
Serial.print('\r');
151+
Serial.print('\n');
152+
}
153+
}
154+
}
155+
156+
#ifdef __cplusplus
157+
}
158+
#endif
159+
#endif

examples/testFullAPI/testFullAPI.ino renamed to examples/testFullAPI/testFullAPI.ino/testFullAPI.ino.ino

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "OPTIGATrustX.h"
2929

30+
#define MAXCMD_LEN 255
3031
#define CERT_LENGTH 512
3132
#define RND_LENGTH 64
3233
#define HASH_LENGTH 32
@@ -39,6 +40,25 @@
3940

4041
#define ASSERT(err) if (ret) { printlnRed("Failed"); while (true); }
4142

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+
4262
static void output_result(char* tag, uint8_t* in, uint16_t in_len)
4363
{
4464
printlnGreen("OK");
@@ -54,19 +74,7 @@ void loop()
5474
{
5575
uint32_t ret = 0;
5676
uint8_t cntr = 10;
57-
uint8_t ifxPublicKey[68];
58-
uint8_t uid[UID_LENGTH];
59-
uint8_t cert[CERT_LENGTH];
60-
uint16_t certLen = CERT_LENGTH;
61-
uint8_t rnd[RND_LENGTH];
62-
uint16_t rndLen = RND_LENGTH;
63-
uint8_t hash[HASH_LENGTH];
64-
uint16_t hashLen = HASH_LENGTH;
65-
uint8_t rawSign[SIGN_LENGTH];
66-
uint8_t formSign[SIGN_LENGTH];
67-
uint16_t signLen = SIGN_LENGTH;
68-
uint8_t pubKey[PUBKEY_LENGTH];
69-
uint16_t pubKeyLen = PUBKEY_LENGTH;
77+
uint8_t ifxPublicKey[68];
7078

7179
/*
7280
* Getting co-processor Unique ID
@@ -89,7 +97,8 @@ void loop()
8997
* Generate a Keypair
9098
*/
9199
printGreen("Generate Key Pair ... ");
92-
ret = trustX.generateKeypair(pubKey, pubKeyLen);
100+
uint16_t ctx = 0;
101+
ret = trustX.generateKeypair(pubKey, pubKeyLen, ctx);
93102
ASSERT(ret);
94103
output_result("Public key", pubKey, pubKeyLen);
95104

@@ -111,20 +120,21 @@ void loop()
111120
output_result("SHA256", hash, hashLen);
112121

113122
/*
114-
* Sign hash with an embedded device key NIST-P256
123+
* Generate a signature NIST-P256
115124
*/
116125
printGreen("Generate Signature ... ");
117126
ret = trustX.calculateSignature(hash, hashLen, formSign, signLen);
118127
ASSERT(ret);
119-
output_result("Signature", hash, hashLen);
128+
output_result("Signature", formSign, signLen);
120129

121130
/*
122131
* Verify just geberated signature
123132
*/
124-
trustX.getPublicKey(ifxPublicKey);
133+
getPublicKey(ifxPublicKey);
125134

126135
printGreen("Verify Signature ... ");
127-
ret = trustX.verifySignature(hash, hashLen, formSign, signLen, ifxPublicKey, sizeof(ifxPublicKey));
136+
ret = trustX.verifySignature(hash, hashLen, formSign, signLen, ifxPublicKey,
137+
sizeof(ifxPublicKey) / sizeof(ifxPublicKey[0]));
128138
ASSERT(ret);
129139
printlnGreen("OK");
130140

@@ -154,30 +164,28 @@ void setup()
154164
*/
155165
printGreen("Begin Trust ... ");
156166
ret = trustX.begin();
157-
ASSERT(ret);
158-
printlnGreen("OK");
167+
ASSERT(ret);
168+
printlnGreen("OK");
159169

160170
/*
161171
* Speed up the chip (min is 6ma, maximum is 15ma)
162172
*/
163173
printGreen("Setting Current Limit... ");
164174
ret = trustX.setCurrentLimit(15);
165-
ASSERT(ret);
166-
printlnGreen("OK");
175+
ASSERT(ret);
176+
printlnGreen("OK");
167177

168178
/*
169179
* Check the return value which we just set
170180
*/
171181
printGreen("Checking Power Limit... ");
172182
uint8_t current_lim = 0;
173183
ret = trustX.getCurrentLimit(current_lim);
174-
ASSERT(ret);
175-
printlnGreen("OK");
184+
ASSERT(ret);
176185
if (current_lim == 15) {
177186
printlnGreen("OK");
178187
} else {
179188
printlnRed("Failed");
180189
while(1);
181190
}
182-
183191
}

0 commit comments

Comments
 (0)