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.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef MyCryptoGeneric_h
  46. #define MyCryptoGeneric_h
  47. #include "hal/crypto/MyCryptoHAL.h"
  48. #define MY_CRYPTO_SHA256_ASM //!< Switch to define correct variable scope for ASM SHA256 implementation
  49. #define SHA256_HASH_BITS 256 //!< Defines the size of a SHA-256 hash value in bits
  50. #define SHA256_HASH_BYTES (SHA256_HASH_BITS/8) //!< Defines the size of a SHA-256 hash value in bytes
  51. #define SHA256_BLOCK_BITS 512 //!< Defines the size of a SHA-256 input block in bits
  52. #define SHA256_BLOCK_BYTES (SHA256_BLOCK_BITS/8) //!< Defines the size of a SHA-256 input block in bytes
  53. /**
  54. * @brief SHA-256 context type
  55. *
  56. * A variable of this type may hold the state of a SHA-256 hashing process
  57. */
  58. typedef struct {
  59. uint32_t h[8]; //!< h
  60. uint64_t length; //!< length
  61. } sha256_ctx_t;
  62. /**
  63. * @brief SHA-256 hash value type
  64. *
  65. * A variable of this type may hold the hash value produced by the
  66. * sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state) function.
  67. */
  68. typedef uint8_t sha256_hash_t[SHA256_HASH_BYTES];
  69. extern "C" { // ASM implementation, see MyASM.S
  70. /**
  71. * @brief initialise a SHA-256 context
  72. *
  73. * This function sets a ::sha256_ctx_t to the initial values for hashing.
  74. * @param state pointer to the SHA-256 hashing context
  75. */
  76. void sha256_init(sha256_ctx_t *state);
  77. /**
  78. * @brief update the context with a given block
  79. *
  80. * This function updates the SHA-256 hash context by processing the given block
  81. * of fixed length.
  82. * @param state pointer to the SHA-256 hash context
  83. * @param block pointer to the block of fixed length (512 bit = 64 byte)
  84. */
  85. void sha256_nextBlock(sha256_ctx_t *state, const void *block);
  86. /**
  87. * @brief finalize the context with the given block
  88. *
  89. * This function finalizes the SHA-256 hash context by processing the given block
  90. * of variable length.
  91. * @param state pointer to the SHA-256 hash context
  92. * @param block pointer to the block of fixed length (512 bit = 64 byte)
  93. * @param length_b the length of the block in bits
  94. */
  95. void sha256_lastBlock(sha256_ctx_t *state, const void *block, uint16_t length_b);
  96. /**
  97. * @brief convert the hash state into the hash value
  98. *
  99. * This function reads the context and writes the hash value to the destination
  100. * @param dest pointer to the location where the hash value should be written
  101. * @param state pointer to the SHA-256 hash context
  102. */
  103. void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state);
  104. /**
  105. * @brief simple SHA-256 hashing function for direct hashing
  106. *
  107. * This function automaticaly hashes a given message of arbitary length with
  108. * the SHA-256 hashing algorithm.
  109. * @param dest pointer to the location where the hash value is going to be written to
  110. * @param msg pointer to the message thats going to be hashed
  111. * @param length_b length of the message in bits
  112. */
  113. void sha256(sha256_hash_t *dest, const void *msg, uint32_t length_b);
  114. }
  115. // MHAC SHA256
  116. #define IPAD 0x36 //!< HMAC, inner hash, xor byte
  117. #define OPAD 0x5C //!< HMAC, outer hash, xor byte
  118. #define HMAC_SHA256_BITS SHA256_HASH_BITS //!< Defines the size of a SHA-256 HMAC hash value in bits
  119. #define HMAC_SHA256_BYTES SHA256_HASH_BYTES //!< Defines the size of a SHA-256 HMAC hash value in bytes
  120. #define HMAC_SHA256_BLOCK_BITS SHA256_BLOCK_BITS //!< Defines the size of a SHA-256 HMAC input block in bits
  121. #define HMAC_SHA256_BLOCK_BYTES SHA256_BLOCK_BYTES //!< Defines the size of a SHA-256 HMAC input block in bytes
  122. /**
  123. * @brief hash context structure
  124. */
  125. typedef struct {
  126. sha256_ctx_t a; //!< a
  127. sha256_ctx_t b; //!< b
  128. } hmac_sha256_ctx_t;
  129. /**
  130. * @brief SHA256 HMAC function
  131. *
  132. * @param dest pointer to the location where the hash value is going to be written to
  133. * @param key pointer to the key that's is needed for the HMAC calculation
  134. * @param keylength_b length of the key
  135. * @param msg pointer to the message thats going to be hashed
  136. * @param msglength_b length of the message
  137. */
  138. void hmac_sha256(void *dest, const void *key, uint16_t keylength_b, const void *msg,
  139. uint32_t msglength_b);
  140. #endif