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.

mangler.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // MANGLER.CPP
  2. //
  3. // Copyright (C) 2004-2020 MicroNeil Research Corporation.
  4. //
  5. // This software is released under the MIT license. See LICENSE.TXT.
  6. //
  7. // Derived from Version 1 of Mangler Encryption Algorythm, 1984.
  8. // Derived from Version 2 of Mangler Encryption Algorythm, 1998.
  9. //
  10. // Mangler encryption engine object.
  11. // Using new optimized chaos driver for uniformity experiments.
  12. // Important in this experiment is proof of highest possible entropy.
  13. #include "mangler.hpp"
  14. namespace codedweller {
  15. unsigned char Mangler::ChaosDriver(void) { // Return the current
  16. return Fill[Fill[Position]^Fill[Position^0xff]]; // chaos engine output
  17. } // value.
  18. // As of version 3 the output of the chaos driver was strengthened for
  19. // cryptography and to increase the sensitivity of the output for use
  20. // as a random number generator. In version 2, the software would simply
  21. // return the fill value at the engine's current position. In the new
  22. // version two distinct fill values are involved in abstracting the
  23. // value of Position and determining the final output value and the Position
  24. // value itself is used to add complexity to the output.
  25. unsigned char Mangler::Rotate(unsigned char i) { // Bitwise rotates i
  26. return (
  27. (i & 0x80)? // This operation is
  28. (i<<1)+1: // described without
  29. (i<<1) // using asm.
  30. );
  31. }
  32. void Mangler::ChaosDriver(unsigned char i) { // Drives chaos engine.
  33. // First we move our mixing position in the fill buffer forward.
  34. Position=( // Move mixing position.
  35. Position+1+ // Move at least 1, then
  36. (Fill[Position]&0x0f) // maybe a few more.
  37. )%256; // But stay within the fill.
  38. // The fill position in version 2 was simply incremented. This allowed
  39. // for an attacker to predict something important about the state of
  40. // the chaos engine. The new method above uses abstraction through the
  41. // fill buffer to introduce "jitter" when setting a new position based
  42. // on data that is hidden from the outside.
  43. // Next we abstract the incoming character through the fill buffer and
  44. // use it to select fill data to rotate and swap.
  45. unsigned char Swap = ((Fill[Position]^Fill[i])+Position+i)%256;
  46. unsigned char Tmp;
  47. Tmp = Fill[Swap];
  48. Fill[Swap]=Fill[Position];
  49. Fill[Position]=Rotate(Tmp);
  50. // Whenever the Swap and Path positions are the same, the result is
  51. // that no data is swapped in the chaos field. We resolve that by
  52. // recalling the ChaosDriver. This has the added effect of increasing
  53. // the complexity and making it more difficult to predict the state
  54. // of the engine... particularly because the engine evloves to a new
  55. // state under these conditions without having exposed that change
  56. // to the outside world.
  57. if(Position==Swap) ChaosDriver(Tmp); // If we didn't swap, recurse.
  58. }
  59. // The encryption / decryption scheme works by modulating an input data
  60. // stream with a chaotic system and allowing the encrypted stream to drive
  61. // the chaotic system of both the transmitter and receiver. This will
  62. // synchronize the two chaotic systems and allow the receiving system to
  63. // "predict" the state of the transmiting system so that it can properly
  64. // demodulate the encrypted stream. Both chaotic systems must start in the
  65. // same state with the same fill data characteristics or else the two
  66. // chaotic systems evolve to further divergent states.
  67. unsigned char Mangler::Encrypt(unsigned char i) {
  68. unsigned char g = ChaosDriver() ^ i; // Take the output of the
  69. ChaosDriver(g); // chaos engine and use it
  70. return g; // to moduleate the input.
  71. } // Then drive the engine
  72. // with the encrypted data.
  73. unsigned char Mangler::Decrypt(unsigned char i) {
  74. unsigned char g = ChaosDriver() ^ i; // Take the output of the
  75. ChaosDriver(i); // chaos engine and use it
  76. return g; // to demodulate the input.
  77. } // then drive the engine
  78. // with the original input.
  79. Mangler::Mangler(void) : Position(0) { // The default constructor sets
  80. for(unsigned int c = 0;c<256;c++) // the key to the root primary
  81. Fill[c]=(unsigned char) c; // value and Position to 0.
  82. }
  83. } // End namespace codedweller