netInterface.cc 22 KB

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