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.

MyCryptoAVR.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * SHA256 and SHA256 implementation for AVR:
  22. *
  23. * This file is part of the AVR-Crypto-Lib.
  24. * Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
  25. *
  26. * This program is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation, either version 3 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  38. *
  39. * Author: Daniel Otte
  40. *
  41. * License: GPLv3 or later
  42. *
  43. * ======================================================================
  44. */
  45. #include "MyCryptoAVR.h"
  46. // SHA256 implementation in ASM, see MyASM.S
  47. void SHA256(uint8_t *dest, const uint8_t *data, size_t dataLength)
  48. {
  49. sha256((sha256_hash_t *)dest, data, dataLength << 3); // data length in bits
  50. }
  51. // SHA256HMAC
  52. /*
  53. * all lengths in bits!
  54. */
  55. void hmac_sha256(void *dest, const void *key, uint16_t keylength_b, const void *msg,
  56. uint32_t msglength_b)
  57. {
  58. sha256_ctx_t s;
  59. uint8_t buffer[HMAC_SHA256_BLOCK_BYTES];
  60. (void)memset((void *)buffer, 0x00, HMAC_SHA256_BLOCK_BYTES);
  61. /* if key is larger than a block we have to hash it*/
  62. if (keylength_b > SHA256_BLOCK_BITS) {
  63. sha256((sha256_hash_t *)buffer, key, keylength_b);
  64. } else {
  65. (void)memcpy((void *)buffer, (const void *)key, (keylength_b + 7) / 8);
  66. }
  67. for (uint8_t i = 0; i < SHA256_BLOCK_BYTES; ++i) {
  68. buffer[i] ^= IPAD;
  69. }
  70. sha256_init(&s);
  71. sha256_nextBlock(&s, buffer);
  72. while (msglength_b >= HMAC_SHA256_BLOCK_BITS) {
  73. sha256_nextBlock(&s, msg);
  74. msg = (uint8_t *)msg + HMAC_SHA256_BLOCK_BYTES;
  75. msglength_b -= HMAC_SHA256_BLOCK_BITS;
  76. }
  77. sha256_lastBlock(&s, msg, msglength_b);
  78. /* since buffer still contains key xor ipad we can do ... */
  79. for (uint8_t i = 0; i < HMAC_SHA256_BLOCK_BYTES; ++i) {
  80. buffer[i] ^= IPAD ^ OPAD;
  81. }
  82. sha256_ctx2hash((sha256_hash_t *)dest, &s); /* save inner hash temporary to dest */
  83. sha256_init(&s);
  84. sha256_nextBlock(&s, buffer);
  85. sha256_lastBlock(&s, dest, SHA256_HASH_BITS);
  86. sha256_ctx2hash((sha256_hash_t *)dest, &s);
  87. }
  88. void SHA256HMAC(uint8_t *dest, const uint8_t *key, size_t keyLength, const uint8_t *data,
  89. size_t dataLength)
  90. {
  91. hmac_sha256(dest, key, keyLength << 3, data, dataLength << 3);
  92. }