Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MySigningAtsha204.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. *******************************
  20. *
  21. * DESCRIPTION
  22. * Signing support created by Patrick "Anticimex" Fallberg <patrick@fallberg.net>
  23. * ATSHA204 signing backend. The Atmel ATSHA204 offers true random number generation and
  24. * HMAC-SHA256 authentication with a readout-protected key.
  25. *
  26. */
  27. #include "MySigning.h"
  28. #ifdef MY_SIGNING_ATSHA204
  29. #define SIGNING_IDENTIFIER (1) //HMAC-SHA256
  30. #if defined(MY_DEBUG_VERBOSE_SIGNING)
  31. #define SIGN_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__)
  32. static char printStr[65];
  33. static char i2h(uint8_t i)
  34. {
  35. uint8_t k = i & 0x0F;
  36. if (k <= 9) {
  37. return '0' + k;
  38. } else {
  39. return 'A' + k - 10;
  40. }
  41. }
  42. static void buf2str(const uint8_t* buf, size_t sz)
  43. {
  44. uint8_t i;
  45. if (sz > 32) {
  46. sz = 32; //clamp to 32 bytes
  47. }
  48. for (i = 0; i < sz; i++) {
  49. printStr[i * 2] = i2h(buf[i] >> 4);
  50. printStr[(i * 2) + 1] = i2h(buf[i]);
  51. }
  52. printStr[sz * 2] = '\0';
  53. }
  54. #else
  55. #define SIGN_DEBUG(x,...)
  56. #endif
  57. static unsigned long _signing_timestamp;
  58. static bool _signing_verification_ongoing = false;
  59. static uint8_t _signing_verifying_nonce[32+9+1];
  60. static uint8_t _signing_signing_nonce[32+9+1];
  61. static uint8_t _signing_temp_message[SHA_MSG_SIZE];
  62. static uint8_t _signing_rx_buffer[SHA204_RSP_SIZE_MAX];
  63. static uint8_t _signing_tx_buffer[SHA204_CMD_SIZE_MAX];
  64. static uint8_t* const _signing_hmac = &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
  65. static uint8_t _signing_node_serial_info[9];
  66. #ifdef MY_SIGNING_NODE_WHITELISTING
  67. static const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
  68. #endif
  69. static bool init_ok = false;
  70. static void signerCalculateSignature(MyMessage &msg, bool signing);
  71. static uint8_t* signerAtsha204AHmac(const uint8_t* nonce, const uint8_t* data);
  72. static uint8_t* signerSha256(const uint8_t* data, size_t sz);
  73. bool signerAtsha204Init(void)
  74. {
  75. init_ok = true;
  76. atsha204_init(MY_SIGNING_ATSHA204_PIN);
  77. (void)atsha204_wakeup(_signing_temp_message);
  78. // Read the configuration lock flag to determine if device is personalized or not
  79. if (atsha204_read(_signing_tx_buffer, _signing_rx_buffer,
  80. SHA204_ZONE_CONFIG, 0x15<<2) != SHA204_SUCCESS) {
  81. SIGN_DEBUG(PSTR("!SGN:BND:INIT FAIL\n")); //Could not read ATSHA204A lock config
  82. init_ok = false;
  83. } else if (_signing_rx_buffer[SHA204_BUFFER_POS_DATA+3] != 0x00) {
  84. SIGN_DEBUG(PSTR("!SGN:BND:PER\n")); //ATSHA204A not personalized
  85. init_ok = false;
  86. }
  87. if (init_ok) {
  88. // Get and cache the serial of the ATSHA204A
  89. if (atsha204_getSerialNumber(_signing_node_serial_info) != SHA204_SUCCESS) {
  90. SIGN_DEBUG(PSTR("!SGN:BND:SER\n")); //Could not get ATSHA204A serial
  91. init_ok = false;
  92. }
  93. }
  94. return init_ok;
  95. }
  96. bool signerAtsha204CheckTimer(void)
  97. {
  98. if (!init_ok) {
  99. return false;
  100. }
  101. if (_signing_verification_ongoing) {
  102. unsigned long time_now = hwMillis();
  103. // If timestamp is taken so late a rollover can take place during the timeout,
  104. // offset both timestamp and current time to make sure no rollover takes place during the
  105. // timeout
  106. if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < _signing_timestamp) {
  107. _signing_timestamp += MY_VERIFICATION_TIMEOUT_MS;
  108. time_now += MY_VERIFICATION_TIMEOUT_MS;
  109. }
  110. if (time_now > _signing_timestamp + MY_VERIFICATION_TIMEOUT_MS) {
  111. SIGN_DEBUG(PSTR("!SGN:BND:TMR\n")); //Verification timeout
  112. // Purge nonce
  113. memset(_signing_signing_nonce, 0xAA, 32);
  114. memset(_signing_verifying_nonce, 0xAA, 32);
  115. _signing_verification_ongoing = false;
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. bool signerAtsha204GetNonce(MyMessage &msg)
  122. {
  123. if (!init_ok) {
  124. return false;
  125. }
  126. // We used a basic whitening technique that XORs each byte in a 32byte random value with current
  127. // hwMillis() counter. This 32-byte random value is then hashed (SHA256) to produce the resulting
  128. // nonce
  129. (void)atsha204_wakeup(_signing_temp_message);
  130. if (atsha204_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL,
  131. RANDOM_COUNT, _signing_tx_buffer, RANDOM_RSP_SIZE, _signing_rx_buffer) !=
  132. SHA204_SUCCESS) {
  133. return false;
  134. }
  135. for (int i = 0; i < 32; i++) {
  136. _signing_verifying_nonce[i] = _signing_rx_buffer[SHA204_BUFFER_POS_DATA+i] ^ (hwMillis()&0xFF);
  137. }
  138. memcpy(_signing_verifying_nonce, signerSha256(_signing_verifying_nonce, 32),
  139. min(MAX_PAYLOAD, 32));
  140. // We just idle the chip now since we expect to use it soon when the signed message arrives
  141. atsha204_idle();
  142. if (MAX_PAYLOAD < 32) {
  143. // We set the part of the 32-byte nonce that does not fit into a message to 0xAA
  144. memset(&_signing_verifying_nonce[MAX_PAYLOAD], 0xAA, 32-MAX_PAYLOAD);
  145. }
  146. // Transfer the first part of the nonce to the message
  147. msg.set(_signing_verifying_nonce, min(MAX_PAYLOAD, 32));
  148. _signing_verification_ongoing = true;
  149. _signing_timestamp = hwMillis(); // Set timestamp to determine when to purge nonce
  150. return true;
  151. }
  152. void signerAtsha204PutNonce(MyMessage &msg)
  153. {
  154. if (!init_ok) {
  155. return;
  156. }
  157. memcpy(_signing_signing_nonce, (uint8_t*)msg.getCustom(), min(MAX_PAYLOAD, 32));
  158. if (MAX_PAYLOAD < 32) {
  159. // We set the part of the 32-byte nonce that does not fit into a message to 0xAA
  160. memset(&_signing_signing_nonce[MAX_PAYLOAD], 0xAA, 32-MAX_PAYLOAD);
  161. }
  162. }
  163. bool signerAtsha204SignMsg(MyMessage &msg)
  164. {
  165. // If we cannot fit any signature in the message, refuse to sign it
  166. if (mGetLength(msg) > MAX_PAYLOAD-2) {
  167. SIGN_DEBUG(PSTR("!SGN:BND:SIG,SIZE,%" PRIu8 ">%" PRIu8 "\n"), mGetLength(msg),
  168. MAX_PAYLOAD-2); //Message too large
  169. return false;
  170. }
  171. // Calculate signature of message
  172. mSetSigned(msg, 1); // make sure signing flag is set before signature is calculated
  173. signerCalculateSignature(msg, true);
  174. if (DO_WHITELIST(msg.destination)) {
  175. // Salt the signature with the senders nodeId and the unique serial of the ATSHA device
  176. // We can reuse the nonce buffer now since it is no longer needed
  177. memcpy(_signing_signing_nonce, _signing_hmac, 32);
  178. _signing_signing_nonce[32] = msg.sender;
  179. memcpy(&_signing_signing_nonce[33], _signing_node_serial_info, 9);
  180. // We can 'void' sha256 because the hash is already put in the correct place
  181. (void)signerSha256(_signing_signing_nonce, 32+1+9);
  182. SIGN_DEBUG(PSTR("SGN:BND:SIG WHI,ID=%" PRIu8 "\n"), msg.sender);
  183. #ifdef MY_DEBUG_VERBOSE_SIGNING
  184. buf2str(_signing_node_serial_info, 9);
  185. SIGN_DEBUG(PSTR("SGN:BND:SIG WHI,SERIAL=%s\n"), printStr);
  186. #endif
  187. }
  188. // Put device back to sleep
  189. atsha204_sleep();
  190. // Overwrite the first byte in the signature with the signing identifier
  191. _signing_hmac[0] = SIGNING_IDENTIFIER;
  192. // Transfer as much signature data as the remaining space in the message permits
  193. memcpy(&msg.data[mGetLength(msg)], _signing_hmac, min(MAX_PAYLOAD-mGetLength(msg), 32));
  194. return true;
  195. }
  196. bool signerAtsha204VerifyMsg(MyMessage &msg)
  197. {
  198. if (!_signing_verification_ongoing) {
  199. SIGN_DEBUG(PSTR("!SGN:BND:VER ONGOING\n"));
  200. return false;
  201. } else {
  202. // Make sure we have not expired
  203. if (!signerCheckTimer()) {
  204. return false;
  205. }
  206. _signing_verification_ongoing = false;
  207. if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
  208. SIGN_DEBUG(PSTR("!SGN:BND:VER,IDENT=%" PRIu8 "\n"), msg.data[mGetLength(msg)]);
  209. return false;
  210. }
  211. signerCalculateSignature(msg, false); // Get signature of message
  212. #ifdef MY_SIGNING_NODE_WHITELISTING
  213. // Look up the senders nodeId in our whitelist and salt the signature with that data
  214. size_t j;
  215. for (j=0; j < NUM_OF(_signing_whitelist); j++) {
  216. if (_signing_whitelist[j].nodeId == msg.sender) {
  217. // We can reuse the nonce buffer now since it is no longer needed
  218. memcpy(_signing_verifying_nonce, _signing_hmac, 32);
  219. _signing_verifying_nonce[32] = msg.sender;
  220. memcpy(&_signing_verifying_nonce[33], _signing_whitelist[j].serial, 9);
  221. // We can 'void' sha256 because the hash is already put in the correct place
  222. (void)signerSha256(_signing_verifying_nonce, 32+1+9);
  223. SIGN_DEBUG(PSTR("SGN:BND:VER WHI,ID=%" PRIu8 "\n"), msg.sender);
  224. #ifdef MY_DEBUG_VERBOSE_SIGNING
  225. buf2str(_signing_whitelist[j].serial, 9);
  226. SIGN_DEBUG(PSTR("SGN:BND:VER WHI,SERIAL=%s\n"), printStr);
  227. #endif
  228. break;
  229. }
  230. }
  231. if (j == NUM_OF(_signing_whitelist)) {
  232. SIGN_DEBUG(PSTR("!SGN:BND:VER WHI,ID=%" PRIu8 " MISSING\n"), msg.sender);
  233. // Put device back to sleep
  234. atsha204_sleep();
  235. return false;
  236. }
  237. #endif
  238. // Put device back to sleep
  239. atsha204_sleep();
  240. // Overwrite the first byte in the signature with the signing identifier
  241. _signing_hmac[0] = SIGNING_IDENTIFIER;
  242. // Compare the calculated signature with the provided signature
  243. if (signerMemcmp(&msg.data[mGetLength(msg)], _signing_hmac,
  244. min(MAX_PAYLOAD-mGetLength(msg), 32))) {
  245. return false;
  246. } else {
  247. return true;
  248. }
  249. }
  250. }
  251. // Helper to calculate signature of msg (returned in _signing_rx_buffer[SHA204_BUFFER_POS_DATA])
  252. // (=_signing_hmac)
  253. static void signerCalculateSignature(MyMessage &msg, bool signing)
  254. {
  255. // Signature is calculated on everything expect the first byte in the header
  256. uint16_t bytes_left = mGetLength(msg)+HEADER_SIZE-1;
  257. int16_t current_pos = 1-(int16_t)HEADER_SIZE; // Start at the second byte in the header
  258. uint8_t* nonce = signing ? _signing_signing_nonce : _signing_verifying_nonce;
  259. #ifdef MY_DEBUG_VERBOSE_SIGNING
  260. buf2str(nonce, 32);
  261. SIGN_DEBUG(PSTR("SGN:BND:NONCE=%s\n"), printStr);
  262. #endif
  263. while (bytes_left) {
  264. uint16_t bytes_to_include = min(bytes_left, 32);
  265. (void)atsha204_wakeup(_signing_temp_message); // Issue wakeup to reset watchdog
  266. memset(_signing_temp_message, 0, 32);
  267. memcpy(_signing_temp_message, (uint8_t*)&msg.data[current_pos], bytes_to_include);
  268. // We can 'void' signerAtsha204AHmac because the HMAC is already put in the correct place
  269. (void)signerAtsha204AHmac(nonce, _signing_temp_message);
  270. // Purge nonce when used
  271. memset(nonce, 0xAA, 32);
  272. bytes_left -= bytes_to_include;
  273. current_pos += bytes_to_include;
  274. if (bytes_left > 0) {
  275. // We will do another pass, use current HMAC as nonce for the next HMAC
  276. memcpy(nonce, _signing_hmac, 32);
  277. atsha204_idle(); // Idle the chip to allow the wakeup call to reset the watchdog
  278. }
  279. }
  280. #ifdef MY_DEBUG_VERBOSE_SIGNING
  281. buf2str(_signing_hmac, 32);
  282. SIGN_DEBUG(PSTR("SGN:BND:HMAC=%s\n"), printStr);
  283. #endif
  284. }
  285. // Helper to calculate a ATSHA204A specific HMAC-SHA256 using provided 32 byte nonce and data
  286. // (zero padded to 32 bytes)
  287. // The pointer to the HMAC is returned, but the HMAC is also stored in
  288. // _signing_rx_buffer[SHA204_BUFFER_POS_DATA] (=_signing_hmac)
  289. static uint8_t* signerAtsha204AHmac(const uint8_t* nonce, const uint8_t* data)
  290. {
  291. // Program the data to sign into the ATSHA204
  292. (void)atsha204_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32,
  293. (uint8_t*)data,
  294. WRITE_COUNT_LONG, _signing_tx_buffer, WRITE_RSP_SIZE, _signing_rx_buffer);
  295. // Program the nonce to use for the signature (has to be done just before GENDIG
  296. // due to chip limitations)
  297. (void)atsha204_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, 32, (uint8_t*)nonce,
  298. NONCE_COUNT_LONG, _signing_tx_buffer, NONCE_RSP_SIZE_SHORT,
  299. _signing_rx_buffer);
  300. // Generate digest of data and nonce
  301. (void)atsha204_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
  302. GENDIG_COUNT_DATA, _signing_tx_buffer, GENDIG_RSP_SIZE,
  303. _signing_rx_buffer);
  304. // Calculate HMAC of message+nonce digest and secret key
  305. (void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
  306. HMAC_COUNT, _signing_tx_buffer, HMAC_RSP_SIZE, _signing_rx_buffer);
  307. return &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
  308. }
  309. // Helper to calculate a generic SHA256 digest of provided buffer (only supports one block)
  310. // The pointer to the hash is returned, but the hash is also stored in
  311. // _signing_rx_buffer[SHA204_BUFFER_POS_DATA])
  312. static uint8_t* signerSha256(const uint8_t* data, size_t sz)
  313. {
  314. // Initiate SHA256 calculator
  315. (void)atsha204_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
  316. SHA_COUNT_SHORT, _signing_tx_buffer, SHA_RSP_SIZE_SHORT,
  317. _signing_rx_buffer);
  318. // Calculate a hash
  319. memset(_signing_temp_message, 0x00, SHA_MSG_SIZE);
  320. memcpy(_signing_temp_message, data, sz);
  321. _signing_temp_message[sz] = 0x80;
  322. // Write length data to the last bytes
  323. _signing_temp_message[SHA_MSG_SIZE-2] = (sz >> 5);
  324. _signing_temp_message[SHA_MSG_SIZE-1] = (sz << 3);
  325. (void)atsha204_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
  326. SHA_COUNT_LONG, _signing_tx_buffer, SHA_RSP_SIZE_LONG, _signing_rx_buffer);
  327. return &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
  328. }
  329. #endif //MY_SIGNING_ATSHA204