Initial
This commit is contained in:
156
src/fprint.h
Normal file
156
src/fprint.h
Normal file
@ -0,0 +1,156 @@
|
||||
#ifndef FPRINT_H
|
||||
#define FPRINT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifndef SUPPRESSCOLLORS
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
#else
|
||||
#define ANSI_COLOR_RED ""
|
||||
#define ANSI_COLOR_GREEN ""
|
||||
#define ANSI_COLOR_YELLOW ""
|
||||
#define ANSI_COLOR_BLUE ""
|
||||
#define ANSI_COLOR_MAGENTA ""
|
||||
#define ANSI_COLOR_CYAN ""
|
||||
#define ANSI_COLOR_RESET ""
|
||||
#endif
|
||||
|
||||
#define MAXCMD_LEN 255
|
||||
#define HEXDUMP_COLS 16
|
||||
|
||||
#ifndef SUPPRESSHEXDUMP
|
||||
#define SUPPRESSHEXDUMP 0
|
||||
#endif
|
||||
#define HEXDUMP(a, b) (SUPPRESSHEXDUMP == 0) ? __hexdump__(a, b) : (void)0;
|
||||
/**
|
||||
*
|
||||
* This function prints a given input in green
|
||||
* color with a \r\n signs at the end
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
inline void printlnGreen(const char c[]) {
|
||||
char tmp[100];
|
||||
sprintf(tmp, "%s%s%s", ANSI_COLOR_GREEN, c, ANSI_COLOR_RESET);
|
||||
Serial.println(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This function prints a given input in red
|
||||
* color with a \r\n signs at the end
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
inline void printlnRed(const char c[]) {
|
||||
char tmp[100];
|
||||
sprintf(tmp, "%s%s%s", ANSI_COLOR_RED, c, ANSI_COLOR_RESET);
|
||||
Serial.println(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This function prints a given input in magenta
|
||||
* color with a \r\n signs at the end
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
inline void printlnMagenta(const char c[]) {
|
||||
char tmp[100];
|
||||
sprintf(tmp, "%s%s%s", ANSI_COLOR_MAGENTA, c, ANSI_COLOR_RESET);
|
||||
Serial.println(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This function prints a given input in magenta
|
||||
* color without a \r\n signs at the end
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
inline void printMagenta(const char c[]) {
|
||||
char tmp[100];
|
||||
sprintf(tmp, "%s%s%s", ANSI_COLOR_MAGENTA, c, ANSI_COLOR_RESET);
|
||||
Serial.print(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This function prints a given input in green
|
||||
* color without a \r\n signs at the end
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
inline void printGreen(const char c[]) {
|
||||
char tmp[100];
|
||||
sprintf(tmp, "%s%s%s", ANSI_COLOR_GREEN, c, ANSI_COLOR_RESET);
|
||||
Serial.print(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Printout data in a standard hex view
|
||||
*
|
||||
* @param[in] p_buf Pointer to data which should be printed out.
|
||||
* @param[in] l_len Length of a data
|
||||
*
|
||||
* @retval None
|
||||
* @example
|
||||
0x000000: 2e 2f 68 65 78 64 75 6d ./hexdum
|
||||
0x000008: 70 00 53 53 48 5f 41 47 p.SSH_AG
|
||||
0x000010: 45 4e 54 5f ENT_
|
||||
*/
|
||||
inline void __hexdump__(const void *p_buf, uint32_t l_len) {
|
||||
unsigned int i, j;
|
||||
char str[MAXCMD_LEN];
|
||||
for (i = 0; i < l_len + ((l_len % HEXDUMP_COLS) ? (HEXDUMP_COLS - l_len % HEXDUMP_COLS) : 0); i++) {
|
||||
/* print offset */
|
||||
if (i % HEXDUMP_COLS == 0) {
|
||||
sprintf(str, "0x%06x: ", i);
|
||||
Serial.print(str);
|
||||
}
|
||||
|
||||
/* print hex data */
|
||||
if (i < l_len) {
|
||||
sprintf(str, "%02x ", 0xFF & ((char *)p_buf)[i]);
|
||||
Serial.print(str);
|
||||
} else /* end of block, just aligning for ASCII dump */
|
||||
{
|
||||
sprintf(str, " ");
|
||||
Serial.print(str);
|
||||
}
|
||||
|
||||
/* print ASCII dump */
|
||||
if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) {
|
||||
for (j = i - (HEXDUMP_COLS - 1); j <= i; j++) {
|
||||
if (j >= l_len) /* end of block, not really printing */
|
||||
{
|
||||
Serial.print(' ');
|
||||
} else if (isprint((int)((char *)p_buf)[j])) /* printable char */
|
||||
{
|
||||
Serial.print(((char *)p_buf)[j]);
|
||||
} else /* other char */
|
||||
{
|
||||
Serial.print('.');
|
||||
}
|
||||
}
|
||||
Serial.print('\r');
|
||||
Serial.print('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
267
src/main.cpp
Normal file
267
src/main.cpp
Normal file
@ -0,0 +1,267 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Infineon Technologies AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE
|
||||
*
|
||||
* Demonstrates use of the
|
||||
* Infineon Technologies AG OPTIGA™ Trust X Arduino library
|
||||
*/
|
||||
|
||||
#include "OPTIGATrustX.h"
|
||||
|
||||
#define MAXCMD_LEN 255
|
||||
#define CERT_LENGTH 512
|
||||
#define RND_LENGTH 64
|
||||
#define HASH_LENGTH 32
|
||||
#define SIGN_LENGTH 80
|
||||
#define PUBKEY_LENGTH 70
|
||||
#define UID_LENGTH 27
|
||||
|
||||
#define SUPPRESSCOLLORS
|
||||
#include "fprint.h"
|
||||
#include <WiFi.h>
|
||||
|
||||
bool oldButton = true;
|
||||
#define button 18
|
||||
|
||||
|
||||
const char* ssid = "LBsPhone";
|
||||
const char* password = "simple";
|
||||
|
||||
const char* host = "192.168.0.111";
|
||||
const int port = 8234;
|
||||
|
||||
#define ASSERT(err) if (ret) { printlnRed("Failed"); while (true); }
|
||||
|
||||
/*
|
||||
* Allocating buffers for further use in loop()
|
||||
*/
|
||||
uint8_t *cert = new uint8_t[CERT_LENGTH];
|
||||
uint16_t certLen = CERT_LENGTH;
|
||||
uint8_t *rnd = new uint8_t[RND_LENGTH];
|
||||
uint16_t rndLen = RND_LENGTH;
|
||||
uint8_t *hash = new uint8_t[HASH_LENGTH];
|
||||
uint16_t hashLen = HASH_LENGTH;
|
||||
uint8_t *rawSign = new uint8_t[SIGN_LENGTH];
|
||||
uint8_t *formSign = new uint8_t[SIGN_LENGTH];
|
||||
uint16_t signLen = SIGN_LENGTH;
|
||||
uint8_t *format = new uint8_t[SIGN_LENGTH];
|
||||
uint16_t formatLen = SIGN_LENGTH;
|
||||
uint8_t *pubKey = new uint8_t[PUBKEY_LENGTH];
|
||||
uint16_t pubKeyLen = PUBKEY_LENGTH;
|
||||
uint8_t *uid = new uint8_t[UID_LENGTH];
|
||||
|
||||
|
||||
|
||||
|
||||
static void output_result(char* tag, uint8_t* in, uint16_t in_len)
|
||||
{
|
||||
printlnGreen("OK");
|
||||
printMagenta(tag);
|
||||
printMagenta(" Length: ");
|
||||
Serial.println(in_len);
|
||||
printMagenta(tag);
|
||||
printlnMagenta(":");
|
||||
HEXDUMP(in, in_len);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
uint8_t cntr = 10;
|
||||
uint8_t ifxPublicKey[68];
|
||||
|
||||
/*
|
||||
* Getting co-processor Unique ID
|
||||
*/
|
||||
printGreen("Get co-processor UID ... ");
|
||||
uint16_t uidLength = UID_LENGTH;
|
||||
ret = trustX.getUniqueID(uid, uidLength);
|
||||
ASSERT(ret);
|
||||
output_result("Co-processor UID", uid, uidLength);
|
||||
|
||||
/*
|
||||
* Getting primary certificate
|
||||
*/
|
||||
printGreen("Reading cert ... ");
|
||||
ret = trustX.getCertificate(cert, certLen);
|
||||
ASSERT(ret);
|
||||
output_result("Certificate", cert, certLen);
|
||||
|
||||
/*
|
||||
* Generate a Keypair
|
||||
*/
|
||||
|
||||
/*
|
||||
printGreen("Generate Key Pair ... ");
|
||||
uint16_t ctx = 0;
|
||||
ret = trustX.generateKeypair(pubKey, pubKeyLen, ctx);
|
||||
ASSERT(ret);
|
||||
output_result("Public key", pubKey, pubKeyLen);
|
||||
*/
|
||||
|
||||
/*
|
||||
* Get random value of RND_LENGTH length
|
||||
*/
|
||||
/*
|
||||
printGreen("Get random value ... ");
|
||||
ret = trustX.getRandom(RND_LENGTH, rnd);
|
||||
ASSERT(ret);
|
||||
output_result("Random", rnd, RND_LENGTH);
|
||||
*/
|
||||
|
||||
/*
|
||||
* Calculate SHA256 value
|
||||
*/
|
||||
|
||||
char cmd[6] = "press\n";
|
||||
|
||||
output_result("PRESS", cmd, 6);
|
||||
|
||||
|
||||
printGreen("Calculate Hash ... ");
|
||||
ret = trustX.sha256(cmd, 6, hash);
|
||||
hashLen = 32;
|
||||
ASSERT(ret);
|
||||
output_result("SHA256", hash, hashLen);
|
||||
|
||||
|
||||
/*
|
||||
* Generate a signature NIST-P256
|
||||
*/
|
||||
printGreen("Generate Signature ... ");
|
||||
ret = trustX.calculateSignature(hash, hashLen, eFIRST_DEVICE_PRIKEY_1, formSign, signLen);
|
||||
Serial.printf("Code: %u\n", ret);
|
||||
ASSERT(ret);
|
||||
output_result("Signature", formSign, signLen);
|
||||
|
||||
printGreen("Format Signature ... ");
|
||||
ret = trustX.formatSignature(formSign, signLen, format, formatLen);
|
||||
ASSERT(ret);
|
||||
output_result("Signature Formated", format, formatLen);
|
||||
|
||||
/*
|
||||
* Verify just geberated signature
|
||||
*/
|
||||
trustX.getPublicKey(ifxPublicKey);
|
||||
|
||||
printGreen("Verify Signature ... ");
|
||||
ret = trustX.verifySignature(hash, hashLen, formSign, signLen);
|
||||
ASSERT(ret);
|
||||
printlnGreen("OK");
|
||||
|
||||
// init wifi
|
||||
// connect to server and send
|
||||
|
||||
/*
|
||||
* Count down 10 seconds and restart the application
|
||||
*/
|
||||
while(cntr) {
|
||||
Serial.print(cntr);
|
||||
Serial.println(" seconds untill restart.");
|
||||
delay(1000);
|
||||
cntr--;
|
||||
}
|
||||
}
|
||||
|
||||
void initWifi(){
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected with IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void sendData(uint8_t* data, uint16_t len) {
|
||||
Serial.print("connecting to ");
|
||||
Serial.println(host);
|
||||
// Use WiFiClient class to create TCP connections
|
||||
WiFiClient client;
|
||||
|
||||
if (!client.connect(host, port)) {
|
||||
Serial.println("connection failed");
|
||||
return;
|
||||
}
|
||||
// This will send the data to the server
|
||||
client.print("hello world");
|
||||
client.stop();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
|
||||
/*
|
||||
* Initialise serial output
|
||||
*/
|
||||
Serial.begin(38400);
|
||||
Serial.println("Initializing ... ");
|
||||
|
||||
/*
|
||||
* Initialise OPTIGA™ Trust X
|
||||
*/
|
||||
printGreen("Begin Trust ... ");
|
||||
ret = trustX.begin();
|
||||
ASSERT(ret);
|
||||
printlnGreen("OK");
|
||||
|
||||
/*
|
||||
* Speed up the chip (min is 6ma, maximum is 15ma)
|
||||
*/
|
||||
printGreen("Setting Current Limit... ");
|
||||
ret = trustX.setCurrentLimit(15);
|
||||
ASSERT(ret);
|
||||
printlnGreen("OK");
|
||||
|
||||
/*
|
||||
* Check the return value which we just set
|
||||
*/
|
||||
printGreen("Checking Power Limit... ");
|
||||
uint8_t current_lim = 0;
|
||||
ret = trustX.getCurrentLimit(current_lim);
|
||||
ASSERT(ret);
|
||||
if (current_lim == 15) {
|
||||
printlnGreen("OK");
|
||||
} else {
|
||||
printlnRed("Failed");
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void checkButton(){
|
||||
if(digitalRead(button) != oldButton){
|
||||
delay(5);
|
||||
if(digitalRead(button) != oldButton){
|
||||
if (oldButton == HIGH) {
|
||||
// Presed down
|
||||
} else {
|
||||
// pressed up
|
||||
}
|
||||
oldButton = digitalRead(button);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user