?? random.c
字號:
rstate->RandAESKey[i] ^= RandAESKey[i]; } AESInit_l (&rstate->RandAESWorksp, AES_ECB, 192, NULL, &rstate->RandAESSched, rstate->RandAESKey, 256, AES_ENC); /* Switch internal state */ AESCrypt_l (rstate->XAES, &rstate->RandAESWorksp, &rstate->RandAESSched, rstate->XAES, 24); /* Set parameter for frequency of key update */ rstate->UpdateKeyAES = update; /* Initialize RoundAES for picking single bytes out of random stream */ rstate->RoundAES = 1; /* Set Flag: PRNG is initialized */ rstate->RandAESInit = 1; return MissingEntropy;}/******************************************************************************//* *//* Function: Switching of RandAES to the next state, generating one *//* random byte *//* Syntax: UCHAR SwitchRandAES_l (STATEAES *rstate) *//* Input: rstate (Initialized state of PRNG) *//* Output: rstate (New internal state of PRNG) *//* Returns: One byte random value *//* *//******************************************************************************/UCHAR __FLINT_APISwitchRandAES_l (STATEAES *rstate){ int i; UCHAR rbyte; AESCrypt_l (rstate->XAES, &rstate->RandAESWorksp, &rstate->RandAESSched, rstate->XAES, 24); rbyte = rstate->XAES[(rstate->RoundAES)++ & 15]; /* RoundAES mod 16 */ if (rstate->UpdateKeyAES) { if (0 == (rstate->RoundAES % rstate->UpdateKeyAES)) { for (i = 0; i < 32; i++) { rstate->RandAESKey[i] ^= rstate->XAES[i]; } AESInit_l (&rstate->RandAESWorksp, AES_ECB, 192, NULL, &rstate->RandAESSched, rstate->RandAESKey, 256, AES_ENC); } } return rbyte;}/******************************************************************************//* *//* Function: Generation of pseudorandom number of CLINT-Type *//* (Preceding Initialization by InitRandAES() required) *//* Syntax: int RandAES_l (CLINT r_l, STATEAES *rstate, int l); *//* Input: l (Number of binary digits of random number to generated) *//* Output: r_l (Pseudorandom number) *//* rstate (Internal state of PRNG) *//* Returns: E_CLINT_OK if everything is OK *//* E_CLINT_RIN if generator is not initialized *//* *//******************************************************************************/int __FLINT_APIRandAES_l (CLINT r_l, STATEAES *rstate, int l){ USHORT i, j; USHORT ls, lr; if (!rstate->RandAESInit) { return E_CLINT_RIN; } l = MIN ((unsigned int)l, CLINTMAXBIT); ls = (USHORT) l >> LDBITPERDGT; lr = (USHORT) l & ((USHORT)BITPERDGT - 1); for (i = 1; i <= ls; i++) { r_l[i] = sRandAES_l(rstate); } if (lr > 0) { r_l[++ls] = sRandAES_l(rstate); j = 1U << (lr - 1); /* j <- 2^(lr - 1) */ r_l[ls] = (r_l[ls] | j) & ((j << 1) - 1); /* Bit lr set to 1, higher bits to 0 */ } else { r_l[ls] |= BASEDIV2; } SETDIGITS_L (r_l, ls); return 0;}/******************************************************************************//* *//* Function: Purging of internat status of RandAES *//* Syntax: void PurgeRandAES_l (STATEAES *rstate); *//* Input: rstate (Initialized internal state of PRNG) *//* Output: rstate (Purged state of PRNG) *//* Returns: - *//* *//******************************************************************************/void __FLINT_APIPurgeRandAES_l (STATEAES *rstate){ /* Purge status of AES based PRNG */ memset (rstate->RandAESKey, 0, sizeof (rstate->RandAESKey)); memset (rstate->XAES, 0, sizeof (rstate->XAES)); memset (&rstate->RandAESWorksp, 0, sizeof (rstate->RandAESWorksp)); memset (&rstate->RandAESSched, 0, sizeof (rstate->RandAESSched)); rstate->RoundAES = 0; /* Reset initialization flag */ rstate->RandAESInit = 0;}/******************************************************************************/#if (CLINTMAXDIGIT >= 128)static const char *MODBBSSTR ="81aa5c97200fb2504c08b92e2f8b7e0805775c72b139b455846028653793ba9d616cd01cef719""5b2f952e046ab62d063b048c8f62b21c7fc7fa4e71eef2588aa59b81f355b5539a471ee483b02""2bdab25b3fb41bc6224d60c17bbcb091294f76cb64c3b6da5504085a958b679d1f1147158189d""4fa76ab721c535a3ecfe4452cc61b9466e315ba4b4392db04f686dbb61084b21c9540f972718f""c96ed25a40a917a07c668048683ec116219621a301610bfba40e1ab11bd1e13aa8476aa6d37b2""61228df85c7df67043c51b37a250b27401aaf837101d2db1a55572dd7a79646ff6e5d20a24e4b""43c6d8ab5e9a77becd76d5f0355252f4318e2066d3f9c42f25";#elif (CLINTMAXDIGIT >= 64)static const char *MODBBSSTR ="a1c0a7edba2a2aee2cb3947c3d1c0468ee5a5791ec3ebb97238bd4c3bdad1a00280f0a7518d56""523003d5cee48a60d606d78b818d81b0ef963555b9b62fc3b5f796815946ed28987596f84ccc1""7f87b9ca5959fc9763bc43521aa467cdcec60cd9fa7548268169750adf746df899cc64b059b7c""194ab4ba492c04c3a6c630103";#elif (CLINTMAXDIGIT >= 48)static const char *MODBBSSTR ="c58d49cd9529aed21da56db12844522c04ace305362219478a99da74751213f8ccdfb52fb7a8b""fc2d5ce18c86c9e447f78b9013071d2fbb4be506f942cc45793e752733c71b07f40c3e54a9bdc""3d9bb18a2c9411e8f898b28d060ea0dc9b309b";#elif (CLINTMAXDIGIT >= 32)static const char *MODBBSSTR ="a3d46604762377bccd0ab8562b46132740b75feb0e3ca7a79022736c6a5ca0b17a03465222af1""a074e31224ea01fc48b3150579c06ef8f073673a5169e8ea021";#elif (CLINTMAXDIGIT >= 16)static const char *MODBBSSTR ="b2c31d33668afb5600be97e13b769fe4f558fc96bc46b8d174d94fb468ff31a5";#elif (CLINTMAXDIGIT >= 8)static const char *MODBBSSTR ="845196304e498ea78ff06d51bd58c9e3";#endif/******************************************************************************//* *//* Function: Initialisation of the Blum-Blum-Shub-PNRG (RandBBS) *//* with entropy generated by GetEntropy_l *//* Syntax: int InitRandBBS_l (STATEBBS *rstate, char *UsrStr, *//* int LenUsrStr, int AddEntropy); *//* Input: UsrStr (String for initialization optionally provided by user) *//* LenUsrStr (Length of UsrStr in bytes) *//* AddEntropy (Number of additionally requested entropy bytes *//* Output: rstate (Initialized internal state of PRNG) *//* Returns: 0: OK *//* n > 0: Number of requested but not generated entropy bytes *//* *//******************************************************************************/int __FLINT_APIInitRandBBS_l (STATEBBS *rstate, char *UsrStr, int LenUsrStr, int AddEntropy){ CLINT Seed_l; int MissingEntropy; /* Get Required Entropy and Seed */ MissingEntropy = GetEntropy_l (Seed_l, NULL, AddEntropy, UsrStr, LenUsrStr); /* Switch internal state */ SeedBBS_l (rstate, Seed_l); /* Security: Purge seed variable Seed_l */ local_memset (Seed_l, 0, sizeof (CLINT)); return MissingEntropy;}/******************************************************************************//* *//* Function: Initialization of BLUM-BLUM-SHUB-PRNG with seed of type CLINT *//* Syntax: int SeedBBS_l (STATEBBS *rstate, CLINT seed_l); *//* Input: seed_l (Seed) *//* Output: rstate (Internal state of PRNG) *//* Returns: E_CLINT_OK if everything is OK *//* E_CLINT_RCP if seed and modulus are not coprime *//* *//******************************************************************************/int __FLINT_APISeedBBS_l (STATEBBS *rstate, CLINT seed_l){ CLINT g_l; str2clint_l (rstate->MODBBS, (char*)MODBBSSTR, 16); gcd_l (rstate->MODBBS, seed_l, g_l); if (!EQONE_L (g_l)) { return E_CLINT_RCP; } msqr_l (seed_l, rstate->XBBS, rstate->MODBBS); /* Set Flag: PRNG is initialized */ rstate->RandBBSInit = 1; return E_CLINT_OK;}/******************************************************************************//* *//* Function: Generation of a random bit acc. to Blum-Blum-Shub *//* Syntax: int SwitchRandBBS_l (STATEBBS *rstate); *//* Input: rstate (Initialized internal state of PRNG) *//* Output: rstate (Internal state of PRNG) *//* Returns: Random bit 0 or 1 *//* *//******************************************************************************/int __FLINT_APISwitchRandBBS_l (STATEBBS * rstate){ msqr_l (rstate->XBBS, rstate->XBBS, rstate->MODBBS); /* Output least significant bit of rstate->XBBS */ return (*LSDPTR_L (rstate->XBBS) & 1);}/******************************************************************************//* *//* Function: Generation of a pseudorandom number of type UCHAR *//* Syntax: UCHAR bRandBBS_l (STATEBBS *rstate); *//* Input: rstate (Initialized internal state of PRNG) *//* Output: rstate (Internal state of PRNG) *//* Returns: Pseudorandom number of type UCHAR *//* *//******************************************************************************/UCHAR __FLINT_APIbRandBBS_l (STATEBBS * rstate){ int i; UCHAR r = SwitchRandBBS_l(rstate); for (i = 1; i < (sizeof (UCHAR) << 3); i++) { r = (r << 1) + SwitchRandBBS_l(rstate); } return r;}/******************************************************************************//* *//* Function: Generation of a pseudorandom number of type USHORT *//* Syntax: USHORT sRandBBS_l (STATEBBS *rstate); *//* Input: rstate (Initialized internal state of PRNG) *//* Output: rstate (Internal state of PRNG) *//* Returns: Pseudorandom number of type USHORT *//* *//******************************************************************************/USHORT __FLINT_APIsRandBBS_l (STATEBBS * rstate){ int i; USHORT r = SwitchRandBBS_l(rstate); for (i = 1; i < (sizeof (USHORT) << 3); i++) { r = (r << 1) + SwitchRandBBS_l(rstate); } return r;}/******************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -