openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rng-tools/6.7-r0/git/rngd_entsource.c
int xread_tpm(void* buf, size_t size, struct rng* ent_src)
{
    size_t bytes_read = 0;
    ssize_t r;
    int retval;
    unsigned char* temp_buf = NULL;
    unsigned char rng_cmd[] = {
        0, 193,        /* TPM_TAG_RQU_COMMAND */
        0, 0,   0, 14, /* length */
        0, 0,   0, 70, /* TPM_ORD_GetRandom */
        0, 0,   0, 0,  /* number of bytes to return */
    };
    char* offset;

    ent_src->rng_fd = open(ent_src->rng_fname, O_RDWR);
    if (ent_src->rng_fd == -1)
    {
        message(LOG_DAEMON | LOG_DEBUG, "Unable to open file: %s",
                ent_src->rng_fname);
        return -1;
    }

    temp_buf = (unsigned char*)malloc(size + TPM_GET_RNG_OVERHEAD);
    memset(temp_buf, 0, (size + TPM_GET_RNG_OVERHEAD));
    if (temp_buf == NULL)
    {
        message(LOG_ERR | LOG_INFO, "No memory");
        close(ent_src->rng_fd);
        return -1;
    }
    /* 32 bits has been reserved for random byte size */
    rng_cmd[13] = (unsigned char)(size & 0xFF);
    rng_cmd[12] = (unsigned char)((size >> 8) & 0xFF);
    rng_cmd[11] = (unsigned char)((size >> 16) & 0xFF);
    rng_cmd[10] = (unsigned char)((size >> 24) & 0xFF);
    offset = buf;
    while (bytes_read < size)
    {
        r = 0;
        while (r < sizeof(rng_cmd))
        {
            retval = write(ent_src->rng_fd, rng_cmd + r, sizeof(rng_cmd) - r);
            if (retval < 0)
            {
                message(LOG_ERR | LOG_INFO, "Error writing %s\n",
                        ent_src->rng_name);
                retval = -1;
                goto error_out;
            }
            r += retval;
        }
        if (r < sizeof(rng_cmd))
        {
            message(LOG_ERR | LOG_INFO, "Error writing %s\n",
                    ent_src->rng_name);
            retval = -1;
            goto error_out;
        }
        r = read(ent_src->rng_fd, temp_buf, size);
        r = (r - TPM_GET_RNG_OVERHEAD);
        if (r <= 0)
        {
            message(LOG_ERR | LOG_INFO,
                    "Error reading from TPM, no entropy gathered");
            retval = -1;
            goto error_out;
        }
        bytes_read = bytes_read + r;
        if (bytes_read > size)
        {
            memcpy(offset, temp_buf + TPM_GET_RNG_OVERHEAD,
                   r - (bytes_read - size));
            break;
        }
        memcpy(offset, temp_buf + TPM_GET_RNG_OVERHEAD, r);
        offset = offset + r;
    }
    retval = 0;
error_out:
    close(ent_src->rng_fd);
    free(temp_buf);
    return retval;
}
/* tpm rng read call to kernel has 13 bytes of overhead
 * the logic to process this involves reading to a temporary_buf
 * and copying the no generated to buf */
tpm rng read call to kernel has 13 bytes of overhead
the logic to process this involves reading to a temporary_buf
and copying the no generated to buf
tpm rng read call to kernel has 13 bytes of overhead the logic to process this involves reading to a temporary_buf and copying the no generated to buf
int xread_tpm(void* buf, size_t size, struct rng* ent_src)
{
    size_t bytes_read = 0;
    ssize_t r;
    int retval;
    unsigned char* temp_buf = NULL;
    unsigned char rng_cmd[] = {
        0, 193,        
        0, 0,   0, 14, 
        0, 0,   0, 70, 
        0, 0,   0, 0,  
    };
    char* offset;
    ent_src->rng_fd = open(ent_src->rng_fname, O_RDWR);
    if (ent_src->rng_fd == -1)
    {
        message(LOG_DAEMON | LOG_DEBUG, "Unable to open file: %s",
                ent_src->rng_fname);
        return -1;
    }
    temp_buf = (unsigned char*)malloc(size + TPM_GET_RNG_OVERHEAD);
    memset(temp_buf, 0, (size + TPM_GET_RNG_OVERHEAD));
    if (temp_buf == NULL)
    {
        message(LOG_ERR | LOG_INFO, "No memory");
        close(ent_src->rng_fd);
        return -1;
    }
    rng_cmd[13] = (unsigned char)(size & 0xFF);
    rng_cmd[12] = (unsigned char)((size >> 8) & 0xFF);
    rng_cmd[11] = (unsigned char)((size >> 16) & 0xFF);
    rng_cmd[10] = (unsigned char)((size >> 24) & 0xFF);
    offset = buf;
    while (bytes_read < size)
    {
        r = 0;
        while (r < sizeof(rng_cmd))
        {
            retval = write(ent_src->rng_fd, rng_cmd + r, sizeof(rng_cmd) - r);
            if (retval < 0)
            {
                message(LOG_ERR | LOG_INFO, "Error writing %s\n",
                        ent_src->rng_name);
                retval = -1;
                goto error_out;
            }
            r += retval;
        }
        if (r < sizeof(rng_cmd))
        {
            message(LOG_ERR | LOG_INFO, "Error writing %s\n",
                    ent_src->rng_name);
            retval = -1;
            goto error_out;
        }
        r = read(ent_src->rng_fd, temp_buf, size);
        r = (r - TPM_GET_RNG_OVERHEAD);
        if (r <= 0)
        {
            message(LOG_ERR | LOG_INFO,
                    "Error reading from TPM, no entropy gathered");
            retval = -1;
            goto error_out;
        }
        bytes_read = bytes_read + r;
        if (bytes_read > size)
        {
            memcpy(offset, temp_buf + TPM_GET_RNG_OVERHEAD,
                   r - (bytes_read - size));
            break;
        }
        memcpy(offset, temp_buf + TPM_GET_RNG_OVERHEAD, r);
        offset = offset + r;
    }
    retval = 0;
error_out:
    close(ent_src->rng_fd);
    free(temp_buf);
    return retval;
}
int xread_tpm ( void * buf , size_t size , struct rng * ent_src ) { size_t bytes_read = 0 ; ssize_t r ; int retval ; unsigned char * temp_buf = NULL ; unsigned char rng_cmd [ ] = { 0 , 193 , 0 , 0 , 0 , 14 , 0 , 0 , 0 , 70 , 0 , 0 , 0 , 0 , } ; char * offset ; ent_src -> rng_fd = open ( ent_src -> rng_fname , O_RDWR ) ; if ( ent_src -> rng_fd == -1 ) { message ( LOG_DAEMON | LOG_DEBUG , " " , ent_src -> rng_fname ) ; return -1 ; } temp_buf = ( unsigned char * ) malloc ( size + TPM_GET_RNG_OVERHEAD ) ; memset ( temp_buf , 0 , ( size + TPM_GET_RNG_OVERHEAD ) ) ; if ( temp_buf == NULL ) { message ( LOG_ERR | LOG_INFO , " " ) ; close ( ent_src -> rng_fd ) ; return -1 ; } rng_cmd [ 13 ] = ( unsigned char ) ( size & 0xFF ) ; rng_cmd [ 12 ] = ( unsigned char ) ( ( size >> 8 ) & 0xFF ) ; rng_cmd [ 11 ] = ( unsigned char ) ( ( size >> 16 ) & 0xFF ) ; rng_cmd [ 10 ] = ( unsigned char ) ( ( size >> 24 ) & 0xFF ) ; offset = buf ; while ( bytes_read < size ) { r = 0 ; while ( r < sizeof ( rng_cmd ) ) { retval = write ( ent_src -> rng_fd , rng_cmd + r , sizeof ( rng_cmd ) - r ) ; if ( retval < 0 ) { message ( LOG_ERR | LOG_INFO , " \n " , ent_src -> rng_name ) ; retval = -1 ; goto error_out ; } r += retval ; } if ( r < sizeof ( rng_cmd ) ) { message ( LOG_ERR | LOG_INFO , " \n " , ent_src -> rng_name ) ; retval = -1 ; goto error_out ; } r = read ( ent_src -> rng_fd , temp_buf , size ) ; r = ( r - TPM_GET_RNG_OVERHEAD ) ; if ( r <= 0 ) { message ( LOG_ERR | LOG_INFO , " " ) ; retval = -1 ; goto error_out ; } bytes_read = bytes_read + r ; if ( bytes_read > size ) { memcpy ( offset , temp_buf + TPM_GET_RNG_OVERHEAD , r - ( bytes_read - size ) ) ; break ; } memcpy ( offset , temp_buf + TPM_GET_RNG_OVERHEAD , r ) ; offset = offset + r ; } retval = 0 ; error_out : close ( ent_src -> rng_fd ) ; free ( temp_buf ) ; return retval ; }
tpm rng read call to kernel has 13 bytes of overhead
 the logic to process this involves reading to a temporary_buf
 and copying the no generated to buf
tpm rng read call to kernel has 13 bytes of overhead the logic to process this involves reading to a temporary_buf and copying the no generated to buf
/* TPM_TAG_RQU_COMMAND */ /* length */ /* TPM_ORD_GetRandom */ /* number of bytes to return */ /* 32 bits has been reserved for random byte size */