pbkdf2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "pbkdf2.h"
  24. #include <string.h>
  25. #include "hmac.h"
  26. #include "memzero.h"
  27. #include "sha2.h"
  28. void pbkdf2_hmac_sha256_Init(PBKDF2_HMAC_SHA256_CTX *pctx, const uint8_t *pass,
  29. int passlen, const uint8_t *salt, int saltlen,
  30. uint32_t blocknr) {
  31. SHA256_CTX ctx = {0};
  32. #if BYTE_ORDER == LITTLE_ENDIAN
  33. REVERSE32(blocknr, blocknr);
  34. #endif
  35. hmac_sha256_prepare(pass, passlen, pctx->odig, pctx->idig);
  36. memzero(pctx->g, sizeof(pctx->g));
  37. pctx->g[8] = 0x80000000;
  38. pctx->g[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
  39. memcpy(ctx.state, pctx->idig, sizeof(pctx->idig));
  40. ctx.bitcount = SHA256_BLOCK_LENGTH * 8;
  41. sha256_Update(&ctx, salt, saltlen);
  42. sha256_Update(&ctx, (uint8_t *)&blocknr, sizeof(blocknr));
  43. sha256_Final(&ctx, (uint8_t *)pctx->g);
  44. #if BYTE_ORDER == LITTLE_ENDIAN
  45. for (uint32_t k = 0; k < SHA256_DIGEST_LENGTH / sizeof(uint32_t); k++) {
  46. REVERSE32(pctx->g[k], pctx->g[k]);
  47. }
  48. #endif
  49. sha256_Transform(pctx->odig, pctx->g, pctx->g);
  50. memcpy(pctx->f, pctx->g, SHA256_DIGEST_LENGTH);
  51. pctx->first = 1;
  52. }
  53. void pbkdf2_hmac_sha256_Update(PBKDF2_HMAC_SHA256_CTX *pctx,
  54. uint32_t iterations) {
  55. for (uint32_t i = pctx->first; i < iterations; i++) {
  56. sha256_Transform(pctx->idig, pctx->g, pctx->g);
  57. sha256_Transform(pctx->odig, pctx->g, pctx->g);
  58. for (uint32_t j = 0; j < SHA256_DIGEST_LENGTH / sizeof(uint32_t); j++) {
  59. pctx->f[j] ^= pctx->g[j];
  60. }
  61. }
  62. pctx->first = 0;
  63. }
  64. void pbkdf2_hmac_sha256_Final(PBKDF2_HMAC_SHA256_CTX *pctx, uint8_t *key) {
  65. #if BYTE_ORDER == LITTLE_ENDIAN
  66. for (uint32_t k = 0; k < SHA256_DIGEST_LENGTH / sizeof(uint32_t); k++) {
  67. REVERSE32(pctx->f[k], pctx->f[k]);
  68. }
  69. #endif
  70. memcpy(key, pctx->f, SHA256_DIGEST_LENGTH);
  71. memzero(pctx, sizeof(PBKDF2_HMAC_SHA256_CTX));
  72. }
  73. void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, const uint8_t *salt,
  74. int saltlen, uint32_t iterations, uint8_t *key,
  75. int keylen) {
  76. uint32_t last_block_size = keylen % SHA256_DIGEST_LENGTH;
  77. uint32_t blocks_count = keylen / SHA256_DIGEST_LENGTH;
  78. if (last_block_size) {
  79. blocks_count++;
  80. } else {
  81. last_block_size = SHA256_DIGEST_LENGTH;
  82. }
  83. for (uint32_t blocknr = 1; blocknr <= blocks_count; blocknr++) {
  84. PBKDF2_HMAC_SHA256_CTX pctx = {0};
  85. pbkdf2_hmac_sha256_Init(&pctx, pass, passlen, salt, saltlen, blocknr);
  86. pbkdf2_hmac_sha256_Update(&pctx, iterations);
  87. uint8_t digest[SHA256_DIGEST_LENGTH] = {0};
  88. pbkdf2_hmac_sha256_Final(&pctx, digest);
  89. uint32_t key_offset = (blocknr - 1) * SHA256_DIGEST_LENGTH;
  90. if (blocknr < blocks_count) {
  91. memcpy(key + key_offset, digest, SHA256_DIGEST_LENGTH);
  92. } else {
  93. memcpy(key + key_offset, digest, last_block_size);
  94. }
  95. }
  96. }
  97. void pbkdf2_hmac_sha512_Init(PBKDF2_HMAC_SHA512_CTX *pctx, const uint8_t *pass,
  98. int passlen, const uint8_t *salt, int saltlen,
  99. uint32_t blocknr) {
  100. SHA512_CTX ctx = {0};
  101. #if BYTE_ORDER == LITTLE_ENDIAN
  102. REVERSE32(blocknr, blocknr);
  103. #endif
  104. hmac_sha512_prepare(pass, passlen, pctx->odig, pctx->idig);
  105. memzero(pctx->g, sizeof(pctx->g));
  106. pctx->g[8] = 0x8000000000000000;
  107. pctx->g[15] = (SHA512_BLOCK_LENGTH + SHA512_DIGEST_LENGTH) * 8;
  108. memcpy(ctx.state, pctx->idig, sizeof(pctx->idig));
  109. ctx.bitcount[0] = SHA512_BLOCK_LENGTH * 8;
  110. ctx.bitcount[1] = 0;
  111. sha512_Update(&ctx, salt, saltlen);
  112. sha512_Update(&ctx, (uint8_t *)&blocknr, sizeof(blocknr));
  113. sha512_Final(&ctx, (uint8_t *)pctx->g);
  114. #if BYTE_ORDER == LITTLE_ENDIAN
  115. for (uint32_t k = 0; k < SHA512_DIGEST_LENGTH / sizeof(uint64_t); k++) {
  116. REVERSE64(pctx->g[k], pctx->g[k]);
  117. }
  118. #endif
  119. sha512_Transform(pctx->odig, pctx->g, pctx->g);
  120. memcpy(pctx->f, pctx->g, SHA512_DIGEST_LENGTH);
  121. pctx->first = 1;
  122. }
  123. void pbkdf2_hmac_sha512_Update(PBKDF2_HMAC_SHA512_CTX *pctx,
  124. uint32_t iterations) {
  125. for (uint32_t i = pctx->first; i < iterations; i++) {
  126. sha512_Transform(pctx->idig, pctx->g, pctx->g);
  127. sha512_Transform(pctx->odig, pctx->g, pctx->g);
  128. for (uint32_t j = 0; j < SHA512_DIGEST_LENGTH / sizeof(uint64_t); j++) {
  129. pctx->f[j] ^= pctx->g[j];
  130. }
  131. }
  132. pctx->first = 0;
  133. }
  134. void pbkdf2_hmac_sha512_Final(PBKDF2_HMAC_SHA512_CTX *pctx, uint8_t *key) {
  135. #if BYTE_ORDER == LITTLE_ENDIAN
  136. for (uint32_t k = 0; k < SHA512_DIGEST_LENGTH / sizeof(uint64_t); k++) {
  137. REVERSE64(pctx->f[k], pctx->f[k]);
  138. }
  139. #endif
  140. memcpy(key, pctx->f, SHA512_DIGEST_LENGTH);
  141. memzero(pctx, sizeof(PBKDF2_HMAC_SHA512_CTX));
  142. }
  143. void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, const uint8_t *salt,
  144. int saltlen, uint32_t iterations, uint8_t *key,
  145. int keylen) {
  146. uint32_t last_block_size = keylen % SHA512_DIGEST_LENGTH;
  147. uint32_t blocks_count = keylen / SHA512_DIGEST_LENGTH;
  148. if (last_block_size) {
  149. blocks_count++;
  150. } else {
  151. last_block_size = SHA512_DIGEST_LENGTH;
  152. }
  153. for (uint32_t blocknr = 1; blocknr <= blocks_count; blocknr++) {
  154. PBKDF2_HMAC_SHA512_CTX pctx = {0};
  155. pbkdf2_hmac_sha512_Init(&pctx, pass, passlen, salt, saltlen, blocknr);
  156. pbkdf2_hmac_sha512_Update(&pctx, iterations);
  157. uint8_t digest[SHA512_DIGEST_LENGTH] = {0};
  158. pbkdf2_hmac_sha512_Final(&pctx, digest);
  159. uint32_t key_offset = (blocknr - 1) * SHA512_DIGEST_LENGTH;
  160. if (blocknr < blocks_count) {
  161. memcpy(key + key_offset, digest, SHA512_DIGEST_LENGTH);
  162. } else {
  163. memcpy(key + key_offset, digest, last_block_size);
  164. }
  165. }
  166. }