DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.

The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.

The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.

More information about the MAXREFDES155# is available on the Maxim Integrated website.

Text.cpp

Committer:
IanBenzMaxim
Date:
2018-01-19
Revision:
15:75404fab3615
Parent:
13:6a6225690c2e

File content as of revision 15:75404fab3615:

/*******************************************************************************
* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
*
* 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 MAXIM INTEGRATED 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.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************/

#include <algorithm>
#include <functional>
#include <list>
#include "Bitmap.hpp"
#include "Display.hpp"
#include "Text.hpp"

using std::string;

static const int invalidWidthHeight = -1;

static const int characterWidth = 5;
static const int characterHeight = 7;

static const unsigned char printableCharBegin = 0x20;
static const unsigned char printableCharEnd = 0x7E;

static const uint8_t characterMap[][characterHeight] = {
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // ' '
    {0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x20}, // '!'
    {0x50, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00}, // '"'
    {0x50, 0x50, 0xF8, 0x50, 0xF8, 0x50, 0x50}, // '#'
    {0x20, 0x78, 0xA0, 0x70, 0x28, 0xF0, 0x20}, // '$'
    {0xC0, 0xC8, 0x10, 0x20, 0x40, 0x98, 0x18}, // '%'
    {0x60, 0x90, 0xA0, 0x40, 0xA8, 0x90, 0x68}, // '&'
    {0x60, 0x20, 0x40, 0x00, 0x00, 0x00, 0x00}, // '''
    {0x10, 0x20, 0x40, 0x40, 0x40, 0x20, 0x10}, // '('
    {0x40, 0x20, 0x10, 0x10, 0x10, 0x20, 0x40}, // ')'
    {0x00, 0x20, 0xA8, 0x70, 0xA8, 0x20, 0x00}, // '*'
    {0x00, 0x20, 0x20, 0xF8, 0x20, 0x20, 0x00}, // '+'
    {0x00, 0x00, 0x00, 0x00, 0x60, 0x20, 0x40}, // ','
    {0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00}, // '-'
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60}, // '.'
    {0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00}, // '/'
    {0x70, 0x88, 0x98, 0xA8, 0xC8, 0x88, 0x70}, // '0'
    {0x20, 0x60, 0x20, 0x20, 0x20, 0x20, 0x70}, // '1'
    {0x70, 0x88, 0x08, 0x10, 0x20, 0x40, 0xF8}, // '2'
    {0xF8, 0x10, 0x20, 0x10, 0x08, 0x88, 0x70}, // '3'
    {0x10, 0x30, 0x50, 0x90, 0xF8, 0x10, 0x10}, // '4'
    {0xF8, 0x80, 0xF0, 0x08, 0x08, 0x88, 0x70}, // '5'
    {0x30, 0x40, 0x80, 0xF0, 0x88, 0x88, 0x70}, // '6'
    {0xF8, 0x08, 0x10, 0x20, 0x40, 0x40, 0x40}, // '7'
    {0x70, 0x88, 0x88, 0x70, 0x88, 0x88, 0x70}, // '8'
    {0x70, 0x88, 0x88, 0x78, 0x08, 0x10, 0x60}, // '9'
    {0x00, 0x60, 0x60, 0x00, 0x60, 0x60, 0x00}, // ':'
    {0x00, 0x60, 0x60, 0x00, 0x60, 0x20, 0x40}, // ';'
    {0x10, 0x20, 0x40, 0x80, 0x40, 0x20, 0x10}, // '<'
    {0x00, 0x00, 0xF8, 0x00, 0xF8, 0x00, 0x00}, // '='
    {0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40}, // '>'
    {0x70, 0x88, 0x08, 0x10, 0x20, 0x00, 0x20}, // '?'
    {0x70, 0x88, 0x08, 0x68, 0xA8, 0xA8, 0x70}, // '@'
    {0x70, 0x88, 0x88, 0x88, 0xF8, 0x88, 0x88}, // 'A'
    {0xF0, 0x88, 0x88, 0xF0, 0x88, 0x88, 0xF0}, // 'B'
    {0x70, 0x88, 0x80, 0x80, 0x80, 0x88, 0x70}, // 'C'
    {0xE0, 0x90, 0x88, 0x88, 0x88, 0x90, 0xE0}, // 'D'
    {0xF8, 0x80, 0x80, 0xF0, 0x80, 0x80, 0xF8}, // 'E'
    {0xF8, 0x80, 0x80, 0xF0, 0x80, 0x80, 0x80}, // 'F'
    {0x70, 0x88, 0x80, 0xB8, 0x88, 0x88, 0x78}, // 'G'
    {0x88, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88}, // 'H'
    {0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70}, // 'I'
    {0x38, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60}, // 'J'
    {0x88, 0x90, 0xA0, 0xC0, 0xA0, 0x90, 0x88}, // 'K'
    {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xF8}, // 'L'
    {0x88, 0xD8, 0xA8, 0xA8, 0x88, 0x88, 0x88}, // 'M'
    {0x88, 0x88, 0xC8, 0xA8, 0x98, 0x88, 0x88}, // 'N'
    {0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70}, // 'O'
    {0xF0, 0x88, 0x88, 0xF0, 0x80, 0x80, 0x80}, // 'P'
    {0x70, 0x88, 0x88, 0x88, 0xA8, 0x90, 0x68}, // 'Q'
    {0xF0, 0x88, 0x88, 0xF0, 0xA0, 0x90, 0x88}, // 'R'
    {0x78, 0x80, 0x80, 0x70, 0x08, 0x08, 0xF0}, // 'S'
    {0xF8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20}, // 'T'
    {0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70}, // 'U'
    {0x88, 0x88, 0x88, 0x88, 0x88, 0x50, 0x20}, // 'V'
    {0x88, 0x88, 0x88, 0xA8, 0xA8, 0xA8, 0x50}, // 'W'
    {0x88, 0x88, 0x50, 0x20, 0x50, 0x88, 0x88}, // 'X'
    {0x88, 0x88, 0x88, 0x50, 0x20, 0x20, 0x20}, // 'Y'
    {0xF8, 0x08, 0x10, 0x20, 0x40, 0x80, 0xF8}, // 'Z'
    {0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x70}, // '['
    {0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00}, // '\'
    {0x70, 0x10, 0x10, 0x10, 0x10, 0x10, 0x70}, // ']'
    {0x20, 0x50, 0x88, 0x00, 0x00, 0x00, 0x00}, // '^'
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}, // '_'
    {0x40, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00}, // '`'
    {0x00, 0x00, 0x70, 0x08, 0x78, 0x88, 0x78}, // 'a'
    {0x80, 0x80, 0xB0, 0xC8, 0x88, 0x88, 0xF0}, // 'b'
    {0x00, 0x00, 0x70, 0x80, 0x80, 0x88, 0x70}, // 'c'
    {0x08, 0x08, 0x68, 0x98, 0x88, 0x88, 0x78}, // 'd'
    {0x00, 0x00, 0x70, 0x88, 0xF8, 0x80, 0x70}, // 'e'
    {0x30, 0x48, 0x40, 0xE0, 0x40, 0x40, 0x40}, // 'f'
    {0x00, 0x78, 0x88, 0x88, 0x78, 0x08, 0x70}, // 'g'
    {0x80, 0x80, 0xB0, 0xC8, 0x88, 0x88, 0x88}, // 'h'
    {0x20, 0x00, 0x60, 0x20, 0x20, 0x20, 0x70}, // 'i'
    {0x10, 0x00, 0x30, 0x10, 0x10, 0x90, 0x60}, // 'j'
    {0x80, 0x80, 0x90, 0xA0, 0xC0, 0xA0, 0x90}, // 'k'
    {0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70}, // 'l'
    {0x00, 0x00, 0xD0, 0xA8, 0xA8, 0x88, 0x88}, // 'm'
    {0x00, 0x00, 0xB0, 0xC8, 0x88, 0x88, 0x88}, // 'n'
    {0x00, 0x00, 0x70, 0x88, 0x88, 0x88, 0x70}, // 'o'
    {0x00, 0x00, 0xF0, 0x88, 0xF0, 0x80, 0x80}, // 'p'
    {0x00, 0x00, 0x68, 0x98, 0x78, 0x08, 0x08}, // 'q'
    {0x00, 0x00, 0xB0, 0xC8, 0x80, 0x80, 0x80}, // 'r'
    {0x00, 0x00, 0x70, 0x80, 0x70, 0x08, 0xF0}, // 's'
    {0x40, 0x40, 0xE0, 0x40, 0x40, 0x48, 0x30}, // 't'
    {0x00, 0x00, 0x88, 0x88, 0x88, 0x98, 0x68}, // 'u'
    {0x00, 0x00, 0x88, 0x88, 0x88, 0x41, 0x20}, // 'v'
    {0x00, 0x00, 0x88, 0x88, 0xA8, 0xA8, 0x50}, // 'w'
    {0x00, 0x00, 0x88, 0x50, 0x20, 0x50, 0x88}, // 'x'
    {0x00, 0x00, 0x88, 0x88, 0x78, 0x08, 0x70}, // 'y'
    {0x00, 0x00, 0xF8, 0x10, 0x20, 0x40, 0xF8}, // 'z'
    {0x10, 0x20, 0x20, 0x40, 0x20, 0x20, 0x10}, // '{'
    {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20}, // '|'
    {0x40, 0x20, 0x20, 0x10, 0x20, 0x20, 0x40}, // '}'
    {0x00, 0x00, 0x40, 0xA8, 0x10, 0x00, 0x00}  // '~'
};

