pwd2key.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
  4. The redistribution and use of this software (with or without changes)
  5. is allowed without the payment of fees or royalties provided that:
  6. source code distributions include the above copyright notice, this
  7. list of conditions and the following disclaimer;
  8. binary distributions include the above copyright notice, this list
  9. of conditions and the following disclaimer in their documentation.
  10. This software is provided 'as is' with no explicit or implied warranties
  11. in respect of its operation, including, but not limited to, correctness
  12. and fitness for purpose.
  13. ---------------------------------------------------------------------------
  14. Issue Date: 20/12/2007
  15. This is an implementation of RFC2898, which specifies key derivation from
  16. a password and a salt value.
  17. */
  18. #include <string.h>
  19. #include "hmac.h"
  20. #if defined(__cplusplus)
  21. extern "C"
  22. {
  23. #endif
  24. void derive_key(const unsigned char pwd[], /* the PASSWORD */
  25. unsigned int pwd_len, /* and its length */
  26. const unsigned char salt[], /* the SALT and its */
  27. unsigned int salt_len, /* length */
  28. unsigned int iter, /* the number of iterations */
  29. unsigned char key[], /* space for the output key */
  30. unsigned int key_len)/* and its required length */
  31. {
  32. unsigned int i, j, k, n_blk, h_size;
  33. unsigned char uu[HMAC_MAX_OUTPUT_SIZE], ux[HMAC_MAX_OUTPUT_SIZE];
  34. hmac_ctx c1[1], c2[1], c3[1];
  35. /* set HMAC context (c1) for password */
  36. h_size = hmac_sha_begin(HMAC_SHA1, c1);
  37. hmac_sha_key(pwd, pwd_len, c1);
  38. /* set HMAC context (c2) for password and salt */
  39. memcpy(c2, c1, sizeof(hmac_ctx));
  40. hmac_sha_data(salt, salt_len, c2);
  41. /* find the number of SHA blocks in the key */
  42. n_blk = 1 + (key_len - 1) / h_size;
  43. for(i = 0; i < n_blk; ++i) /* for each block in key */
  44. {
  45. /* ux[] holds the running xor value */
  46. memset(ux, 0, h_size);
  47. /* set HMAC context (c3) for password and salt */
  48. memcpy(c3, c2, sizeof(hmac_ctx));
  49. /* enter additional data for 1st block into uu */
  50. uu[0] = (unsigned char)((i + 1) >> 24);
  51. uu[1] = (unsigned char)((i + 1) >> 16);
  52. uu[2] = (unsigned char)((i + 1) >> 8);
  53. uu[3] = (unsigned char)(i + 1);
  54. /* this is the key mixing iteration */
  55. for(j = 0, k = 4; j < iter; ++j)
  56. {
  57. /* add previous round data to HMAC */
  58. hmac_sha_data(uu, k, c3);
  59. /* obtain HMAC for uu[] */
  60. hmac_sha_end(uu, h_size, c3);
  61. /* xor into the running xor block */
  62. for(k = 0; k < h_size; ++k)
  63. ux[k] ^= uu[k];
  64. /* set HMAC context (c3) for password */
  65. memcpy(c3, c1, sizeof(hmac_ctx));
  66. }
  67. /* compile key blocks into the key output */
  68. j = 0; k = i * h_size;
  69. while(j < h_size && k < key_len)
  70. key[k++] = ux[j++];
  71. }
  72. }
  73. #ifdef TEST
  74. #include <stdio.h>
  75. struct
  76. { unsigned int pwd_len;
  77. unsigned int salt_len;
  78. unsigned int it_count;
  79. unsigned char *pwd;
  80. unsigned char salt[32];
  81. unsigned char key[32];
  82. } tests[] =
  83. {
  84. { 8, 4, 5, (unsigned char*)"password",
  85. {
  86. 0x12, 0x34, 0x56, 0x78
  87. },
  88. {
  89. 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7,
  90. 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5
  91. }
  92. },
  93. { 8, 8, 5, (unsigned char*)"password",
  94. {
  95. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  96. },
  97. {
  98. 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
  99. 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49
  100. }
  101. },
  102. { 8, 21, 1, (unsigned char*)"password",
  103. {
  104. "ATHENA.MIT.EDUraeburn"
  105. },
  106. {
  107. 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
  108. 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15
  109. }
  110. },
  111. { 8, 21, 2, (unsigned char*)"password",
  112. {
  113. "ATHENA.MIT.EDUraeburn"
  114. },
  115. {
  116. 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
  117. 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d
  118. }
  119. },
  120. { 8, 21, 1200, (unsigned char*)"password",
  121. {
  122. "ATHENA.MIT.EDUraeburn"
  123. },
  124. {
  125. 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
  126. 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b
  127. }
  128. }
  129. };
  130. int main()
  131. { unsigned int i, j, key_len = 256;
  132. unsigned char key[256];
  133. printf("\nTest of RFC2898 Password Based Key Derivation");
  134. for(i = 0; i < 5; ++i)
  135. {
  136. derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt,
  137. tests[i].salt_len, tests[i].it_count, key, key_len);
  138. printf("\ntest %i: ", i + 1);
  139. printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good");
  140. for(j = 0; j < key_len && j < 64; j += 4)
  141. {
  142. if(j % 16 == 0)
  143. printf("\n");
  144. printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]);
  145. }
  146. printf(j < key_len ? " ... \n" : "\n");
  147. }
  148. printf("\n");
  149. return 0;
  150. }
  151. #if defined(__cplusplus)
  152. }
  153. #endif
  154. #endif