2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 *************************************************************************
34 -------- ---------- ----------------------------------------------
35 Eddy 2009/01/19 Create AES-128, AES-192, AES-256, AES-CBC
39 #include "crypt_biginteger.h"
42 ========================================================================
44 Diffie-Hellman public key generation
48 GValueLength The length of G in bytes
50 PValueLength The length of P in bytes
51 PrivateKey Private key
52 PrivateKeyLength The length of Private key in bytes
56 PublicKeyLength The length of public key in bytes
60 PublicKey = G^PrivateKey (mod P)
61 ========================================================================
63 void DH_PublicKey_Generate (
68 IN UINT8 PrivateKey[],
69 IN UINT PrivateKeyLength,
70 OUT UINT8 PublicKey[],
71 INOUT UINT *PublicKeyLength)
73 PBIG_INTEGER pBI_G = NULL;
74 PBIG_INTEGER pBI_P = NULL;
75 PBIG_INTEGER pBI_PrivateKey = NULL;
76 PBIG_INTEGER pBI_PublicKey = NULL;
79 * 1. Check the input parameters
80 * - GValueLength, PValueLength and PrivateLength must be large than zero
81 * - PublicKeyLength must be large or equal than PValueLength
82 * - PValue must be odd
84 * - PValue must be prime number (no implement)
85 * - GValue must be greater than 0 but less than the PValue (no implement)
87 if (GValueLength == 0) {
88 DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: G length is (%d)\n", GValueLength));
91 if (PValueLength == 0) {
92 DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: P length is (%d)\n", PValueLength));
95 if (PrivateKeyLength == 0) {
96 DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: private key length is (%d)\n", PrivateKeyLength));
99 if (*PublicKeyLength < PValueLength) {
100 DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: public key length(%d) must be large or equal than P length(%d)\n",
101 *PublicKeyLength, PValueLength));
104 if (!(PValue[PValueLength - 1] & 0x1)) {
105 DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: P value must be odd\n"));
110 * 2. Transfer parameters to BigInteger structure
112 BigInteger_Init(&pBI_G);
113 BigInteger_Init(&pBI_P);
114 BigInteger_Init(&pBI_PrivateKey);
115 BigInteger_Init(&pBI_PublicKey);
116 BigInteger_Bin2BI(GValue, GValueLength, &pBI_G);
117 BigInteger_Bin2BI(PValue, PValueLength, &pBI_P);
118 BigInteger_Bin2BI(PrivateKey, PrivateKeyLength, &pBI_PrivateKey);
121 * 3. Calculate PublicKey = G^PrivateKey (mod P)
122 * - BigInteger Operation
123 * - Montgomery reduction
125 BigInteger_Montgomery_ExpMod(pBI_G, pBI_PrivateKey, pBI_P, &pBI_PublicKey);
128 * 4. Transfer BigInteger structure to char array
130 BigInteger_BI2Bin(pBI_PublicKey, PublicKey, PublicKeyLength);
132 BigInteger_Free(&pBI_G);
133 BigInteger_Free(&pBI_P);
134 BigInteger_Free(&pBI_PrivateKey);
135 BigInteger_Free(&pBI_PublicKey);
136 } /* End of DH_PublicKey_Generate */
140 ========================================================================
142 Diffie-Hellman secret key generation
146 PublicKeyLength The length of Public key in bytes
147 PValue Array in UINT8
148 PValueLength The length of P in bytes
149 PrivateKey Private key
150 PrivateKeyLength The length of Private key in bytes
154 SecretKeyLength The length of secret key in bytes
158 SecretKey = PublicKey^PrivateKey (mod P)
159 ========================================================================
161 void DH_SecretKey_Generate (
162 IN UINT8 PublicKey[],
163 IN UINT PublicKeyLength,
165 IN UINT PValueLength,
166 IN UINT8 PrivateKey[],
167 IN UINT PrivateKeyLength,
168 OUT UINT8 SecretKey[],
169 INOUT UINT *SecretKeyLength)
171 PBIG_INTEGER pBI_P = NULL;
172 PBIG_INTEGER pBI_SecretKey = NULL;
173 PBIG_INTEGER pBI_PrivateKey = NULL;
174 PBIG_INTEGER pBI_PublicKey = NULL;
177 * 1. Check the input parameters
178 * - PublicKeyLength, PValueLength and PrivateLength must be large than zero
179 * - SecretKeyLength must be large or equal than PValueLength
180 * - PValue must be odd
182 * - PValue must be prime number (no implement)
184 if (PublicKeyLength == 0) {
185 DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: public key length is (%d)\n", PublicKeyLength));
188 if (PValueLength == 0) {
189 DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: P length is (%d)\n", PValueLength));
192 if (PrivateKeyLength == 0) {
193 DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: private key length is (%d)\n", PrivateKeyLength));
196 if (*SecretKeyLength < PValueLength) {
197 DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: secret key length(%d) must be large or equal than P length(%d)\n",
198 *SecretKeyLength, PValueLength));
201 if (!(PValue[PValueLength - 1] & 0x1)) {
202 DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: P value must be odd\n"));
207 * 2. Transfer parameters to BigInteger structure
209 BigInteger_Init(&pBI_P);
210 BigInteger_Init(&pBI_PrivateKey);
211 BigInteger_Init(&pBI_PublicKey);
212 BigInteger_Init(&pBI_SecretKey);
214 BigInteger_Bin2BI(PublicKey, PublicKeyLength, &pBI_PublicKey);
215 BigInteger_Bin2BI(PValue, PValueLength, &pBI_P);
216 BigInteger_Bin2BI(PrivateKey, PrivateKeyLength, &pBI_PrivateKey);
219 * 3. Calculate SecretKey = PublicKey^PrivateKey (mod P)
220 * - BigInteger Operation
221 * - Montgomery reduction
223 BigInteger_Montgomery_ExpMod(pBI_PublicKey, pBI_PrivateKey, pBI_P, &pBI_SecretKey);
226 * 4. Transfer BigInteger structure to char array
228 BigInteger_BI2Bin(pBI_SecretKey, SecretKey, SecretKeyLength);
230 BigInteger_Free(&pBI_P);
231 BigInteger_Free(&pBI_PrivateKey);
232 BigInteger_Free(&pBI_PublicKey);
233 BigInteger_Free(&pBI_SecretKey);
234 } /* End of DH_SecretKey_Generate */