The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

113 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. BlowFish encryption class.
  23. @tags{Cryptography}
  24. */
  25. class JUCE_API BlowFish
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates an object that can encode/decode based on the specified key.
  30. The key data can be up to 72 bytes long.
  31. */
  32. BlowFish (const void* keyData, int keyBytes);
  33. /** Creates a copy of another blowfish object. */
  34. BlowFish (const BlowFish&);
  35. /** Copies another blowfish object. */
  36. BlowFish& operator= (const BlowFish&) noexcept;
  37. /** Destructor. */
  38. ~BlowFish() noexcept;
  39. //==============================================================================
  40. /** Encrypts a pair of 32-bit integers. */
  41. void encrypt (uint32& data1, uint32& data2) const noexcept;
  42. /** Decrypts a pair of 32-bit integers. */
  43. void decrypt (uint32& data1, uint32& data2) const noexcept;
  44. //==============================================================================
  45. /** Encrypts a memory block */
  46. void encrypt (MemoryBlock& data) const;
  47. /** Decrypts a memory block */
  48. void decrypt (MemoryBlock& data) const;
  49. //==============================================================================
  50. /** Encrypts data in-place
  51. @param buffer The message that should be encrypted. See bufferSize on size
  52. requirements!
  53. @param sizeOfMsg The size of the message that should be encrypted in bytes
  54. @param bufferSize The size of the buffer in bytes. To accommodate the encrypted
  55. data, the buffer must be larger than the message: the size of
  56. the buffer needs to be equal or greater than the size of the
  57. message in bytes rounded to the next integer which is divisible
  58. by eight. If the message size in bytes is already divisible by eight
  59. then you need to add eight bytes to the buffer size. If in doubt
  60. simply use bufferSize = sizeOfMsg + 8.
  61. @returns The size of the decrypted data in bytes or -1 if the decryption failed.
  62. */
  63. int encrypt (void* buffer, size_t sizeOfMsg, size_t bufferSize) const noexcept;
  64. /** Decrypts data in-place
  65. @param buffer The encrypted data that should be decrypted
  66. @param bytes The size of the encrypted data in bytes
  67. @returns The size of the decrypted data in bytes or -1 if the decryption failed.
  68. */
  69. int decrypt (void* buffer, size_t bytes) const noexcept;
  70. private:
  71. //==============================================================================
  72. static int pad (void*, size_t, size_t) noexcept;
  73. static int unpad (const void*, size_t) noexcept;
  74. bool apply (void*, size_t, void (BlowFish::*op) (uint32&, uint32&) const) const;
  75. //==============================================================================
  76. uint32 p[18];
  77. HeapBlock<uint32> s[4];
  78. uint32 F (uint32) const noexcept;
  79. JUCE_LEAK_DETECTOR (BlowFish)
  80. };
  81. } // namespace juce