static bool charPrintable(unsigned char c) {
  return (c >= printableCharBegin) && (c <= printableCharEnd);
}

static void removeNonprintableChars(string & s) {
  s.erase(std::remove_if(s.begin(), s.end(),
                         std::not1(std::ptr_fun(charPrintable))),
          s.end());
}

static bool compareStringLength(const string & a, const string & b) {
  return a.length() < b.length();
}

namespace {

// Functor trims string if it exceeds a certain length.
class TrimString {
public:
  TrimString(string::size_type maxSize) : maxSize(maxSize) {}

  void operator()(string & s) {
    if (s.size() > maxSize) {
      s.resize(maxSize);
    }
  }

private:
  string::size_type maxSize;
};

} // namespace

// Split a string based on a character token. Token will be removed.
static std::list<string> tokenizeString(const string & toSplit, char token) {
  std::list<string> toSplitLines;
  string::size_type beginIdx = 0, endIdx;
  do {
    endIdx = toSplit.find(token, beginIdx);
    toSplitLines.push_back(toSplit.substr(
        beginIdx, endIdx == string::npos ? string::npos : endIdx - beginIdx));
    beginIdx = endIdx + 1;
  } while (endIdx != string::npos);
  return toSplitLines;
}

// Word wrap string into lines based on a specified line length.
static std::list<string> wrapLines(const string & toSplit,
                                   const string::size_type lineLen) {
  std::list<string> toSplitLines;
  string::size_type beginIdx = 0;
  if (lineLen > 0) {
    // Split lines as much as necessary.
    string::size_type endIdx = lineLen;
    while (((toSplit.length() - beginIdx) > lineLen) &&
           (endIdx != string::npos)) {
      endIdx = toSplit.rfind(' ', endIdx);
      if ((endIdx == string::npos) || (endIdx <= beginIdx)) {
        // Current word is too long to split. Find end of current word.
        endIdx = toSplit.find(' ', beginIdx);
      }
      if (endIdx != string::npos) {
        toSplitLines.push_back(toSplit.substr(beginIdx, endIdx - beginIdx));
        beginIdx = endIdx + 1;
        endIdx = beginIdx + lineLen;
      }
    }
  }
  // Last line is any remaining characters.
  toSplitLines.push_back(toSplit.substr(beginIdx, toSplit.length() - beginIdx));
  return toSplitLines;
}

