MCPcopy Create free account
hub / github.com/BernardoGiordano/Checkpoint / randomBytes

Method randomBytes

3ds/source/script/scripthost.cpp:495–532  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

493 }
494
495 bool randomBytes(void* out, size_t size) override
496 {
497 // ps:ps owns the hardware RNG. Opened and closed around the call
498 // rather than held: this runs a handful of times per script run.
499 if (R_SUCCEEDED(psInit())) {
500 const Result res = PS_GenerateRandomBytes(out, size);
501 psExit();
502 if (R_SUCCEEDED(res)) {
503 return true;
504 }
505 }
506 // ps unavailable. Every seal draws a fresh salt as well as a fresh
507 // nonce, and the salt is what the key is derived from, so what this
508 // fallback has to guarantee is not secrecy but that two seals never
509 // come out with the same pair — a tick counter that never repeats
510 // within a boot, hashed with a call counter, does that.
511 static u32 calls = 0;
512 uint8_t* dest = (uint8_t*)out;
513 while (size > 0) {
514 const u64 tick = svcGetSystemTick();
515 const u64 now = osGetTime();
516 const u32 seq = calls++;
517 uint8_t digest[32];
518 mbedtls_sha256_context ctx;
519 mbedtls_sha256_init(&ctx);
520 mbedtls_sha256_starts_ret(&ctx, 0);
521 mbedtls_sha256_update_ret(&ctx, (const unsigned char*)&tick, sizeof(tick));
522 mbedtls_sha256_update_ret(&ctx, (const unsigned char*)&now, sizeof(now));
523 mbedtls_sha256_update_ret(&ctx, (const unsigned char*)&seq, sizeof(seq));
524 mbedtls_sha256_finish_ret(&ctx, digest);
525 mbedtls_sha256_free(&ctx);
526 const size_t chunk = size < sizeof(digest) ? size : sizeof(digest);
527 memcpy(dest, digest, chunk);
528 dest += chunk;
529 size -= chunk;
530 }
531 return true;
532 }
533
534 void lowerPriority(void) override
535 {

Callers 1

ckpt_device_sealFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected