Search code examples
arduinoaesloracmac

CMACLib function not running in Arduino loramesh sketch


I have made a LoRa Mesh network which needs some authentication so I used the CMACLib library, a lightweight authentication library for Arduino to generate MACTags and use the CMAC algorithm based on AES.

This library's one specific function "subkeys = cmac_generate_subkeys(key)" doesn't work in my loramesh code but it works fine in a normal receiving-sending sketch using LoRa. For Mesh network code it is inspired from lora-mesh. I have made some changes of my own, like routing table entries, sending mode, flags but I don't think that is the problem. Can somebody help me?

So after calling the function the node is not getting initialized, it should be working, i know LoRa is simplex device and it can not work like loramesh class, but it's only about sending a one time authentication, if somebody know how to implement RHMesh::MeshApplicationMessage Struct Reference i will just make a payload transfer in that, maybe that will work..

#include <LoRa.h>
#include <EEPROM.h>
#include <RHRouter.h>
#include <RHMesh.h>
#include <RH_RF95.h>
#include <AESLib.h>
#include <CMACLib.h>
#define RH_HAVE_SERIAL
#define LED 13
#define N_NODES 2

uint8_t nodeId;
uint8_t routes[N_NODES]; // full routing table for mesh
int16_t rssi[N_NODES]; // signal strength info

// 16 bytes key
  uint8_t key[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};

  // struct for holding the subkeys that are derived from our key
  struct cmac_subkeys_t subkeys;

// Singleton instance of the radio driver
RH_RF95 rf95;

// Class to manage message delivery and receipt, using the driver declared above
RHMesh *manager;

// message buffer
char buf[RH_MESH_MAX_MESSAGE_LEN];

void setup() {
  randomSeed(analogRead(0));
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  nodeId = EEPROM.read(0);
  if (nodeId > 10) {
    Serial.print(F("EEPROM nodeId invalid: "));
    Serial.println(nodeId);
    nodeId = 1;
  }
  Serial.print(F("initializing node "));

  manager = new RHMesh(rf95, nodeId);

  if (!manager->init()) {
    Serial.println(F("init failed"));
  } else {
    Serial.println("done");
  }

  if (!LoRa.begin(868E6)) {
      Serial.println("Starting LoRa failed!");
      while (1);
    }

  rf95.setTxPower(50, false);
  rf95.setFrequency(868.0);

  // long range configuration requires for on-air time
  boolean longRange = false;
  if (longRange) {
    RH_RF95::ModemConfig modem_config = {
      0x78, // Reg 0x1D: BW=125kHz, Coding=4/8, Header=explicit
      0xC4, // Reg 0x1E: Spread=4096chips/symbol, CRC=enable
      0x08  // Reg 0x26: LowDataRate=On, Agc=Off.  0x0C is LowDataRate=ON, ACG=ON
    };
    rf95.setModemRegisters(&modem_config);
    }
    else
    {
    if (!rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128)) {
      Serial.println(F("set config failed"));
    }
  }

  Serial.println("RF95 ready");
  for(uint8_t n=1;n<=N_NODES;n++) {
    routes[n-1] = 0;
    rssi[n-1] = 0;
  }

  Serial.print(F("mem = "));
  Serial.println(freeMem());
  subkeys = cmac_generate_subkeys(key);
}

so after initializing node the manager doesn't respond it just shows in serial monitor "initializing node" that's it. wherever i put subkeys = cmac_generate_subkeys(key) it doesn't matter. I thought that initializing with lora and rh95 maybe was problem so made a different sketch with only rf95(rhmesh is subclass-library of rf95) but same problem persists, maybe the radiohead library won't allow the foreign function to work or what? please help..


Solution

  • I used arduino mega instead of uno and the code is working, the code was not facing dynamic memory(37%) or program storage space(51%), but changing the board worked, i don't know how?