random_nz.c File Reference

Random non-zero bytes. More...

#include <stddef.h>
#include <stdint.h>
#include <ipxe/rbg.h>
#include <ipxe/random_nz.h>

Go to the source code of this file.


int get_random_nz (void *data, size_t len)
 Get random non-zero bytes. More...

Detailed Description

Random non-zero bytes.

The RSA algorithm requires the generation of random non-zero bytes, i.e. bytes in the range [0x01,0xff].

This algorithm is designed to comply with ANS X9.82 Part 1-2006 Section 9.2.1. This standard is not freely available, but most of the text appears to be shared with NIST SP 800-90, which can be downloaded from


Where possible, references are given to both documents. In the case of any disagreement, ANS X9.82 takes priority over NIST SP 800-90. (In particular, note that some algorithms that are Approved by NIST SP 800-90 are not Approved by ANS X9.82.)

Definition in file random_nz.c.

Function Documentation



◆ get_random_nz()

int get_random_nz ( void *  data,
size_t  len 

Get random non-zero bytes.

dataOutput buffer
lenLength of output buffer
Return values
rcReturn status code

This algorithm is designed to be isomorphic to the Simple Discard Method described in ANS X9.82 Part 1-2006 Section 9.2.1 (NIST SP 800-90 Section B.5.1.1).

Definition at line 62 of file random_nz.c.

62  {
63  uint8_t *bytes = data;
64  int rc;
66  while ( len ) {
68  /* Generate random byte */
69  if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
70  return rc;
72  /* Move to next byte if this byte is acceptable */
73  if ( *bytes != 0 ) {
74  bytes++;
75  len--;
76  }
77  }
79  return 0;
80 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int rbg_generate(const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate bits using RBG.
Definition: rbg.h:36
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint8_t bytes[64]
Definition: ib_mad.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References bytes, data, len, NULL, rbg_generate(), and rc.

Referenced by rsa_encrypt().