1
0

CryptoAuth.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/CryptoAuth_pvt.h"
  16. #include "crypto/AddressCalc.h"
  17. #include "crypto/ReplayProtector.h"
  18. #include "crypto/random/Random.h"
  19. #include "benc/Dict.h"
  20. #include "benc/List.h"
  21. #include "benc/String.h"
  22. #include "util/log/Log.h"
  23. #include "memory/Allocator.h"
  24. #include "util/Assert.h"
  25. #include "util/AddrTools.h"
  26. #include "util/Bits.h"
  27. #include "util/Defined.h"
  28. #include "util/Endian.h"
  29. #include "util/Hex.h"
  30. #include "util/events/Time.h"
  31. #include "wire/Error.h"
  32. #include "wire/Headers.h"
  33. #include "wire/Message.h"
  34. #include "crypto_box_curve25519xsalsa20poly1305.h"
  35. #include "crypto_hash_sha256.h"
  36. #include "crypto_scalarmult_curve25519.h"
  37. #include <stdint.h>
  38. #include <stdbool.h>
  39. #define USE_RES __attribute__ ((warn_unused_result))
  40. static inline void printHexKey(uint8_t output[65], uint8_t key[32])
  41. {
  42. if (key) {
  43. Hex_encode(output, 65, key, 32);
  44. } else {
  45. Bits_memcpyConst(output, "NULL", 5);
  46. }
  47. }
  48. static inline void printHexPubKey(uint8_t output[65], uint8_t privateKey[32])
  49. {
  50. if (privateKey) {
  51. uint8_t publicKey[32];
  52. crypto_scalarmult_curve25519_base(publicKey, privateKey);
  53. printHexKey(output, publicKey);
  54. } else {
  55. printHexKey(output, NULL);
  56. }
  57. }
  58. /**
  59. * Get a shared secret.
  60. *
  61. * @param outputSecret an array to place the shared secret in.
  62. * @param myPrivateKey
  63. * @param herPublicKey
  64. * @param logger
  65. * @param passwordHash a 32 byte value known to both ends, this must be provably pseudorandom
  66. * the first 32 bytes of a sha256 output from hashing a password is ok,
  67. * whatever she happens to send me in the Auth field is NOT ok.
  68. * If this field is null, the secret will be generated without the password.
  69. */
  70. static inline void getSharedSecret(uint8_t outputSecret[32],
  71. uint8_t myPrivateKey[32],
  72. uint8_t herPublicKey[32],
  73. uint8_t passwordHash[32],
  74. struct Log* logger)
  75. {
  76. if (passwordHash == NULL) {
  77. crypto_box_curve25519xsalsa20poly1305_beforenm(outputSecret, herPublicKey, myPrivateKey);
  78. } else {
  79. union {
  80. struct {
  81. uint8_t key[32];
  82. uint8_t passwd[32];
  83. } components;
  84. uint8_t bytes[64];
  85. } buff;
  86. crypto_scalarmult_curve25519(buff.components.key, myPrivateKey, herPublicKey);
  87. Bits_memcpyConst(buff.components.passwd, passwordHash, 32);
  88. crypto_hash_sha256(outputSecret, buff.bytes, 64);
  89. }
  90. if (Defined(Log_KEYS)) {
  91. uint8_t myPublicKeyHex[65];
  92. printHexPubKey(myPublicKeyHex, myPrivateKey);
  93. uint8_t herPublicKeyHex[65];
  94. printHexKey(herPublicKeyHex, herPublicKey);
  95. uint8_t passwordHashHex[65];
  96. printHexKey(passwordHashHex, passwordHash);
  97. uint8_t outputSecretHex[65] = "NULL";
  98. printHexKey(outputSecretHex, outputSecret);
  99. Log_keys(logger,
  100. "Generated a shared secret:\n"
  101. " myPublicKey=%s\n"
  102. " herPublicKey=%s\n"
  103. " passwordHash=%s\n"
  104. " outputSecret=%s\n",
  105. myPublicKeyHex, herPublicKeyHex, passwordHashHex, outputSecretHex);
  106. }
  107. }
  108. static inline void hashPassword_sha256(struct CryptoAuth_Auth* auth, const String* password)
  109. {
  110. uint8_t tempBuff[32];
  111. crypto_hash_sha256(auth->secret, (uint8_t*) password->bytes, password->len);
  112. crypto_hash_sha256(tempBuff, auth->secret, 32);
  113. Bits_memcpyConst(auth->challenge.bytes, tempBuff, CryptoHeader_Challenge_SIZE);
  114. CryptoHeader_setAuthChallengeDerivations(&auth->challenge, 0);
  115. auth->challenge.challenge.type = 1;
  116. }
  117. static inline uint8_t* hashPassword(struct CryptoAuth_Auth* auth,
  118. const String* password,
  119. const uint8_t authType)
  120. {
  121. switch (authType) {
  122. case 1:
  123. hashPassword_sha256(auth, password);
  124. break;
  125. default:
  126. Assert_true(!"Unsupported auth type.");
  127. };
  128. return auth->secret;
  129. }
  130. /**
  131. * Search the authorized passwords for one matching this auth header.
  132. *
  133. * @param auth the auth header.
  134. * @param context the CryptoAuth engine to search in.
  135. * @return an Auth struct with a if one is found, otherwise NULL.
  136. */
  137. static inline struct CryptoAuth_Auth* getAuth(union CryptoHeader_Challenge auth,
  138. struct CryptoAuth_pvt* context)
  139. {
  140. if (auth.challenge.type != 1) {
  141. return NULL;
  142. }
  143. for (uint32_t i = 0; i < context->passwordCount; i++) {
  144. if (Bits_memcmp(auth.bytes, &context->passwords[i], CryptoHeader_Challenge_KEYSIZE) == 0) {
  145. return &context->passwords[i];
  146. }
  147. }
  148. Log_debug(context->logger, "Got unrecognized auth, password count = [%d]",
  149. context->passwordCount);
  150. return NULL;
  151. }
  152. static inline void getPasswordHash_typeOne(uint8_t output[32],
  153. uint16_t derivations,
  154. struct CryptoAuth_Auth* auth)
  155. {
  156. Bits_memcpyConst(output, auth->secret, 32);
  157. if (derivations) {
  158. union {
  159. uint8_t bytes[2];
  160. uint8_t asShort;
  161. } deriv = { .asShort = derivations };
  162. output[0] ^= deriv.bytes[0];
  163. output[1] ^= deriv.bytes[1];
  164. crypto_hash_sha256(output, output, 32);
  165. }
  166. }
  167. static inline uint8_t* tryAuth(union CryptoHeader* cauth,
  168. uint8_t hashOutput[32],
  169. struct CryptoAuth_Session_pvt* session,
  170. struct CryptoAuth_Auth** retAuth)
  171. {
  172. struct CryptoAuth_Auth* auth = getAuth(cauth->handshake.auth, session->context);
  173. if (auth != NULL) {
  174. uint16_t deriv = CryptoHeader_getAuthChallengeDerivations(&cauth->handshake.auth);
  175. getPasswordHash_typeOne(hashOutput, deriv, auth);
  176. if (deriv == 0) {
  177. *retAuth = auth;
  178. }
  179. return hashOutput;
  180. }
  181. return NULL;
  182. }
  183. /**
  184. * Decrypt and authenticate.
  185. *
  186. * @param nonce a 24 byte number, may be random, cannot repeat.
  187. * @param msg a message to encipher and authenticate.
  188. * @param secret a shared secret.
  189. * @return 0 if decryption is succeddful, otherwise -1.
  190. */
  191. static inline USE_RES int decryptRndNonce(uint8_t nonce[24],
  192. struct Message* msg,
  193. uint8_t secret[32])
  194. {
  195. if (msg->length < 16) {
  196. return -1;
  197. }
  198. Assert_true(msg->padding >= 16);
  199. uint8_t* startAt = msg->bytes - 16;
  200. uint8_t paddingSpace[16];
  201. Bits_memcpyConst(paddingSpace, startAt, 16);
  202. Bits_memset(startAt, 0, 16);
  203. if (crypto_box_curve25519xsalsa20poly1305_open_afternm(
  204. startAt, startAt, msg->length + 16, nonce, secret) != 0)
  205. {
  206. return -1;
  207. }
  208. Bits_memcpyConst(startAt, paddingSpace, 16);
  209. Message_shift(msg, -16, NULL);
  210. return 0;
  211. }
  212. /**
  213. * Encrypt and authenticate.
  214. * Shifts the message by 16 bytes.
  215. *
  216. * @param nonce a 24 byte number, may be random, cannot repeat.
  217. * @param msg a message to encipher and authenticate.
  218. * @param secret a shared secret.
  219. */
  220. static inline void encryptRndNonce(uint8_t nonce[24],
  221. struct Message* msg,
  222. uint8_t secret[32])
  223. {
  224. Assert_true(msg->padding >= 32);
  225. uint8_t* startAt = msg->bytes - 32;
  226. // This function trashes 16 bytes of the padding so we will put it back
  227. uint8_t paddingSpace[16];
  228. Bits_memcpyConst(paddingSpace, startAt, 16);
  229. Bits_memset(startAt, 0, 32);
  230. crypto_box_curve25519xsalsa20poly1305_afternm(
  231. startAt, startAt, msg->length + 32, nonce, secret);
  232. Bits_memcpyConst(startAt, paddingSpace, 16);
  233. Message_shift(msg, 16, NULL);
  234. }
  235. /**
  236. * Decrypt a packet.
  237. *
  238. * @param nonce a counter.
  239. * @param msg the message to decrypt, decrypted in place.
  240. * @param secret the shared secret.
  241. * @param isInitiator true if we started the connection.
  242. */
  243. static inline USE_RES int decrypt(uint32_t nonce,
  244. struct Message* msg,
  245. uint8_t secret[32],
  246. bool isInitiator)
  247. {
  248. union {
  249. uint32_t ints[2];
  250. uint8_t bytes[24];
  251. } nonceAs = { .ints = {0, 0} };
  252. nonceAs.ints[!isInitiator] = Endian_hostToLittleEndian32(nonce);
  253. return decryptRndNonce(nonceAs.bytes, msg, secret);
  254. }
  255. /**
  256. * Encrypt a packet.
  257. *
  258. * @param nonce a counter.
  259. * @param msg the message to decrypt, decrypted in place.
  260. * @param secret the shared secret.
  261. * @param isInitiator true if we started the connection.
  262. */
  263. static inline void encrypt(uint32_t nonce,
  264. struct Message* msg,
  265. uint8_t secret[32],
  266. bool isInitiator)
  267. {
  268. union {
  269. uint32_t ints[2];
  270. uint8_t bytes[24];
  271. } nonceAs = { .ints = {0, 0} };
  272. nonceAs.ints[isInitiator] = Endian_hostToLittleEndian32(nonce);
  273. encryptRndNonce(nonceAs.bytes, msg, secret);
  274. }
  275. static inline bool knowHerKey(struct CryptoAuth_Session_pvt* session)
  276. {
  277. return !Bits_isZero(session->pub.herPublicKey, 32);
  278. }
  279. static void getIp6(struct CryptoAuth_Session_pvt* session, uint8_t* addr)
  280. {
  281. if (knowHerKey(session)) {
  282. uint8_t ip6[16];
  283. AddressCalc_addressForPublicKey(ip6, session->pub.herPublicKey);
  284. AddrTools_printIp(addr, ip6);
  285. }
  286. }
  287. #define cryptoAuthDebug(wrapper, format, ...) \
  288. { \
  289. uint8_t addr[40] = "unknown"; \
  290. getIp6((session), addr); \
  291. Log_debug((session)->context->logger, \
  292. "%p %s [%s]: " format, (void*)(session), (session)->name, addr, __VA_ARGS__); \
  293. }
  294. #define cryptoAuthDebug0(wrapper, format) \
  295. cryptoAuthDebug(session, format "%s", "")
  296. static void reset(struct CryptoAuth_Session_pvt* session)
  297. {
  298. session->nextNonce = 0;
  299. session->isInitiator = false;
  300. Bits_memset(session->ourTempPrivKey, 0, 32);
  301. Bits_memset(session->ourTempPubKey, 0, 32);
  302. Bits_memset(session->herTempPubKey, 0, 32);
  303. Bits_memset(session->sharedSecret, 0, 32);
  304. session->established = false;
  305. Bits_memset(&session->pub.replayProtector, 0, sizeof(struct ReplayProtector));
  306. }
  307. static void resetIfTimeout(struct CryptoAuth_Session_pvt* session)
  308. {
  309. if (session->nextNonce == 1) {
  310. // Lets not reset the session, we just sent one or more hello packets and
  311. // have not received a response, if they respond after we reset then we'll
  312. // be in a tough state.
  313. return;
  314. }
  315. uint64_t nowSecs = Time_currentTimeSeconds(session->context->eventBase);
  316. if (nowSecs - session->timeOfLastPacket > session->context->pub.resetAfterInactivitySeconds) {
  317. cryptoAuthDebug(session, "No traffic in [%d] seconds, resetting connection.",
  318. (int) (nowSecs - session->timeOfLastPacket));
  319. session->timeOfLastPacket = nowSecs;
  320. reset(session);
  321. }
  322. }
  323. static void encryptHandshake(struct Message* message,
  324. struct CryptoAuth_Session_pvt* session,
  325. int setupMessage)
  326. {
  327. Message_shift(message, sizeof(union CryptoHeader), NULL);
  328. union CryptoHeader* header = (union CryptoHeader*) message->bytes;
  329. // garbage the auth challenge and set the nonce which follows it
  330. Random_bytes(session->context->rand, (uint8_t*) &header->handshake.auth,
  331. sizeof(union CryptoHeader_Challenge) + 24);
  332. // set the permanent key
  333. Bits_memcpyConst(&header->handshake.publicKey, session->context->pub.publicKey, 32);
  334. Assert_true(knowHerKey(session));
  335. uint8_t calculatedIp6[16];
  336. AddressCalc_addressForPublicKey(calculatedIp6, session->pub.herPublicKey);
  337. if (!Bits_isZero(session->pub.herIp6, 16)) {
  338. // If someone starts a CA session and then discovers the key later and memcpy's it into the
  339. // result of getHerPublicKey() then we want to make sure they didn't memcpy in an invalid
  340. // key.
  341. Assert_true(!Bits_memcmp(session->pub.herIp6, calculatedIp6, 16));
  342. }
  343. // Password auth
  344. uint8_t* passwordHash = NULL;
  345. struct CryptoAuth_Auth auth;
  346. if (session->password != NULL) {
  347. passwordHash = hashPassword(&auth, session->password, session->authType);
  348. Bits_memcpyConst(header->handshake.auth.bytes,
  349. &auth.challenge,
  350. sizeof(union CryptoHeader_Challenge));
  351. }
  352. header->handshake.auth.challenge.type = session->authType;
  353. // Packet authentication option is deprecated, it must always be enabled.
  354. CryptoHeader_setPacketAuthRequired(&header->handshake.auth, 1);
  355. header->handshake.auth.challenge.additional = 0;
  356. // Set the session state
  357. header->nonce = Endian_hostToBigEndian32(session->nextNonce);
  358. if (session->nextNonce == 0 || session->nextNonce == 2) {
  359. // If we're sending a hello or a key
  360. // Here we make up a temp keypair
  361. Random_bytes(session->context->rand, session->ourTempPrivKey, 32);
  362. crypto_scalarmult_curve25519_base(session->ourTempPubKey, session->ourTempPrivKey);
  363. if (Defined(Log_KEYS)) {
  364. uint8_t tempPrivateKeyHex[65];
  365. Hex_encode(tempPrivateKeyHex, 65, session->ourTempPrivKey, 32);
  366. uint8_t tempPubKeyHex[65];
  367. Hex_encode(tempPubKeyHex, 65, session->ourTempPubKey, 32);
  368. Log_keys(session->context->logger, "Generating temporary keypair\n"
  369. " myTempPrivateKey=%s\n"
  370. " myTempPublicKey=%s\n",
  371. tempPrivateKeyHex, tempPubKeyHex);
  372. }
  373. }
  374. Bits_memcpyConst(header->handshake.encryptedTempKey, session->ourTempPubKey, 32);
  375. if (Defined(Log_KEYS)) {
  376. uint8_t tempKeyHex[65];
  377. Hex_encode(tempKeyHex, 65, header->handshake.encryptedTempKey, 32);
  378. Log_keys(session->context->logger,
  379. "Wrapping temp public key:\n"
  380. " %s\n",
  381. tempKeyHex);
  382. }
  383. cryptoAuthDebug(session, "Sending %s%s packet",
  384. ((session->nextNonce & 1) ? "repeat " : ""),
  385. ((session->nextNonce < 2) ? "hello" : "key"));
  386. uint8_t sharedSecret[32];
  387. if (session->nextNonce < 2) {
  388. getSharedSecret(sharedSecret,
  389. session->context->privateKey,
  390. session->pub.herPublicKey,
  391. passwordHash,
  392. session->context->logger);
  393. session->isInitiator = true;
  394. Assert_true(session->nextNonce <= 1);
  395. session->nextNonce = 1;
  396. } else {
  397. // Handshake2
  398. // herTempPubKey was set by decryptHandshake()
  399. Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
  400. getSharedSecret(sharedSecret,
  401. session->context->privateKey,
  402. session->herTempPubKey,
  403. passwordHash,
  404. session->context->logger);
  405. Assert_true(session->nextNonce <= 3);
  406. session->nextNonce = 3;
  407. if (Defined(Log_KEYS)) {
  408. uint8_t tempKeyHex[65];
  409. Hex_encode(tempKeyHex, 65, session->herTempPubKey, 32);
  410. Log_keys(session->context->logger,
  411. "Using their temp public key:\n"
  412. " %s\n",
  413. tempKeyHex);
  414. }
  415. }
  416. // Shift message over the encryptedTempKey field.
  417. Message_shift(message, 32 - CryptoHeader_SIZE, NULL);
  418. encryptRndNonce(header->handshake.nonce, message, sharedSecret);
  419. if (Defined(Log_KEYS)) {
  420. uint8_t sharedSecretHex[65];
  421. printHexKey(sharedSecretHex, sharedSecret);
  422. uint8_t nonceHex[49];
  423. Hex_encode(nonceHex, 49, header->handshake.nonce, 24);
  424. uint8_t cipherHex[65];
  425. printHexKey(cipherHex, message->bytes);
  426. Log_keys(session->context->logger,
  427. "Encrypting message with:\n"
  428. " nonce: %s\n"
  429. " secret: %s\n"
  430. " cipher: %s\n",
  431. nonceHex, sharedSecretHex, cipherHex);
  432. }
  433. // Shift it back -- encryptRndNonce adds 16 bytes of authenticator.
  434. Message_shift(message, CryptoHeader_SIZE - 32 - 16, NULL);
  435. }
  436. /** @return 0 on success, -1 otherwise. */
  437. int CryptoAuth_encrypt(struct CryptoAuth_Session* sessionPub, struct Message* msg)
  438. {
  439. struct CryptoAuth_Session_pvt* session =
  440. Identity_check((struct CryptoAuth_Session_pvt*) sessionPub);
  441. // If there has been no incoming traffic for a while, reset the connection to state 0.
  442. // This will prevent "connection in bad state" situations from lasting forever.
  443. // this will reset the session if it has timed out.
  444. resetIfTimeout(session);
  445. // If the nonce wraps, start over.
  446. if (session->nextNonce >= 0xfffffff0) {
  447. reset(session);
  448. }
  449. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  450. // nextNonce 0: sending hello, we are initiating connection.
  451. // nextNonce 1: sending another hello, nothing received yet.
  452. // nextNonce 2: sending key, hello received.
  453. // nextNonce 3: sending key again, no data packet recieved yet.
  454. // nextNonce >3: handshake complete
  455. //
  456. // if it's a blind handshake, every message will be empty and nextNonce will remain
  457. // zero until the first message is received back.
  458. if (session->nextNonce < 5) {
  459. if (session->nextNonce < 4) {
  460. encryptHandshake(msg, session, 0);
  461. return 0;
  462. } else {
  463. cryptoAuthDebug0(session, "Doing final step to send message. nonce=4");
  464. Assert_ifParanoid(!Bits_isZero(session->ourTempPrivKey, 32));
  465. Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
  466. getSharedSecret(session->sharedSecret,
  467. session->ourTempPrivKey,
  468. session->herTempPubKey,
  469. NULL,
  470. session->context->logger);
  471. }
  472. }
  473. Assert_true(msg->length > 0 && "Empty packet during handshake");
  474. Assert_true(msg->padding >= 36 || !"not enough padding");
  475. encrypt(session->nextNonce, msg, session->sharedSecret, session->isInitiator);
  476. Message_push32(msg, session->nextNonce, NULL);
  477. session->nextNonce++;
  478. return 0;
  479. }
  480. /** Call the external interface and tell it that a message has been received. */
  481. static inline void updateTime(struct CryptoAuth_Session_pvt* session, struct Message* message)
  482. {
  483. session->timeOfLastPacket = Time_currentTimeSeconds(session->context->eventBase);
  484. }
  485. static inline USE_RES bool decryptMessage(struct CryptoAuth_Session_pvt* session,
  486. uint32_t nonce,
  487. struct Message* content,
  488. uint8_t secret[32])
  489. {
  490. // Decrypt with authentication and replay prevention.
  491. if (decrypt(nonce, content, secret, session->isInitiator)) {
  492. cryptoAuthDebug0(session, "DROP authenticated decryption failed");
  493. return false;
  494. }
  495. if (!ReplayProtector_checkNonce(nonce, &session->pub.replayProtector)) {
  496. cryptoAuthDebug(session, "DROP nonce checking failed nonce=[%u]", nonce);
  497. return false;
  498. }
  499. return true;
  500. }
  501. static bool ip6MatchesKey(uint8_t ip6[16], uint8_t key[32])
  502. {
  503. uint8_t calculatedIp6[16];
  504. AddressCalc_addressForPublicKey(calculatedIp6, key);
  505. return !Bits_memcmp(ip6, calculatedIp6, 16);
  506. }
  507. static USE_RES int decryptHandshake(struct CryptoAuth_Session_pvt* session,
  508. const uint32_t nonce,
  509. struct Message* message,
  510. union CryptoHeader* header)
  511. {
  512. if (message->length < CryptoHeader_SIZE) {
  513. cryptoAuthDebug0(session, "DROP runt");
  514. return -1;
  515. }
  516. // handshake
  517. // nextNonce 0: recieving hello.
  518. // nextNonce 1: recieving key, we sent hello.
  519. // nextNonce 2: recieving first data packet or duplicate hello.
  520. // nextNonce 3: recieving first data packet.
  521. // nextNonce >3: handshake complete
  522. if (knowHerKey(session)) {
  523. if (Bits_memcmp(session->pub.herPublicKey, header->handshake.publicKey, 32)) {
  524. cryptoAuthDebug0(session, "DROP a packet with different public key than this session");
  525. return -1;
  526. }
  527. } else if (Bits_isZero(session->pub.herIp6, 16)) {
  528. // ok fallthrough
  529. } else if (!ip6MatchesKey(session->pub.herIp6, header->handshake.publicKey)) {
  530. cryptoAuthDebug0(session, "DROP packet with public key not matching ip6 for session");
  531. return -1;
  532. }
  533. String* user = NULL;
  534. struct CryptoAuth_Auth* auth = NULL;
  535. uint8_t passwordHashStore[32];
  536. uint8_t* passwordHash = tryAuth(header, passwordHashStore, session, &auth);
  537. uint8_t* restrictedToip6 = NULL;
  538. if (auth) {
  539. user = auth->user;
  540. if (auth->restrictedToip6) {
  541. restrictedToip6 = auth->restrictedToip6;
  542. if (!ip6MatchesKey(auth->restrictedToip6, header->handshake.publicKey)) {
  543. cryptoAuthDebug0(session, "DROP packet with key not matching restrictedToip6");
  544. return -1;
  545. }
  546. }
  547. }
  548. if (session->requireAuth && !user) {
  549. cryptoAuthDebug0(session, "DROP message because auth was not given");
  550. return -1;
  551. }
  552. if (passwordHash == NULL && header->handshake.auth.challenge.type != 0) {
  553. cryptoAuthDebug0(session, "DROP message with unrecognized authenticator");
  554. return -1;
  555. }
  556. // What the nextNonce will become if this packet is valid.
  557. uint32_t nextNonce;
  558. // The secret for decrypting this message.
  559. uint8_t sharedSecret[32];
  560. uint8_t* herPermKey = NULL;
  561. if (nonce < 2) {
  562. if (nonce == 0) {
  563. cryptoAuthDebug(session, "Received a hello packet, using auth: %d",
  564. (passwordHash != NULL));
  565. } else {
  566. cryptoAuthDebug0(session, "Received a repeat hello packet");
  567. }
  568. // Decrypt message with perminent keys.
  569. if (!knowHerKey(session) || session->nextNonce == 0) {
  570. herPermKey = header->handshake.publicKey;
  571. if (Defined(Log_DEBUG) && Bits_isZero(header->handshake.publicKey, 32)) {
  572. cryptoAuthDebug0(session, "Node sent public key of ZERO!");
  573. }
  574. } else {
  575. herPermKey = session->pub.herPublicKey;
  576. if (Bits_memcmp(header->handshake.publicKey, herPermKey, 32)) {
  577. cryptoAuthDebug0(session, "DROP packet contains different perminent key");
  578. return -1;
  579. }
  580. }
  581. getSharedSecret(sharedSecret,
  582. session->context->privateKey,
  583. herPermKey,
  584. passwordHash,
  585. session->context->logger);
  586. nextNonce = 2;
  587. } else {
  588. if (nonce == 2) {
  589. cryptoAuthDebug0(session, "Received a key packet");
  590. } else if (nonce == 3) {
  591. cryptoAuthDebug0(session, "Received a repeat key packet");
  592. } else {
  593. cryptoAuthDebug(session, "Received a packet of unknown type! nonce=%u", nonce);
  594. }
  595. if (Bits_memcmp(header->handshake.publicKey, session->pub.herPublicKey, 32)) {
  596. cryptoAuthDebug0(session, "DROP packet contains different perminent key");
  597. return -1;
  598. }
  599. if (!session->isInitiator) {
  600. cryptoAuthDebug0(session, "DROP a stray key packet");
  601. return -1;
  602. }
  603. // We sent the hello, this is a key
  604. getSharedSecret(sharedSecret,
  605. session->ourTempPrivKey,
  606. session->pub.herPublicKey,
  607. passwordHash,
  608. session->context->logger);
  609. nextNonce = 4;
  610. }
  611. // Shift it on top of the authenticator before the encrypted public key
  612. Message_shift(message, 48 - CryptoHeader_SIZE, NULL);
  613. if (Defined(Log_KEYS)) {
  614. uint8_t sharedSecretHex[65];
  615. printHexKey(sharedSecretHex, sharedSecret);
  616. uint8_t nonceHex[49];
  617. Hex_encode(nonceHex, 49, header->handshake.nonce, 24);
  618. uint8_t cipherHex[65];
  619. printHexKey(cipherHex, message->bytes);
  620. Log_keys(session->context->logger,
  621. "Decrypting message with:\n"
  622. " nonce: %s\n"
  623. " secret: %s\n"
  624. " cipher: %s\n",
  625. nonceHex, sharedSecretHex, cipherHex);
  626. }
  627. // Decrypt her temp public key and the message.
  628. if (decryptRndNonce(header->handshake.nonce, message, sharedSecret)) {
  629. // just in case
  630. Bits_memset(header, 0, CryptoHeader_SIZE);
  631. cryptoAuthDebug(session, "DROP message with nonce [%d], decryption failed", nonce);
  632. return -1;
  633. }
  634. if (Bits_isZero(header->handshake.encryptedTempKey, 32)) {
  635. // we need to reject 0 public keys outright because they will be confused with "unknown"
  636. return -1;
  637. }
  638. if (Defined(Log_KEYS)) {
  639. uint8_t tempKeyHex[65];
  640. Hex_encode(tempKeyHex, 65, header->handshake.encryptedTempKey, 32);
  641. Log_keys(session->context->logger,
  642. "Unwrapping temp public key:\n"
  643. " %s\n",
  644. tempKeyHex);
  645. }
  646. Message_shift(message, -32, NULL);
  647. // Post-decryption checking
  648. if (nonce == 0) {
  649. // A new hello packet
  650. if (!Bits_memcmp(session->herTempPubKey, header->handshake.encryptedTempKey, 32)) {
  651. // possible replay attack or duped packet
  652. cryptoAuthDebug0(session, "DROP dupe hello packet with same temp key");
  653. return -1;
  654. }
  655. } else if (nonce == 2 && session->nextNonce >= 4) {
  656. // we accept a new key packet and let it change the session since the other end might have
  657. // killed off the session while it was in the midst of setting up.
  658. if (!Bits_memcmp(session->herTempPubKey, header->handshake.encryptedTempKey, 32)) {
  659. Assert_true(!Bits_isZero(session->herTempPubKey, 32));
  660. cryptoAuthDebug0(session, "DROP dupe key packet with same temp key");
  661. return -1;
  662. }
  663. } else if (nonce == 3 && session->nextNonce >= 4) {
  664. // Got a repeat key packet, make sure the temp key is the same as the one we know.
  665. if (Bits_memcmp(session->herTempPubKey, header->handshake.encryptedTempKey, 32)) {
  666. Assert_true(!Bits_isZero(session->herTempPubKey, 32));
  667. cryptoAuthDebug0(session, "DROP repeat key packet with different temp key");
  668. return -1;
  669. }
  670. }
  671. // If Alice sent a hello packet then Bob sent a hello packet and they crossed on the wire,
  672. // somebody has to yield and the other has to stand firm otherwise they will either deadlock
  673. // each believing their hello packet is superior or they will livelock, each switching to the
  674. // other's session and never synchronizing.
  675. // In this event whoever has the lower permanent public key wins.
  676. // If we receive a (possibly repeat) key packet
  677. if (nextNonce == 4) {
  678. if (session->nextNonce <= 4) {
  679. // and have not yet begun sending "run" data
  680. Assert_true(session->nextNonce <= nextNonce);
  681. session->nextNonce = nextNonce;
  682. session->pub.userName = user;
  683. Bits_memcpyConst(session->herTempPubKey, header->handshake.encryptedTempKey, 32);
  684. } else {
  685. // It's a (possibly repeat) key packet and we have begun sending run data.
  686. // We will change the shared secret to the one specified in the new key packet but
  687. // intentionally avoid de-incrementing the nonce just in case
  688. getSharedSecret(session->sharedSecret,
  689. session->ourTempPrivKey,
  690. header->handshake.encryptedTempKey,
  691. NULL,
  692. session->context->logger);
  693. cryptoAuthDebug0(session, "New key packet but we are already sending data");
  694. }
  695. } else if (nextNonce != 2) {
  696. Assert_true(!"should never happen");
  697. } else if (!session->isInitiator || session->established) {
  698. // This is a hello packet and we are either in ESTABLISHED state or we are
  699. // not the initiator of the connection.
  700. // If the case is that we are in ESTABLISHED state, the other side tore down the session
  701. // and we have not so lets tear it down.
  702. // If we are not in ESTABLISHED state then we don't allow resetting of the session unless
  703. // they are the sender of the hello packet or their permanent public key is lower.
  704. // this is a tie-breaker in case hello packets cross on the wire.
  705. if (session->established) {
  706. reset(session);
  707. }
  708. // We got a (possibly repeat) hello packet and we have not sent any hello packet,
  709. // new session.
  710. if (session->nextNonce == 3) {
  711. // We sent a key packet so the next packet is a repeat key but we got another hello
  712. // We'll just keep steaming along sending repeat key packets
  713. nextNonce = 3;
  714. }
  715. Assert_true(session->nextNonce <= nextNonce);
  716. session->nextNonce = nextNonce;
  717. session->pub.userName = user;
  718. if (restrictedToip6) {
  719. Bits_memcpyConst(session->pub.herIp6, restrictedToip6, 16);
  720. }
  721. Bits_memcpyConst(session->herTempPubKey, header->handshake.encryptedTempKey, 32);
  722. } else if (Bits_memcmp(header->handshake.publicKey, session->context->pub.publicKey, 32) < 0) {
  723. // It's a hello and we are the initiator but their permant public key is numerically lower
  724. // than ours, this is so that in the event of two hello packets crossing on the wire, the
  725. // nodes will agree on who is the initiator.
  726. cryptoAuthDebug0(session, "Incoming hello from node with lower key, resetting");
  727. reset(session);
  728. Assert_true(session->nextNonce <= nextNonce);
  729. session->nextNonce = nextNonce;
  730. session->pub.userName = user;
  731. if (restrictedToip6) {
  732. Bits_memcpyConst(session->pub.herIp6, restrictedToip6, 16);
  733. }
  734. Bits_memcpyConst(session->herTempPubKey, header->handshake.encryptedTempKey, 32);
  735. } else {
  736. cryptoAuthDebug0(session, "Incoming hello from node with higher key, not resetting");
  737. }
  738. if (herPermKey && herPermKey != session->pub.herPublicKey) {
  739. Bits_memcpyConst(session->pub.herPublicKey, herPermKey, 32);
  740. }
  741. Bits_memset(&session->pub.replayProtector, 0, sizeof(struct ReplayProtector));
  742. return 0;
  743. }
  744. /** @return 0 on success, -1 otherwise. */
  745. int CryptoAuth_decrypt(struct CryptoAuth_Session* sessionPub, struct Message* msg)
  746. {
  747. struct CryptoAuth_Session_pvt* session =
  748. Identity_check((struct CryptoAuth_Session_pvt*) sessionPub);
  749. union CryptoHeader* header = (union CryptoHeader*) msg->bytes;
  750. if (msg->length < 20) {
  751. cryptoAuthDebug0(session, "DROP runt");
  752. return -1;
  753. }
  754. Assert_true(msg->padding >= 12 || "need at least 12 bytes of padding in incoming message");
  755. Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
  756. Assert_true(!(msg->capacity % 4) || !"length fault");
  757. Message_shift(msg, -4, NULL);
  758. uint32_t nonce = Endian_bigEndianToHost32(header->nonce);
  759. if (!session->established) {
  760. if (nonce > 3 && nonce != UINT32_MAX) {
  761. if (session->nextNonce < 3) {
  762. // This is impossible because we have not exchanged hello and key messages.
  763. cryptoAuthDebug0(session, "DROP Received a run message to an un-setup session");
  764. return -1;
  765. }
  766. cryptoAuthDebug(session, "Trying final handshake step, nonce=%u\n", nonce);
  767. uint8_t secret[32];
  768. Assert_ifParanoid(!Bits_isZero(session->ourTempPrivKey, 32));
  769. Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
  770. getSharedSecret(secret,
  771. session->ourTempPrivKey,
  772. session->herTempPubKey,
  773. NULL,
  774. session->context->logger);
  775. // We'll optimistically advance the nextNonce value because decryptMessage()
  776. // passes the message on to the upper level and if this message causes a
  777. // response, we want the CA to be in ESTABLISHED state.
  778. // if the decryptMessage() call fails, we CryptoAuth_reset() it back.
  779. session->nextNonce += 3;
  780. if (decryptMessage(session, nonce, msg, secret)) {
  781. cryptoAuthDebug0(session, "Final handshake step succeeded");
  782. Bits_memcpyConst(session->sharedSecret, secret, 32);
  783. // Now we're in run mode, no more handshake packets will be accepted
  784. Bits_memset(session->ourTempPrivKey, 0, 32);
  785. Bits_memset(session->ourTempPubKey, 0, 32);
  786. Bits_memset(session->herTempPubKey, 0, 32);
  787. session->established = true;
  788. updateTime(session, msg);
  789. return 0;
  790. }
  791. reset(session);
  792. cryptoAuthDebug0(session, "DROP Final handshake step failed");
  793. return -1;
  794. }
  795. Message_shift(msg, 4, NULL);
  796. return decryptHandshake(session, nonce, msg, header);
  797. } else if (nonce > 3 && nonce != UINT32_MAX) {
  798. Assert_ifParanoid(!Bits_isZero(session->sharedSecret, 32));
  799. if (decryptMessage(session, nonce, msg, session->sharedSecret)) {
  800. updateTime(session, msg);
  801. return 0;
  802. } else {
  803. cryptoAuthDebug0(session, "DROP Failed to decrypt message");
  804. return -1;
  805. }
  806. } else if (nonce < 2) {
  807. cryptoAuthDebug(session, "hello packet during established session nonce=[%d]", nonce);
  808. Message_shift(msg, 4, NULL);
  809. return decryptHandshake(session, nonce, msg, header);
  810. } else {
  811. // setup keys are already zeroed, not much we can do here.
  812. cryptoAuthDebug(session, "DROP key packet during established session nonce=[%d]", nonce);
  813. return -1;
  814. }
  815. Assert_true(0);
  816. }
  817. /////////////////////////////////////////////////////////////////////////////////////////////////
  818. struct CryptoAuth* CryptoAuth_new(struct Allocator* allocator,
  819. const uint8_t* privateKey,
  820. struct EventBase* eventBase,
  821. struct Log* logger,
  822. struct Random* rand)
  823. {
  824. struct CryptoAuth_pvt* ca = Allocator_calloc(allocator, sizeof(struct CryptoAuth_pvt), 1);
  825. Identity_set(ca);
  826. ca->allocator = allocator;
  827. ca->passwords = Allocator_calloc(allocator, sizeof(struct CryptoAuth_Auth), 232);
  828. ca->passwordCount = 0;
  829. ca->passwordCapacity = 232;
  830. ca->eventBase = eventBase;
  831. ca->logger = logger;
  832. ca->pub.resetAfterInactivitySeconds = CryptoAuth_DEFAULT_RESET_AFTER_INACTIVITY_SECONDS;
  833. ca->rand = rand;
  834. if (privateKey != NULL) {
  835. Bits_memcpyConst(ca->privateKey, privateKey, 32);
  836. } else {
  837. Random_bytes(rand, ca->privateKey, 32);
  838. }
  839. crypto_scalarmult_curve25519_base(ca->pub.publicKey, ca->privateKey);
  840. if (Defined(Log_KEYS)) {
  841. uint8_t publicKeyHex[65];
  842. printHexKey(publicKeyHex, ca->pub.publicKey);
  843. uint8_t privateKeyHex[65];
  844. printHexKey(privateKeyHex, ca->privateKey);
  845. Log_keys(logger,
  846. "Initialized CryptoAuth:\n myPrivateKey=%s\n myPublicKey=%s\n",
  847. privateKeyHex,
  848. publicKeyHex);
  849. }
  850. return &ca->pub;
  851. }
  852. int32_t CryptoAuth_addUser(String* password,
  853. uint8_t authType,
  854. String* user,
  855. struct CryptoAuth* ca)
  856. {
  857. return CryptoAuth_addUser_ipv6(password, authType, user, NULL, ca);
  858. }
  859. int32_t CryptoAuth_addUser_ipv6(String* password,
  860. uint8_t authType,
  861. String* user,
  862. String* ipv6,
  863. struct CryptoAuth* ca)
  864. {
  865. struct CryptoAuth_pvt* context = Identity_check((struct CryptoAuth_pvt*) ca);
  866. if (authType != 1) {
  867. return CryptoAuth_addUser_INVALID_AUTHTYPE;
  868. }
  869. if (context->passwordCount == context->passwordCapacity) {
  870. // TODO(cjd): realloc password space and increase buffer.
  871. return CryptoAuth_addUser_OUT_OF_SPACE;
  872. }
  873. struct CryptoAuth_Auth a;
  874. hashPassword_sha256(&a, password);
  875. for (uint32_t i = 0; i < context->passwordCount; i++) {
  876. if (!Bits_memcmp(a.secret, context->passwords[i].secret, 32) ||
  877. String_equals(user, context->passwords[i].user)) {
  878. return CryptoAuth_addUser_DUPLICATE;
  879. }
  880. }
  881. a.user = String_new(user->bytes, context->allocator);
  882. if (ipv6) {
  883. a.restrictedToip6 = Allocator_malloc(context->allocator, 16);
  884. if (AddrTools_parseIp(a.restrictedToip6,ipv6->bytes) < 0) {
  885. Log_debug(context->logger, "Ipv6 parsing error!");
  886. return CryptoAuth_addUser_INVALID_IP;
  887. }
  888. } else {
  889. a.restrictedToip6 = NULL;
  890. }
  891. Bits_memcpyConst(&context->passwords[context->passwordCount],
  892. &a,
  893. sizeof(struct CryptoAuth_Auth));
  894. context->passwordCount++;
  895. return 0;
  896. }
  897. int CryptoAuth_removeUsers(struct CryptoAuth* context, String* user)
  898. {
  899. struct CryptoAuth_pvt* ctx = Identity_check((struct CryptoAuth_pvt*) context);
  900. if (!user) {
  901. int count = ctx->passwordCount;
  902. Log_debug(ctx->logger, "Flushing [%d] users", count);
  903. ctx->passwordCount = 0;
  904. return count;
  905. }
  906. int count = 0;
  907. int i = 0;
  908. while (i < (int)ctx->passwordCount) {
  909. if (String_equals(ctx->passwords[i].user, user)) {
  910. Bits_memcpyConst(&ctx->passwords[i],
  911. &ctx->passwords[ctx->passwordCount--],
  912. sizeof(struct CryptoAuth_Auth));
  913. count++;
  914. } else {
  915. i++;
  916. }
  917. }
  918. Log_debug(ctx->logger, "Removing [%d] user(s) identified by [%s]", count, user->bytes);
  919. return count;
  920. }
  921. List* CryptoAuth_getUsers(struct CryptoAuth* context, struct Allocator* alloc)
  922. {
  923. struct CryptoAuth_pvt* ctx = Identity_check((struct CryptoAuth_pvt*) context);
  924. uint32_t count = ctx->passwordCount;
  925. List* users = List_new(alloc);
  926. for (uint32_t i = 0; i < count; i++) {
  927. List_addString(users, String_clone(ctx->passwords[i].user, alloc), alloc);
  928. }
  929. return users;
  930. }
  931. struct CryptoAuth_Session* CryptoAuth_newSession(struct CryptoAuth* ca,
  932. struct Allocator* alloc,
  933. const uint8_t herPublicKey[32],
  934. const uint8_t herIp6[16],
  935. const bool requireAuth,
  936. char* name)
  937. {
  938. struct CryptoAuth_pvt* context = Identity_check((struct CryptoAuth_pvt*) ca);
  939. struct CryptoAuth_Session_pvt* session =
  940. Allocator_calloc(alloc, sizeof(struct CryptoAuth_Session_pvt), 1);
  941. Identity_set(session);
  942. session->context = context;
  943. session->requireAuth = requireAuth;
  944. session->name = name;
  945. session->timeOfLastPacket = Time_currentTimeSeconds(context->eventBase);
  946. session->alloc = alloc;
  947. if (herPublicKey != NULL) {
  948. Bits_memcpyConst(session->pub.herPublicKey, herPublicKey, 32);
  949. uint8_t calculatedIp6[16];
  950. AddressCalc_addressForPublicKey(calculatedIp6, herPublicKey);
  951. Bits_memcpyConst(session->pub.herIp6, calculatedIp6, 16);
  952. if (herIp6 != NULL) {
  953. Assert_true(!Bits_memcmp(calculatedIp6, herIp6, 16));
  954. }
  955. } else if (herIp6) {
  956. Bits_memcpyConst(session->pub.herIp6, herIp6, 16);
  957. }
  958. return &session->pub;
  959. }
  960. void CryptoAuth_setAuth(const String* password,
  961. const uint8_t authType,
  962. struct CryptoAuth_Session* caSession)
  963. {
  964. struct CryptoAuth_Session_pvt* session =
  965. Identity_check((struct CryptoAuth_Session_pvt*)caSession);
  966. if (!password && (session->password || session->authType)) {
  967. session->password = NULL;
  968. session->authType = 0;
  969. } else if (!session->password || !String_equals(session->password, password)) {
  970. session->password = String_clone(password, session->alloc);
  971. session->authType = authType;
  972. } else if (authType != session->authType) {
  973. session->authType = authType;
  974. } else {
  975. return;
  976. }
  977. reset(session);
  978. }
  979. int CryptoAuth_getState(struct CryptoAuth_Session* caSession)
  980. {
  981. struct CryptoAuth_Session_pvt* session =
  982. Identity_check((struct CryptoAuth_Session_pvt*)caSession);
  983. switch (session->nextNonce) {
  984. case 0:
  985. return CryptoAuth_NEW;
  986. case 1: // Sent a hello, waiting for the key
  987. return CryptoAuth_HANDSHAKE1;
  988. case 2: // Received a hello, sent a key packet.
  989. case 3: // Received a hello, sent multiple key packets.
  990. return CryptoAuth_HANDSHAKE2;
  991. case 4:
  992. // state 4 = waiting for first data packet to prove the handshake succeeded.
  993. // At this point you have sent a challenge and received a response so it is safe
  994. // to assume you are not being hit with replay packets.
  995. //
  996. // Sent a hello, received one or more keys, waiting for data.
  997. // In this state data packets will be sent but no data packets have yet been received.
  998. return CryptoAuth_HANDSHAKE3;
  999. default:
  1000. // Received data.
  1001. return (session->established) ? CryptoAuth_ESTABLISHED : CryptoAuth_HANDSHAKE3;
  1002. }
  1003. }
  1004. void CryptoAuth_resetIfTimeout(struct CryptoAuth_Session* caSession)
  1005. {
  1006. struct CryptoAuth_Session_pvt* session =
  1007. Identity_check((struct CryptoAuth_Session_pvt*)caSession);
  1008. resetIfTimeout(session);
  1009. }
  1010. void CryptoAuth_reset(struct CryptoAuth_Session* caSession)
  1011. {
  1012. struct CryptoAuth_Session_pvt* session =
  1013. Identity_check((struct CryptoAuth_Session_pvt*)caSession);
  1014. reset(session);
  1015. }
  1016. // For testing:
  1017. void CryptoAuth_encryptRndNonce(uint8_t nonce[24], struct Message* msg, uint8_t secret[32])
  1018. {
  1019. encryptRndNonce(nonce, msg, secret);
  1020. }
  1021. int CryptoAuth_decryptRndNonce(uint8_t nonce[24], struct Message* msg, uint8_t secret[32])
  1022. {
  1023. return decryptRndNonce(nonce, msg, secret);
  1024. }