home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / os2 / bfish / BLOWFISH.DOC < prev    next >
Encoding:
Text File  |  1997-07-22  |  15.4 KB  |  347 lines

  1.                          ╔═════════════════════════════╗ 
  2.                          ║ BLOWFISH for OS/2 and Win32 ║
  3.                          ║        Version 1.61         ║
  4.                          ║       July 22nd, 1997       ║
  5.                          ╚═════════════════════════════╝
  6.  
  7. This programme implements the Blowfish encryption algorithm, developed by 
  8. Bruce Schneier, as originally documented in Dr. Dobb's Journal, April 1994, 
  9. and revised in the September 1995 edition.  I have taken this algorithm and 
  10. added an interface for fast, intelligent file encryption under OS/2 and
  11. 32-bit versions of Windows.
  12.  
  13. In keeping with the public-domain nature of the algorithm, this programme is 
  14. freeware.  I developed it on my own time, purely out of technical interest,
  15. and I hope others will find it as useful as I do.  Please use and distribute 
  16. it as widely as possible.  If you find it valuable, let me know. If you have 
  17. any problems, questions, comments or suggestions, please e-mail me:
  18.  
  19.     Matthew Spencer -  msspenc@ibm.net
  20.  
  21. ════════════════════════════════════════════════════════════════════════════════
  22.  
  23. How to Use It.
  24. ──────────────
  25.  
  26. [Note for Windows users: replace "BLOWFISH" with "BFW" in all the examples.]
  27.  
  28. The programme has 2 modes - command-line or interactive.  In command-line mode,
  29. the you invoke it as follows:
  30.  
  31.    BLOWFISH [options] SOURCEFILE [TARGETFILE] "This is the key"
  32.  
  33. To use interactive mode, just type BLOWFISH, and you will be prompted for
  34. the names of the file(s), and the key.  You will also be asked to enter the
  35. key twice, for confirmation purposes.
  36.  
  37. Note that:
  38.  
  39.  - Keys are CASE SENSITIVE, and can be from 1 to 56 bytes in length.
  40.    Longer keys are truncated to 56 bytes.  The key can contain any of the 
  41.    256 ASCII characters, including NULLs and graphics characters.
  42.  
  43.  - Multi-word keys MUST be surrounded by double quotes (if passed on the 
  44.    command line).  This is not required in interactive mode.
  45.  
  46.  - The key is visible when you use command-line mode.  If you don't want 
  47.    someone looking over your shoulder to see the key, then use interactive
  48.    mode (asterisks are displayed, instead of the key).
  49.  
  50.  - The target file name is optional.  You can supply it, and a second, 
  51.    encrypted  file will be created, and the original will be left untouched.
  52.    If you don't supply the target file name, the original file is REPLACED
  53.    by the encrypted file.
  54.  
  55.  - Type "BLOWFISH -?" for more extensive help.
  56.  
  57. To install the programme, put BLOWFISH.EXE in a directory in your PATH, and
  58. put ENCRYPT.DLL in a directory in your LIBPATH.
  59.  
  60.  
  61. Advanced Usage.
  62. ───────────────
  63.  
  64. New with version 1.60 are a bunch of extra features:
  65.  
  66.  - Wildcards are now supported for source and target filenames.  I have tried
  67.    to make the behaviour match what you would expect from OS/2 (or DOS, or
  68.    even UNIX) commands.  You use the usual '?' to match a single character,
  69.    or '*' to match multiple characters.  [Note to Windows users: version 1.61
  70.    does not support wildcards in target filenames, since the windows API does
  71.    not have an equivalent to OS/2's DosEditName().  You can still get the same 
  72.    effect by copying the files first, then encrypting the copies.  I intend to
  73.    fix this in a future version.]
  74.  
  75.    When wildcards are used in the target filename, then the rules used in say, 
  76.    the OS/2 'copy' command are applied. For example:
  77.  
  78.      blowfish c:\config.sys *.enc "the key"
  79.  
  80.    will produce a new, encrypted file c:\config.enc, using the key "the key",
  81.    and leaving the original file c:\config.sys untouched.  
  82.  
  83.    Actually, this also illustrates a small difference in behaviour compared
  84.    to the copy command.  The command:
  85.  
  86.      copy c:\config.sys *.enc
  87.  
  88.    will put the file config.enc in the CURRENT DIRECTORY, whereas blowfish will
  89.    put it in the SAME DIRECTORY as the source file, UNLESS you specify a 
  90.    directory for the target.  The reason for his is that I figured that this
  91.    is the most useful behaviour, especially when you start doing subdirectory
  92.    searches (see below).  If you really want the target to end up in the 
  93.    current directory, then just do something like:
  94.  
  95.      blowfish c:\config.sys .\*.enc "the key"
  96.  
  97.    Hopefully, blowfish is also a little smarter than the OS/2 copy when 
  98.    processing multiple files which expand to the same target name.  For
  99.    example, if you happend to also have a file named c:\config.bak, then
  100.    the command
  101.  
  102.      copy c:\config.* *.enc
  103.  
  104.    will first copy config.bak to config.enc, and then copy config.sys to
  105.    config.enc, happily overwriting the result of the first copy.  
  106.  
  107.    Blowfish on other the hand, detects this and does not process the 2nd
  108.    file, giving an error message.
  109.  
  110.    Note that you can also use a directory as a source (or target) filename.
  111.    So, if you decide to invoke:
  112.  
  113.      blowfish c:\os2 "the key"
  114.  
  115.    you will encrypt every file in the c:\os2 directory.  Probably not a good
  116.    idea, but you can do it if you want.
  117.  
  118. There are now also a number of optional switches which increase the 
  119. functionality.  Note that switches can be placed anywhere on the command
  120. line, and that '-s' and '/s' can be used interchangeably.
  121.  
  122.  -s : Search subdirectories for matching file names.  The search includes
  123.       the starting directory, if specified, or the current directory if not.
  124.  
  125.  -o : Overwrite the original file, so it cannot be un-deleted.  This overwrite
  126.       is done to US Department of Defense standards, which entails overwriting 
  127.       the file 3 times.  For those interested, the process requires first
  128.       writing every byte with a given pattern (I use 00110101), then writing 
  129.       with the complementary pattern (11001010), then re-writing with a 
  130.       different pattern again (10010111).  The intent is to cycle the magnetic
  131.       media enough times to hide any trace of the original data. (see Bruce
  132.       Schneier's Applied Cryptography, 2nd Ed, p. 229, for more details).
  133.  
  134.  -q : Quiet mode.  Suppress all informational messages.  Error messages are
  135.       still displayed.
  136.  
  137.  -v : Verbose mode.  Display lots of messages about the processing.  Useful for
  138.       debugging unexpected behaviour.
  139.  
  140.  -h : Allows encryption / decryption of hidden or system files.  These are 
  141.       skipped during normal processing.
  142.  
  143.  -c : Compress the file, before encrypting.  The algorithm is as used in the 
  144.       freeware "gzip" utility - similar to pkzip's 'deflate'.  The compression
  145.       is applied before encryption, and its effectiveness is highly dependent
  146.       on the source data.  Don't bother using it on already compressed files
  147.       like .zip or .jpg - it just slows it down.  On the other hand, many text
  148.       files, documents, spreadsheets, database files and the like respond very
  149.       well to compression.  Highly repetitive data like XXX.TXT (see below)
  150.       compresses extremely well (7900 bytes compresses to 76 bytes, including
  151.       the 20 byte header).  This switch is ignored during decryption, but if
  152.       decrypting files which have been compressed, then they are automatically 
  153.       decompressed.
  154.      
  155.  -i : Force interactive mode (see above).  This is the only way you can combine 
  156.       using switches with the privacy of interactive mode.  For example:
  157.  
  158.         blowfish -c -s -i
  159.  
  160.       will prompt for source, target and key, then encrypt and compress matching
  161.       files, in the current or specified directory, and its subdirectories.
  162.  
  163. Version 1.61 also has these additional switches:
  164.  
  165.  -e : Force encryption - ignore files which are already encrypted.  The way I see
  166.       this working is as follows:  Say you have a subdirectory C:\DOCS, in which
  167.       you normally keep all files encrypted.  In the course of the day you decrypt
  168.       some of the files, in various subdirectories under DOCS.  Before you shut
  169.       down and go home, you want to make sure all the files are encrypted again.
  170.       The command:
  171.  
  172.         blowfish c:\docs "the key" -s -e
  173.  
  174.       will search through the DOCS directory (and subdirectories), find all the 
  175.       files which are not encrypted, and encrypt them.  It won't touch any file
  176.       which is aready encrypted, so it's fast.
  177.  
  178.  -d : Force decryption.  This is the complement of the "-e" switch.  It decrypts
  179.       any file which is encrypted, ignoring files which are already decrypted.
  180.  
  181. ════════════════════════════════════════════════════════════════════════════════
  182.  
  183. Technical Stuff.
  184. ────────────────
  185.  
  186. Blowfish is a new algorithm, optimised for 32-bit processors, and it is 
  187. extremely fast. It is also (theoretically) extremely secure - many orders of 
  188. magnitude better than DES - if sufficiently long keys are used.  I don't 
  189. guarantee the security - I didn't develop the algorithm, so use it at your own 
  190. risk.  That said, the only real risk is that you will delete the original file, 
  191. and forget the key.  If you do, good luck, because I won't be able to help.
  192.  
  193. Encrypted files have a short header added, so they will be a few bytes larger
  194. than the original.  There are no restrictions on the type of file - both text
  195. and binary are supported, and both HPFS long filenames and FAT 8.3 filenames
  196. can be used.  This version is fully 32-bit, optimised for 486 processors or
  197. better, and will therefore only run on OS/2 2.0, 2.1, Warp or above. 
  198.  
  199. Version 1.61 also includes 32-bit Windows executables, so it requires Windows
  200. NT or Windows 95.
  201.  
  202. Unlike some other Blowfish-based encryption utilities, this one has a higher
  203. level of security in the form of "block chaining".  In simple terms, this 
  204. means that if the same piece of text appears more than once in a file, it will 
  205. be encrypted into a different ciphertext each time.  You can test this by 
  206. encrypting a file with highly repetitive content.  Say, a text file with the 
  207. expression "Hello world" written over and over.  Other utilities which just 
  208. make a single pass will leave a definite pattern in the encrypted file.  This
  209. one will not.  This chaining process makes it virtually impossible to decode 
  210. the encrypted file by comparing the plaintext to the ciphertext.  
  211.  
  212. The encryption speed is also pretty good.  Currently, it encrypts files at over
  213. 290 KBytes/sec (60Mhz Pentium), or 225 KBytes/sec (486-DX2/66).  Your mileage
  214. may vary, but this is fairly ordinary hardware - IDE drive, HPFS formatted,
  215. standard cache.
  216.  
  217. Because encryption is a potentially destructive process, I have endeavoured to 
  218. make this programme as robust as possible.  For example:
  219.  
  220.  - Replacement of a file during encryption is done by writing to a temporary 
  221.    file, and then replacing the original only if no errors occurred.  Any 
  222.    error will leave the original file intact.  If there is insufficient space
  223.    on the drive to create the target file, the encryption will not proceed.
  224.  
  225.  - The programme includes a signal handler, so a ctrl-break during execution 
  226.    is handled correctly - the programme terminates cleanly, temporary files 
  227.    are deleted and the source file is left untouched.  
  228.  
  229.  - The header added to the encrypted file contains a hash value based on the 
  230.    original key.  Before an encrypted file is decrypted, the supplied key is 
  231.    hashed, and compared to the stored hash value.  If they do not match, the 
  232.    decryption is aborted. This prevents you from decrypting with the wrong key
  233.    and ending up with complete garbage.  This does not compromise the security,
  234.    since any number of keys could hash to the same value, so the real key 
  235.    cannnot be reverse-engineered from the hash value.  For those of you who 
  236.    are interested, the odds of picking a wrong key which hashes to the right
  237.    value are 1-in-10,007.
  238.  
  239.  - File date, time and attributes are preserved.  The encrypted file will have
  240.    the same timestamp and attributes as the original.  BLOWFISH also works
  241.    on hidden, system or read-only files.
  242.  
  243. Planned enhancements include:
  244.  
  245.  - Maintain EA's on files - currently they are stripped off during encryption.
  246.  
  247.  - Include a DOS or family mode version, for compatibilty.
  248.  
  249.  - Enhance the interface to ENCRYPT.DLL to allow calling from REXX.
  250.  
  251. Other files in this package:
  252.  
  253.  - BLOWFISH.DOC
  254.    This file.
  255.  
  256.  - RELEASE.LOG
  257.    History of development and changes.
  258.  
  259.  - XXX.TXT
  260.    Try encrypting this file with this version of BLOWFISH, and compare the 
  261.    results with using any other one.  This should demonstrate the value of 
  262.    block chaining.
  263.  
  264.  - BF.EXE
  265.    Statically-linked version of BLOWFISH.EXE.  This does not require ENCRYPT.DLL,
  266.    so it is easier to install.  Just put it somewhere in your PATH.
  267.  
  268.  - BFW.EXE
  269.    32-bit Windows version of BF.EXE, for use on Windows NT or Windows 95.
  270.  
  271. Further information, including Bruce Schneier's original source and 
  272. documentation is available at (among other places): 
  273.  
  274.    ftp://ftp.ox.ac.uk/pub/crypto/misc/
  275.  
  276. Also see the Blowfish page at Counterpane Systems:
  277.  
  278.    http://www.counterpane.com/blowfish.html
  279.  
  280. ════════════════════════════════════════════════════════════════════════════════
  281.  
  282. Programming Interface.
  283. ──────────────────────
  284.  
  285. For the programmers out there, you can add encryption to your own programmes.
  286. There are several files included to let you do this:
  287.  
  288.  - ENCRYPT.H
  289.    Header file for encryption API's.  There are 3 functions:  Initialise(),
  290.    EncryptBlock(), and DecryptBlock().  You call Initialise() once, passing
  291.    the encryption key, then the other functions to encrypt or decrypt, 
  292.    repectively.  The only limitation is that the length of the data to be
  293.    encrypted must be at least 8 bytes (and less than 4 GB). 
  294.  
  295.    [Version 1.61: you also need to call SetVersion(), BEFORE calling any of
  296.    the other API's.  The parameter is the version number, as an integer 
  297.    (e.g. pass 161 for compatibilty with version 1.61).  The reason for this
  298.    is that EncryptBlock() and DecryptBlock() contain version-specific code,
  299.    for backward-compatibility with versions 1.51 and earlier (which contained
  300.    a minor bug in the block-chaining algorithm).
  301.  
  302.  - SAMPLE.C
  303.    Sample which demonstrates using the API.  Very simple and self-explanatory.
  304.  
  305.  - SAMPLE2.C
  306.    Same as SAMPLE.C, but uses the _System linkage interface, which is required
  307.    for Borland C++.
  308.  
  309.  - SAMPLE.EXE
  310.    Compiled version of SAMPLE.C.  Shows how comprehensively the encryption 
  311.    works. 
  312.  
  313.  - ENCRYPT.LIB
  314.    Import Library for ENCRYPT.DLL.  Link with this to use the functions in 
  315.    the DLL.
  316.  
  317.  - ENCRYPT.OBJ
  318.    Object file with the encryption functions.  Link with this to build a 
  319.    stand-alone executable which does not require ENCRYPT.DLL.
  320.  
  321.  - ENCRYPTW.OBJ
  322.    32-bit Windows version of ENCRYPT.OBJ.
  323.  
  324. ════════════════════════════════════════════════════════════════════════════════
  325.  
  326. Key Permutations.
  327. ─────────────────
  328.  
  329. As an aside, the total possible number of keys is calculated as
  330. (2^8 + 2^16 + ... + 2^448), which is:
  331.  
  332. 729,689,072,234,021,035,218,144,685,565,996,708,998,165,444,454,719,307,576,
  333. 711,729,373,504,540,223,646,542,633,352,584,245,974,403,072,765,511,887,075,
  334. 064,905,822,972,615,393,536
  335.  
  336. A brute-force approach to finding the key, trying say 1,000,000,000 keys per
  337. second, would take on average:
  338.  
  339. 11,561,225,698,944,486,196,956,433,403,775,900,401,142,124,946,997,225,827,
  340. 957,635,076,392,129,633,173,095,270,764,452,687,244,505,334,258,079,066,327,
  341. 525,935.2 years.
  342.  
  343. By way of comparison, the 56 bit DES key has 72,057,594,037,927,936 possible
  344. combinations.  At the same rate, this would take 417 days to crack.
  345.  
  346. ════════════════════════════════════════════════════════════════════════════════
  347.