📄 random.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/bios/crypto/random.c Version : $Id: random.c,v 1.14 2004/01/11 09:56:30 dgil Exp $ Description: secure 8 bytes random challenging The Original Code is jayaCard code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): 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; see http://www.gnu.org/licenses/gpl.html History Rev Description 012603 dgil wrote it from scratch ============================================================================*/#include "precomp.h"/* ============================================================================ __bios_random() Fill a buffer of 8 bytes with random values. This function must be very secure. Algo: start: init values loop: extract two bits of a hardware random (HAL_RANDOM_BYTE() call) if 10 or 01, use these two bits to generate one good final bit loop: until u.bBlock[] has been filled end: if same value in all u.bBlock[] cell re-start: secure: use global semaphore ========================================================================= */#ifdef HALCFG_DEV_RANDOM_NULL/* special version returning always 00000...000 */void __bios_random(void){ HAL_HARDWARE(HARDWARE_RNG); gGlobalSem++; u.bBlock[JAYA_BCRYPTO_RANDOM0+0] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+1] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+2] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+3] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+4] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+5] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+6] = 0x00; u.bBlock[JAYA_BCRYPTO_RANDOM0+7] = 0x00; gGlobalSem++; HAL_HARDWARE(HARDWARE_NONE);}#elsevoid __bios_random(void){ jbyte nbyte; jbyte vbyte; jbyte vbit; HAL_HARDWARE(HARDWARE_RNG); gGlobalSem++;start: nbyte = 0; vbit = 1; do { vbyte = HAL_RANDOM_BYTE(); do { if ((vbyte&0x3)==0x01) { /* extract a bit ONE */ u.bBlock[JAYA_BCRYPTO_RANDOM0+nbyte] |= vbit; /* next bit ? */ if (vbit==0x80) { /* next byte ? */ if (nbyte==7) goto end; /* next byte ! */ nbyte++; vbit = 1; } else { /* next bit ! */ vbit<<=1; } } else if ((vbyte&0x3)==0x02) { /* extract a bit ZERO */ u.bBlock[JAYA_BCRYPTO_RANDOM0+nbyte] &= ~vbit; /* next bit ? */ if (vbit==0x80) { /* next byte ? */ if (nbyte==7) goto end; /* next byte ! */ nbyte++; vbit = 1; } else { /* next bit ! */ vbit<<=1; } } /* next two bits for extraction */ vbyte>>=2; } while (vbyte!=0); /* endless waiting nbyte>7 to jump outside with a bad goto ;-) */ } while (1);end: /* strange de-generate(d) case !? */ if ( (u.bBlock[JAYA_BCRYPTO_RANDOM0+0]==u.bBlock[JAYA_BCRYPTO_RANDOM0+1]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+1]==u.bBlock[JAYA_BCRYPTO_RANDOM0+2]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+2]==u.bBlock[JAYA_BCRYPTO_RANDOM0+3]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+3]==u.bBlock[JAYA_BCRYPTO_RANDOM0+4]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+4]==u.bBlock[JAYA_BCRYPTO_RANDOM0+5]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+5]==u.bBlock[JAYA_BCRYPTO_RANDOM0+6]) && (u.bBlock[JAYA_BCRYPTO_RANDOM0+6]==u.bBlock[JAYA_BCRYPTO_RANDOM0+7]) ) goto start; /* ok. Enough to have a good feeling ... */ gGlobalSem++; HAL_HARDWARE(HARDWARE_NONE);}#endif/* ========================================================================= That's all folks ! ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -