1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * L. David Baron <dbaron@dbaron.org> (original author)
24 : * Kai Engert <kaie@netscape.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "prlog.h"
41 : #include "nsEntropyCollector.h"
42 : #include "nsMemory.h"
43 : #include "nsAlgorithm.h"
44 :
45 1404 : nsEntropyCollector::nsEntropyCollector()
46 1404 : :mBytesCollected(0), mWritePointer(mEntropyCache)
47 : {
48 : // We could use the uninitialized memory in mEntropyCache as initial
49 : // random data, but that means (if any entropy is collected before NSS
50 : // initialization and then forwarded) that we'll get warnings from
51 : // tools like valgrind for every later operation that depends on the
52 : // entropy.
53 1404 : memset(mEntropyCache, 0, sizeof(mEntropyCache));
54 1404 : }
55 :
56 2806 : nsEntropyCollector::~nsEntropyCollector()
57 : {
58 5612 : }
59 :
60 17883 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsEntropyCollector,
61 : nsIEntropyCollector,
62 : nsIBufEntropyCollector)
63 :
64 : NS_IMETHODIMP
65 0 : nsEntropyCollector::RandomUpdate(void *new_entropy, PRInt32 bufLen)
66 : {
67 0 : if (bufLen > 0) {
68 0 : if (mForwardTarget) {
69 0 : return mForwardTarget->RandomUpdate(new_entropy, bufLen);
70 : }
71 : else {
72 0 : const unsigned char *InputPointer = (const unsigned char *)new_entropy;
73 0 : const unsigned char *PastEndPointer = mEntropyCache + entropy_buffer_size;
74 :
75 : // if the input is large, we only take as much as we can store
76 0 : PRInt32 bytes_wanted = NS_MIN(bufLen, PRInt32(entropy_buffer_size));
77 :
78 : // remember the number of bytes we will have after storing new_entropy
79 : mBytesCollected = NS_MIN(PRInt32(entropy_buffer_size),
80 0 : mBytesCollected + bytes_wanted);
81 :
82 : // as the above statements limit bytes_wanted to the entropy_buffer_size,
83 : // this loop will iterate at most twice.
84 0 : while (bytes_wanted > 0) {
85 :
86 : // how many bytes to end of cyclic buffer?
87 0 : const PRInt32 space_to_end = PastEndPointer - mWritePointer;
88 :
89 : // how many bytes can we copy, not reaching the end of the buffer?
90 0 : const PRInt32 this_time = NS_MIN(space_to_end, bytes_wanted);
91 :
92 : // copy at most to the end of the cyclic buffer
93 0 : for (PRInt32 i = 0; i < this_time; ++i) {
94 :
95 0 : unsigned int old = *mWritePointer;
96 :
97 : // combine new and old value already stored in buffer
98 : // this logic comes from PSM 1
99 0 : *mWritePointer++ = ((old << 1) | (old >> 7)) ^ *InputPointer++;
100 : }
101 :
102 0 : PR_ASSERT(mWritePointer <= PastEndPointer);
103 0 : PR_ASSERT(mWritePointer >= mEntropyCache);
104 :
105 : // have we arrived at the end of the buffer?
106 0 : if (PastEndPointer == mWritePointer) {
107 : // reset write pointer back to begining of our buffer
108 0 : mWritePointer = mEntropyCache;
109 : }
110 :
111 : // subtract the number of bytes we have already copied
112 0 : bytes_wanted -= this_time;
113 : }
114 : }
115 : }
116 :
117 0 : return NS_OK;
118 : }
119 :
120 : NS_IMETHODIMP
121 328 : nsEntropyCollector::ForwardTo(nsIEntropyCollector *aCollector)
122 : {
123 328 : NS_PRECONDITION(!mForwardTarget, "|ForwardTo| should only be called once.");
124 :
125 328 : mForwardTarget = aCollector;
126 328 : mForwardTarget->RandomUpdate(mEntropyCache, mBytesCollected);
127 328 : mBytesCollected = 0;
128 :
129 328 : return NS_OK;
130 : }
131 :
132 : NS_IMETHODIMP
133 328 : nsEntropyCollector::DontForward()
134 : {
135 328 : mForwardTarget = nsnull;
136 328 : return NS_OK;
137 : }
|