Browse Source

ChaCha20 decrypt

pull/1313/head
orignal 6 years ago
parent
commit
9188e3ad3f
  1. 17
      libi2pd/Crypto.cpp
  2. 2
      libi2pd/Crypto.h
  3. 10
      libi2pd/LeaseSet.cpp

17
libi2pd/Crypto.cpp

@ -1228,6 +1228,23 @@ namespace crypto
#endif #endif
} }
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
{
#if OPENSSL_AEAD_CHACHA20_POLY1305
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
EVP_EncryptInit_ex(ctx, EVP_chacha20 (), 0, key, nonce);
int outlen = 0;
EVP_EncryptUpdate(ctx, out, &outlen, msg, msgLen);
EVP_EncryptFinal_ex(ctx, NULL, &outlen);
EVP_CIPHER_CTX_free (ctx);
#else
chacha::Chacha20State state;
chacha::Chacha20Init (state, nonce, key, 0);
if (out != msg) memcpy (out, msg, msgLen);
chacha::Chacha20Encrypt (state, out, msgLen);
#endif
}
// init and terminate // init and terminate
/* std::vector <std::unique_ptr<std::mutex> > m_OpenSSLMutexes; /* std::vector <std::unique_ptr<std::mutex> > m_OpenSSLMutexes;

2
libi2pd/Crypto.h

@ -290,6 +290,8 @@ namespace crypto
void AEADChaCha20Poly1305Encrypt (const std::vector<std::pair<uint8_t *, size_t> >& bufs, const uint8_t * key, const uint8_t * nonce, uint8_t * mac); // encrypt multiple buffers with zero ad void AEADChaCha20Poly1305Encrypt (const std::vector<std::pair<uint8_t *, size_t> >& bufs, const uint8_t * key, const uint8_t * nonce, uint8_t * mac); // encrypt multiple buffers with zero ad
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out);
// init and terminate // init and terminate
void InitCrypto (bool precomputation); void InitCrypto (bool precomputation);
void TerminateCrypto (); void TerminateCrypto ();

10
libi2pd/LeaseSet.cpp

@ -473,11 +473,15 @@ namespace data
H ("subcredential", { {credential, 32}, {blindedPublicKey, blindedKeyLen} }, subcredential); H ("subcredential", { {credential, 32}, {blindedPublicKey, blindedKeyLen} }, subcredential);
// outerInput = subcredential || publishedTimestamp // outerInput = subcredential || publishedTimestamp
memcpy (subcredential + 32, publishedTimestamp, 4); memcpy (subcredential + 32, publishedTimestamp, 4);
// outerSalt = outerCiphertext[32:end] // outerSalt = outerCiphertext[0:32]
// keys = HKDF(outerSalt, outerInput, "ELS2_L1K", 44) // keys = HKDF(outerSalt, outerInput, "ELS2_L1K", 44)
uint8_t outerKey[44]; uint8_t outerKey[44];
HKDF (outerCiphertext + lenOuterCiphertext - 32, {subcredential, 36}, "ELS2_L1K", outerKey, 44); HKDF (outerCiphertext, {subcredential, 36}, "ELS2_L1K", outerKey, 44);
// decrypt using chacha20 // decrypt Layer 1
// outerKey = keys[0:31]
// outerIV = keys[32:43]
std::vector<uint8_t> outerPlainText (lenOuterCiphertext - 32);
i2p::crypto::ChaCha20 (outerCiphertext + 32, lenOuterCiphertext - 32, outerKey, outerKey + 32, outerPlainText.data ());
} }
} }

Loading…
Cancel
Save