Changeset 102495 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Dec 6, 2023 10:09:35 AM (12 months ago)
- Location:
- trunk/src/VBox/Runtime/common/crypto
- Files:
-
- 1 deleted
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/crypto/shacrypt-tmpl.cpp.h
r102491 r102495 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - Crypto - SHA-crypt, code template for SHA-512 core.4 * 5 * This is almost identical to shacrypt-256.cpp.h, fixes generally applies to6 * both. Diff the files after updates!3 * IPRT - Crypto - SHA-crypt, code template the core code. 4 * 5 * This is included a couple of times from shacrypt.cpp with different set of 6 * defines for each variation. 7 7 */ 8 8 … … 39 39 40 40 41 RTDECL(int) RTCrShaCrypt 512(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString)41 RTDECL(int) RTCrShaCryptTmpl(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString) 42 42 { 43 uint8_t abHash[ RTSHA512_HASH_SIZE];44 int rc = RTCrShaCrypt 512Ex(pszPhrase, pszSalt, cRounds, abHash);43 uint8_t abHash[TMPL_HASH_SIZE]; 44 int rc = RTCrShaCryptTmplEx(pszPhrase, pszSalt, cRounds, abHash); 45 45 if (RT_SUCCESS(rc)) 46 rc = RTCrShaCrypt 512ToString(abHash, pszSalt, cRounds, pszString, cbString);46 rc = RTCrShaCryptTmplToString(abHash, pszSalt, cRounds, pszString, cbString); 47 47 return rc; 48 48 } … … 50 50 51 51 52 RTR3DECL(int) RTCrShaCrypt 512Ex(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, uint8_t pabHash[RTSHA512_HASH_SIZE])52 RTR3DECL(int) RTCrShaCryptTmplEx(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, uint8_t pabHash[TMPL_HASH_SIZE]) 53 53 { 54 54 /* … … 70 70 * Get started... 71 71 */ 72 RTSHA512CONTEXT CtxA;73 RTSha512Init(&CtxA); /* Step 1. */74 RTSha512Update(&CtxA, pszPhrase, cchPhrase); /* Step 2. */75 RTSha512Update(&CtxA, pszSalt, cchSalt); /* Step 3. */76 77 RTSHA512CONTEXT CtxB;78 RTSha512Init(&CtxB); /* Step 4. */79 RTSha512Update(&CtxB, pszPhrase, cchPhrase); /* Step 5. */80 RTSha512Update(&CtxB, pszSalt, cchSalt); /* Step 6. */81 RTSha512Update(&CtxB, pszPhrase, cchPhrase); /* Step 7. */82 uint8_t abDigest[ RTSHA512_HASH_SIZE];83 RTSha512Final(&CtxB, abDigest); /* Step 8. */72 TMPL_HASH_CONTEXT_T CtxA; 73 TmplHashInit(&CtxA); /* Step 1. */ 74 TmplHashUpdate(&CtxA, pszPhrase, cchPhrase); /* Step 2. */ 75 TmplHashUpdate(&CtxA, pszSalt, cchSalt); /* Step 3. */ 76 77 TMPL_HASH_CONTEXT_T CtxB; 78 TmplHashInit(&CtxB); /* Step 4. */ 79 TmplHashUpdate(&CtxB, pszPhrase, cchPhrase); /* Step 5. */ 80 TmplHashUpdate(&CtxB, pszSalt, cchSalt); /* Step 6. */ 81 TmplHashUpdate(&CtxB, pszPhrase, cchPhrase); /* Step 7. */ 82 uint8_t abDigest[TMPL_HASH_SIZE]; 83 TmplHashFinal(&CtxB, abDigest); /* Step 8. */ 84 84 85 85 size_t cbLeft = cchPhrase; 86 while (cbLeft > RTSHA512_HASH_SIZE)/* Step 9. */87 { 88 RTSha512Update(&CtxA, abDigest, sizeof(abDigest));89 cbLeft -= RTSHA512_HASH_SIZE;90 } 91 RTSha512Update(&CtxA, abDigest, cbLeft); /* Step 10. */86 while (cbLeft > TMPL_HASH_SIZE) /* Step 9. */ 87 { 88 TmplHashUpdate(&CtxA, abDigest, sizeof(abDigest)); 89 cbLeft -= TMPL_HASH_SIZE; 90 } 91 TmplHashUpdate(&CtxA, abDigest, cbLeft); /* Step 10. */ 92 92 93 93 size_t iPhraseBit = cchPhrase; … … 95 95 { 96 96 if ((iPhraseBit & 1) != 0) 97 RTSha512Update(&CtxA, abDigest, sizeof(abDigest)); /* a) */97 TmplHashUpdate(&CtxA, abDigest, sizeof(abDigest)); /* a) */ 98 98 else 99 RTSha512Update(&CtxA, pszPhrase, cchPhrase); /* b) */99 TmplHashUpdate(&CtxA, pszPhrase, cchPhrase); /* b) */ 100 100 iPhraseBit >>= 1; 101 101 } 102 102 103 RTSha512Final(&CtxA, abDigest); /* Step 12. */104 105 RTSha512Init(&CtxB); /* Step 13. */103 TmplHashFinal(&CtxA, abDigest); /* Step 12. */ 104 105 TmplHashInit(&CtxB); /* Step 13. */ 106 106 for (size_t i = 0; i < cchPhrase; i++) /* Step 14. */ 107 RTSha512Update(&CtxB, pszPhrase, cchPhrase);108 109 uint8_t abDigestTemp[ RTSHA512_HASH_SIZE];110 RTSha512Final(&CtxB, abDigestTemp); /* Step 15. */107 TmplHashUpdate(&CtxB, pszPhrase, cchPhrase); 108 109 uint8_t abDigestTemp[TMPL_HASH_SIZE]; 110 TmplHashFinal(&CtxB, abDigestTemp); /* Step 15. */ 111 111 112 112 /* … … 119 119 AssertPtrReturn(pabSeqP, VERR_NO_MEMORY); 120 120 cbLeft = cbSeqP; 121 while (cbLeft > RTSHA512_HASH_SIZE)121 while (cbLeft > TMPL_HASH_SIZE) 122 122 { 123 123 memcpy(pb, abDigestTemp, sizeof(abDigestTemp)); /* a) */ 124 pb += RTSHA512_HASH_SIZE;125 cbLeft -= RTSHA512_HASH_SIZE;124 pb += TMPL_HASH_SIZE; 125 cbLeft -= TMPL_HASH_SIZE; 126 126 } 127 127 memcpy(pb, abDigestTemp, cbLeft); /* b) */ 128 128 129 RTSha512Init(&CtxB); /* Step 17. */129 TmplHashInit(&CtxB); /* Step 17. */ 130 130 131 131 for (size_t i = 0; i < 16 + (unsigned)abDigest[0]; i++) /* Step 18. */ 132 RTSha512Update(&CtxB, pszSalt, cchSalt);133 134 RTSha512Final(&CtxB, abDigestTemp); /* Step 19. */132 TmplHashUpdate(&CtxB, pszSalt, cchSalt); 133 134 TmplHashFinal(&CtxB, abDigestTemp); /* Step 19. */ 135 135 136 136 /* … … 139 139 /* Step 20. */ 140 140 size_t const cbSeqS = cchSalt; 141 #if 0 /* Given that the salt has a fixed range (8 thru 16 bytes), and SHA-512 141 #if 0 /* Given that the salt has a fixed range (8 thru 16 bytes), and SHA-512/256 142 142 * producing 64 bytes, we can safely skip the loop part here (a) and go 143 143 * straight for step (b). Further, we can drop the whole memory allocation, … … 149 149 pb = pabSeqS; 150 150 cbLeft = cbSeqS; 151 while (cbLeft > RTSHA512_HASH_SIZE)151 while (cbLeft > TMPL_HASH_SIZE) 152 152 { 153 153 memcpy(pb, (void *)abDigestTemp, sizeof(abDigestTemp)); /* a) */ 154 pb += RTSHA512_HASH_SIZE;155 cbLeft -= RTSHA512_HASH_SIZE154 pb += TMPL_HASH_SIZE; 155 cbLeft -= TMPL_HASH_SIZE 156 156 } 157 157 memcpy(pb, abDigestTemp, cbLeft); /* b) */ 158 158 #else 159 AssertCompile(RT_SHACRYPT_SALT_MAX_LEN < RTSHA512_HASH_SIZE);159 AssertCompile(RT_SHACRYPT_SALT_MAX_LEN < TMPL_HASH_SIZE); 160 160 uint8_t abSeqS[RT_SHACRYPT_SALT_MAX_LEN + 2]; 161 161 uint8_t * const pabSeqS = abSeqS; … … 166 166 for (uint32_t iRound = 0; iRound < cRounds; iRound++) 167 167 { 168 RTSHA512CONTEXT CtxC;169 RTSha512Init(&CtxC); /* a) */168 TMPL_HASH_CONTEXT_T CtxC; 169 TmplHashInit(&CtxC); /* a) */ 170 170 171 171 if ((iRound & 1) != 0) 172 RTSha512Update(&CtxC, pabSeqP, cbSeqP); /* b) */172 TmplHashUpdate(&CtxC, pabSeqP, cbSeqP); /* b) */ 173 173 else 174 RTSha512Update(&CtxC, abDigest, sizeof(abDigest)); /* c) */174 TmplHashUpdate(&CtxC, abDigest, sizeof(abDigest)); /* c) */ 175 175 176 176 if (iRound % 3 != 0) /* d) */ 177 RTSha512Update(&CtxC, pabSeqS, cbSeqS);177 TmplHashUpdate(&CtxC, pabSeqS, cbSeqS); 178 178 179 179 if (iRound % 7 != 0) 180 RTSha512Update(&CtxC, pabSeqP, cbSeqP); /* e) */180 TmplHashUpdate(&CtxC, pabSeqP, cbSeqP); /* e) */ 181 181 182 182 if ((iRound & 1) != 0) 183 RTSha512Update(&CtxC, abDigest, sizeof(abDigest)); /* f) */183 TmplHashUpdate(&CtxC, abDigest, sizeof(abDigest)); /* f) */ 184 184 else 185 RTSha512Update(&CtxC, pabSeqP, cbSeqP); /* g) */186 187 RTSha512Final(&CtxC, abDigest); /* h) */185 TmplHashUpdate(&CtxC, pabSeqP, cbSeqP); /* g) */ 186 187 TmplHashFinal(&CtxC, abDigest); /* h) */ 188 188 } 189 189 … … 191 191 * Done. 192 192 */ 193 memcpy(pabHash, abDigest, RTSHA512_HASH_SIZE);193 memcpy(pabHash, abDigest, TMPL_HASH_SIZE); 194 194 195 195 /* 196 196 * Cleanup. 197 197 */ 198 RTMemWipeThoroughly(abDigestTemp, RTSHA512_HASH_SIZE, 3);198 RTMemWipeThoroughly(abDigestTemp, TMPL_HASH_SIZE, 3); 199 199 RTMemWipeThoroughly(pabSeqP, cbSeqP, 3); 200 200 RTMemTmpFree(pabSeqP); … … 210 210 211 211 212 RTR3DECL(int) RTCrShaCrypt 512ToString(uint8_t const pabHash[RTSHA512_HASH_SIZE], const char *pszSalt, uint32_t cRounds,213 char *pszString, size_t cbString)212 RTR3DECL(int) RTCrShaCryptTmplToString(uint8_t const pabHash[TMPL_HASH_SIZE], const char *pszSalt, uint32_t cRounds, 213 char *pszString, size_t cbString) 214 214 { 215 215 /* … … 237 237 } 238 238 239 size_t const cchNeeded = sizeof( RT_SHACRYPT_ID_STR_512) - 1239 size_t const cchNeeded = sizeof(TMPL_SHACRYPT_ID_STR) - 1 240 240 + (cRounds != RT_SHACRYPT_ROUNDS_DEFAULT ? cchRounds + sizeof("rounds=$") - 1 : 0) 241 241 + cchSalt + 1 242 + RTSHA512_HASH_SIZE * 4 / 3242 + TMPL_HASH_SIZE * 4 / 3 243 243 + 1; 244 244 AssertReturn(cbString > cchNeeded, VERR_BUFFER_OVERFLOW); … … 247 247 * Do the formatting. 248 248 */ 249 memcpy(pszString, RT_STR_TUPLE( RT_SHACRYPT_ID_STR_512));250 size_t off = sizeof( RT_SHACRYPT_ID_STR_512) - 1;249 memcpy(pszString, RT_STR_TUPLE(TMPL_SHACRYPT_ID_STR)); 250 size_t off = sizeof(TMPL_SHACRYPT_ID_STR) - 1; 251 251 252 252 if (cRounds != RT_SHACRYPT_ROUNDS_DEFAULT) … … 264 264 pszString[off++] = '$'; 265 265 266 #if TMPL_HASH_BITS == 512 266 267 BASE64_ENCODE(pszString, off, pabHash[ 0], pabHash[21], pabHash[42], 4); 267 268 BASE64_ENCODE(pszString, off, pabHash[22], pabHash[43], pabHash[ 1], 4); … … 287 288 BASE64_ENCODE(pszString, off, 0, 0, pabHash[63], 2); 288 289 290 #elif TMPL_HASH_BITS == 256 291 BASE64_ENCODE(pszString, off, pabHash[00], pabHash[10], pabHash[20], 4); 292 BASE64_ENCODE(pszString, off, pabHash[21], pabHash[ 1], pabHash[11], 4); 293 BASE64_ENCODE(pszString, off, pabHash[12], pabHash[22], pabHash[ 2], 4); 294 BASE64_ENCODE(pszString, off, pabHash[ 3], pabHash[13], pabHash[23], 4); 295 BASE64_ENCODE(pszString, off, pabHash[24], pabHash[ 4], pabHash[14], 4); 296 BASE64_ENCODE(pszString, off, pabHash[15], pabHash[25], pabHash[ 5], 4); 297 BASE64_ENCODE(pszString, off, pabHash[ 6], pabHash[16], pabHash[26], 4); 298 BASE64_ENCODE(pszString, off, pabHash[27], pabHash[ 7], pabHash[17], 4); 299 BASE64_ENCODE(pszString, off, pabHash[18], pabHash[28], pabHash[ 8], 4); 300 BASE64_ENCODE(pszString, off, pabHash[ 9], pabHash[19], pabHash[29], 4); 301 BASE64_ENCODE(pszString, off, 0, pabHash[31], pabHash[30], 3); 302 303 #else 304 # error "TMPL_HASH_BITS" 305 #endif 306 289 307 pszString[off] = '\0'; 290 308 Assert(off < cbString); … … 293 311 } 294 312 313 314 #undef TMPL_HASH_BITS 315 #undef TMPL_HASH_SIZE 316 #undef TMPL_HASH_CONTEXT_T 317 #undef TmplHashInit 318 #undef TmplHashUpdate 319 #undef TmplHashFinal 320 #undef TMPL_SHACRYPT_ID_STR 321 #undef RTCrShaCryptTmpl 322 #undef RTCrShaCryptTmplEx 323 #undef RTCrShaCryptTmplToString 324 325 -
trunk/src/VBox/Runtime/common/crypto/shacrypt.cpp
r102488 r102495 128 128 } 129 129 130 #include "shacrypt-256.cpp.h" 131 #include "shacrypt-512.cpp.h" 130 131 /* 132 * The algorithm for the SHA-256 and SHA-512 encryption is identical, except for 133 * how the bytes are distributed in the final step. So we use a pre-processor 134 * code template for the implementation. 135 */ 136 137 /* SHA-256*/ 138 #define TMPL_HASH_BITS 256 139 #define TMPL_HASH_SIZE RTSHA256_HASH_SIZE 140 #define TMPL_HASH_CONTEXT_T RTSHA256CONTEXT 141 #define TmplHashInit RTSha256Init 142 #define TmplHashUpdate RTSha256Update 143 #define TmplHashFinal RTSha256Final 144 #define TMPL_SHACRYPT_ID_STR RT_SHACRYPT_ID_STR_256 145 #define RTCrShaCryptTmpl RTCrShaCrypt256 146 #define RTCrShaCryptTmplEx RTCrShaCrypt256Ex 147 #define RTCrShaCryptTmplToString RTCrShaCrypt256ToString 148 #include "shacrypt-tmpl.cpp.h" 149 150 /* SHA-512*/ 151 #define TMPL_HASH_BITS 512 152 #define TMPL_HASH_SIZE RTSHA512_HASH_SIZE 153 #define TMPL_HASH_CONTEXT_T RTSHA512CONTEXT 154 #define TmplHashInit RTSha512Init 155 #define TmplHashUpdate RTSha512Update 156 #define TmplHashFinal RTSha512Final 157 #define TMPL_SHACRYPT_ID_STR RT_SHACRYPT_ID_STR_512 158 #define RTCrShaCryptTmpl RTCrShaCrypt512 159 #define RTCrShaCryptTmplEx RTCrShaCrypt512Ex 160 #define RTCrShaCryptTmplToString RTCrShaCrypt512ToString 161 #include "shacrypt-tmpl.cpp.h" 132 162 133 163
Note:
See TracChangeset
for help on using the changeset viewer.