VirtualBox

Changeset 102335 in vbox


Ignore:
Timestamp:
Nov 27, 2023 5:01:59 PM (12 months ago)
Author:
vboxsync
Message:

IPRT/crypto/shacrypt: Added RTCrShaCryptGenerateSalt() to avoid code duplication. bugref:10551

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/crypto/shacrypt.h

    r102298 r102335  
    5959
    6060/**
     61 * Creates a randomized salt for the RTCrShaCryptXXX functions.
     62 *
     63 * @returns IPRT status code.
     64 * @param   szSalt      Where to store the generated salt.
     65 * @param   cchSalt     Number of characters the generated salt should use.
     66 *                      Must be >= RT_SHACRYPT_MIN_SALT_LEN and <= RT_SHACRYPT_MAX_SALT_LEN.
     67 */
     68RTR3DECL(int) RTCrShaCryptGenerateSalt(char szSalt[RT_SHACRYPT_MAX_SALT_LEN + 1], size_t cchSalt);
     69
     70
     71/**
    6172 * Calculates a SHAcrypt (SHA-256) digest.
    6273 *
    63  * @returns VBox status code.
     74 * @returns IPRT status code.
    6475 * @param   pszKey              Key (password) to use.
    6576 * @param   pszSalt             Salt to use.
     
    7687 * Returns a SHAcrypt (SHA-256) digest as a printable scheme.
    7788 *
    78  * @returns VBox status code.
     89 * @returns IPRT status code.
    7990 * @param   abHash              SHAcrypt (SHA-256) digest to return printable scheme for.
    8091 * @param   pszSalt             Salt to use. Must match the salt used when generating \a pabHash via RTSha256Crypt().
     
    92103 * Calculates a SHAcrypt (SHA-512) digest.
    93104 *
    94  * @returns VBox status code.
     105 * @returns IPRT status code.
    95106 * @param   pszKey              Key (password) to use.
    96107 * @param   pszSalt             Salt to use.
     
    107118 * Returns a SHAcrypt (SHA-512) digest as a printable scheme.
    108119 *
    109  * @returns VBox status code.
     120 * @returns IPRT status code.
    110121 * @param   abHash              SHAcrypt (SHA-512) digest to return printable scheme for.
    111122 * @param   pszSalt             Salt to use. Must match the salt used when generating \a pabHash via RTSha512Crypt().
  • trunk/include/iprt/mangling.h

    r102296 r102335  
    37343734# define RTCrPkixCanCertHandleDigestType                RT_MANGLER(RTCrPkixCanCertHandleDigestType)
    37353735# define RTCrRandBytes                                  RT_MANGLER(RTCrRandBytes)
     3736# define RTCrShaCryptGenerateSalt                       RT_MANGLER(RTCrShaCryptGenerateSalt)
    37363737# define RTCrShaCrypt256                                RT_MANGLER(RTCrShaCrypt256)
    37373738# define RTCrShaCrypt256ToString                        RT_MANGLER(RTCrShaCrypt256ToString)
  • trunk/src/VBox/Runtime/common/crypto/shacrypt.cpp

    r102297 r102335  
    4343#include <iprt/types.h>
    4444#include <iprt/mem.h>
     45#include <iprt/rand.h>
    4546#include <iprt/sha.h>
    4647#include <iprt/string.h>
    4748
     49
     50
     51RTR3DECL(int) RTCrShaCryptGenerateSalt(char szSalt[RT_SHACRYPT_MAX_SALT_LEN + 1], size_t cchSalt)
     52{
     53    AssertMsgReturn(cchSalt >= RT_SHACRYPT_MIN_SALT_LEN && cchSalt <= RT_SHACRYPT_MAX_SALT_LEN, ("len=%zu\n", cchSalt),
     54                    VERR_INVALID_PARAMETER);
     55
     56    static const char aRange[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890./";
     57    for (size_t i = 0; i < cchSalt; i++)
     58        szSalt[i] = aRange[RTRandU32Ex(0, sizeof(aRange) - 2)];
     59
     60    szSalt[RT_SHACRYPT_MAX_SALT_LEN] = '\0';
     61    return VINF_SUCCESS;
     62}
    4863
    4964
  • trunk/src/VBox/Runtime/testcase/tstRTShaCrypt.cpp

    r102297 r102335  
    191191        else
    192192        {
    193             static const char aRange[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?+\"%&/()[]{}=#";
    194             for (unsigned s = 0; s < RT_SHACRYPT_MAX_SALT_LEN; s++) /* Always go with a strong salt by default. */
    195                 szSalt[s] = aRange[RTRandU32Ex(0, sizeof(aRange) - 2)];
     193            rc = RTCrShaCryptGenerateSalt(szSalt, RT_SHACRYPT_MAX_SALT_LEN);
     194            RTTEST_CHECK_RC_OK(hTest, rc);
    196195            pszSalt = szSalt;
    197196        }
  • trunk/src/VBox/Runtime/tools/RTMkPasswd.cpp

    r102297 r102335  
    164164    if (!pszSalt)
    165165    {
    166         static const char aRange[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?+\"%&/()[]{}=#";
    167         for (unsigned i = 0; i < RT_SHACRYPT_MAX_SALT_LEN; i++) /* Always go with a strong salt by default. */
    168             szSalt[i] = aRange[RTRandU32Ex(0, sizeof(aRange) - 2)];
     166        int vrc2 = RTCrShaCryptGenerateSalt(szSalt, RT_SHACRYPT_MAX_SALT_LEN);
     167        AssertRCReturn(vrc2, RTEXITCODE_FAILURE);
    169168        pszSalt = szSalt;
    170169    }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette