pbkdf2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(
  29. PBKDF2_HMAC_SHA256_CTX* pctx,
  30. const uint8_t* pass,
  31. int passlen,
  32. const uint8_t* salt,
  33. int saltlen,
  34. uint32_t blocknr) {
  35. SHA256_CTX ctx = {0};
  36. #if BYTE_ORDER == LITTLE_ENDIAN
  37. REVERSE32(blocknr, blocknr);
  38. #endif
  39. hmac_sha256_prepare(pass, passlen, pctx->odig, pctx->idig);
  40. memzero(pctx->g, sizeof(pctx->g));
  41. pctx->g[8] = 0x80000000;
  42. pctx->g[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
  43. memcpy(ctx.state, pctx->idig, sizeof(pctx->idig));
  44. ctx.bitcount = SHA256_BLOCK_LENGTH * 8;
  45. sha256_Update(&ctx, salt, saltlen);
  46. sha256_Update(&ctx, (uint8_t*)&blocknr, sizeof(blocknr));
  47. sha256_Final(&ctx, (uint8_t*)pctx->g);
  48. #if BYTE_ORDER == LITTLE_ENDIAN
  49. for(uint32_t k = 0; k < SHA256_DIGEST_LENGTH / sizeof(uint32_t); k++) {
  50. REVERSE32(pctx->g[k], pctx->g[k]);
  51. }
  52. #endif
  53. sha256_Transform(pctx->odig, pctx->g, pctx->g);
  54. memcpy(pctx->f, pctx->g, SHA256_DIGEST_LENGTH);
  55. pctx->first = 1;
  56. }
  57. void pbkdf2_hmac_sha256_Update(PBKDF2_HMAC_SHA256_CTX* pctx, uint32_t iterations) {
  58. for(uint32_t i = pctx->first; i < iterations; i++) {
  59. sha256_Transform(pctx->idig, pctx->g, pctx->g);
  60. sha256_Transform(pctx->odig, pctx->g, pctx->g);
  61. for(uint32_t j = 0; j < SHA256_DIGEST_LENGTH / sizeof(uint32_t); j++) {
  62. pctx->f[j] ^= pctx->g[j];
  63. }
  64. }
  65. pctx->first = 0;
  66. }
  67. void pbkdf2_hmac_sha256_Final(PBKDF2_HMAC_SHA256_CTX* pctx, uint8_t* key) {
  68. #if BYTE_ORDER == LITTLE_ENDIAN
  69. for(uint32_t k = 0; k < SHA256_DIGEST_LENGTH / sizeof(uint32_t); k++) {
  70. REVERSE32(pctx->f[k], pctx->f[k]);
  71. }
  72. #endif
  73. memcpy(key, pctx->f, SHA256_DIGEST_LENGTH);
  74. memzero(pctx, sizeof(PBKDF2_HMAC_SHA256_CTX));
  75. }
  76. void pbkdf2_hmac_sha256(
  77. const uint8_t* pass,
  78. int passlen,
  79. const uint8_t* salt,
  80. int saltlen,
  81. uint32_t iterations,
  82. uint8_t* key,
  83. int keylen) {
  84. uint32_t last_block_size = keylen % SHA256_DIGEST_LENGTH;
  85. uint32_t blocks_count = keylen / SHA256_DIGEST_LENGTH;
  86. if(last_block_size) {
  87. blocks_count++;
  88. } else {
  89. last_block_size = SHA256_DIGEST_LENGTH;
  90. }
  91. for(uint32_t blocknr = 1; blocknr <= blocks_count; blocknr++) {
  92. PBKDF2_HMAC_SHA256_CTX pctx = {0};
  93. pbkdf2_hmac_sha256_Init(&pctx, pass, passlen, salt, saltlen, blocknr);
  94. pbkdf2_hmac_sha256_Update(&pctx, iterations);
  95. uint8_t digest[SHA256_DIGEST_LENGTH] = {0};
  96. pbkdf2_hmac_sha256_Final(&pctx, digest);
  97. uint32_t key_offset = (blocknr - 1) * SHA256_DIGEST_LENGTH;
  98. if(blocknr < blocks_count) {
  99. memcpy(key + key_offset, digest, SHA256_DIGEST_LENGTH);
  100. } else {
  101. memcpy(key + key_offset, digest, last_block_size);
  102. }
  103. }
  104. }
  105. void pbkdf2_hmac_sha512_Init(
  106. PBKDF2_HMAC_SHA512_CTX* pctx,
  107. const uint8_t* pass,
  108. int passlen,
  109. const uint8_t* salt,
  110. int saltlen,
  111. uint32_t blocknr) {
  112. SHA512_CTX ctx = {0};
  113. #if BYTE_ORDER == LITTLE_ENDIAN
  114. REVERSE32(blocknr, blocknr);
  115. #endif
  116. hmac_sha512_prepare(pass, passlen, pctx->odig, pctx->idig);
  117. memzero(pctx->g, sizeof(pctx->g));
  118. pctx->g[8] = 0x8000000000000000;
  119. pctx->g[15] = (SHA512_BLOCK_LENGTH + SHA512_DIGEST_LENGTH) * 8;
  120. memcpy(ctx.state, pctx->idig, sizeof(pctx->idig));
  121. ctx.bitcount[0] = SHA512_BLOCK_LENGTH * 8;
  122. ctx.bitcount[1] = 0;
  123. sha512_Update(&ctx, salt, saltlen);
  124. sha512_Update(&ctx, (uint8_t*)&blocknr, sizeof(blocknr));
  125. sha512_Final(&ctx, (uint8_t*)pctx->g);
  126. #if BYTE_ORDER == LITTLE_ENDIAN
  127. for(uint32_t k = 0; k < SHA512_DIGEST_LENGTH / sizeof(uint64_t); k++) {
  128. REVERSE64(pctx->g[k], pctx->g[k]);
  129. }
  130. #endif
  131. sha512_Transform(pctx->odig, pctx->g, pctx->g);
  132. memcpy(pctx->f, pctx->g, SHA512_DIGEST_LENGTH);
  133. pctx->first = 1;
  134. }
  135. void pbkdf2_hmac_sha512_Update(PBKDF2_HMAC_SHA512_CTX* pctx, uint32_t iterations) {
  136. for(uint32_t i = pctx->first; i < iterations; i++) {
  137. sha512_Transform(pctx->idig, pctx->g, pctx->g);
  138. sha512_Transform(pctx->odig, pctx->g, pctx->g);
  139. for(uint32_t j = 0; j < SHA512_DIGEST_LENGTH / sizeof(uint64_t); j++) {
  140. pctx->f[j] ^= pctx->g[j];
  141. }
  142. }
  143. pctx->first = 0;
  144. }
  145. void pbkdf2_hmac_sha512_Final(PBKDF2_HMAC_SHA512_CTX* pctx, uint8_t* key) {
  146. #if BYTE_ORDER == LITTLE_ENDIAN
  147. for(uint32_t k = 0; k < SHA512_DIGEST_LENGTH / sizeof(uint64_t); k++) {
  148. REVERSE64(pctx->f[k], pctx->f[k]);
  149. }
  150. #endif
  151. memcpy(key, pctx->f, SHA512_DIGEST_LENGTH);
  152. memzero(pctx, sizeof(PBKDF2_HMAC_SHA512_CTX));
  153. }
  154. void pbkdf2_hmac_sha512(
  155. const uint8_t* pass,
  156. int passlen,
  157. const uint8_t* salt,
  158. int saltlen,
  159. uint32_t iterations,
  160. uint8_t* key,
  161. int keylen) {
  162. uint32_t last_block_size = keylen % SHA512_DIGEST_LENGTH;
  163. uint32_t blocks_count = keylen / SHA512_DIGEST_LENGTH;
  164. if(last_block_size) {
  165. blocks_count++;
  166. } else {
  167. last_block_size = SHA512_DIGEST_LENGTH;
  168. }
  169. for(uint32_t blocknr = 1; blocknr <= blocks_count; blocknr++) {
  170. PBKDF2_HMAC_SHA512_CTX pctx = {0};
  171. pbkdf2_hmac_sha512_Init(&pctx, pass, passlen, salt, saltlen, blocknr);
  172. pbkdf2_hmac_sha512_Update(&pctx, iterations);
  173. uint8_t digest[SHA512_DIGEST_LENGTH] = {0};
  174. pbkdf2_hmac_sha512_Final(&pctx, digest);
  175. uint32_t key_offset = (blocknr - 1) * SHA512_DIGEST_LENGTH;
  176. if(blocknr < blocks_count) {
  177. memcpy(key + key_offset, digest, SHA512_DIGEST_LENGTH);
  178. } else {
  179. memcpy(key + key_offset, digest, last_block_size);
  180. }
  181. }
  182. }