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.

src/SensorNode.cpp

Committer:
IanBenzMaxim
Date:
2019-10-04
Revision:
17:5926077e5345
Parent:
16:a004191a79ab

File content as of revision 17:5926077e5345:

/*******************************************************************************
* Copyright (C) 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 <string>
#include <MaximInterfaceCore/HexString.hpp>
#include <mbed-os/drivers/I2C.h>
#include "SensorNode.hpp"

#define TRY MaximInterfaceCore_TRY
#define TRY_VALUE MaximInterfaceCore_TRY_VALUE

using namespace MaximInterfaceCore;
using MaximInterfaceDevices::DS2476;
using MaximInterfaceDevices::DS28C36;

// I2C address of the MLX90614.
static const uint8_t mlx90614Addr = 0xB4;

SensorNode::SensorNode(Sleep & sleep, I2CMaster & i2c, DS2476 & ds2476)
    : i2c(&i2c), ds28c36(sleep, i2c), ds2476(&ds2476) {}

SensorNode::State SensorNode::detect() {
  if (Result<DS28C36::Page::array> page =
          ds28c36.readMemory(DS28C36::romOptionsPage)) {
    const DS28C36::RomOptions romOptions(page.value());
    copy(romOptions.romId(), make_span(romId));
    copy(romOptions.manId(), make_span(manId));
  } else {
    return Disconnected;
  }

  if (!valid(romId)) {
    return Disconnected;
  }

  {
    const Result<void> result = authenticate();
    if (!result) {
      return (result.error() == DS28C36::AuthenticationError) ? Invalid
                                                              : Disconnected;
    }
  }

  const Result<bool> laserEnabled = getLaserEnabled();
  return laserEnabled
             ? (laserEnabled.value() ? ValidLaserEnabled : ValidLaserDisabled)
             : Disconnected;
}

Result<double> SensorNode::readTemp(TempStyle style) {
  uint8_t data[2];
  switch (style) {
  case ObjectTemp:
  default:
    data[0] = 0x07;
    break;

  case AmbientTemp:
    data[0] = 0x06;
    break;
  }
  TRY(i2c->writePacket(mlx90614Addr, make_span(data, 1),
                       I2CMaster::StopOnError));
  TRY(i2c->readPacket(mlx90614Addr, data, I2CMaster::Stop));
  return ((static_cast<unsigned int>(data[1]) << 8) | data[0]) * 0.02 - 273.15;
}

Result<void> SensorNode::authenticate() {
  // Compute slave secret on coprocessor.
  DS28C36::PageAuthenticationData message;
  message.setPageNum(0);
  if (const Result<DS28C36::Page::array> page =
          ds28c36.readMemory(message.pageNum())) {
    message.setPage(page.value());
  } else {
    return page.error();
  }
  message.setRomId(romId);
  message.setManId(manId);
  TRY(ds2476->writeBuffer(message.result()));
  TRY(ds2476->computeSha2UniqueSecret(DS2476::SecretNumA));

  // Compute HMAC on coprocessor.
  // ROM ID, Page 0, and MAN ID are already in message.
  TRY(ds2476->readRng(message.challenge()));
  TRY(ds2476->writeBuffer(message.result()));
  DS2476::Page::array computedHmac;
  TRY_VALUE(computedHmac, ds2476->computeSha2Hmac());

  // Compute HMAC on device.
  TRY(ds28c36.writeBuffer(message.challenge()));
  DS28C36::Page::array deviceHmac;
  TRY_VALUE(deviceHmac, ds28c36.computeAndReadPageAuthentication(
                            message.pageNum(), DS28C36::SecretNumA));

  return (computedHmac == deviceHmac) ? makeResult(none)
                                      : DS28C36::AuthenticationError;
}

Result<void> SensorNode::setLaserEnabled(bool enabled,
                                         const PrintHandler & print) {
  if (print) {
    print((std::string(enabled ? "Enabling" : "Disabling") + " laser").c_str());
  }

  // Compute write HMAC.
  // ROM ID and MAN ID are already in message.
  if (print) {
    print("Reading GPIO Control page");
    print((std::string(
               "Modify copy of GPIO Control page to set GPIO B state to ") +
           (enabled ? "conducting" : "high-impedance"))
              .c_str());
  }
  DS28C36::WriteAuthenticationData message;
  message.setPageNum(DS28C36::gpioControlPage);
  if (const Result<DS28C36::Page::array> oldPage =
          ds28c36.readMemory(message.pageNum())) {
    message.setOldPage(oldPage.value());
  } else {
    return oldPage.error();
  }
  message.setNewPage(message.oldPage());
  DS28C36::GpioControl(message.newPage()).setPiobConducting(enabled);
  message.setRomId(romId);
  message.setManId(manId);
  if (print) {
    print(("Creating HMAC message (ROM ID | Old Page Data | New Page Data | "
           "Page # | MAN ID): " +
           toHexString(message.result()))
              .c_str());
    print("Writing HMAC message to DS2476 buffer");
  }
  TRY(ds2476->writeBuffer(message.result()));
  DS2476::Page::array hmac;
  TRY_VALUE(hmac, ds2476->computeSha2Hmac());

  if (print) {
    print(("DS2476 computes write authentication HMAC from slave secret "
           "(Secret S) and message in buffer: " +
           toHexString(hmac))
              .c_str());
    print("Write authentication HMAC is written to DS28C36 buffer");
    print("DS28C36 computes an HMAC to compare to the write authentication "
          "HMAC after receiving Page # and New Page Data");
  }

  // Write page data.
  TRY(ds28c36.writeBuffer(hmac));
  TRY(ds28c36.authenticatedSha2WriteMemory(
      message.pageNum(), DS28C36::SecretNumA, message.newPage()));
  if (print) {
    print("DS28C36 updates page data which changes GPIO B state");
    print((std::string("Laser is now ") + (enabled ? "enabled" : "disabled"))
              .c_str());
  }
  return none;
}

Result<bool> SensorNode::getLaserEnabled() {
  DS28C36::Page::array page;
  TRY_VALUE(page, ds28c36.readMemory(DS28C36::gpioControlPage));
  return DS28C36::GpioControl(page).piobConducting();
}