Text::Text()
    : m_text(), m_wordWrap(false), m_charSpacing(1), m_lineSpacing(1),
      m_textLines(), m_preferredWidth(invalidWidthHeight),
      m_preferredHeight(invalidWidthHeight) {}

void Text::setText(const string & text) {
  if (m_text != text) {
    m_text = text;
    invalidate();
    m_preferredWidth = m_preferredHeight = invalidWidthHeight;
  }
}

void Text::setWordWrap(bool wordWrap) {
  if (m_wordWrap != wordWrap) {
    m_wordWrap = wordWrap;
    invalidate();
    m_preferredWidth = m_preferredHeight = invalidWidthHeight;
  }
}

void Text::setLineSpacing(int lineSpacing) {
  if (lineSpacing > 0 && m_lineSpacing != lineSpacing) {
    m_lineSpacing = lineSpacing;
    invalidate();
    m_preferredHeight = invalidWidthHeight;
  }
}

void Text::setCharSpacing(int charSpacing) {
  if (charSpacing > 0 && m_charSpacing != charSpacing) {
    m_charSpacing = charSpacing;
    invalidate();
    m_preferredWidth = invalidWidthHeight;
    if (wordWrap()) {
      m_preferredHeight = invalidWidthHeight;
    }
  }
}

int Text::preferredWidth() const {
  if (m_preferredWidth == invalidWidthHeight) {
    calculateLayout();
  }
  return m_preferredWidth;
}

