com.fitbur.bouncycastle.crypto.tls.Chacha20Poly1305 Maven / Gradle / Ivy
package com.fitbur.bouncycastle.crypto.tls;
import java.io.IOException;
import com.fitbur.bouncycastle.crypto.Mac;
import com.fitbur.bouncycastle.crypto.engines.ChaChaEngine;
import com.fitbur.bouncycastle.crypto.generators.Poly1305KeyGenerator;
import com.fitbur.bouncycastle.crypto.macs.Poly1305;
import com.fitbur.bouncycastle.crypto.params.KeyParameter;
import com.fitbur.bouncycastle.crypto.params.ParametersWithIV;
import com.fitbur.bouncycastle.util.Arrays;
import com.fitbur.bouncycastle.util.Pack;
public class Chacha20Poly1305 implements TlsCipher
{
protected TlsContext context;
protected ChaChaEngine encryptCipher;
protected ChaChaEngine com.fitburcryptCipher;
public Chacha20Poly1305(TlsContext context) throws IOException
{
if (!TlsUtils.isTLSv12(context))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
this.context = context;
byte[] key_block = TlsUtils.calculateKeyBlock(context, 64);
KeyParameter client_write_key = new KeyParameter(key_block, 0, 32);
KeyParameter server_write_key = new KeyParameter(key_block, 32, 32);
this.encryptCipher = new ChaChaEngine(20);
this.com.fitburcryptCipher = new ChaChaEngine(20);
KeyParameter encryptKey, com.fitburcryptKey;
if (context.isServer())
{
encryptKey = server_write_key;
com.fitburcryptKey = client_write_key;
}
else
{
encryptKey = client_write_key;
com.fitburcryptKey = server_write_key;
}
byte[] dummyNonce = new byte[8];
this.encryptCipher.init(true, new ParametersWithIV(encryptKey, dummyNonce));
this.com.fitburcryptCipher.init(false, new ParametersWithIV(com.fitburcryptKey, dummyNonce));
}
public int getPlaintextLimit(int ciphertextLimit)
{
return ciphertextLimit - 16;
}
public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len) throws IOException
{
int ciphertextLength = len + 16;
KeyParameter macKey = initRecordMAC(encryptCipher, true, seqNo);
byte[] output = new byte[ciphertextLength];
encryptCipher.processBytes(plaintext, offset, len, output, 0);
byte[] additionalData = getAdditionalData(seqNo, type, len);
byte[] mac = calculateRecordMAC(macKey, additionalData, output, 0, len);
System.arraycopy(mac, 0, output, len, mac.length);
return output;
}
public byte[] com.fitburcodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len) throws IOException
{
if (getPlaintextLimit(len) < 0)
{
throw new TlsFatalAlert(AlertDescription.com.fitburcode_error);
}
int plaintextLength = len - 16;
byte[] receivedMAC = Arrays.copyOfRange(ciphertext, offset + plaintextLength, offset + len);
KeyParameter macKey = initRecordMAC(com.fitburcryptCipher, false, seqNo);
byte[] additionalData = getAdditionalData(seqNo, type, plaintextLength);
byte[] calculatedMAC = calculateRecordMAC(macKey, additionalData, ciphertext, offset, plaintextLength);
if (!Arrays.constantTimeAreEqual(calculatedMAC, receivedMAC))
{
throw new TlsFatalAlert(AlertDescription.bad_record_mac);
}
byte[] output = new byte[plaintextLength];
com.fitburcryptCipher.processBytes(ciphertext, offset, plaintextLength, output, 0);
return output;
}
protected KeyParameter initRecordMAC(ChaChaEngine cipher, boolean forEncryption, long seqNo)
{
byte[] nonce = new byte[8];
TlsUtils.writeUint64(seqNo, nonce, 0);
cipher.init(forEncryption, new ParametersWithIV(null, nonce));
byte[] firstBlock = new byte[64];
cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0);
// NOTE: The BC implementation puts 'r' after 'k'
System.arraycopy(firstBlock, 0, firstBlock, 32, 16);
KeyParameter macKey = new KeyParameter(firstBlock, 16, 32);
Poly1305KeyGenerator.clamp(macKey.getKey());
return macKey;
}
protected byte[] calculateRecordMAC(KeyParameter macKey, byte[] additionalData, byte[] buf, int off, int len)
{
Mac mac = new Poly1305();
mac.init(macKey);
updateRecordMAC(mac, additionalData, 0, additionalData.length);
updateRecordMAC(mac, buf, off, len);
byte[] output = new byte[mac.getMacSize()];
mac.doFinal(output, 0);
return output;
}
protected void updateRecordMAC(Mac mac, byte[] buf, int off, int len)
{
mac.update(buf, off, len);
byte[] longLen = Pack.longToLittleEndian(len & 0xFFFFFFFFL);
mac.update(longLen, 0, longLen.length);
}
protected byte[] getAdditionalData(long seqNo, short type, int len) throws IOException
{
/*
* additional_data = seq_num + TLSCompressed.type + TLSCompressed.version +
* TLSCompressed.length
*/
byte[] additional_data = new byte[13];
TlsUtils.writeUint64(seqNo, additional_data, 0);
TlsUtils.writeUint8(type, additional_data, 8);
TlsUtils.writeVersion(context.getServerVersion(), additional_data, 9);
TlsUtils.writeUint16(len, additional_data, 11);
return additional_data;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy