nop
nop

Reputation: 6341

Making a function use std::span instead of the old way

I just learned that I could use VS 2019 with C++ 20 and I'm trying to make use of it. I'm trying to make the following function use std::span, because that way data_size and key_size would be redundant.

Everything is alright except *data and data++. What is it supposed to be?

int rc4(rc4_context* context, const std::uint8_t* data, const std::size_t data_size, const std::uint8_t* key, const std::size_t key_size, std::uint8_t* output)
{
    std::uint32_t i, j;

    // Check parameters
    if (!context || !key)
        return ERROR_INVALID_PARAMETER;

    // Clear context
    context->i = 0;
    context->j = 0;

    // Initialize the S array with identity permutation
    for (i = 0; i < 256; i++)
    {
        context->s[i] = static_cast<std::uint8_t>(i);
    }

    // S is then processed for 256 iterations
    for (i = 0, j = 0; i < 256; i++)
    {
        // Randomize the permutations using the supplied key
        j = (j + context->s[i] + key[i % key_size]) % 256;

        // Swap the values of S[i] and S[j]
        const auto temp = context->s[i];
        context->s[i] = context->s[j];
        context->s[j] = temp;
    }

    // Restore context
    i = context->i;
    j = context->j;
    auto* s = context->s;

    // Encryption loop
    for (size_t x = 0; x < data_size; ++x)
    {
        // Adjust indices
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;

        // Swap the values of S[i] and S[j]
        const auto temp = s[i];
        s[i] = s[j];
        s[j] = temp;

        // Valid input and output?
        if (data && output)
        {
            // XOR the input data with the RC4 stream
            *output = *data ^ s[(s[i] + s[j]) % 256];

            // Increment data pointers
            data++;
            output++;
        }
    }

    // Save context
    context->i = i;
    context->j = j;

    return NO_ERROR;
}

My attempt

int rc4(rc4_context* context, const std::span<uint8_t*> data, const std::span<std::uint8_t*> key, std::uint8_t* output)
{
    // INITIALIZATION
    std::uint32_t i, j;

    // Check parameters
    if (!context || !key.empty())
        return ERROR_INVALID_PARAMETER;

    // Clear context
    context->i = 0;
    context->j = 0;

    // Initialize the S array with identity permutation
    for (i = 0; i < 256; i++)
    {
        context->s[i] = static_cast<std::uint8_t>(i);
    }

    // S is then processed for 256 iterations
    for (i = 0, j = 0; i < 256; i++)
    {
        // Randomize the permutations using the supplied key
        j = (j + context->s[i] + key[i % key.size()]) % 256;

        // Swap the values of S[i] and S[j]
        const auto temp = context->s[i];
        context->s[i] = context->s[j];
        context->s[j] = temp;
    }

    // MAIN LOGIC PART
    // Restore context
    i = context->i;
    j = context->j;
    auto* s = context->s;

    // Encryption loop
    for (size_t x = 0; x < data.size(); ++x)
    {
        // Adjust indices
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;

        // Swap the values of S[i] and S[j]
        const auto temp = s[i];
        s[i] = s[j];
        s[j] = temp;

        // Valid input and output?
        if (data.empty() && output)
        {
            // XOR the input data with the RC4 stream
            *output = *data ^ s[(s[i] + s[j]) % 256];

            // Increment data pointers
            data++;
            output++;
        }
    }

    // Save context
    context->i = i;
    context->j = j;

    return NO_ERROR;
}

Upvotes: 0

Views: 407

Answers (1)

Barry
Barry

Reputation: 303216

First, the pair of parameters (const std::uint8_t* data, const std::size_t data_size) can be replaced by std::span<uint8_t>, not std::span<uint8_t*>.

Second, you don't need to bother incrementing data since you can rewrite this to be a range-based for loop:

for (uint8_t elem : data) {
    // Adjust indices
    i = (i + 1) % 256;
    j = (j + s[i]) % 256;    

    // Swap the values of S[i] and S[j]
    std::swap(s[i], s[j]);

    // Valid output?
    if (output) {
        // XOR the input data with the RC4 stream
        *output++ = elem ^ s[(s[i] + s[j]) % 256];
    }   
}

Upvotes: 5

Related Questions