Infineon Team
Published © MIT

Low Power Cryptographic Dice

Use an Optiga security chip to generate random numbers for super low power electric dice!

BeginnerFull instructions provided1 hour1,053
Low Power Cryptographic Dice

Things used in this project

Hardware components

XMC2GO - industrial microcontroller kit
Infineon XMC2GO - industrial microcontroller kit
×1
SparkFun Lumenati 3x3
SparkFun Lumenati 3x3
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
S2GO SECURITY OPTIGA M
Infineon S2GO SECURITY OPTIGA M
×1

Software apps and online services

PlatformIO IDE
PlatformIO IDE

Story

Read more

Code

Cryptographic_Dice_XMC2Go

Arduino
generates a random number (1-6) using an OPTIGA Trust M chip and displaying it on a 3x3 LED Matrix
#include <Arduino.h>
/**
 * MIT License
 *
 * Copyright (c) 2020 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 M Arduino library
 */

#include <APA102.h>

// Define which pins to use.
const uint8_t dataPin = 5;
const uint8_t clockPin = 4;

int LED_brightness = 1000; //set 0-7905
int dieArray0[9]={0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0};
int dieArray1[9]={0 ,0 ,0 ,0 ,LED_brightness ,0 ,0 ,0 ,0};
int dieArray2[9]={LED_brightness ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,LED_brightness};
int dieArray3[9]={LED_brightness ,0 ,0 ,0 ,LED_brightness ,0 ,0 ,0 ,LED_brightness};
int dieArray4[9]={LED_brightness ,0 ,LED_brightness ,0 ,0 ,0 ,LED_brightness ,0 ,LED_brightness};
int dieArray5[9]={LED_brightness ,0 ,LED_brightness ,0 ,LED_brightness ,0 ,LED_brightness ,0 ,LED_brightness};
int dieArray6[9]={LED_brightness ,LED_brightness ,LED_brightness ,0 ,0 ,0 ,LED_brightness ,LED_brightness ,LED_brightness};
int dieArray9[9]={LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness ,LED_brightness};

int randomnumber;

#include "OPTIGATrustM.h"



#define RND_MAXLENGTH     256

  uint32_t ret = 0;
  uint8_t  cntr = 10;
  uint8_t  *rnd = new uint8_t[RND_MAXLENGTH];
  uint32_t ts = 0;

#define SUPPRESSCOLLORS
#include "fprint.h"


// Create an object for writing to the LED strip.
APA102<dataPin, clockPin> ledStrip;

// Set the number of LEDs to control.
const uint16_t ledCount = 9;

// We define "power" in this sketch to be the product of the
// 8-bit color channel value and the 5-bit brightness register.
// The maximum possible power is 255 * 31 (7905).
const uint16_t maxPower = 255 * 31;

// The power we want to use on the first LED is 1, which
// corresponds to the dimmest possible white.
const uint16_t minPower = 1;

// Calculate what the ratio between the powers of consecutive
// LEDs needs to be in order to reach the max power on the last
// LED of the strip.
const float multiplier = pow(maxPower / minPower, 1.0 / (ledCount - 1));

void sendWhite(uint16_t power)
{
  // Choose the lowest possible 5-bit brightness that will work.
  uint8_t brightness5Bit = 1;
  while(brightness5Bit * 255 < power && brightness5Bit < 31)
  {
    brightness5Bit++;
  }

  // Uncomment this line to simulate an LED strip that does not
  // have the extra 5-bit brightness register.  You will notice
  // that roughly the first third of the LED strip turns off
  // because the brightness8Bit equals zero.
  //brightness = 31;

  // Set brightness8Bit to be power divided by brightness5Bit,
  // rounded to the nearest whole number.
  uint8_t brightness8Bit = (power + (brightness5Bit / 2)) / brightness5Bit;

  // Send the white color to the LED strip.  At this point,
  // brightness8Bit multiplied by brightness5Bit should be
  // approximately equal to power.
  ledStrip.sendColor(brightness8Bit, brightness8Bit, brightness8Bit, brightness5Bit);
}

void setup() 
{
  /* Initialise the 3x3 Matrix */
  ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray0[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5);
  uint32_t ret = 0;
  
  /* Initialise a serial port for debug output */
  Serial.begin(9600);
  delay(10);

 /* Initialise the Optiga TrustM */
  ret = trustM.begin();
  if (ret) {
    printlnRed("Failed");
    while (true);
  }
  ret = trustM.setCurrentLimit(8);
  if (ret) {
    printlnRed("Failed");
    while (true);
  }
}

void loop()
{
  /* Initialise Memmory Area */
  memset(rnd, 0, RND_MAXLENGTH);
  
  /*Generate Random value */
  ret = trustM.getRandom(8, rnd);
  int rnum = 0xFF & ((char*) rnd)[0];
			Serial.println(rnum);
  
  /*Limit range to 1-6*/
    randomnumber = rnum / 34;
  
//Display
  switch (randomnumber) {
  case 1:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray1[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break; 
  case 2:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray2[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break;
  case 3:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray3[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break;
  case 4:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray4[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break;
   case 5:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray5[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break;
  case 6:
       ledStrip.startFrame();  for(uint16_t i = 0; i < ledCount; i++){sendWhite(dieArray6[i]);delay(1);}   ledStrip.endFrame(ledCount);  delay(5); while(1){};
    break;
  default:
       Serial.println("again");
    break;  
  }
 delete rnd;

}

Credits

Infineon Team

Infineon Team

90 projects • 134 followers

Comments