1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Copyright (C) 2005-2010 Freescale Semiconductor, Inc. All Rights Reserved.
*/
/*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
/*!
* @file fsl_shw_rand.c
*
* This file implements Random Number Generation functions of the FSL SHW API
* in USER MODE for talking to a standalone RNGA/RNGC device driver.
*
* It contains the fsl_shw_get_random() and fsl_shw_add_entropy() functions.
*
* These routines will build a request block and pass it to the SHW driver.
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <signal.h>
#ifdef FSL_DEBUG
#include <stdio.h>
#include <errno.h>
#include <string.h>
#endif /* FSL_DEBUG */
#include "shw_driver.h"
extern fsl_shw_return_t validate_uco(fsl_shw_uco_t * uco);
#if defined(FSL_HAVE_RNGA) || defined(FSL_HAVE_RNGB) || defined(FSL_HAVE_RNGC)
/* REQ-S2LRD-PINTFC-API-BASIC-RNG-002 */
fsl_shw_return_t fsl_shw_get_random(fsl_shw_uco_t * user_ctx,
uint32_t length, uint8_t * data)
{
fsl_shw_return_t ret = FSL_RETURN_ERROR_S;
/* perform a sanity check / update uco */
ret = validate_uco(user_ctx);
if (ret == FSL_RETURN_OK_S) {
struct get_random_req *req = malloc(sizeof(*req));
if (req == NULL) {
ret = FSL_RETURN_NO_RESOURCE_S;
} else {
init_req(&req->hdr, user_ctx);
req->size = length;
req->random = data;
ret =
send_req(SHW_USER_REQ_GET_RANDOM, &req->hdr,
user_ctx);
}
}
return ret;
}
fsl_shw_return_t fsl_shw_add_entropy(fsl_shw_uco_t * user_ctx,
uint32_t length, uint8_t * data)
{
fsl_shw_return_t ret = FSL_RETURN_ERROR_S;
/* perform a sanity check on the uco */
ret = validate_uco(user_ctx);
if (ret == FSL_RETURN_OK_S) {
struct add_entropy_req *req = malloc(sizeof(*req));
if (req == NULL) {
ret = FSL_RETURN_NO_RESOURCE_S;
} else {
init_req(&req->hdr, user_ctx);
req->size = length;
req->entropy = data;
ret =
send_req(SHW_USER_REQ_ADD_ENTROPY, &req->hdr,
user_ctx);
}
}
return ret;
}
#else /* no H/W RNG block */
fsl_shw_return_t fsl_shw_get_random(fsl_shw_uco_t * user_ctx,
uint32_t length, uint8_t * data)
{
(void)user_ctx;
(void)length;
(void)data;
return FSL_RETURN_ERROR_S;
}
fsl_shw_return_t fsl_shw_add_entropy(fsl_shw_uco_t * user_ctx,
uint32_t length, uint8_t * data)
{
(void)user_ctx;
(void)length;
(void)data;
return FSL_RETURN_ERROR_S;
}
#endif
|