]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/rt3090/common/crypt_dh.c
Staging: add rt3090 wireless driver
[mv-sheeva.git] / drivers / staging / rt3090 / common / crypt_dh.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
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.                                   *
14  *                                                                       *
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.                          *
19  *                                                                       *
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.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         crypt_dh.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When                    What
34         --------        ----------              ----------------------------------------------
35         Eddy        2009/01/19      Create AES-128, AES-192, AES-256, AES-CBC
36 */
37
38 #include "crypt_dh.h"
39 #include "crypt_biginteger.h"
40
41 /*
42 ========================================================================
43 Routine Description:
44     Diffie-Hellman public key generation
45
46 Arguments:
47     GValue           Array in UINT8
48     GValueLength     The length of G in bytes
49     PValue           Array in UINT8
50     PValueLength     The length of P in bytes
51     PrivateKey       Private key
52     PrivateKeyLength The length of Private key in bytes
53
54 Return Value:
55     PublicKey       Public key
56     PublicKeyLength The length of public key in bytes
57
58 Note:
59     Reference to RFC2631
60     PublicKey = G^PrivateKey (mod P)
61 ========================================================================
62 */
63 void DH_PublicKey_Generate (
64     IN UINT8 GValue[],
65     IN UINT GValueLength,
66     IN UINT8 PValue[],
67     IN UINT PValueLength,
68     IN UINT8 PrivateKey[],
69     IN UINT PrivateKeyLength,
70     OUT UINT8 PublicKey[],
71     INOUT UINT *PublicKeyLength)
72 {
73     PBIG_INTEGER pBI_G = NULL;
74     PBIG_INTEGER pBI_P = NULL;
75     PBIG_INTEGER pBI_PrivateKey = NULL;
76     PBIG_INTEGER pBI_PublicKey = NULL;
77
78     /*
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
83      *
84      *    - PValue must be prime number (no implement)
85      *    - GValue must be greater than 0 but less than the PValue (no implement)
86      */
87     if (GValueLength == 0) {
88         DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: G length is (%d)\n", GValueLength));
89         return;
90     } /* End of if */
91     if (PValueLength == 0) {
92         DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: P length is (%d)\n", PValueLength));
93         return;
94     } /* End of if */
95     if (PrivateKeyLength == 0) {
96         DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: private key length is (%d)\n", PrivateKeyLength));
97         return;
98     } /* End of if */
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));
102         return;
103     } /* End of if */
104     if (!(PValue[PValueLength - 1] & 0x1)) {
105         DBGPRINT(RT_DEBUG_ERROR, ("DH_PublicKey_Generate: P value must be odd\n"));
106         return;
107     } /* End of if */
108
109     /*
110      * 2. Transfer parameters to BigInteger structure
111      */
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);
119
120     /*
121      * 3. Calculate PublicKey = G^PrivateKey (mod P)
122      *    - BigInteger Operation
123      *    - Montgomery reduction
124      */
125     BigInteger_Montgomery_ExpMod(pBI_G, pBI_PrivateKey, pBI_P, &pBI_PublicKey);
126
127     /*
128      * 4. Transfer BigInteger structure to char array
129      */
130     BigInteger_BI2Bin(pBI_PublicKey, PublicKey, PublicKeyLength);
131
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 */
137
138
139 /*
140 ========================================================================
141 Routine Description:
142     Diffie-Hellman secret key generation
143
144 Arguments:
145     PublicKey        Public key
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
151
152 Return Value:
153     SecretKey        Secret key
154     SecretKeyLength  The length of secret key in bytes
155
156 Note:
157     Reference to RFC2631
158     SecretKey = PublicKey^PrivateKey (mod P)
159 ========================================================================
160 */
161 void DH_SecretKey_Generate (
162     IN UINT8 PublicKey[],
163     IN UINT PublicKeyLength,
164     IN UINT8 PValue[],
165     IN UINT PValueLength,
166     IN UINT8 PrivateKey[],
167     IN UINT PrivateKeyLength,
168     OUT UINT8 SecretKey[],
169     INOUT UINT *SecretKeyLength)
170 {
171     PBIG_INTEGER pBI_P = NULL;
172     PBIG_INTEGER pBI_SecretKey = NULL;
173     PBIG_INTEGER pBI_PrivateKey = NULL;
174     PBIG_INTEGER pBI_PublicKey = NULL;
175
176     /*
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
181      *
182      *    - PValue must be prime number (no implement)
183      */
184     if (PublicKeyLength == 0) {
185         DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: public key length is (%d)\n", PublicKeyLength));
186         return;
187     } /* End of if */
188     if (PValueLength == 0) {
189         DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: P length is (%d)\n", PValueLength));
190         return;
191     } /* End of if */
192     if (PrivateKeyLength == 0) {
193         DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: private key length is (%d)\n", PrivateKeyLength));
194         return;
195     } /* End of if */
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));
199         return;
200     } /* End of if */
201     if (!(PValue[PValueLength - 1] & 0x1)) {
202         DBGPRINT(RT_DEBUG_ERROR, ("DH_SecretKey_Generate: P value must be odd\n"));
203         return;
204     } /* End of if */
205
206     /*
207      * 2. Transfer parameters to BigInteger structure
208      */
209     BigInteger_Init(&pBI_P);
210     BigInteger_Init(&pBI_PrivateKey);
211     BigInteger_Init(&pBI_PublicKey);
212     BigInteger_Init(&pBI_SecretKey);
213
214     BigInteger_Bin2BI(PublicKey, PublicKeyLength, &pBI_PublicKey);
215     BigInteger_Bin2BI(PValue, PValueLength, &pBI_P);
216     BigInteger_Bin2BI(PrivateKey, PrivateKeyLength, &pBI_PrivateKey);
217
218     /*
219      * 3. Calculate SecretKey = PublicKey^PrivateKey (mod P)
220      *    - BigInteger Operation
221      *    - Montgomery reduction
222      */
223     BigInteger_Montgomery_ExpMod(pBI_PublicKey, pBI_PrivateKey, pBI_P, &pBI_SecretKey);
224
225     /*
226      * 4. Transfer BigInteger structure to char array
227      */
228     BigInteger_BI2Bin(pBI_SecretKey, SecretKey, SecretKeyLength);
229
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 */