Advertisement
Guest User

Blowfish Source Code

a guest
Mar 27th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.32 KB | None | 0 0
  1. /********** blowfish.h **********/
  2.  
  3. #ifndef ___BLOWFISH_H___
  4. #define ___BLOWFISH_H___
  5.  
  6.  
  7. #define NUM_SUBKEYS  18
  8. #define NUM_S_BOXES  4
  9. #define NUM_ENTRIES  256
  10.  
  11. #define MAX_STRING   256
  12. #define MAX_PASSWD   56  // 448bits
  13.  
  14. // #define BIG_ENDIAN
  15. // #define LITTLE_ENDIAN
  16.  
  17.  
  18. #ifdef BIG_ENDIAN
  19. struct WordByte
  20. {
  21.     unsigned int zero:8;
  22.     unsigned int one:8;
  23.     unsigned int two:8;
  24.     unsigned int three:8;
  25. };
  26. #endif
  27.  
  28. #ifdef LITTLE_ENDIAN
  29. struct WordByte
  30. {
  31.     unsigned int three:8;
  32.     unsigned int two:8;
  33.     unsigned int one:8;
  34.     unsigned int zero:8;
  35. };
  36. #endif
  37.  
  38. union Word
  39. {
  40.     unsigned int word;
  41.     WordByte byte;
  42. };
  43.  
  44. struct DWord
  45. {
  46.     Word word0;
  47.     Word word1;
  48. };
  49.  
  50.  
  51. class Blowfish
  52. {
  53. private:
  54.   unsigned int PA[NUM_SUBKEYS];
  55.   unsigned int SB[NUM_S_BOXES][NUM_ENTRIES];
  56.  
  57.   void Gen_Subkeys(char *);
  58.   inline void BF_En(Word *,Word *);
  59.   inline void BF_De(Word *,Word *);
  60.  
  61. public:
  62.   Blowfish();
  63.   ~Blowfish();
  64.  
  65.   void Reset();
  66.   void Set_Passwd(char * = NULL);
  67.   void Encrypt(void *,unsigned int);
  68.   void Decrypt(void *,unsigned int);
  69. };
  70.  
  71.  
  72. #endif
  73.  
  74.  
  75.  
  76. /********** blowfish.cc **********/
  77.  
  78. #include <iostream.h>
  79. #include <string.h>
  80. #include "blowfish.h"
  81.  
  82.  
  83. #define F(x)    (((SB[0][x.byte.zero] + SB[1][x.byte.one]) ^ SB[2][x.byte.two]) +"\aEnter your password: ";
  84.     cin.get(New_Passwd,MAX_STRING,'\n');
  85.     len = strlen(New_Passwd);
  86.     }
  87.     while (len > MAX_PASSWD);
  88.     Passwd = New_Passwd;
  89.   }
  90.   else
  91.     len = strlen(Passwd);
  92.  
  93.   Reset();
  94.   if (len > 0)
  95.     Gen_Subkeys(Passwd);
  96.  
  97.   for (i=0;i<MAX_STRING;i++)
  98.     New_Passwd[i] = '\0';
  99.    Passwd = NULL;
  100.    len = 0;
  101. }
  102.  
  103. void Blowfish::Encrypt(void *Ptr,unsigned int N_Bytes)
  104. {
  105.   unsigned int i;
  106.   DWord *Work;
  107.  
  108.   if (N_Bytes%8)
  109.   {
  110.     cerr << "\aBlowfish requires the input to be a multiple of 8 bytes (64
  111. bits) to work.\n";
  112.     return;
  113.   }
  114.  
  115.     N_Bytes /= 8;
  116.   Work = (DWord *)Ptr;
  117.  
  118.   for (i=0;i<N_Bytes;i++)
  119.   {
  120.     BF_En(&Work->word0,&Work->word1);
  121.     Work++;
  122.   }
  123.  
  124.   Work = NULL;
  125. }
  126.  
  127. void Blowfish::Decrypt(void *Ptr,unsigned int N_Bytes)
  128. {
  129.   unsigned int i;
  130.   DWord *Work;
  131.  
  132.   if (N_Bytes%8)
  133.   {
  134.     cerr << "\aBlowfish requires the input to be a multiple of 8 bytes (64
  135. bits) to work.\n";
  136.     return;
  137.   }
  138.  
  139.     N_Bytes /= 8;
  140.   Work = (DWord *)Ptr;
  141.   for (i=0;i<N_Bytes;i++)
  142.   {
  143.     BF_De(&Work->word0,&Work->word1);
  144.     Work++;
  145.   }
  146.  
  147.   Work = NULL;
  148. }
  149.  
  150.  
  151.  
  152. /********* BFtest.cc **********/
  153. #include <stdio.h>
  154. #include <iostream.h>
  155. #include <stdlib.h>
  156. #include <time.h>
  157. #include "blowfish.h"
  158.  
  159. #define BUFF_SIZE 1048576  // 1MB
  160. #define NUM_TRIALS 100
  161.  
  162.  
  163. int Test(Blowfish *);
  164. double Speed(Blowfish *);
  165.  
  166.  
  167. void main()
  168. {
  169.     int result;
  170.     double speed;
  171.     Blowfish BF;
  172.  
  173.     cout << "Blowfish verification: ";
  174.     if (result = Test(&BF))
  175.     {
  176.         cout << "\aFailed " << (result>0?"en":"de") << "crypting test vector " <<
  177. abs(result) << endl;
  178.         return;
  179.     }
  180.     else
  181.         cout << "Passed" << endl;
  182.  
  183.     if ((speed = Speed(&BF)) <= 0)
  184.         cout << "Not enough time elapsed for the test, or something funny happend." <<
  185. endl;
  186.     else
  187.         cout << "The throughput is " << speed << "MB/s" << endl;
  188. }
  189.  
  190.  
  191. int Test(Blowfish *BF)
  192. {
  193.     unsigned int i;
  194.     DWord Test_Vect;
  195.     char *Passwd[2] = {"abcdefghijklmnopqrstuvwxyz","Who is John Galt?"};
  196.     unsigned int Clr0[2] = {0x424c4f57,0xfedcba98};
  197.     unsigned int Clr1[2] = {0x46495348,0x76543210};
  198.     unsigned int Crypt0[2] = {0x324ed0fe,0xcc91732b};
  199.     unsigned int Crypt1[2] = {0xf413a203,0x8022f684};
  200.    
  201.     for (i=0;i<2;i++)
  202.     {
  203.         Test_Vect.word0.word = Clr0[i];
  204.         Test_Vect.word1.word = Clr1[i];
  205.         BF->Set_Passwd(Passwd[i]);
  206.         BF->Encrypt((void *)&Test_Vect,8);
  207.         if (Test_Vect.word0.word != Crypt0[i] || Test_Vect.word1.word != Crypt1[i])
  208.             return (i+1);
  209.         BF->Decrypt((void *)&Test_Vect,8);
  210.         if (Test_Vect.word0.word != Clr0[i] || Test_Vect.word1.word != Clr1[i])
  211.             return -(i+1);
  212.     }
  213.     return 0;
  214. }
  215.  
  216.  
  217. double Speed(Blowfish *BF)
  218. {
  219.     char *buff;
  220.     unsigned int i;
  221.     time_t begin,end;
  222.  
  223.     buff = new char[BUFF_SIZE];
  224.     if (buff == NULL)
  225.     {
  226.         cerr << "\aRan out of memory for the test buffer\n";
  227.         return 0;
  228.     }
  229.    
  230.     srand(0);
  231.     for (i=0;i<BUFF_SIZE;i++)
  232.         buff[i] = rand()%256;
  233.     BF->Set_Passwd("ianchan");
  234.  
  235.     begin = time(NULL);
  236.     for (i=0;i<NUM_TRIALS;i++)
  237.         BF->Encrypt((void *)buff,BUFF_SIZE);
  238.     end = time(NULL);
  239.  
  240.     delete []buff;
  241.     if (end-begin < 10)
  242.         return 0;
  243.     else
  244.         return double(NUM_TRIALS)/(end-begin);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement