home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 193_01 / crypt.doc < prev    next >
Text File  |  1985-11-14  |  17KB  |  334 lines

  1. 12/9/85
  2. INTRODUCTION
  3.  
  4. The "Infinite Key Encryption System" article in issue #94 (August 
  5. 1984)  of Dr.  Dobbs contains an excellent tutorial on Encryption 
  6. Systems.  At  the  time it was published,  I read  it  with  only 
  7. passing  interest.  About six months ago,  I developed a need for 
  8. this type of utility and so begins my story.  The  first thing  I 
  9. did was write a shell cypher program (cypher.c-Listing #1). Since 
  10. I  had already written a generic file copy utility which  allowed 
  11. modifications  during transfer,  it was a simple matter to modify 
  12. the argument passing to include multiple keys and add a  cypher() 
  13. function  call to encrypt the file with a simple exclusive-or'ing 
  14. algorithm (cypher1.c-Listing #2).  While this method did  encrypt 
  15. the  file  and  allowed for easy decryption (using the  same  run 
  16. string),   there   were  definite   detectable  patterns  in  the 
  17. resultant  file.  These patterns,  a function of the key  period, 
  18. were  easily found in areas of repetitive  characters.  (  eg.  a 
  19. string of asterisks or spaces, the hex 1A's at the end of a file, 
  20. etc  )  Another drawback to this scheme is the inability to  pass 
  21. nonprintable  characters in the runstring,  thereby limiting  the 
  22. number  of encryption tokens.  Sooooo it was back to the  drawing 
  23. board.
  24.  
  25. Rereading  the aforementioned article with  renewed  interest,  I 
  26. gained  an  insight  into the methods and  schemes  of  practical 
  27. modern  cyphers.  I don't intend to cover these concepts,  so  if 
  28. you're  interested  or lacking in the basics,  avail yourself  of 
  29. that article, and those in its  bibliography.
  30.  
  31. While the tutorial portion was excellent,  I disagreed with a few 
  32. points on implementation.  First,  there was the code itself  and 
  33. the intimation that assembly language was required for reasonable 
  34. speed.  The  MAC Assembler is only used with CP/M-80 and I wanted 
  35. more portable source,  so I decided to write it in C.  Second,  I 
  36. felt  that a random key  of some prime length could be  generated 
  37. solely from the original key (cypher2.c - Listing  #3).  Although 
  38. some  keys  may  work better than others,  a  means  to  evaluate 
  39. results  can  render this method very  functional.  And  last,  I 
  40. disagreed  with the need for passing information in the encrypted 
  41. file.  It  seemed  unnecessary and cumbersome.  My  goal  was  to 
  42. develop  a  method  that  worked entirely  from  the  run-string.  
  43. Overall  I  must commend the work done by John  Thomas  and  Joan 
  44. Thersites  for  presenting  such a complete  treatment  of  thier 
  45. topic.
  46.  
  47.  
  48. THE ULTIMATE CYPHER
  49.  
  50. The  ultimate cypher is like the ultimate weapon.  No matter  how 
  51. sophisticated,  an  anti-weapon  (anti-cypher) can eventually  be 
  52. developed, IF there is a need. The user must make some judgements 
  53. regarding  needs  and  level  of  protection.  The  2  algorithms 
  54. mentioned  are relatively simple to implement and use.  The  same 
  55. keys  will  encypher  or decypher the file and  key  order  isn't 
  56. important.  The experts (and it becomes quite obvious) point  out 
  57. that  the  strongest  cypher  schemes utilize  a  combination  of 
  58. transposition and substitution.  However,  when  transposition is 
  59. added,  the  order  of decyphering must be the exact  reverse  of 
  60. encyphering.  The  last cypher  module contains an algorithm  for 
  61. transposing  the file tokens along with the random generated  key 
  62. encryption scheme (cypher3.c-Listing #4).  This is a small  price 
  63. to pay for the added security.
  64.  
  65.  
  66. THE NEED FOR TOOLS
  67.  
  68. As  I  progressed in my quest of the   ultimate   cypher/decypher 
  69. algorithm,  I  became  aware of the deficiencies of the  standard 
  70. CP/M utilities at my disposal, so I developed my own.
  71.  
  72. The first tool,  fv.c (file view - Listing #5),  replaced my CP/M 
  73. dump.com.  Dump provides a continual display on screen of the hex 
  74. contents of a file.   Since most encryption is performed on  text 
  75. files, it is most beneficial to include the ascii form along with 
  76. the  hex.  And,  since most algorithms use an exclusive-or as the 
  77. means of encryption it was easy enough to dump two files and  the 
  78. exclusive-ored difference between them.
  79.  
  80. The next tool,  fstat.c (file statistics - Listing #6) calculates 
  81. and  display  the statistical characteristics of the  file.  This 
  82. tool scans the file, counting the occurances of each element, and 
  83. provides a 16 x 16 display of the distribution of characters.  It 
  84. calculates  mean,   median,  mode  and  range  of  the  character 
  85. distribution and displays it's histogram.  As one might  suspect, 
  86. each  file type  has a definite signature.  In fact after limited 
  87. use  of  this  utility the user will be  able  to  recognize  the 
  88. histogram patterns for text, Wordstar, and many other files.
  89.  
  90. While experimenting with various schemes, it becomes obvious that 
  91. the most difficult file to disguise is one that contains a single 
  92. byte for every entry or some sequential scheme.  So the next task 
  93. was  developing a utility,  makef.c (make file - Listing #7),  to 
  94. generate  a known sequential or uni-character file of  some  user 
  95. defined length.
  96.  
  97. Finally the last utility,  sp.c (search pattern - Listing #7),  I 
  98. needed  was  a  search scheme to look  for  repetitive   patterns 
  99. occuring  within  a file and provide some  information  regarding 
  100. location and depth of the repetition. Also included is the option 
  101. to   calculate the delta characteristics of a file to search  for 
  102. repetitive mathematical as well character sequences.
  103.  
  104.  
  105. CYPHER.C - LISTING #1
  106.  
  107. The cypher shell program is provided for use as is,  or for  user 
  108. modification.  It contains the argument passing and file handling 
  109. source  code  needed to copy from an existing file to a new  file 
  110. via  a  16 Kbyte buffer with a cypher function  being  called  to 
  111. encrypt  the  file (If the file is less than 16k the  input  file 
  112. name  may be the same as the output thus destroying the  original 
  113. contents)  A  16K buffer was chosen because it should fit  easily 
  114. with  most  compilers.   This  value  may  be  adjusted  to  meet 
  115. individual  compiler needs.  Any of the 3 algorithms that  follow 
  116. may be included or linked with this shell. Caution is recommended 
  117. to  ensure that the function name is appropriate for  the  method 
  118. you  use.  The  keys  are  passed in the CP/M  command  line  and 
  119. therefore  limited by its length as well as the argument  passing 
  120. capability of the C-compiler.
  121.  
  122.  
  123. CYPHER1.C - LISTING #2
  124.  
  125. This minimal cypher algorithm uses an exclusive-or'ing scheme  to 
  126. encrypt  the file with the keys passed.  If the user employs keys 
  127. of some prime length and performs multiple passes the results can 
  128. be  quite difficult to decypher.  It's quick and easy to use  and 
  129. provides  an excellent means to test the basics of  cryptography. 
  130. Since the keys are limited to only include printable  characters, 
  131. we  don't  take full advantage of the 256 codes available  for  a 
  132. byte.
  133.  
  134. CYPHER2.C - LISTING #3
  135.  
  136. Now  something a little more difficult for the code  breaker,  an 
  137. algorithm  which grew out of the previous listing and generates a 
  138. prime length key for each user key passed. One of 50 prime values 
  139. (betweem  1009  and 1999) is selected as a function  of  the  key 
  140. passed.  The  prime key is then generated using a simple summing-
  141. anding-exclusive oring algorithm and the file is encrypted  using 
  142. this  new  key.  If  two  or more  keys  are  used,  this  method 
  143. guarantees  a  cypher  period in excess of  1,000,000,  which  is 
  144. significantly larger than most text files.
  145.  
  146. The  key generation scheme is based entirely on the original  key 
  147. length  and its contents and I fail to see how this can be worked 
  148. backwards to regain the original, especially if multiple keys are 
  149. used.  Some  keys will generate shorter periods within the  prime 
  150. length,  but  this is easily tested with the  tools  provided.  I 
  151. welcome feedback or suggestions for improving this algorithm.
  152.  
  153.  
  154. CYPHER3.C - LISTING #4
  155.  
  156. Adding  chaos to disorder has probably driven many a code-cracker 
  157. to drink.  And this is just what we're trying to accomplish.  The 
  158. cypher2.c  algorithm  was  modified slightly to  test  the  first 
  159. character of each key passed.  If the key begins with a dash (-), 
  160. then  the  buffer is transposed by some value between 2  and  17. 
  161. Else,  it  encrypts  the file using the  algorithm  as  described 
  162. above.  This  simple but very effective method puts the icing  on 
  163. the  cake,  however it completely reverses the decryption method. 
  164. If the original runstring was
  165.  
  166.           cypher file.txt file.new ABRAHAM -2 LINCOLN -3
  167.  
  168. then the decryption runstring would be 
  169.  
  170.           cypher file.new file.doc -3 LINCOLN -2 ABRAHAM
  171.  
  172.  
  173. FV.C - LISTING #5
  174.  
  175. As I mentioned earlier,  this is my replacement for the CP/M dump 
  176. utility.  It  allows  the user to pass one or two  files  in  the 
  177. runstring  for  display.  If  one  file name  is  passed  in  the 
  178. runstring,  the output appears much like the CP/M dump.com output 
  179. with  the  addition of the ascii display.  If two file names  are 
  180. passed,  the output consists of a line from file one, a line from 
  181. file  two and a third line containing the exclusive oring of  the 
  182. two files (labeled "dif"). In all cases, non printable characters 
  183. are  replaced with a carrot ( ^ ) in the ascii portion and  nulls 
  184. are  replaced  with an  equal sign ( =  ),  to  readily  identify 
  185. comparisons between two files. The comparative output is purely a 
  186. byte  for  byte operation and no attempt is made to  realign  the 
  187. file  to comparing characters as in a compare utility.  The first 
  188. file length controls display length.  Figures 1 shows an  example 
  189. of  screen output from the runstring <fv cypher1.c>  and Figure 2 
  190. from the runstring <fv cypher1.c cypher2.c>
  191.  
  192.  
  193. FSTAT.C - LISTING #6
  194.  
  195. Descriptive  statistics is the name of the game here and as  with 
  196. any statistical evaluation, one must be brutally honest (at least 
  197. with oneself) to draw an objective conclusion. The entire file is 
  198. read,  16Kbytes at a time. As we read, the occurrences of each of 
  199. the 256 tokens is accumulated and we obtain the sum of all  bytes 
  200. as well as the min and max token occurrences.  The sum is divided 
  201. by  the total characters to obtain the mean,  the max becomes the 
  202. mode  and the range is the difference between min and  max.  Next 
  203. the  256  byte array is copied to a second array  and  sorted  to 
  204. obtain the median.
  205.  
  206. With   all  calculations  completed,   the  numerical  values  of 
  207. occurrences  are displayed in a 16 X 16 display  for  evaluation. 
  208. The  statistical  characteristics are displayed and  the  program 
  209. pauses  to  await some keyboard entry to display  the  histogram. 
  210. Depressing  the space bar prints a scaled horizontal histogram of 
  211. 16 groups (0-15, 16-31, . . ., 241-256).
  212.  
  213. The ideal random file (which is what we'd like to see) would have 
  214. the following characteristics:
  215.  
  216.      mean      127.5
  217.      mode      not critical
  218.      range     < 20% of the total bytes divided by 256
  219.      median    at midpoint of range
  220.  
  221.      histogram reasonably flat
  222.  
  223. Remember I said ideal !  This is where judgement and self honesty 
  224. come  into  play.  A  sequential file will  display  these  ideal 
  225. characteristics as well as a true random file.  Also,  files that 
  226. look  too  good statistically should be just as suspect as  those 
  227. that don't. Figure 3 is the output produced by this article as is 
  228. and Figure 4 when its encrypted with the runstring
  229.  
  230.     cypher crypt-tb.art new frederick -1 angelo -2 scacchitti -3
  231.  
  232. In   all  fairness, I  must  state  that  other  possibly  better 
  233. statistical  methods  are better for  determining  randomness.  I 
  234. opted  to use descriptive statistic because they are more  easily 
  235. understood and implemented.
  236.  
  237.  
  238. MAKEF.C - LISTING #7
  239.  
  240. A simple enough utility but an absolutely necessity if we are  to 
  241. evaluate  encryption schemes.  A filename of n 256 byte blocks is 
  242. created and if a value between 0 and 255 is passed the file  will 
  243. be  filled  with  this value.  If no value (or one that  is  non-
  244. numeric) is passed, each block contains a sequential count from 0 
  245. to 255.  An added benefit to this program is its ability to clean 
  246. a disk.  The user simply creates a file the size of the remaining 
  247. disk  space  and then erases it.  This results in all  free  disk 
  248. space being set as the user defined.
  249.  
  250.  
  251. SP.C - LISTING #8
  252.  
  253. Along  with the fstat utility this one confirms or  denys  (maybe 
  254. even  questions) the statistical data we received.  A brute force 
  255. search  method is used to find matching character strings in  the 
  256. file.  By default,  the search starts at the first character  and 
  257. searches the first 128 bytes for a match of 4 or more characters. 
  258. If  the  match depth exceeds the block size it is  searching  the 
  259. program  will return to CP/M after displaying the match data.  If 
  260. not it will continue in its search. Block size to search, minimum 
  261. depth  of  comparison  and  starting  point  in  the  buffer  may 
  262. optionally be changed in the runstring. 
  263.  
  264. Also  if  an  additional argument is passed ( one more  than  the 
  265. three mentioned) the software converts the data in the buffer  to 
  266. delta-form.  That is,  element[n] = element[n] - element[n+1] for 
  267. all data in the buffer.  After the conversion is made the  search 
  268. scheme  continues as before.  This feature allows us to  evaluate 
  269. the file for some mathematical sequence.
  270.  
  271. One drawback to this program as it stands is the limiting  factor 
  272. of the buffer size.  No attempt is made to search beyond it. This 
  273. shouldn't matter for most text files.
  274.  
  275.  
  276. SMALL C, CP/M, AND MISCELLANEOUS
  277.  
  278. chkkbd() is a function which enables us to stop display (program) 
  279. activity  if Control-S is depressed.  Following with a  Control-C 
  280. causes  a  return to CP/M and any other character will allow  the 
  281. program to continue. This function calls a bdos() function so the 
  282. user  may have to modify this for other  operating  systems.  The 
  283. getchx()  function  is a non-echoing version of  getchar()  which 
  284. uses BDOS function 6. getchar() may be substituted for getchx().
  285.  
  286. calloc(), malloc() and cfree() functions are used for the dynamic 
  287. allocation and deallocation of memory. My allocation/deallocation 
  288. scheme  is  of the simple variety where the programmer  must  pay 
  289. heed to order or pay the consequences.  The source code contained 
  290. here should work with most implementations of these functions.
  291.  
  292. Printer  output may be obtained from any of the programs by using 
  293. the  CP/M  Control  P function.  It was the  simplest  method  to 
  294. implement.
  295.  
  296. Math  functions  (especially  floating point) are  difficult  for 
  297. Small-C.  There  are several routines in the fstat.c source  that 
  298. perform the necessary long and fractional calculations.  It's not 
  299. necessary  to  change these however,  if your  compiler  supports 
  300. floats and longs, have at it.
  301.  
  302. Each program will display the usage if entered without the proper 
  303. number of arguments in the run string.  Also, since most software 
  304. users  begin  to feel uncomfortable when their  computer  is  off 
  305. somewhere  performing exotic calculations,  each program displays 
  306. status to the screen thus putting these fears at rest.  
  307.  
  308.  
  309. CYPHER BENCHMARKS
  310.  
  311. My version, written in Small C, is generic enough to adapt to any 
  312. C  compiler.  Running   on  a  4 Mhz Z80 based  CP/M  system,  it 
  313. benchmarks  at less than 1K/sec for file I/O,  16K/sec  for  file 
  314. transposition  and  approximately 4K/sec per key for  encryption. 
  315. Key  encryption is difficult to benchmark since it  includes  the 
  316. time  to generate the prime length key which varies from 1009  to 
  317. 1999 characters in length.
  318.  
  319.  
  320. AND FINALLY
  321.  
  322. These tools should be employed with a measure of common sense.  A 
  323. strong  cypher  is  only indicated when  both  statistically  and 
  324. pattern-whise  indicated.  (And it doesn't hurt to view the  file 
  325. either) My intent in developing these utilities was to enable the 
  326. cryptographer-programmer  a means to evaluate the strength of  an  
  327. encryption  scheme  as  well as the  resistance  of   schemes  to 
  328. "cracking".  However  (like  most  tools) these can be  used  for 
  329. destructive as well as constructive purposes.  The author assumes 
  330. no  liability  for  illegal  use of  these  tools  and  sincerely 
  331. believes  they  can only result in  stronger  encryption  schemes 
  332. being developed.
  333.  
  334.