Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions ports/cxd56/common-hal/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
//
// SPDX-License-Identifier: MIT

#include <stdlib.h>

#include "genhdr/mpversion.h"
#include "py/objstr.h"
#include "py/objtuple.h"

// The CXD5602 has no true hardware random number generator wired up in this
// port. This function used to fill the buffer from the C library rand(), a
// deterministic PRNG, and return true -- meaning os.urandom() handed back
// predictable bytes while presenting itself as a cryptographically secure
// source. That is the same class of defect behind the 2026 Coldcard wallet
// key-recovery incident, where a silent PRNG fallback stood in for hardware
// entropy.
Comment on lines +15 to +17
//
// Fail closed instead: report that no secure entropy is available, so that
// os.urandom() raises NotImplementedError ("No hardware random available")
// rather than silently returning non-random data. This matches the broadcom,
// silabs and litex ports, which also lack a hardware RNG.
//
// The correct long-term fix is to wire a real hardware entropy source for the
// CXD5602 if the silicon exposes one; that requires the chip and datasheet and
// is left as a follow-up.
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length) {
uint32_t i = 0;

while (i < length) {
uint32_t new_random = rand();
for (int j = 0; j < 4 && i < length; j++) {
buffer[i] = new_random & 0xff;
i++;
new_random >>= 8;
}
}

return true;
(void)buffer;
(void)length;
return false;
}
Loading