Share Pastebin
Guest
Public paste!

Zermelo

By: a guest | Feb 3rd, 2009 | Syntax: C++ | Size: 5.41 KB | Hits: 799 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. //PBKDF2 Beta 1.1.cpp : Defines the entry point for the console application.
  2. //PBKDF2 Beta 1.1 using the HMAC-SHA-1 as the Pseduo-Random Function
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <cmath>
  8. using namespace std;
  9. unsigned long Rol(unsigned long x, int y);
  10. unsigned long Ror(unsigned long x, int y);
  11. unsigned long f(unsigned long B,unsigned long C,unsigned long D, int t);
  12.  
  13. unsigned long H[5];
  14. unsigned long T[512]={0};
  15. void HMAC(string text, string key);
  16. void SHA1(string s);
  17. void PBKDF2(string P,string S, int c, int dkLen);
  18.  
  19. int main()
  20.  
  21. {
  22.         char P[8192];
  23.         char S[8192];
  24.         int dkLen,c;
  25.         c = 4096;
  26.         dkLen = 32;
  27.  
  28.         cout << "Enter Password: ";
  29.         cin.getline(P, 8192, '\n');
  30.         cout << "Enter Salt: ";
  31.         cin.getline(S, 8192, '\n');;
  32.         cout << "Enter Number of Iterations c: ";
  33.         cin >> c;
  34.         cout << "Enter intended length in bytes of the derived key: ";
  35.         cin >> dkLen;
  36.  
  37.         cout << "Derived Key: " << endl;
  38.         PBKDF2(P,S,c,dkLen);
  39.  
  40.         for(int i=0; i<(dkLen/4); i++)
  41.         {
  42.                 cout << hex << T[i];
  43.         }
  44.         cout << endl << "Press Enter to End" << endl;
  45.         cin >> c;
  46.         return 0;
  47. }
  48.  
  49. void PBKDF2(string P, string S,int c,int dkLen)
  50. {
  51.         string U;
  52.         unsigned long L[5]={0,0,0,0,0};
  53.         int hlen,l,r;
  54.         char t;
  55.        
  56.         hlen = 20;
  57.         //requires new version of Ceiling Function: l = ceil(dkLen/hlen)
  58.         l = (dkLen/hlen)+1;
  59.         r = dkLen -(l-1)*hlen;
  60.                
  61.         for(int i=1; i<=l; i++)
  62.         {
  63.                 t = 0;
  64.                 U = S + t + t + t;
  65.                 t = i;
  66.                 U = U + t;
  67.                 L[0] = L[1] = L[2] = L[3] = L[4] = 0;
  68.                 for(int j=1; j<=c; j++)
  69.                 {
  70.                         HMAC(U,P);
  71.                         L[0] = L[0]^H[0];
  72.                         L[1] = L[1]^H[1];
  73.                         L[2] = L[2]^H[2];
  74.                         L[3] = L[3]^H[3];
  75.                         L[4] = L[4]^H[4];
  76.                         U = "";
  77.                         for(int x=0; x<5; x++)
  78.                         {
  79.                                 for(int y=0; y<4; y++)
  80.                                 {
  81.                                         t = ((H[x] >> 8*(3-y)) % 256);
  82.                                         U = U + t;
  83.                                 }
  84.                         }
  85.                 }
  86.                
  87.                 T[5*(i-1)] = L[0];
  88.                 T[5*(i-1)+1] = L[1];
  89.                 T[5*(i-1)+2] = L[2];
  90.                 T[5*(i-1)+3] = L[3];
  91.                 T[5*(i-1)+4] = L[4];
  92.         }
  93. }
  94.  
  95. // HMAC function
  96. void HMAC(string text, string key)
  97. {
  98.         char c;
  99.         string s;
  100.         unsigned long Key[16] = {0};
  101.         unsigned long X[16] = {0};
  102.         unsigned long Y[16] = {0};
  103.         unsigned long ipad = 0x36363636;
  104.         unsigned long opad = 0x5c5c5c5c;
  105.         int k;
  106.         s = "";
  107.  
  108.         //Process string key into sub-key
  109.         //Hash key in case it is less than 64 bytes
  110.         if (key.length() > 64)
  111.         {
  112.                 SHA1(key);
  113.                 Key[0] = H[0];
  114.                 Key[1] = H[1];
  115.                 Key[2] = H[2];
  116.                 Key[3] = H[3];
  117.                 Key[4] = H[4];
  118.         }
  119.         else
  120.         {
  121.                 for(int i=0; i<16; i++)
  122.                 {
  123.                         for(int j=0; j<4; j++)
  124.                         {
  125.                                 if (4*i+j <= key.length())
  126.                                 {
  127.                                         k = key[4*i+j];
  128.                                 }
  129.                                 else
  130.                                 {
  131.                                         k = 0;
  132.                                 }
  133.                                 if (k<0)
  134.                                 {
  135.                                         k = k + 256;
  136.                                 }
  137.                                 Key[i]= Key[i] + k*pow(256,(double)3-j);
  138.                         }
  139.                 }
  140.         }
  141.        
  142.         for(int i=0; i<16; i++)
  143.         {
  144.                 X[i] = Key[i]^ipad;
  145.                 Y[i] = Key[i]^opad;
  146.         }
  147.  
  148.         //Turn X-Array into a String
  149.         for(int i=0; i<16; i++)
  150.         {
  151.                 for(int j=0; j<4; j++)
  152.                 {
  153.                         c = ((X[i] >> 8*(3-j)) % 256);
  154.                         s = s + c;
  155.                 }
  156.         }
  157.  
  158.         //Append text to string
  159.         s = s + text;
  160.  
  161.         //Hash X-Array
  162.         SHA1(s);
  163.  
  164.         s = "";
  165.  
  166.         //Turn Y-Array into a String
  167.         for(int i=0; i<16; i++)
  168.         {
  169.                 for(int j=0; j<4; j++)
  170.                 {
  171.                         c = ((Y[i] >> 8*(3-j)) % 256);
  172.                         s = s + c;
  173.                 }
  174.         }
  175.  
  176.         //Append Hashed X-Array to Y-Array in string
  177.         for(int i=0; i<5; i++)
  178.         {
  179.                 for(int j=0; j<4; j++)
  180.                 {
  181.                         c = ((H[i] >> 8*(3-j)) % 256);
  182.                         s = s + c;
  183.                 }
  184.         }
  185.  
  186.         //Hash final concatenated string
  187.         SHA1(s);
  188. }
  189.  
  190. // SHA-1 Algorithm
  191. void SHA1(string s)
  192. {
  193.         unsigned long K[80];
  194.         unsigned long A,B,C,D,E,TEMP;
  195.         int r,k,ln;
  196.         H[0]=0x67452301;
  197.         H[1]=0xefcdab89;
  198.         H[2]=0x98badcfe;
  199.         H[3]=0x10325476;
  200.         H[4]=0xc3d2e1f0;
  201.  
  202.         ln=s.length();
  203.         r = int((ln+1)/64);
  204.  
  205.         if (((ln+1) % 64) > 56)
  206.                 {
  207.                 r=r+1;
  208.                 }
  209.  
  210.         // initialize Constants
  211.         for(int t=0; t<80; t++)
  212.                 {
  213.                         if (t<20)
  214.                                 {
  215.                                         K[t] = 0x5a827999;
  216.                                 }
  217.  
  218.                         if ((t>19)&(t<40))
  219.                                 {
  220.                                         K[t] = 0x6ED9EBA1;
  221.                                 }
  222.                         if ((t>39)&(t<60))
  223.                                 {
  224.                                         K[t] = 0x8F1BBCDC;
  225.                                 }
  226.                         if (t>59)
  227.                                 {
  228.                                         K[t] = 0xca62c1d6;
  229.                                 }
  230.                 }
  231.  
  232.         for(int l=0; l <= r; l++)
  233.         {
  234.                 unsigned long W[80]={0};
  235.                 //Initialize Text
  236.                 for (int i=0; i<16; i++)
  237.                         {
  238.                         for(int j=0; j<4; j++)
  239.                                 {
  240.                                         if (4*i+j <= ln)
  241.                                         {
  242.                                                 k = s[64*l+4*i+j];
  243.                                         }
  244.                                         else
  245.                                         {
  246.                                                 k =0;
  247.                                         }
  248.                                
  249.                                         if (k<0)
  250.                                         {
  251.                                                 k = k +256;
  252.                                         }
  253.  
  254.                                         if (4*i+j == ln)
  255.                                                 {
  256.                                                         k = 0x80;
  257.                                                 }
  258.  
  259.                                         W[i]= W[i] + k*pow(256,(double)3-j);
  260.                                 }
  261.                         }
  262.                 if ((W[14]==0)&(W[15]==0))
  263.                 {
  264.                         W[15]=8*s.length();
  265.                 }
  266.  
  267.         // Hash Cycle
  268.  
  269.                 for (int t = 16; t <80; t++)
  270.                         {
  271.                                 W[t] = Rol(W[t-3]^W[t-8]^W[t-14]^W[t-16],1);
  272.                         }
  273.  
  274.                 A = H[0];
  275.                 B = H[1];
  276.                 C = H[2];
  277.                 D = H[3];
  278.                 E = H[4];
  279.  
  280.                 for(int t = 0; t < 80; t++)
  281.                 {
  282.                         TEMP = Rol(A,5) + f(B,C,D,t) + E + W[t] + K[t];
  283.                         E = D;
  284.                         D = C;
  285.                         C = Rol(B,30);
  286.                         B = A;
  287.                         A = TEMP;
  288.                 }
  289.  
  290.                 H[0] = H[0] + A;
  291.                 H[1] = H[1] + B;
  292.                 H[2] = H[2] + C;
  293.                 H[3] = H[3] + D;
  294.                 H[4] = H[4] + E;
  295.  
  296.                 ln = ln - 64;
  297.         }
  298.  
  299. }
  300.  
  301. unsigned long f(unsigned long B,unsigned long C,unsigned long D, int t)
  302. {
  303.         if (t < 20)
  304.                 {
  305.                         return ((B & C)^((~B) & D));
  306.                 }
  307.         if ((t > 19) & (t < 40))
  308.                 {
  309.                         return (B ^ C ^ D);
  310.                 }
  311.         if ((t > 39) & (t < 60))
  312.                 {
  313.                         return ((B & C)^(B & D)^(C & D));
  314.                 }
  315.         if (t > 59)
  316.                 {
  317.                         return (B ^ C ^ D);
  318.                 }
  319. }
  320.  
  321.  
  322. unsigned long Rol(unsigned long x, int y)
  323. {
  324.         if (y % 32 == 0) {return x;}
  325.         else {return ((x << y)^(x >> -y));}
  326. }
  327.  
  328. unsigned long Ror(unsigned long x, int y)
  329. {
  330.         if (y % 32 == 0) {return x;}
  331.         else {return ((x >> y)^(x << -y));}
  332. }