int Text::preferredHeight() const {
  if (m_preferredHeight == invalidWidthHeight) {
    calculateLayout();
  }
  return m_preferredHeight;
}

void Text::resized() {
  m_preferredWidth = invalidWidthHeight;
  if (wordWrap()) {
    m_preferredHeight = invalidWidthHeight;
  }
}

void Text::doRender(Bitmap & bitmap, int xOffset, int yOffset) const {
  using std::list;

  // Ensure layout is up to date.
  if (m_preferredWidth == invalidWidthHeight ||
      m_preferredHeight == invalidWidthHeight) {
    calculateLayout();
  }

  // Render each line.
  int lineNum = 0;
  for (list<string>::const_iterator lineIt = m_textLines.begin();
       lineIt != m_textLines.end(); lineIt++) {
    // Render each character.
    for (string::size_type charNum = 0; charNum < lineIt->length(); charNum++) {
      bitmap.overlay(xOffset + x() + charNum * (characterWidth + m_charSpacing),
                     yOffset + y() +
                         lineNum * (characterHeight + m_lineSpacing),
                     &characterMap[(*lineIt)[charNum] - printableCharBegin][0],
                     characterHeight, characterWidth);
    }
    lineNum++;
  }
}

void Text::calculateLayout() const {
  using std::list;

  // Split string into lines.
  m_textLines = tokenizeString(m_text, '\n');

  // Remove non-printable characters.
  std::for_each(m_textLines.begin(), m_textLines.end(),
                removeNonprintableChars);

  const int lineLen =
      (width() / (characterWidth + m_charSpacing)) +
      (((width() % (characterWidth + m_charSpacing)) >= characterWidth) ? 1
                                                                        : 0);
  const int numLines =
      (height() / (characterHeight + m_lineSpacing)) +
      (((height() % (characterHeight + m_lineSpacing)) >= characterHeight) ? 1
                                                                           : 0);

  // Word wrap lines if enabled.
  if (m_wordWrap) {
    list<string>::iterator lineIt = m_textLines.begin();
    while (lineIt != m_textLines.end()) {
      list<string>::iterator nextLineIt = lineIt;
      nextLineIt++;

      // Wrap current line.
      list<string> wrappedLines = wrapLines(*lineIt, lineLen);
      m_textLines.splice(lineIt, wrappedLines);
      // Remove old line.
      m_textLines.erase(lineIt);

      lineIt = nextLineIt;
    }
  }

  // Calculate preferred size.
  string::size_type maxLineLength =
      std::max_element(m_textLines.begin(), m_textLines.end(),
                       compareStringLength)->length();
  m_preferredWidth = (maxLineLength > 0)
                         ? (maxLineLength * characterWidth) +
                               ((maxLineLength - 1) * m_charSpacing)
                         : 1;
  m_preferredHeight = (m_textLines.size() * characterHeight) +
                      ((m_textLines.size() - 1) * m_lineSpacing);

  // Remove clipped text.
  if (m_textLines.size() > static_cast<list<string>::size_type>(numLines))
    m_textLines.resize(numLines);
  std::for_each(m_textLines.begin(), m_textLines.end(), TrimString(lineLen));
}