Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior in crypto random routines #34

Open
0ne-bey0nd opened this issue Mar 1, 2024 · 0 comments
Open

Strange behavior in crypto random routines #34

0ne-bey0nd opened this issue Mar 1, 2024 · 0 comments

Comments

@0ne-bey0nd
Copy link

0ne-bey0nd commented Mar 1, 2024

the code

#include "ENCRYPTO_utils/constants.h"
#include "ENCRYPTO_utils/typedefs.h"
#include <ENCRYPTO_utils/crypto/crypto.h>
#include <cstdint>
#include <iostream>
void print_bytes(uint8_t *bytes, uint32_t num_bytes) {
    for (uint32_t i = 0; i < num_bytes; i++) {
        std::cout << (int)bytes[i] << " ";
    }
    std::cout << std::endl;
}

#define RANDOM_BYTES_NUM 16

int main() {
    uint8_t random_bytes[RANDOM_BYTES_NUM];
    uint8_t seed = 42;
    seclvl secure_level = ST;

    uint32_t random_bytes_num = RANDOM_BYTES_NUM;
    crypto crypto_obj(secure_level.symbits, &seed);

    crypto_obj.gen_rnd(random_bytes, random_bytes_num);
    print_bytes(random_bytes, random_bytes_num);

    std::cout << "seed: " << (int)seed << std::endl;
    std ::cout << "random_bytes_num: " << random_bytes_num << std::endl;

    crypto_obj.gen_rnd_from_seed(random_bytes, random_bytes_num, &seed);
    print_bytes(random_bytes, random_bytes_num);
}

Output

152 227 184 124 228 237 196 24 218 98 124 189 49 97 171 171 
seed: 42
random_bytes_num: 16
152 227 184 124 228 237 196 24 218 98 124 189 49 97 171 171 

Now I just exchange the sequence of these two lines like :

    uint32_t random_bytes_num = RANDOM_BYTES_NUM;
    crypto crypto_obj(secure_level.symbits, &seed);

to

    uint32_t random_bytes_num = RANDOM_BYTES_NUM;
    crypto crypto_obj(secure_level.symbits, &seed);

so the code is

#include "ENCRYPTO_utils/constants.h"
#include "ENCRYPTO_utils/typedefs.h"
#include <ENCRYPTO_utils/crypto/crypto.h>
#include <cstdint>
#include <iostream>
void print_bytes(uint8_t *bytes, uint32_t num_bytes) {
    for (uint32_t i = 0; i < num_bytes; i++) {
        std::cout << (int)bytes[i] << " ";
    }
    std::cout << std::endl;
}

#define RANDOM_BYTES_NUM 16

int main() {
    uint8_t random_bytes[RANDOM_BYTES_NUM];
    uint8_t seed = 42;
    seclvl secure_level = ST;

    crypto crypto_obj(secure_level.symbits, &seed);
    uint32_t random_bytes_num = RANDOM_BYTES_NUM;
    crypto_obj.gen_rnd(random_bytes, random_bytes_num);
    print_bytes(random_bytes, random_bytes_num);

    std::cout << "seed: " << (int)seed << std::endl;
    std ::cout << "random_bytes_num: " << random_bytes_num << std::endl;

    crypto_obj.gen_rnd_from_seed(random_bytes, random_bytes_num, &seed);
    print_bytes(random_bytes, random_bytes_num);
}

now the strange things have happened, the output turns to

236 115 137 45 80 65 119 233 123 140 77 2 95 172 158 251 
seed: 42
random_bytes_num: 16
152 227 184 124 228 237 196 24 218 98 124 189 49 97 171 171

It does confuse me and I need help 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant