📄 crypt_biginteger.c
字号:
/*
*************************************************************************
* Ralink Tech Inc.
* 5F., No.36, Taiyuan St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2002-2007, Ralink Technology, Inc.
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************
*/
#include "crypt_biginteger.h"
#ifdef __KERNEL__
#define DEBUGPRINT(fmt, args...) printk(KERN_ERR fmt, ## args)
#else
#define DEBUGPRINT(fmt, args...) printf(fmt, ## args)
#endif /* __KERNEL__ */
#define UINT32_HBITS(value) (((value) >> 0x10) & 0xffff)
#define UINT32_LBITS(value) ((value) & 0xffff)
#define UINT32_GETBYTE(value, index) (((value) >> ((index)*8)) & 0xff)
#define UINT64_HBITS(value) (((value) >> 0x20) & 0xffffffff)
#define UINT64_LBITS(value) ((value) & 0xffffffff)
static UINT8 WPS_DH_P_VALUE[192] =
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22,
0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B,
0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5,
0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05,
0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A,
0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96,
0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB,
0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04,
0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x23, 0x73, 0x27,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
static UINT8 WPS_DH_R_VALUE[193] =
{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
};
static UINT8 WPS_DH_X_VALUE[184] =
{
0x36, 0xf0, 0x25, 0x5d, 0xde, 0x97, 0x3d, 0xcb,
0x3b, 0x39, 0x9d, 0x74, 0x7f, 0x23, 0xe3, 0x2e,
0xd6, 0xfd, 0xb1, 0xf7, 0x75, 0x98, 0x33, 0x8b,
0xfd, 0xf4, 0x41, 0x59, 0xc4, 0xec, 0x64, 0xdd,
0xae, 0xb5, 0xf7, 0x86, 0x71, 0xcb, 0xfb, 0x22,
0x10, 0x6a, 0xe6, 0x4c, 0x32, 0xc5, 0xbc, 0xe4,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 0x67, 0x98, 0x44, 0x60,
0x0b, 0xe4, 0x96, 0x47, 0xa8, 0x7c, 0x7b, 0x37,
0xf8, 0x05, 0x65, 0x64, 0x96, 0x9b, 0x7f, 0x02,
0xdc, 0x54, 0x1a, 0x4e, 0xd4, 0x05, 0x3f, 0x54,
0xd6, 0x2a, 0x0e, 0xea, 0xb2, 0x70, 0x52, 0x1b,
0x22, 0xc2, 0x96, 0xe9, 0xd4, 0x6f, 0xec, 0x23,
0x8e, 0x1a, 0xbd, 0x78, 0x02, 0x23, 0xb7, 0x6b,
0xb8, 0xfe, 0x61, 0x21, 0x19, 0x6b, 0x7e, 0x88,
0x1c, 0x72, 0x9c, 0x7e, 0x04, 0xb9, 0xf7, 0x96,
0x07, 0xcd, 0x0a, 0x62, 0x8e, 0x43, 0x41, 0x30,
0x04, 0xa5, 0x41, 0xff, 0x93, 0xae, 0x1c, 0xeb,
0xb0, 0x04, 0xa7, 0x50, 0xdb, 0x10, 0x2d, 0x39,
0xb9, 0x05, 0x2b, 0xb4, 0x7a, 0x58, 0xf1, 0x70,
0x7e, 0x8c, 0xd2, 0xac, 0x98, 0xb5, 0xfb, 0x62,
0x8f, 0x23, 0x31, 0xb1, 0x3b, 0x01, 0xe0, 0x18,
0xf4, 0x66, 0xee, 0x5f, 0xbc, 0xd4, 0x9d, 0x68,
0xd0, 0xab, 0x92, 0xe1, 0x83, 0x97, 0xf2, 0x45,
0x8e, 0x0e, 0x3e, 0x21, 0x67, 0x47, 0x8c, 0x73,
0xf1, 0x15, 0xd2, 0x7d, 0x32, 0xc6, 0x95, 0xe0,
};
static UINT8 Value_0[1] = {0x00};
static UINT8 Value_1[1] = {0x01};
static PBIG_INTEGER pBI_U = NULL, pBI_S = NULL, pBI_O = NULL;
static UINT Bits_Of_R = 0;
VOID BigInteger_Print (
IN PBIG_INTEGER pBI)
{
int i = 0, j = 0;
if ((pBI == NULL) || (pBI->pIntegerArray == NULL))
return;
if (strlen(pBI->Name) != 0)
DEBUGPRINT("Name=%s\n", pBI->Name);
DEBUGPRINT("AllocSize=%d, ArrayLength=%d, IntegerLength=%d, Signed=%d\n", pBI->AllocSize, pBI->ArrayLength, pBI->IntegerLength, pBI->Signed);
for (i = (pBI->ArrayLength - 1), j = 0;i >=0;i--,j++) {
DEBUGPRINT("%08x, ", pBI->pIntegerArray[i]);
if ((j%8) == 7)
DEBUGPRINT("\n");
} /* End od for */
DEBUGPRINT("\n\n");
} /* End of BigInteger_Print */
VOID BigInteger_Init (
INOUT PBIG_INTEGER *pBI)
{
if (*pBI != NULL)
BigInteger_Free(pBI);
if ((*pBI = (PBIG_INTEGER) kmalloc(sizeof(BIG_INTEGER), GFP_ATOMIC)) == NULL) {
DEBUGPRINT("BigInteger_Init: allocate %d bytes memory failure.\n", (sizeof(BIG_INTEGER)));
return;
} /* End of if */
NdisZeroMemory(*pBI, sizeof(BIG_INTEGER));
(*pBI)->pIntegerArray = NULL;
(*pBI)->Signed = 1;
} /* End of BigInteger_Init */
VOID BigInteger_Free_AllocSize (
IN PBIG_INTEGER *pBI)
{
if ((*pBI != NULL) && ((*pBI)->pIntegerArray != NULL)) {
kfree((*pBI)->pIntegerArray);
NdisZeroMemory(*pBI, sizeof(BIG_INTEGER));
(*pBI)->pIntegerArray = NULL;
(*pBI)->Signed = 1;
} /* End of if */
} /* End of BigInteger_Free_AllocSize */
VOID BigInteger_Free (
IN PBIG_INTEGER *pBI)
{
if (*pBI != NULL) {
BigInteger_Free_AllocSize(pBI);
kfree(*pBI);
} /* End of if */
*pBI = NULL;
} /* End of BigInteger_Free */
VOID BigInteger_AllocSize (
IN PBIG_INTEGER *pBI,
IN UINT Length)
{
UINT ArrayLength = 0;
if (Length == 0)
return;
if (*pBI == NULL)
BigInteger_Init(pBI);
/* Caculate array size */
ArrayLength = Length >> 0x2;
if ((Length & 0x3) != 0)
ArrayLength++;
if (((*pBI)->pIntegerArray != NULL) && ((*pBI)->AllocSize < (sizeof(UINT32)*ArrayLength)))
BigInteger_Free_AllocSize(pBI);
if ((*pBI)->pIntegerArray == NULL) {
if (((*pBI)->pIntegerArray = (UINT32 *) kmalloc(sizeof(UINT32)*ArrayLength, GFP_ATOMIC)) == NULL) {
DEBUGPRINT("BigInteger_AllocSize: allocate %d bytes memory failure.\n", (sizeof(UINT32)*ArrayLength));
return;
} /* End of if */
(*pBI)->AllocSize = sizeof(UINT32)*ArrayLength;
} /* End of if */
NdisZeroMemory((*pBI)->pIntegerArray, (*pBI)->AllocSize);
(*pBI)->ArrayLength = ArrayLength;
(*pBI)->IntegerLength = Length;
} /* End of BigInteger_AllocSize */
VOID BigInteger_ClearHighBits (
IN PBIG_INTEGER pBI)
{
INT BIArrayIndex, ShiftIndex = 0;
UINT8 value;
if ((pBI == NULL) || (pBI->pIntegerArray == NULL))
return;
BIArrayIndex = pBI->ArrayLength - 1;
while ((BIArrayIndex >= 0) && (pBI->pIntegerArray[BIArrayIndex] == 0))
BIArrayIndex--;
if (BIArrayIndex >= 0) {
value = 0;
ShiftIndex = 4;
while (value == 0) {
ShiftIndex--;
value = UINT32_GETBYTE(pBI->pIntegerArray[BIArrayIndex], ShiftIndex);
} /* End of while */
} /* End of if */
if ((BIArrayIndex == -1) && (ShiftIndex == -1)) {
pBI->IntegerLength = 1;
pBI->ArrayLength = 1;
pBI->Signed = 1;
} else {
pBI->IntegerLength = (BIArrayIndex*4) + ShiftIndex + 1;
pBI->ArrayLength = BIArrayIndex + 1;
} /* End of if */
} /* End of BigInteger_ClearHighBits */
VOID BigInteger_BI2Bin (
IN PBIG_INTEGER pBI,
OUT UINT8 *pValue,
OUT UINT *Length)
{
INT ValueIndex, BIArrayIndex, ShiftIndex;
UINT32 Number;
if (pBI == NULL) {
DEBUGPRINT("BigInteger_BI2Bin: pBI is NUll\n");
*Length = 0;
return;
} /* End of if */
if (*Length < (sizeof(UINT8) * pBI->IntegerLength)) {
DEBUGPRINT("BigInteger_BI2Bin: length(%d) is not enough.\n", *Length);
*Length = 0;
return;
} /* End of if */
if (pBI->pIntegerArray == NULL) {
*Length = 0;
return;
} /* End of if */
BigInteger_ClearHighBits(pBI);
if ((ShiftIndex = pBI->IntegerLength & 0x3) == 0)
ShiftIndex = 4;
BIArrayIndex = pBI->ArrayLength - 1;
ValueIndex = 0;
Number = pBI->pIntegerArray[BIArrayIndex];
while (ValueIndex < pBI->IntegerLength)
{
pValue[ValueIndex++] = (UINT8) UINT32_GETBYTE(Number, ShiftIndex - 1);
if ((--ShiftIndex) == 0) {
ShiftIndex = 4;
BIArrayIndex--;
Number = pBI->pIntegerArray[BIArrayIndex];
} /* End of if */
} /* End of while */
*Length = pBI->IntegerLength;
} /* End of BigInteger_BI2Bin */
VOID BigInteger_Bin2BI (
IN UINT8 *pValue,
IN UINT Length,
OUT PBIG_INTEGER *pBI)
{
INT ValueIndex, BIArrayIndex, ShiftIndex;
UINT32 Number;
BigInteger_AllocSize(pBI, Length);
if ((*pBI)->pIntegerArray != NULL) {
Number = 0;
if ((ShiftIndex = Length & 0x3) == 0)
ShiftIndex = 4;
BIArrayIndex = (*pBI)->ArrayLength - 1;
ValueIndex = 0;
while (ValueIndex < Length)
{
Number = (Number << 8) | (UINT8) pValue[ValueIndex++];
if ((--ShiftIndex) == 0) {
(*pBI)->pIntegerArray[BIArrayIndex] = Number;
ShiftIndex = 4;
BIArrayIndex--;
Number = 0;
} /* End of if */
} /* End of while */
} /* End of if */
} /* End of BigInteger_Bin2BI */
/* Calculate the bits of BigInteger, the highest bit is 1 */
VOID BigInteger_BitsOfBI (
IN PBIG_INTEGER pBI,
OUT UINT *Bits_Of_P)
{
UINT32 Number, Index;
Number = pBI->pIntegerArray[pBI->ArrayLength - 1];
Index = 0;
while ((!(Number & 0x80000000)) && (Index < 32)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -