title: How to calculate PTK from PMK (WPA simulation)
body: <p>Im writing a WPA simulation but I cant figure out the transform for the PTK from PMK.</p>
  
  <p>I calculated the PMK = PBKDF2_HMAC_SHA1(passphrase,essid,strlen(essid),4096,32) using openssl library. The PTK is something like some_hash_function(PMK,anouce+snouce+bssid+clientssid,iterations) but it isnt clear in the text Im working from. I tried checking aircrack source code but it's all a little confusing.</p>
  
  <p>Can anyone tell me which hash function i need and what the arguments need to be?</p>
  
  <p>EDIT: Found a solution. Hope this helps someone, code dump follows:</p>
  
  <pre><code>    /* Calculate PMK */
      pmk = (unsigned char *) error_checked_malloc(sizeof(unsigned char) * PMK_SIZE);
      if(PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase)-1, essid, 
                                              strlen(essid), PMK_ITERATION, PMK_SIZE, pmk) != 0) /* ignore LF or NULL with strlen(passphrase)-1 */
                      {
      if(SHOWPMK || verbosity &gt;= 2)
              printf("\nPMK(#%04d)", num);
      else
              fprintf(stderr, "%s: PKCS5_PBKDF2_HMAC_SHA1 failed on PMK\n", PROG_NAME);
  
      /* Calculate PTK */
      /* PTK = PRF-X(PMK, "Pairwise key expansion", Min(AA, SA) || Max(AA, SA) || Min(ANonce, SNonce) || Max(ANonce, SNonce)) */
  
      HMAC(EVP_sha1(), name, 4, somestring, 36, ptk, NULL);
      // PTK[0:16] = KCK
      **************************/
  
      ptk = (unsigned char *) error_checked_malloc(sizeof(unsigned char) * PTK_SIZE);
      if(HMAC(EVP_sha1(), pmk, PMK_SIZE, pke, PKE_SIZE, ptk, NULL) != 0)
      {
              if(SHOWPTK || verbosity &gt;= 2)
                      printf("\nPTK(#%04d)", num);
              else
                      fprintf(stderr, "%s: HMAC_SHA1 failed on PTK\n", PROG_NAME);
  
      /* Calculate MIC | PRF-384 CCMP or PRF-512 TKIP */
      if(TKIPFLAG)
              HMAC(EVP_md5(), ptk, PTK_SIZE-4, eapol, eapolsize, kckmic, NULL);
      else
              HMAC(EVP_sha1(), ptk, PTK_SIZE-4, eapol, eapolsize, kckmic, NULL);
  </code></pre>