MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / sys_getrandom

Function sys_getrandom

components/lwp/lwp_syscall.c:8401–8465  ·  view source on GitHub ↗

* @brief Get random data from the kernel's random number generator. * * This function retrieves cryptographically secure random data from the kernel's random number generator * and stores it in the buffer provided by the user. The data can be used for cryptographic operations or * other applications requiring randomization. * * @param[out] buf A pointer to the buffer where the random dat

Source from the content-addressed store, hash-verified

8399 * @see sys_random(), sys_getentropy()
8400 */
8401sysret_t sys_getrandom(void *buf, size_t buflen, unsigned int flags)
8402{
8403 int ret = -1;
8404 int count = 0;
8405 void *kmem = RT_NULL;
8406 rt_device_t rd_dev = RT_NULL;
8407
8408 if (flags & GRND_RANDOM)
8409 rd_dev = rt_device_find("random");
8410 else
8411 rd_dev = rt_device_find("urandom");
8412
8413 if (rd_dev == RT_NULL)
8414 {
8415 return -EFAULT;
8416 }
8417
8418 if (rt_device_open(rd_dev, RT_DEVICE_OFLAG_RDONLY) != RT_EOK)
8419 {
8420 return -EFAULT;
8421 }
8422
8423 if (!lwp_user_accessable(buf, buflen))
8424 {
8425 rt_device_close(rd_dev);
8426 return -EFAULT;
8427 }
8428
8429#ifdef ARCH_MM_MMU
8430 kmem = kmem_get(buflen);
8431 if (!kmem)
8432 {
8433 rt_device_close(rd_dev);
8434 return -ENOMEM;
8435 }
8436
8437 while (count < buflen)
8438 {
8439 ret = rt_device_read(rd_dev, count, (char *)kmem + count, buflen - count);
8440 if (ret <= 0)
8441 break;
8442 count += ret;
8443 }
8444 rt_device_close(rd_dev);
8445
8446 ret = count;
8447 if (count > 0)
8448 {
8449 ret = lwp_put_to_user(buf, kmem, count);
8450 }
8451 kmem_put(kmem);
8452#else
8453 while (count < buflen)
8454 {
8455 ret = rt_device_read(rd_dev, count, (char *)kmem + count, buflen - count);
8456 if (ret <= 0)
8457 break;
8458 count += ret;

Callers

nothing calls this directly

Calls 8

rt_device_findFunction · 0.85
rt_device_openFunction · 0.85
lwp_user_accessableFunction · 0.85
rt_device_closeFunction · 0.85
kmem_getFunction · 0.85
rt_device_readFunction · 0.85
lwp_put_to_userFunction · 0.85
kmem_putFunction · 0.85

Tested by

no test coverage detected