Line data Source code
1 : // Copyright (c) 2022 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <crypto/pkcs5_pbkdf2_hmac_sha512.h> 6 : #include <crypto/hmac_sha512.h> 7 : 8 : #include <cstring> // memcpy 9 : 10 244 : PKCS5_PBKDF2_HMAC_SHA512::PKCS5_PBKDF2_HMAC_SHA512( 11 : const char* pass, size_t passlen, 12 : const unsigned char* salt, int saltlen, 13 : int iter, 14 : size_t keylen, unsigned char* out) 15 122 : { 16 : unsigned char digtmp[CHMAC_SHA512::OUTPUT_SIZE], *p, itmp[4]; 17 122 : int cplen, j, k, tkeylen, mdlen{CHMAC_SHA512::OUTPUT_SIZE}; 18 122 : unsigned long i = 1; 19 : 20 122 : const unsigned char *upass = reinterpret_cast<const unsigned char *>(pass); 21 122 : p = out; 22 122 : tkeylen = keylen; 23 : 24 244 : while (tkeylen) { 25 122 : cplen = (tkeylen > mdlen) ? mdlen : tkeylen; 26 : /* We are unlikely to ever use more than 256 blocks (5120 bits!) 27 : * but just in case... 28 : */ 29 122 : itmp[0] = (unsigned char)((i >> 24) & 0xff); 30 122 : itmp[1] = (unsigned char)((i >> 16) & 0xff); 31 122 : itmp[2] = (unsigned char)((i >> 8) & 0xff); 32 122 : itmp[3] = (unsigned char)(i & 0xff); 33 122 : CHMAC_SHA512(upass, passlen).Write(salt, saltlen).Write(itmp, 4).Finalize(digtmp); 34 122 : memcpy(p, digtmp, cplen); 35 249859 : for (j = 1; j < iter; ++j) { 36 249737 : CHMAC_SHA512(upass, passlen).Write(digtmp, mdlen).Finalize(digtmp); 37 16232905 : for (k = 0; k < cplen; ++k) { 38 15983168 : p[k] ^= digtmp[k]; 39 15983168 : } 40 249737 : } 41 122 : tkeylen-= cplen; 42 122 : ++i; 43 122 : p+= cplen; 44 : } 45 244 : }