2
0

pbkdf-nettle.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * QEMU Crypto PBKDF support (Password-Based Key Derivation Function)
  3. *
  4. * Copyright (c) 2015-2016 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include <nettle/pbkdf2.h>
  22. #include <nettle/hmac.h>
  23. #include "qapi/error.h"
  24. #include "crypto/pbkdf.h"
  25. bool qcrypto_pbkdf2_supports(QCryptoHashAlgorithm hash)
  26. {
  27. switch (hash) {
  28. case QCRYPTO_HASH_ALG_SHA1:
  29. case QCRYPTO_HASH_ALG_SHA224:
  30. case QCRYPTO_HASH_ALG_SHA256:
  31. case QCRYPTO_HASH_ALG_SHA384:
  32. case QCRYPTO_HASH_ALG_SHA512:
  33. case QCRYPTO_HASH_ALG_RIPEMD160:
  34. return true;
  35. default:
  36. return false;
  37. }
  38. }
  39. int qcrypto_pbkdf2(QCryptoHashAlgorithm hash,
  40. const uint8_t *key, size_t nkey,
  41. const uint8_t *salt, size_t nsalt,
  42. uint64_t iterations,
  43. uint8_t *out, size_t nout,
  44. Error **errp)
  45. {
  46. union {
  47. struct hmac_md5_ctx md5;
  48. struct hmac_sha1_ctx sha1;
  49. struct hmac_sha224_ctx sha224;
  50. struct hmac_sha256_ctx sha256;
  51. struct hmac_sha384_ctx sha384;
  52. struct hmac_sha512_ctx sha512;
  53. struct hmac_ripemd160_ctx ripemd160;
  54. } ctx;
  55. if (iterations > UINT_MAX) {
  56. error_setg_errno(errp, ERANGE,
  57. "PBKDF iterations %llu must be less than %u",
  58. (long long unsigned)iterations, UINT_MAX);
  59. return -1;
  60. }
  61. switch (hash) {
  62. case QCRYPTO_HASH_ALG_MD5:
  63. hmac_md5_set_key(&ctx.md5, nkey, key);
  64. PBKDF2(&ctx.md5, hmac_md5_update, hmac_md5_digest,
  65. MD5_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  66. break;
  67. case QCRYPTO_HASH_ALG_SHA1:
  68. hmac_sha1_set_key(&ctx.sha1, nkey, key);
  69. PBKDF2(&ctx.sha1, hmac_sha1_update, hmac_sha1_digest,
  70. SHA1_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  71. break;
  72. case QCRYPTO_HASH_ALG_SHA224:
  73. hmac_sha224_set_key(&ctx.sha224, nkey, key);
  74. PBKDF2(&ctx.sha224, hmac_sha224_update, hmac_sha224_digest,
  75. SHA224_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  76. break;
  77. case QCRYPTO_HASH_ALG_SHA256:
  78. hmac_sha256_set_key(&ctx.sha256, nkey, key);
  79. PBKDF2(&ctx.sha256, hmac_sha256_update, hmac_sha256_digest,
  80. SHA256_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  81. break;
  82. case QCRYPTO_HASH_ALG_SHA384:
  83. hmac_sha384_set_key(&ctx.sha384, nkey, key);
  84. PBKDF2(&ctx.sha384, hmac_sha384_update, hmac_sha384_digest,
  85. SHA384_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  86. break;
  87. case QCRYPTO_HASH_ALG_SHA512:
  88. hmac_sha512_set_key(&ctx.sha512, nkey, key);
  89. PBKDF2(&ctx.sha512, hmac_sha512_update, hmac_sha512_digest,
  90. SHA512_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  91. break;
  92. case QCRYPTO_HASH_ALG_RIPEMD160:
  93. hmac_ripemd160_set_key(&ctx.ripemd160, nkey, key);
  94. PBKDF2(&ctx.ripemd160, hmac_ripemd160_update, hmac_ripemd160_digest,
  95. RIPEMD160_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
  96. break;
  97. default:
  98. error_setg_errno(errp, ENOSYS,
  99. "PBKDF does not support hash algorithm %s",
  100. QCryptoHashAlgorithm_str(hash));
  101. return -1;
  102. }
  103. return 0;
  104. }