Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. // Copyright (c) 2014-2018, The Monero Project
  2. //
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification, are
  6. // permitted provided that the following conditions are met:
  7. //
  8. // 1. Redistributions of source code must retain the above copyright notice, this list of
  9. // conditions and the following disclaimer.
  10. //
  11. // 2. Redistributions in binary form must reproduce the above copyright notice, this list
  12. // of conditions and the following disclaimer in the documentation and/or other
  13. // materials provided with the distribution.
  14. //
  15. // 3. Neither the name of the copyright holder nor the names of its contributors may be
  16. // used to endorse or promote products derived from this software without specific
  17. // prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  20. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  22. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
  30.  
  31. #pragma once
  32.  
  33. #include <stdint.h>
  34. #include <stddef.h>
  35.  
  36. #define CHACHA_KEY_SIZE 32
  37. #define CHACHA_IV_SIZE 8
  38.  
  39. #if defined(__cplusplus)
  40. #include <memory.h>
  41.  
  42. #include "memwipe.h"
  43. #include "hash.h"
  44.  
  45. namespace crypto {
  46. extern "C" {
  47. #endif
  48. void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher);
  49. void chacha20(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher);
  50. #if defined(__cplusplus)
  51. }
  52.  
  53. using chacha_key = tools::scrubbed_arr<uint8_t, CHACHA_KEY_SIZE>;
  54.  
  55. #pragma pack(push, 1)
  56. // MS VC 2012 doesn't interpret `class chacha_iv` as POD in spite of [9.0.10], so it is a struct
  57. struct chacha_iv {
  58. uint8_t data[CHACHA_IV_SIZE];
  59. };
  60. #pragma pack(pop)
  61.  
  62. static_assert(sizeof(chacha_key) == CHACHA_KEY_SIZE && sizeof(chacha_iv) == CHACHA_IV_SIZE, "Invalid structure size");
  63.  
  64. inline void chacha8(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) {
  65. chacha8(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher);
  66. }
  67.  
  68. inline void chacha20(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) {
  69. chacha20(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher);
  70. }
  71.  
  72. inline void generate_chacha_key(const void *data, size_t size, chacha_key& key) {
  73. static_assert(sizeof(chacha_key) <= sizeof(hash), "Size of hash must be at least that of chacha_key");
  74. tools::scrubbed_arr<char, HASH_SIZE> pwd_hash;
  75. crypto::cn_slow_hash(data, size, pwd_hash.data(), 0/*variant*/, 0/*prehashed*/, 0/*height*/);
  76. memcpy(&key, pwd_hash.data(), sizeof(key));
  77. }
  78.  
  79. inline void generate_chacha_key_prehashed(const void *data, size_t size, chacha_key& key) {
  80. static_assert(sizeof(chacha_key) <= sizeof(hash), "Size of hash must be at least that of chacha_key");
  81. tools::scrubbed_arr<char, HASH_SIZE> pwd_hash;
  82. crypto::cn_slow_hash(data, size, pwd_hash.data(), 0/*variant*/, 1/*prehashed*/, 0/*height*/);
  83. memcpy(&key, pwd_hash.data(), sizeof(key));
  84. }
  85.  
  86. inline void generate_chacha_key(std::string password, chacha_key& key) {
  87. return generate_chacha_key(password.data(), password.size(), key);
  88. }
  89. }
  90.  
  91. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement