Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /********************************************************************* |
2 | * |
||
3 | * Random Number Generator |
||
4 | * Library for Microchip TCP/IP Stack |
||
5 | * - Provides a cryptographically secure method for generating |
||
6 | * random data |
||
7 | * |
||
8 | ********************************************************************* |
||
9 | * FileName: Random.c |
||
10 | * Dependencies: StackTsk.c |
||
11 | * Tick.c |
||
12 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
13 | * Compiler: Microchip C32 v1.05 or higher |
||
14 | * Microchip C30 v3.12 or higher |
||
15 | * Microchip C18 v3.30 or higher |
||
16 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
17 | * Company: Microchip Technology, Inc. |
||
18 | * |
||
19 | * Software License Agreement |
||
20 | * |
||
21 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
22 | * reserved. |
||
23 | * |
||
24 | * Microchip licenses to you the right to use, modify, copy, and |
||
25 | * distribute: |
||
26 | * (i) the Software when embedded on a Microchip microcontroller or |
||
27 | * digital signal controller product ("Device") which is |
||
28 | * integrated into Licensee's product; or |
||
29 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
30 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
31 | * used in conjunction with a Microchip ethernet controller for |
||
32 | * the sole purpose of interfacing with the ethernet controller. |
||
33 | * |
||
34 | * You should refer to the license agreement accompanying this |
||
35 | * Software for additional information regarding your rights and |
||
36 | * obligations. |
||
37 | * |
||
38 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
39 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
40 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
41 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
42 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
43 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
44 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
45 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
46 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
47 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
48 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
49 | * |
||
50 | * |
||
51 | * Author Date Comment |
||
52 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
53 | * Elliott Wood 5/09/07 Original (Rev 1.0) |
||
54 | ********************************************************************/ |
||
55 | |||
56 | #define __RANDOM_C |
||
57 | |||
58 | #include "TCPIPConfig.h" |
||
59 | |||
60 | #if defined(STACK_USE_SSL_SERVER) || defined(STACK_USE_SSL_CLIENT) |
||
61 | |||
62 | #include "TCPIP Stack/TCPIP.h" |
||
63 | |||
64 | static HASH_SUM randHash; |
||
65 | static BYTE output[20]; |
||
66 | static BYTE bCount; |
||
67 | |||
68 | /********************************************************************* |
||
69 | * Function: void RandomInit(void) |
||
70 | * |
||
71 | * PreCondition: None |
||
72 | * |
||
73 | * Input: None |
||
74 | * |
||
75 | * Output: Random number generator is initialized. |
||
76 | * |
||
77 | * Side Effects: None |
||
78 | * |
||
79 | * Overview: Sets up the random generator structure. |
||
80 | * |
||
81 | * Note: Data may not be secure until several packets have |
||
82 | * been received. |
||
83 | ********************************************************************/ |
||
84 | void RandomInit(void) |
||
85 | { |
||
86 | unsigned char i; |
||
87 | unsigned long dw; |
||
88 | |||
89 | SHA1Initialize(&randHash); |
||
90 | |||
91 | // Add some starting entropy to the pool. This is slow. |
||
92 | for(i = 0; i < 5; i++) |
||
93 | { |
||
94 | dw = GenerateRandomDWORD(); |
||
95 | RandomAdd(((BYTE*)&dw)[0]); |
||
96 | RandomAdd(((BYTE*)&dw)[1]); |
||
97 | RandomAdd(((BYTE*)&dw)[2]); |
||
98 | RandomAdd(((BYTE*)&dw)[3]); |
||
99 | } |
||
100 | |||
101 | bCount = 20; |
||
102 | } |
||
103 | |||
104 | /********************************************************************* |
||
105 | * Function: BYTE RandomGet(void) |
||
106 | * |
||
107 | * PreCondition: None |
||
108 | * |
||
109 | * Input: None |
||
110 | * |
||
111 | * Output: A random byte is generated |
||
112 | * |
||
113 | * Side Effects: None |
||
114 | * |
||
115 | * Overview: None |
||
116 | * |
||
117 | * Note: None |
||
118 | ********************************************************************/ |
||
119 | BYTE RandomGet(void) |
||
120 | { |
||
121 | if(bCount >= 20u) |
||
122 | {//we need to get new random bytes |
||
123 | SHA1Calculate(&randHash, output); |
||
124 | RandomAdd(output[0]); |
||
125 | bCount = 0; |
||
126 | } |
||
127 | |||
128 | //return the random byte |
||
129 | return output[bCount++]; |
||
130 | } |
||
131 | |||
132 | |||
133 | /********************************************************************* |
||
134 | * Function: void RandomAdd(BYTE data) |
||
135 | * |
||
136 | * PreCondition: None |
||
137 | * |
||
138 | * Input: a random byte to add to the seed |
||
139 | * |
||
140 | * Output: None |
||
141 | * |
||
142 | * Side Effects: None |
||
143 | * |
||
144 | * Overview: Hashes the byte and an asynchronous timer value |
||
145 | * |
||
146 | * Note: None |
||
147 | ********************************************************************/ |
||
148 | void RandomAdd(BYTE data) |
||
149 | { |
||
150 | DWORD dTemp; |
||
151 | |||
152 | SHA1AddData(&randHash, &data, 1); |
||
153 | dTemp = TickGet(); |
||
154 | SHA1AddData(&randHash, (BYTE*)&dTemp, 1); |
||
155 | |||
156 | bCount = 20; |
||
157 | } |
||
158 | |||
159 | #endif //#if defined(STACK_USE_SSL_SERVER) || defined(STACK_USE_SSL_CLIENT) |
Powered by WebSVN v2.8.3