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 6246 : 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 3123 : { 16 : unsigned char digtmp[CHMAC_SHA512::OUTPUT_SIZE], *p, itmp[4]; 17 3123 : int cplen, j, k, tkeylen, mdlen{CHMAC_SHA512::OUTPUT_SIZE}; 18 3123 : unsigned long i = 1; 19 : 20 3123 : const unsigned char *upass = reinterpret_cast<const unsigned char *>(pass); 21 3123 : p = out; 22 3123 : tkeylen = keylen; 23 : 24 6246 : while (tkeylen) { 25 3123 : 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 3123 : itmp[0] = (unsigned char)((i >> 24) & 0xff); 30 3123 : itmp[1] = (unsigned char)((i >> 16) & 0xff); 31 3123 : itmp[2] = (unsigned char)((i >> 8) & 0xff); 32 3123 : itmp[3] = (unsigned char)(i & 0xff); 33 3123 : CHMAC_SHA512(upass, passlen).Write(salt, saltlen).Write(itmp, 4).Finalize(digtmp); 34 3123 : memcpy(p, digtmp, cplen); 35 6395907 : for (j = 1; j < iter; ++j) { 36 6392784 : CHMAC_SHA512(upass, passlen).Write(digtmp, mdlen).Finalize(digtmp); 37 415530960 : for (k = 0; k < cplen; ++k) { 38 409138176 : p[k] ^= digtmp[k]; 39 409138176 : } 40 6392784 : } 41 3123 : tkeylen-= cplen; 42 3123 : ++i; 43 3123 : p+= cplen; 44 : } 45 6246 : }