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.

106 lines
3.9KB

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