cybersecurity · beginner · ~10 min
Match entropy source to the use case.
Match each use case to the right entropy source. Picking rand() for anything security-sensitive is the root of many CVEs.
Implement int recommend_entropy_source(int use_case) that maps a use-case code to a recommended source code.
Source codes:
0 = rand() — fast, deterministic, NOT for security.2 = getrandom(2) — Linux 3.17+, preferred for security (no fd needed).Use cases:
0 = Bingo number for a game UI1 = session token for a web server2 = cryptographic nonce3 = filling a debug buffer with garbage4 = CSRF tokenuse_case: one of the integers above. The grader passes fixed values.Returns int:
0 for cases 0 and 3 (non-security).2 for cases 1, 2, and 4 (security — prefer getrandom).-1 for any unknown use case.recommend_entropy_source(0) -> 0 (game)
recommend_entropy_source(1) -> 2 (session token)
recommend_entropy_source(4) -> 2 (CSRF token)
recommend_entropy_source(99) -> -1 (unknown)
rand()/srand(time(NULL)) is the entropy source used in too many security CVEs. Choosing the right call matters.
An integer use_case (0..4 are defined).
An int: 0 (non-security), 2 (security/getrandom), or -1 (unknown).
Decision table only — no real syscalls.
int recommend_entropy_source(int use_case) { /* TODO */ (void)use_case; return -1; }
Using rand() for security. Using /dev/urandom when getrandom is available.
Unknown use_case.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.