ARACrypt.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // ARACrypt.cpp : implementation file
  2. //
  3. // Note: A Special Thanks to Mr. Warren Ward for his Sept. 1998 CUJ article:
  4. // "Stream Encryption" Copyright (c) 1998 by Warren Ward
  5. #include "stdafx.h"
  6. #include "ARACrypt.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CARACrypt
  14. CARACrypt::CARACrypt()
  15. :
  16. // Initialize the shift registers to non-zero
  17. // values. If the shift register contains all
  18. // 0's when the generator starts, it will not
  19. // produce a usable sequence of bits. The
  20. // numbers used here have no special features
  21. // except for being non-zero.
  22. m_LFSR_A( 0x13579BDF ),
  23. m_LFSR_B( 0x2468ACE0 ),
  24. m_LFSR_C( 0xFDB97531 ),
  25. // Initialize the masks to magic numbers.
  26. // These values are primitive polynomials mod
  27. // 2, described in Applied Cryptography,
  28. // second edition, by Bruce Schneier (New York:
  29. // John Wiley and Sons, 1994). See Chapter 15:
  30. // Random Sequence Generators and Stream
  31. // Ciphers, particularly the discussion on
  32. // Linear Feedback Shift Registers.
  33. //
  34. // The primitive polynomials used here are:
  35. // Register A: ( 32, 7, 6, 2, 0 )
  36. // Register B: ( 31, 6, 0 )
  37. // Register C: ( 29, 2, 0 )
  38. //
  39. // The bits that must be set to "1" in the
  40. // XOR masks are:
  41. // Register A: ( 31, 6, 5, 1 )
  42. // Register B: ( 30, 5 )
  43. // Register C: ( 28, 1 )
  44. //
  45. // Developer's Note
  46. // DO NOT CHANGE THESE NUMBERS WITHOUT
  47. // REFERRING TO THE DISCUSSION IN SCHNEIER'S
  48. // BOOK. They are some of very few
  49. // near-32-bit values that will act as
  50. // maximal-length random generators.
  51. m_Mask_A( 0x80000062 ),
  52. m_Mask_B( 0x40000020 ),
  53. m_Mask_C( 0x10000002 ),
  54. // Set up LFSR "rotate" masks.
  55. // These masks limit the number of bits
  56. // used in the shift registers. Each one
  57. // provides the most-significant bit (MSB)
  58. // when performing a "rotate" operation. Here
  59. // are the shift register sizes and the byte
  60. // mask needed to place a "1" bit in the MSB
  61. // for Rotate-1, and a zero in the MSB for
  62. // Rotate-0. All the shift registers are stored
  63. // in an unsigned 32-bit integer, but these
  64. // masks effectively make the registers 32
  65. // bits (A), 31 bits (B), and 29 bits (C).
  66. //
  67. // Bit | 3 2 1 0
  68. // Pos'n | 1098 7654 3210 9876 5432 1098 7654 3210
  69. // ===== | ==========================================
  70. // Value | 8421-8421 8421-8421 8421-8421 8421-8421
  71. // ===== | ==========================================
  72. // |
  73. // A-Rot0 | 0111 1111 1111 1111 1111 1111 1111 1111
  74. // A-Rot1 | 1000 0000 0000 0000 0000 0000 0000 0000
  75. // |
  76. // B-Rot0 | 0011 1111 1111 1111 1111 1111 1111 1111
  77. // B-Rot1 | 1100 0000 0000 0000 0000 0000 0000 0000
  78. // |
  79. // C-Rot0 | 0000 1111 1111 1111 1111 1111 1111 1111
  80. // C-Rot1 | 1111 0000 0000 0000 0000 0000 0000 0000
  81. //
  82. //
  83. // Reg Size MSB Position Rotate-0 Mask Rotate-1 Mask
  84. // A 32 31 0x7FFFFFFF 0x80000000
  85. // B 31 30 0x3FFFFFFF 0xC0000000
  86. // C 29 28 0x0FFFFFFF 0xF0000000
  87. //
  88. m_Rot0_A( 0x7FFFFFFF ),
  89. m_Rot0_B( 0x3FFFFFFF ),
  90. m_Rot0_C( 0x0FFFFFFF ),
  91. m_Rot1_A( 0x80000000 ),
  92. m_Rot1_B( 0xC0000000 ),
  93. m_Rot1_C( 0xF0000000 ),
  94. m_csKey(NULL)
  95. {
  96. }
  97. // Everything is on the frame.
  98. CARACrypt::~CARACrypt()
  99. {
  100. if (m_csKey != NULL)
  101. delete[] m_csKey;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CARACrypt operations
  105. void CARACrypt::SetKey(char* csKey)
  106. {
  107. char* csSeed;
  108. //m_csKey = csKey;
  109. size_t iLen = strlen(csKey);
  110. m_csKey = new char[iLen + 1];
  111. strncpy_s(m_csKey, iLen + 1, csKey, iLen);
  112. m_csKey[iLen] = '\n';
  113. if (strlen(m_csKey) == 0)
  114. {
  115. //Put some really outrageous characters in seed just in case.
  116. csSeed = "\x43\xC8\x21\xD3\xF4\xB3\x10\x27\x09\xAA\x18\x56";
  117. //TO DO: Add Message to Event Log and/or window when there is one.
  118. // AfxMessageBox("WARNING: Missing Pass Phrase. Default in effect!");
  119. }
  120. else
  121. {
  122. csSeed = m_csKey;
  123. }
  124. // Make sure seed is at least 12 bytes long .
  125. int nIdx = 0;
  126. //int length = strlen(m_csKey);
  127. for (nIdx = 0; strlen(m_csKey) < 12; nIdx++)
  128. {
  129. csSeed += csSeed[nIdx];
  130. }
  131. // LFSR A, B, and C get the first, second, and
  132. // third four bytes of the seed, respectively.
  133. for (nIdx = 0; nIdx < 4; nIdx++)
  134. {
  135. m_LFSR_A = ((m_LFSR_A <<= 8) |
  136. ((unsigned long int) csSeed[nIdx + 0]));
  137. m_LFSR_B = (( m_LFSR_B <<= 8) |
  138. ((unsigned long int) csSeed[nIdx + 4]));
  139. m_LFSR_C = (( m_LFSR_C <<= 8) |
  140. ((unsigned long int) csSeed[nIdx + 8]));
  141. }
  142. // If any LFSR contains 0x00000000, load a
  143. // non-zero default value instead. There is
  144. // no particular "good" value. The ones here
  145. // were selected to be distinctive during
  146. // testing and debugging.
  147. if (0x00000000 == m_LFSR_A)
  148. m_LFSR_A = 0x13579BDF;
  149. if (0x00000000 == m_LFSR_B)
  150. m_LFSR_B = 0x2468ACE0;
  151. if (0x00000000 == m_LFSR_C)
  152. m_LFSR_C = 0xFDB97531;
  153. return;
  154. }
  155. void CARACrypt::GetKey(char* csKey)
  156. {
  157. csKey = m_csKey;
  158. }
  159. //***********************************************
  160. // CARACrypt::TransformChar
  161. //***********************************************
  162. // Transform a single character. If it is
  163. // plaintext, it will be encrypted. If it is
  164. // encrypted, and if the LFSRs are in the same
  165. // state as when it was encrypted (that is, the
  166. // same keys loaded into them and the same number
  167. // of calls to TransformChar after the keys
  168. // were loaded), the character will be decrypted
  169. // to its original value.
  170. //
  171. // DEVELOPER'S NOTE
  172. // This code contains corrections to the LFSR
  173. // operations that supercede the code examples
  174. // in Applied Cryptography (first edition, up to
  175. // at least the 4th printing, and second edition,
  176. // up to at least the 6th printing). More recent
  177. // errata sheets may show the corrections.
  178. //***********************************************
  179. void CARACrypt::TransformChar(unsigned char& cTarget)
  180. {
  181. int Counter = 0;
  182. unsigned char Crypto = '\0';
  183. unsigned long int Out_B = (m_LFSR_B & 0x00000001);
  184. unsigned long int Out_C = (m_LFSR_C & 0x00000001);
  185. // Cycle the LFSRs eight times to get eight
  186. // pseudo-random bits. Assemble these into
  187. // a single random character (Crypto).
  188. for (Counter = 0; Counter < 8; Counter++)
  189. {
  190. if (m_LFSR_A & 0x00000001)
  191. {
  192. // The least-significant bit of LFSR
  193. // A is "1". XOR LFSR A with its
  194. // feedback mask.
  195. m_LFSR_A = (((m_LFSR_A ^ m_Mask_A) >> 1) | m_Rot1_A);
  196. // Clock shift register B once.
  197. if ( m_LFSR_B & 0x00000001 )
  198. {
  199. // The LSB of LFSR B is "1". XOR
  200. // LFSR B with its feedback mask.
  201. m_LFSR_B = (((m_LFSR_B ^ m_Mask_B) >> 1) | m_Rot1_B);
  202. Out_B = 0x00000001;
  203. }
  204. else
  205. {
  206. // The LSB of LFSR B is "0". Rotate
  207. // the LFSR contents once.
  208. m_LFSR_B = (( m_LFSR_B >> 1) & m_Rot0_B);
  209. Out_B = 0x00000000;
  210. }
  211. }
  212. else
  213. {
  214. // The LSB of LFSR A is "0".
  215. // Rotate the LFSR contents once.
  216. m_LFSR_A = (( m_LFSR_A >> 1) & m_Rot0_A);
  217. // Clock shift register C once.
  218. if ( m_LFSR_C & 0x00000001 )
  219. {
  220. // The LSB of LFSR C is "1".
  221. // XOR LFSR C with its feedback mask.
  222. m_LFSR_C = ((( m_LFSR_C ^ m_Mask_C) >> 1) | m_Rot1_C);
  223. Out_C = 0x00000001;
  224. }
  225. else
  226. {
  227. // The LSB of LFSR C is "0". Rotate
  228. // the LFSR contents once.
  229. m_LFSR_C = ((m_LFSR_C >> 1) & m_Rot0_C);
  230. Out_C = 0x00000000;
  231. }
  232. }
  233. // XOR the output from LFSRs B and C and
  234. // rotate it into the right bit of Crypto.
  235. //The follwing conversion warning is unecessary here as
  236. //'loss of data' is a side effect and harmless.
  237. #pragma warning(disable : 4244)
  238. Crypto = ((Crypto << 1) | (Out_B ^ Out_C));
  239. #pragma warning(default : 4244)
  240. }
  241. // XOR the resulting character with the
  242. // input character to encrypt/decrypt it.
  243. //The follwing conversion warning not necessary here either.
  244. //The 'loss of data', so to speak is a side effect and harmless.
  245. #pragma warning(disable : 4244)
  246. cTarget = ( cTarget ^ Crypto );
  247. if (cTarget == NULL) //No nulls allowed here. There is
  248. cTarget = ( cTarget ^ Crypto ); //no working std::string available.
  249. #pragma warning( default : 4244 )
  250. return;
  251. }
  252. //***********************************************
  253. // CARACrypt::TransformString
  254. //***********************************************
  255. // Encrypt or decrypt a standard string in place.
  256. // The string to transform is passed in as
  257. // Target.
  258. //***********************************************
  259. void CARACrypt::TransformString(char* csKey, char* csTarget)
  260. {
  261. // Reset the shift registers.
  262. SetKey(csKey);
  263. // Transform each character in the string.
  264. //
  265. // DEVELOPER'S NOTE
  266. // ==========================================
  267. // DO NOT TREAT THE OUTPUT STRING AS A NULL-
  268. // TERMINATED STRING.
  269. // ==========================================
  270. // The transformation process can create '\0'
  271. // characters in the output string. Always
  272. // use the length() method to retrieve the full
  273. // string when it has been transformed.
  274. // bek NOTE: The above note does not apply to this
  275. // implementation of Warren Ward's method.
  276. // ARACrypt knows about NULLs and transforms them
  277. // into XOR NULLs so the entire result can be
  278. // treated as a 'normal' NULL terminated string.
  279. int nLen = (int)strlen(csTarget);
  280. char* tempStr;
  281. tempStr = csTarget;
  282. for (int nPos = 0; nPos < nLen; nPos++)
  283. {
  284. //The follwing conversion warning not necessary here either.
  285. //The 'loss of data', so to speak is a side effect and harmless.
  286. #pragma warning(disable : 4244)
  287. unsigned char cBuff = *tempStr;
  288. TransformChar((unsigned char&) cBuff);
  289. *csTarget = cBuff;
  290. tempStr ++;
  291. csTarget ++;
  292. }
  293. return;
  294. }