| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 1996, David Mazieres <dm@uun.org> | 2 * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| 3 * Copyright (c) 2008, Damien Miller <djm@openbsd.org> | 3 * Copyright (c) 2008, Damien Miller <djm@openbsd.org> |
| 4 * | 4 * |
| 5 * Permission to use, copy, modify, and distribute this software for any | 5 * Permission to use, copy, modify, and distribute this software for any |
| 6 * purpose with or without fee is hereby granted, provided that the above | 6 * purpose with or without fee is hereby granted, provided that the above |
| 7 * copyright notice and this permission notice appear in all copies. | 7 * copyright notice and this permission notice appear in all copies. |
| 8 * | 8 * |
| 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * compatible with RSA Labs "RC4" cipher (the actual description of | 23 * compatible with RSA Labs "RC4" cipher (the actual description of |
| 24 * which is a trade secret). The same algorithm is used as a stream | 24 * which is a trade secret). The same algorithm is used as a stream |
| 25 * cipher called "arcfour" in Tatu Ylonen's ssh package. | 25 * cipher called "arcfour" in Tatu Ylonen's ssh package. |
| 26 * | 26 * |
| 27 * RC4 is a registered trademark of RSA Laboratories. | 27 * RC4 is a registered trademark of RSA Laboratories. |
| 28 */ | 28 */ |
| 29 | 29 |
| 30 #include "config.h" | 30 #include "config.h" |
| 31 #include "CryptographicallyRandomNumber.h" | 31 #include "CryptographicallyRandomNumber.h" |
| 32 | 32 |
| 33 #include "OSRandomSource.h" | |
| 34 #include "StdLibExtras.h" | 33 #include "StdLibExtras.h" |
| 35 #include "ThreadingPrimitives.h" | 34 #include "ThreadingPrimitives.h" |
| 36 | 35 |
| 37 namespace WTF { | 36 namespace WTF { |
| 38 | 37 |
| 38 static RandomNumberSource sourceFunction; |
| 39 |
| 40 void setRandomSource(RandomNumberSource source) |
| 41 { |
| 42 sourceFunction = source; |
| 43 } |
| 44 |
| 39 namespace { | 45 namespace { |
| 40 | 46 |
| 41 class ARC4Stream { | 47 class ARC4Stream { |
| 42 public: | 48 public: |
| 43 ARC4Stream(); | 49 ARC4Stream(); |
| 44 | 50 |
| 45 uint8_t i; | 51 uint8_t i; |
| 46 uint8_t j; | 52 uint8_t j; |
| 47 uint8_t s[256]; | 53 uint8_t s[256]; |
| 48 }; | 54 }; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 m_stream.s[m_stream.i] = m_stream.s[m_stream.j]; | 96 m_stream.s[m_stream.i] = m_stream.s[m_stream.j]; |
| 91 m_stream.s[m_stream.j] = si; | 97 m_stream.s[m_stream.j] = si; |
| 92 } | 98 } |
| 93 m_stream.j = m_stream.i; | 99 m_stream.j = m_stream.i; |
| 94 } | 100 } |
| 95 | 101 |
| 96 void ARC4RandomNumberGenerator::stir() | 102 void ARC4RandomNumberGenerator::stir() |
| 97 { | 103 { |
| 98 unsigned char randomness[128]; | 104 unsigned char randomness[128]; |
| 99 size_t length = sizeof(randomness); | 105 size_t length = sizeof(randomness); |
| 100 cryptographicallyRandomValuesFromOS(randomness, length); | 106 (*sourceFunction)(randomness, length); |
| 101 addRandomData(randomness, length); | 107 addRandomData(randomness, length); |
| 102 | 108 |
| 103 // Discard early keystream, as per recommendations in: | 109 // Discard early keystream, as per recommendations in: |
| 104 // http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps | 110 // http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps |
| 105 for (int i = 0; i < 256; i++) | 111 for (int i = 0; i < 256; i++) |
| 106 getByte(); | 112 getByte(); |
| 107 m_count = 1600000; | 113 m_count = 1600000; |
| 108 } | 114 } |
| 109 | 115 |
| 110 void ARC4RandomNumberGenerator::stirIfNeeded() | 116 void ARC4RandomNumberGenerator::stirIfNeeded() |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 163 } |
| 158 | 164 |
| 159 ARC4RandomNumberGenerator& sharedRandomNumberGenerator() | 165 ARC4RandomNumberGenerator& sharedRandomNumberGenerator() |
| 160 { | 166 { |
| 161 DEFINE_STATIC_LOCAL(ARC4RandomNumberGenerator, randomNumberGenerator, ()); | 167 DEFINE_STATIC_LOCAL(ARC4RandomNumberGenerator, randomNumberGenerator, ()); |
| 162 return randomNumberGenerator; | 168 return randomNumberGenerator; |
| 163 } | 169 } |
| 164 | 170 |
| 165 } | 171 } |
| 166 | 172 |
| 173 |
| 167 uint32_t cryptographicallyRandomNumber() | 174 uint32_t cryptographicallyRandomNumber() |
| 168 { | 175 { |
| 169 return sharedRandomNumberGenerator().randomNumber(); | 176 return sharedRandomNumberGenerator().randomNumber(); |
| 170 } | 177 } |
| 171 | 178 |
| 172 void cryptographicallyRandomValues(void* buffer, size_t length) | 179 void cryptographicallyRandomValues(void* buffer, size_t length) |
| 173 { | 180 { |
| 174 sharedRandomNumberGenerator().randomValues(buffer, length); | 181 sharedRandomNumberGenerator().randomValues(buffer, length); |
| 175 } | 182 } |
| 176 | 183 |
| 177 } | 184 } |
| OLD | NEW |