pfodParser  3.61.0
The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections
pfodMAC.h
Go to the documentation of this file.
1 #ifndef pfodMAC_h
2 #define pfodMAC_h
9 /*
10  (c)2014-2017 Forward Computing and Control Pty. Ltd.
11  NSW Australia, www.forward.com.au
12  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
13  This code may be freely used for both private and commercial use
14  Provide this copyright is maintained.
15 */
16 
17 #include <Arduino.h>
18 #include "pfodStream.h"
19 #include "pfodEEPROM.h"
20 
21 #define MAX_ELAPSED_TIME 0x7fffffff
22 
23 class pfodMAC {
24  public:
25 
27  boolean init(int eepromAddress); // if eepromAddress < 0 disable EEPROM use
28 
35  static void resetPowerCycles(int eepromAddress = 0);
36 
37  void setSecretKey(byte *key, int len);
38 
39  void setDebugStream(Print* out);
40  boolean isValid(); // powercycles > 0
41  boolean isBigEndian(); // is 1 stored as 0x00000001 false for Atmel 8 bit micros
42  void noEEPROM(); // disable eeprom
43 
44  unsigned int readSecretKey(byte* bRtn, int keyAdd); // returns 0 length if EEPROM disabled
45 
46  // utility methods
47  uint8_t eeprom_read(int address); // always returns 0 if EEPROM disabled
48  static void eeprom_write(int address, uint8_t value); // does nothing if EEPROM disabled
49 
50  // useful for debugging
51  void printBytesInHex(Print *out, const byte* b, size_t len);
52 
68  boolean buildChallenge(byte* challengeBytes, uint32_t mS_sinceLastConnection);
69 
70  void initHash(); // start a new hash by initializing with the secret key
71  void putByteToHash(byte b); // add a byte to the hash
72  void putBytesToHash(byte* b, int len); // add a byte array to the hash
73  void putLongToHash(uint32_t l); // add a long to the hash (in BigEndian format)
74  void finishHash(); // finish the hash and calculate the result
75  byte* getHashedResult(); // get pointer to the 8 byte result
76 
77 
85  boolean checkMsgHash(const byte* msgHash, uint16_t len);
86 
87  void reverseBytes(byte *b, int len); // swap from order of bytes upto len, converts between Endians, Big to Little, Little to Big
88 
89  const static uint8_t msgHashByteSize = 4; // number of byte in per msg hash
90  const static uint8_t challengeHashByteSize = 8; // number of bytes in challenge hash
91  const static uint8_t challengeByteSize = 8; // number of bytes in challenge
92  const static uint8_t PowerCyclesOffset; // == 0 offset to power cycles from eepromAddress
93  const static uint8_t KeyOffset; // offset to keyLen from eepromAddress, length (1 byte) comes first then key
94  const static uint8_t maxKeySize = 16; // max size of key 128 bits
95 
96  private:
97  boolean initCalled; // set true after first init() call. Stops multiple calls to get readPowerCycles
98  boolean cmpToResult(const byte* response, uint16_t len);
99  int CHAP_EEPROM_Address; // set to -1 if eeprom disabled
100  Print *debugOut;
101  boolean disableEEPROM; // true if NO EEPROM or eepromAddress arg is < 0
102  const static uint8_t powerCycleSize; // sizeof(uint16_t)
103  const static uint8_t timerSize; // sizeof(uint32_t)
104  const static uint8_t counterSize; // int
105  uint16_t connectionCounter; // increment each connection
106  uint16_t powerCycles;
107  byte key[maxKeySize]; // the secret key after reading from EEPROM and expanding to 16 bytes
108  uint8_t keyLen; // 0 if no key, else 16 (after padding key with 0 if shorter)
109  boolean bigEndian; // true if bigEndian uC
110  uint8_t *hashChallenge(byte *challenge); // hashes challenge returns pointer to 8 byte result
111 
120  uint16_t readPowerCycles(int powerCycleAdd);
121 
122 };
123 
124 
125 #endif // pfodMAC_h
126 
boolean buildChallenge(byte *challengeBytes, uint32_t mS_sinceLastConnection)
buildChallenge – NOTE: each call to this method increments the connection counter!...
static const uint8_t challengeHashByteSize
Definition: pfodMAC.h:90
boolean isBigEndian()
void initHash()
uint8_t eeprom_read(int address)
static const uint8_t PowerCyclesOffset
Definition: pfodMAC.h:92
void noEEPROM()
boolean init(int eepromAddress)
static void eeprom_write(int address, uint8_t value)
unsigned int readSecretKey(byte *bRtn, int keyAdd)
boolean checkMsgHash(const byte *msgHash, uint16_t len)
Converts msgHash from hex Digits to bytes and then compares the resulting byte[] with the current has...
byte * getHashedResult()
static const uint8_t maxKeySize
Definition: pfodMAC.h:94
void finishHash()
void setDebugStream(Print *out)
static const uint8_t challengeByteSize
Definition: pfodMAC.h:91
static const uint8_t msgHashByteSize
Definition: pfodMAC.h:89
boolean isValid()
static void resetPowerCycles(int eepromAddress=0)
Use this call to reset the power cycles to 0xffff Then remove the call from the code for normal opera...
void printBytesInHex(Print *out, const byte *b, size_t len)
void putLongToHash(uint32_t l)
static const uint8_t KeyOffset
Definition: pfodMAC.h:93
void setSecretKey(byte *key, int len)
void reverseBytes(byte *b, int len)
void putBytesToHash(byte *b, int len)
void putByteToHash(byte b)