2
0

netInterface.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/event.h"
  24. #include "network/netConnection.h"
  25. #include "network/netInterface.h"
  26. #include "io/bitStream.h"
  27. #include "math/mRandom.h"
  28. #include "game/gameInterface.h"
  29. #include "netInterface_ScriptBinding.h"
  30. NetInterface *GNet = NULL;
  31. NetInterface::NetInterface()
  32. {
  33. AssertFatal(GNet == NULL, "ERROR: Multiple net interfaces declared.");
  34. GNet = this;
  35. mLastTimeoutCheckTime = 0;
  36. mAllowConnections = true;
  37. }
  38. void NetInterface::initRandomData()
  39. {
  40. mRandomDataInitialized = true;
  41. U32 seed = Platform::getRealMilliseconds();
  42. #ifdef TORQUE_ALLOW_JOURNALING
  43. if(Game->isJournalReading())
  44. Game->journalRead(&seed);
  45. else if(Game->isJournalWriting())
  46. Game->journalWrite(seed);
  47. #endif //TORQUE_ALLOW_JOURNALING
  48. RandomR250 myRandom(seed);
  49. for(U32 i = 0; i < 12; i++)
  50. mRandomHashData[i] = myRandom.randI();
  51. }
  52. void NetInterface::addPendingConnection(NetConnection *connection)
  53. {
  54. Con::printf("Adding a pending connection");
  55. mPendingConnections.push_back(connection);
  56. }
  57. void NetInterface::removePendingConnection(NetConnection *connection)
  58. {
  59. for(U32 i = 0; i < (U32)mPendingConnections.size(); i++)
  60. if(mPendingConnections[i] == connection)
  61. mPendingConnections.erase(i);
  62. }
  63. NetConnection *NetInterface::findPendingConnection(const NetAddress *address, U32 connectSequence)
  64. {
  65. for(U32 i = 0; i < (U32)mPendingConnections.size(); i++)
  66. if(Net::compareAddresses(address, mPendingConnections[i]->getNetAddress()) &&
  67. connectSequence == mPendingConnections[i]->getSequence())
  68. return mPendingConnections[i];
  69. return NULL;
  70. }
  71. void NetInterface::processPacketReceiveEvent(PacketReceiveEvent *prEvent)
  72. {
  73. U32 dataSize = prEvent->size - PacketReceiveEventHeaderSize;
  74. BitStream pStream(prEvent->data, dataSize);
  75. // Determine what to do with this packet:
  76. if(prEvent->data[0] & 0x01) // it's a protocol packet...
  77. {
  78. // if the LSB of the first byte is set, it's a game data packet
  79. // so pass it to the appropriate connection.
  80. // lookup the connection in the addressTable
  81. NetConnection *conn = NetConnection::lookup(&prEvent->sourceAddress);
  82. if(conn)
  83. conn->processRawPacket(&pStream);
  84. }
  85. else
  86. {
  87. // Otherwise, it's either a game info packet or a
  88. // connection handshake packet.
  89. U8 packetType;
  90. pStream.read(&packetType);
  91. NetAddress *addr = &prEvent->sourceAddress;
  92. if(packetType <= GameHeartbeat)
  93. handleInfoPacket(addr, packetType, &pStream);
  94. else
  95. {
  96. // check if there's a connection already:
  97. switch(packetType)
  98. {
  99. case ConnectChallengeRequest:
  100. handleConnectChallengeRequest(addr, &pStream);
  101. break;
  102. case ConnectRequest:
  103. handleConnectRequest(addr, &pStream);
  104. break;
  105. case ConnectChallengeResponse:
  106. handleConnectChallengeResponse(addr, &pStream);
  107. break;
  108. case ConnectAccept:
  109. handleConnectAccept(addr, &pStream);
  110. break;
  111. case Disconnect:
  112. handleDisconnect(addr, &pStream);
  113. break;
  114. case ConnectReject:
  115. handleConnectReject(addr, &pStream);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. //-----------------------------------------------------------------------------
  123. // Connection handshaking basic overview:
  124. // The torque engine does a two phase connect handshake to
  125. // prevent a spoofed source address Denial-of-Service (DOS) attack
  126. //
  127. // Basically, the initiator of a connection (client) sends a
  128. // Connect Challenge Request packet to the server to initiate the connection
  129. // The server then hashes the source address of the client request
  130. // with some random magic server data to come up with a 16-byte key that
  131. // the client can then use to gain entry to the server.
  132. // This way there are no partially active connection records on the
  133. // server at all.
  134. //
  135. // The client then sends a Connect Request packet to the server,
  136. // including any game specific data necessary to start a connection (a
  137. // server password, for instance), along with the key the server sent
  138. // on the Connect Challenge Response packet.
  139. //
  140. // The server, on receipt of the Connect Request, compares the
  141. // entry key with a computed key, makes sure it can create the requested
  142. // NetConnection subclass, and then passes all processing on to the connection
  143. // instance.
  144. //
  145. // If the subclass reads and accepts he connect request successfully, the
  146. // server sends a Connect Accept packet - otherwise the connection
  147. // is rejected with the sendConnectReject function
  148. //-----------------------------------------------------------------------------
  149. //-----------------------------------------------------------------------------
  150. void NetInterface::sendConnectChallengeRequest(NetConnection *conn)
  151. {
  152. Con::printf("Sending Connect challenge Request");
  153. BitStream *out = BitStream::getPacketStream();
  154. out->write(U8(ConnectChallengeRequest));
  155. out->write(conn->getSequence());
  156. conn->mConnectSendCount++;
  157. conn->mConnectLastSendTime = Platform::getVirtualMilliseconds();
  158. BitStream::sendPacketStream(conn->getNetAddress());
  159. }
  160. void NetInterface::handleConnectChallengeRequest(const NetAddress *addr, BitStream *stream)
  161. {
  162. char buf[256];
  163. Net::addressToString(addr, buf);
  164. Con::printf("Got Connect challenge Request from %s", buf);
  165. if(!mAllowConnections)
  166. return;
  167. U32 connectSequence;
  168. stream->read(&connectSequence);
  169. if(!mRandomDataInitialized)
  170. initRandomData();
  171. U32 addressDigest[4];
  172. computeNetMD5(addr, connectSequence, addressDigest);
  173. BitStream *out = BitStream::getPacketStream();
  174. out->write(U8(ConnectChallengeResponse));
  175. out->write(connectSequence);
  176. out->write(addressDigest[0]);
  177. out->write(addressDigest[1]);
  178. out->write(addressDigest[2]);
  179. out->write(addressDigest[3]);
  180. BitStream::sendPacketStream(addr);
  181. }
  182. //-----------------------------------------------------------------------------
  183. void NetInterface::handleConnectChallengeResponse(const NetAddress *address, BitStream *stream)
  184. {
  185. Con::printf("Got Connect challenge Response");
  186. U32 connectSequence;
  187. stream->read(&connectSequence);
  188. NetConnection *conn = findPendingConnection(address, connectSequence);
  189. if(!conn || conn->getConnectionState() != NetConnection::AwaitingChallengeResponse)
  190. return;
  191. U32 addressDigest[4];
  192. stream->read(&addressDigest[0]);
  193. stream->read(&addressDigest[1]);
  194. stream->read(&addressDigest[2]);
  195. stream->read(&addressDigest[3]);
  196. conn->setAddressDigest(addressDigest);
  197. conn->setConnectionState(NetConnection::AwaitingConnectResponse);
  198. conn->mConnectSendCount = 0;
  199. Con::printf("Sending Connect Request");
  200. sendConnectRequest(conn);
  201. }
  202. //-----------------------------------------------------------------------------
  203. void NetInterface::sendConnectRequest(NetConnection *conn)
  204. {
  205. BitStream *out = BitStream::getPacketStream();
  206. out->write(U8(ConnectRequest));
  207. out->write(conn->getSequence());
  208. U32 addressDigest[4];
  209. conn->getAddressDigest(addressDigest);
  210. out->write(addressDigest[0]);
  211. out->write(addressDigest[1]);
  212. out->write(addressDigest[2]);
  213. out->write(addressDigest[3]);
  214. out->writeString(conn->getClassName());
  215. conn->writeConnectRequest(out);
  216. conn->mConnectSendCount++;
  217. conn->mConnectLastSendTime = Platform::getVirtualMilliseconds();
  218. BitStream::sendPacketStream(conn->getNetAddress());
  219. }
  220. //-----------------------------------------------------------------------------
  221. void NetInterface::handleConnectRequest(const NetAddress *address, BitStream *stream)
  222. {
  223. if(!mAllowConnections)
  224. return;
  225. Con::printf("Got Connect Request");
  226. U32 connectSequence;
  227. stream->read(&connectSequence);
  228. // see if the connection is in the main connection table:
  229. NetConnection *connect = NetConnection::lookup(address);
  230. if(connect && connect->getSequence() == connectSequence)
  231. {
  232. sendConnectAccept(connect);
  233. return;
  234. }
  235. U32 addressDigest[4];
  236. U32 computedAddressDigest[4];
  237. stream->read(&addressDigest[0]);
  238. stream->read(&addressDigest[1]);
  239. stream->read(&addressDigest[2]);
  240. stream->read(&addressDigest[3]);
  241. computeNetMD5(address, connectSequence, computedAddressDigest);
  242. if(addressDigest[0] != computedAddressDigest[0] ||
  243. addressDigest[1] != computedAddressDigest[1] ||
  244. addressDigest[2] != computedAddressDigest[2] ||
  245. addressDigest[3] != computedAddressDigest[3])
  246. return; // bogus connection attempt
  247. if(connect)
  248. {
  249. if(connect->getSequence() > connectSequence)
  250. return; // the existing connection should be kept - the incoming request is stale.
  251. else
  252. connect->deleteObject(); // disconnect this one, and allow the new one to be created.
  253. }
  254. char connectionClass[255];
  255. stream->readString(connectionClass);
  256. ConsoleObject *co = ConsoleObject::create(connectionClass);
  257. NetConnection *conn = dynamic_cast<NetConnection *>(co);
  258. if(!conn || !conn->canRemoteCreate())
  259. {
  260. delete co;
  261. return;
  262. }
  263. conn->registerObject();
  264. conn->setNetAddress(address);
  265. conn->setNetworkConnection(true);
  266. conn->setSequence(connectSequence);
  267. const char *errorString = NULL;
  268. if(!conn->readConnectRequest(stream, &errorString))
  269. {
  270. sendConnectReject(conn, errorString);
  271. conn->deleteObject();
  272. return;
  273. }
  274. conn->setNetworkConnection(true);
  275. conn->onConnectionEstablished(false);
  276. conn->setEstablished();
  277. conn->setConnectSequence(connectSequence);
  278. sendConnectAccept(conn);
  279. }
  280. //-----------------------------------------------------------------------------
  281. void NetInterface::sendConnectAccept(NetConnection *conn)
  282. {
  283. BitStream *out = BitStream::getPacketStream();
  284. out->write(U8(ConnectAccept));
  285. out->write(conn->getSequence());
  286. conn->writeConnectAccept(out);
  287. BitStream::sendPacketStream(conn->getNetAddress());
  288. }
  289. void NetInterface::handleConnectAccept(const NetAddress *address, BitStream *stream)
  290. {
  291. U32 connectSequence;
  292. stream->read(&connectSequence);
  293. NetConnection *conn = findPendingConnection(address, connectSequence);
  294. if(!conn || conn->getConnectionState() != NetConnection::AwaitingConnectResponse)
  295. return;
  296. const char *errorString = NULL;
  297. if(!conn->readConnectAccept(stream, &errorString))
  298. {
  299. conn->handleStartupError(errorString);
  300. removePendingConnection(conn);
  301. conn->deleteObject();
  302. return;
  303. }
  304. removePendingConnection(conn); // remove from the pending connection list
  305. conn->setNetworkConnection(true);
  306. conn->onConnectionEstablished(true); // notify the connection that it has been established
  307. conn->setEstablished(); // installs the connection in the connection table, and causes pings/timeouts to happen
  308. conn->setConnectSequence(connectSequence);
  309. }
  310. void NetInterface::sendConnectReject(NetConnection *conn, const char *reason)
  311. {
  312. if(!reason)
  313. return; // if the stream is NULL, we reject silently
  314. BitStream *out = BitStream::getPacketStream();
  315. out->write(U8(ConnectReject));
  316. out->write(conn->getSequence());
  317. out->writeString(reason);
  318. BitStream::sendPacketStream(conn->getNetAddress());
  319. }
  320. void NetInterface::handleConnectReject(const NetAddress *address, BitStream *stream)
  321. {
  322. U32 connectSequence;
  323. stream->read(&connectSequence);
  324. NetConnection *conn = findPendingConnection(address, connectSequence);
  325. if(!conn || (conn->getConnectionState() != NetConnection::AwaitingChallengeResponse &&
  326. conn->getConnectionState() != NetConnection::AwaitingConnectResponse))
  327. return;
  328. removePendingConnection(conn);
  329. char reason[256];
  330. stream->readString(reason);
  331. conn->onConnectionRejected(reason);
  332. conn->deleteObject();
  333. }
  334. void NetInterface::handleDisconnect(const NetAddress *address, BitStream *stream)
  335. {
  336. NetConnection *conn = NetConnection::lookup(address);
  337. if(!conn)
  338. return;
  339. U32 connectSequence;
  340. char reason[256];
  341. stream->read(&connectSequence);
  342. stream->readString(reason);
  343. if(conn->getSequence() != connectSequence)
  344. return;
  345. conn->onDisconnect(reason);
  346. conn->deleteObject();
  347. }
  348. void NetInterface::handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream)
  349. {
  350. }
  351. void NetInterface::processClient()
  352. {
  353. NetObject::collapseDirtyList(); // collapse all the mask bits...
  354. for(NetConnection *walk = NetConnection::getConnectionList();
  355. walk; walk = walk->getNext())
  356. {
  357. if(walk->isConnectionToServer() && (walk->isLocalConnection() || walk->isNetworkConnection()))
  358. walk->checkPacketSend(false);
  359. }
  360. }
  361. void NetInterface::processServer()
  362. {
  363. NetObject::collapseDirtyList(); // collapse all the mask bits...
  364. for(NetConnection *walk = NetConnection::getConnectionList();
  365. walk; walk = walk->getNext())
  366. {
  367. if(!walk->isConnectionToServer() && (walk->isLocalConnection() || walk->isNetworkConnection()))
  368. walk->checkPacketSend(false);
  369. }
  370. }
  371. void NetInterface::startConnection(NetConnection *conn)
  372. {
  373. addPendingConnection(conn);
  374. conn->mConnectionSendCount = 0;
  375. conn->setConnectSequence(Platform::getVirtualMilliseconds());
  376. conn->setConnectionState(NetConnection::AwaitingChallengeResponse);
  377. // This is a the client side of the connection, so set the connection to
  378. // server flag. We need to set this early so that if the connection times
  379. // out, its onRemove() will handle the cleanup properly.
  380. conn->setIsConnectionToServer();
  381. // Everything set, so send off the request.
  382. sendConnectChallengeRequest(conn);
  383. }
  384. void NetInterface::sendDisconnectPacket(NetConnection *conn, const char *reason)
  385. {
  386. Con::printf("Issuing Disconnect packet.");
  387. // send a disconnect packet...
  388. U32 connectSequence = conn->getSequence();
  389. BitStream *out = BitStream::getPacketStream();
  390. out->write(U8(Disconnect));
  391. out->write(connectSequence);
  392. out->writeString(reason);
  393. BitStream::sendPacketStream(conn->getNetAddress());
  394. }
  395. void NetInterface::checkTimeouts()
  396. {
  397. U32 time = Platform::getVirtualMilliseconds();
  398. if(time > mLastTimeoutCheckTime + TimeoutCheckInterval)
  399. {
  400. for(U32 i = 0; i < (U32)mPendingConnections.size();)
  401. {
  402. NetConnection *pending = mPendingConnections[i];
  403. if(pending->getConnectionState() == NetConnection::AwaitingChallengeResponse &&
  404. time > pending->mConnectLastSendTime + ChallengeRetryTime)
  405. {
  406. if(pending->mConnectSendCount > ChallengeRetryCount)
  407. {
  408. pending->onConnectTimedOut();
  409. removePendingConnection(pending);
  410. pending->deleteObject();
  411. continue;
  412. }
  413. else
  414. sendConnectChallengeRequest(pending);
  415. }
  416. else if(pending->getConnectionState() == NetConnection::AwaitingConnectResponse &&
  417. time > pending->mConnectLastSendTime + ConnectRetryTime)
  418. {
  419. if(pending->mConnectSendCount > ConnectRetryCount)
  420. {
  421. pending->onConnectTimedOut();
  422. removePendingConnection(pending);
  423. pending->deleteObject();
  424. continue;
  425. }
  426. else
  427. sendConnectRequest(pending);
  428. }
  429. i++;
  430. }
  431. mLastTimeoutCheckTime = time;
  432. NetConnection *walk = NetConnection::getConnectionList();
  433. while(walk)
  434. {
  435. NetConnection *next = walk->getNext();
  436. if(walk->checkTimeout(time))
  437. {
  438. // this baddie timed out
  439. walk->onTimedOut();
  440. walk->deleteObject();
  441. }
  442. walk = next;
  443. }
  444. }
  445. }
  446. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  447. #define F2(x, y, z) F1(z, x, y)
  448. #define F3(x, y, z) (x ^ y ^ z)
  449. #define F4(x, y, z) (y ^ (x | ~z))
  450. inline U32 rotlFixed(U32 x, unsigned int y)
  451. {
  452. return (x >> y) | (x << (32 - y));
  453. }
  454. #define MD5STEP(f, w, x, y, z, data, s) w = rotlFixed(w + f(x, y, z) + data, s) + x
  455. void NetInterface::computeNetMD5(const NetAddress *address, U32 connectSequence, U32 digest[4])
  456. {
  457. digest[0] = 0x67452301L;
  458. digest[1] = 0xefcdab89L;
  459. digest[2] = 0x98badcfeL;
  460. digest[3] = 0x10325476L;
  461. U32 a, b, c, d;
  462. a=digest[0];
  463. b=digest[1];
  464. c=digest[2];
  465. d=digest[3];
  466. U32 in[16];
  467. in[0] = address->type;
  468. in[1] = (U32(address->netNum[0]) << 24) |
  469. (U32(address->netNum[1]) << 16) |
  470. (U32(address->netNum[2]) << 8) |
  471. (U32(address->netNum[3]));
  472. in[2] = address->port;
  473. in[3] = connectSequence;
  474. for(U32 i = 0; i < 12; i++)
  475. in[i + 4] = mRandomHashData[i];
  476. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  477. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  478. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  479. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  480. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  481. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  482. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  483. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  484. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  485. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  486. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  487. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  488. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  489. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  490. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  491. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  492. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  493. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  494. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  495. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  496. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  497. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  498. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  499. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  500. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  501. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  502. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  503. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  504. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  505. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  506. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  507. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  508. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  509. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  510. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  511. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  512. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  513. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  514. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  515. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  516. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  517. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  518. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  519. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  520. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  521. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  522. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  523. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  524. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  525. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  526. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  527. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  528. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  529. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  530. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  531. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  532. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  533. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  534. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  535. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  536. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  537. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  538. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  539. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  540. digest[0]+=a;
  541. digest[1]+=b;
  542. digest[2]+=c;
  543. digest[3]+=d;
  544. }