<commit_before>/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.uniqush.client;

import java.io.StreamCorruptedException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

class KeySet {
	private final int IV_LENGTH = 16;
	private final Charset UTF_8 = Charset.forName("UTF-8");
	public byte[] serverEncrKey;
	public byte[] serverAuthKey;
	public byte[] clientEncrKey;
	public byte[] clientAuthKey;
	
	private Cipher encryptCipher;
	private Cipher decryptCipher;
	
	private Mac clientHmac;
	private Mac serverHmac;

	public int getEncryptHmacSize() {
		return clientHmac.getMacLength();
	}

	public int getDecryptHmacSize() {
		return serverHmac.getMacLength();
	}
	
	public int getEncryptedSize(int length) {
		return encryptCipher.getOutputSize(length);
	}
	
	public int getDecryptedSize(int length) {
		return decryptCipher.getOutputSize(length);
	}
	
	public boolean bytesEq(byte[] a, int aOffset, byte[] b, int bOffset, int length) {
		for (int i = 0; i < length; i++) {
			if (a[i + aOffset] != b[i + bOffset]) {
				return false;
			}
		}
		return true;
	}
	
	public void decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException, StreamCorruptedException {
		int hmacSz = getDecryptHmacSize();
		int len = decryptCipher.update(input, inputOffset, input.length - inputOffset - hmacSz, output, outputOffset);
		byte[] hmac = new byte[hmacSz];
		serverHmac.reset();
		
		byte[] prefix = new byte[2];
		int datalen = input.length - inputOffset - hmacSz;
		prefix[0] = (byte)(datalen & 0xFF);
		prefix[1] = (byte)((datalen >> 8) & 0xFF);
		serverHmac.update(prefix);
		serverHmac.update(input, inputOffset, getEncryptedSize(len));
		serverHmac.doFinal(hmac, 0);
		if (!bytesEq(hmac, 0, input, inputOffset + getEncryptedSize(len), hmacSz)) {
			throw new StreamCorruptedException("unmached hmac");
		}
		return;
	}
	
	public void encrypt(byte[] input, int inputOffset, byte[] output, int outputOffset) throws IllegalBlockSizeException, ShortBufferException, BadPaddingException {
		// encrypt-then-hmac
		int inputLen = input.length - inputOffset;
		int len = encryptCipher.update(input, inputOffset, inputLen, output, outputOffset);
		System.out.printf("encrypted len=%d; input length=%d\n", len, inputLen);

		byte[] prefix = new byte[2];
		prefix[0] = (byte)(inputLen & 0xFF);
		prefix[1] = (byte)((inputLen >> 8) & 0xFF);
		clientHmac.reset();
		clientHmac.update(prefix);<commit_msg>removed some debug messages<commit_after>/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.uniqush.client;

import java.io.StreamCorruptedException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

class KeySet {
	private final int IV_LENGTH = 16;
	private final Charset UTF_8 = Charset.forName("UTF-8");
	public byte[] serverEncrKey;
	public byte[] serverAuthKey;
	public byte[] clientEncrKey;
	public byte[] clientAuthKey;
	
	private Cipher encryptCipher;
	private Cipher decryptCipher;
	
	private Mac clientHmac;
	private Mac serverHmac;

	public int getEncryptHmacSize() {
		return clientHmac.getMacLength();
	}

	public int getDecryptHmacSize() {
		return serverHmac.getMacLength();
	}
	
	public int getEncryptedSize(int length) {
		return encryptCipher.getOutputSize(length);
	}
	
	public int getDecryptedSize(int length) {
		return decryptCipher.getOutputSize(length);
	}
	
	public boolean bytesEq(byte[] a, int aOffset, byte[] b, int bOffset, int length) {
		for (int i = 0; i < length; i++) {
			if (a[i + aOffset] != b[i + bOffset]) {
				return false;
			}
		}
		return true;
	}
	
	public void decrypt(byte[] input, int inputOffset, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException, StreamCorruptedException {
		int hmacSz = getDecryptHmacSize();
		int len = decryptCipher.update(input, inputOffset, input.length - inputOffset - hmacSz, output, outputOffset);
		byte[] hmac = new byte[hmacSz];
		serverHmac.reset();
		
		byte[] prefix = new byte[2];
		int datalen = input.length - inputOffset - hmacSz;
		prefix[0] = (byte)(datalen & 0xFF);
		prefix[1] = (byte)((datalen >> 8) & 0xFF);
		serverHmac.update(prefix);
		serverHmac.update(input, inputOffset, getEncryptedSize(len));
		serverHmac.doFinal(hmac, 0);
		if (!bytesEq(hmac, 0, input, inputOffset + getEncryptedSize(len), hmacSz)) {
			throw new StreamCorruptedException("unmached hmac");
		}
		return;
	}
	
	public void encrypt(byte[] input, int inputOffset, byte[] output, int outputOffset) throws IllegalBlockSizeException, ShortBufferException, BadPaddingException {
		// encrypt-then-hmac
		int inputLen = input.length - inputOffset;
		int len = encryptCipher.update(input, inputOffset, inputLen, output, outputOffset);
		
		byte[] prefix = new byte[2];
		prefix[0] = (byte)(inputLen & 0xFF);
		prefix[1] = (byte)((inputLen >> 8) & 0xFF);
		clientHmac.reset();
		clientHmac.update(prefix);
{'fraction_non_alphanumeric': 0.10645478835606806, 'fraction_numerical': 0.007593868654197722, 'mean_word_length': 5.022015241320914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '910380', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2152, 'n_words': 622}