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.8KB

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