1 /*---------------------------------------------------------------------------*
2   Project:  NitroSDK - CRYPTO - demos
3   File:     prng.c
4 
5   Copyright 2006-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-10-20#$
14   $Rev: 9005 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 /*---------------------------------------------------------------------------*
18   Pseudorandom Number Generator
19  *---------------------------------------------------------------------------*/
20 
21 #include <nitro.h>
22 #include <nitro/crypto.h>
23 
24 #include "prng.h"
25 
26 /*---------------------------------------------------------------------------*
27     Variable Definitions
28  *---------------------------------------------------------------------------*/
29 static u8 RandomPool[MATH_SHA1_DIGEST_SIZE];
30 static u32 RandomCount = 0;
31 
32 /*---------------------------------------------------------------------------*
33     Function Definitions
34  *---------------------------------------------------------------------------*/
35 
36 /*---------------------------------------------------------------------------*
37   Name:         AddRandomSeed
38 
39   Description:  Adds a random seed to the PRNG.
40 
41   Arguments:    random_seed: Pointer to the random seed to be added
42                 len: Length of the random seed to be added
43 
44   Returns:      None.
45  *---------------------------------------------------------------------------*/
AddRandomSeed(u8 * random_seed,u32 len)46 void AddRandomSeed(u8* random_seed, u32 len)
47 {
48     MATHSHA1Context context;
49     MATH_SHA1Init(&context);
50     MATH_SHA1Update(&context, RandomPool, sizeof(RandomPool));
51     MATH_SHA1Update(&context, random_seed, len);
52     MATH_SHA1GetHash(&context, RandomPool);
53 }
54 
55 /*---------------------------------------------------------------------------*
56   Name:         ChurnRandomPool
57 
58   Description:  Adds the current system status as a random seed to the PRNG.
59 
60   Arguments:    None.
61 
62   Returns:      None.
63  *---------------------------------------------------------------------------*/
ChurnRandomPool(void)64 void ChurnRandomPool(void)
65 {
66     u32 data[OS_LOW_ENTROPY_DATA_SIZE/sizeof(u32)];
67     OS_GetLowEntropyData(data);
68     AddRandomSeed((u8*)data, sizeof(data));
69 }
70 
71 /*---------------------------------------------------------------------------*
72   Name:         GetRandomBytes
73 
74   Description:  Gets a random number from the PRNG.
75 
76   Arguments:    Pointer to the buffer that stores the random number
77                 len: Length of the random number to be obtained in bytes
78 
79   Returns:      None.
80  *---------------------------------------------------------------------------*/
GetRandomBytes(u8 * buffer,u32 len)81 void GetRandomBytes(u8* buffer, u32 len)
82 {
83     MATHSHA1Context context;
84     u8 tmp[MATH_SHA1_DIGEST_SIZE];
85     u32 n;
86 
87     while (len > 0)
88     {
89         MATH_SHA1Init(&context);
90         MATH_SHA1Update(&context, RandomPool, sizeof(RandomPool));
91         MATH_SHA1Update(&context, &RandomCount, sizeof(RandomCount));
92         MATH_SHA1GetHash(&context, tmp);
93         RandomCount++;
94 
95         n = (len < MATH_SHA1_DIGEST_SIZE) ? len : MATH_SHA1_DIGEST_SIZE;
96         MI_CpuCopy8(tmp, buffer, n);
97         buffer += n;
98         len -= n;
99     }
100 }
101 
102 /*---------------------------------------------------------------------------*
103   End of file
104  *---------------------------------------------------------------------------*/
105