/** * Given a candidate key OTP encrypts a message using either using the key directly or a key * pseudo randomly derived from the candidate, in order to match the length of the message. * * <p> * The key candidate is assumed to be uniformly random. Thus, if its size is longer or equal to * the message, the candidate key will be used directly as a OTP key. Otherwise, the key will be * deterministically <i>stretched</i> to match the length of the message in some secure way (e.g., * using a hash function). * </p> * * @param message the message to be encrypted * @param keyCandidate the candidate key * @return the resulting cipher text */ public static byte[] encrypt(byte[] message, byte[] keyCandidate) { return encrypt(message, keyCandidate, message.length); }
@Override public void send(StrictBitVector messageZero, StrictBitVector messageOne) { int maxBitLength = Math.max(messageZero.getSize(), messageOne.getSize()); Pair<byte[], byte[]> seedMessages = sendRandomOt(); byte[] encryptedZeroMessage = PseudoOtp.encrypt(messageZero.toByteArray(), seedMessages.getFirst(), maxBitLength / Byte.SIZE); byte[] encryptedOneMessage = PseudoOtp.encrypt(messageOne.toByteArray(), seedMessages.getSecond(), maxBitLength / Byte.SIZE); network.send(otherId, encryptedZeroMessage); network.send(otherId, encryptedOneMessage); }
/** * Adjust the random, preprocessed message, to fit the specific messages to send. * * @param messageZero The actual zero message to send * @param messageOne The actual one message to send */ private void doActualSend(byte[] messageZero, byte[] messageOne) { // Find the correct preprocessed random OT messages StrictBitVector randomZero = randomMessages.getFirst().get(offset); StrictBitVector randomOne = randomMessages.getSecond().get(offset); int maxLength = Math.max(messageZero.length, messageOne.length); // Receive a bit from the receiver indicating whether the zero and one // messages should be switched around byte[] switchBit = network.receive(resources.getOtherId()); // If false (indicated by byte 0x00), then don't switch around if (switchBit[0] == 0x00) { network.send(resources.getOtherId(), PseudoOtp.encrypt(messageZero, randomZero.toByteArray(), maxLength)); network.send(resources.getOtherId(), PseudoOtp.encrypt(messageOne, randomOne.toByteArray(), maxLength)); } else { network.send(resources.getOtherId(), PseudoOtp.encrypt(messageOne, randomZero.toByteArray(), maxLength)); network.send(resources.getOtherId(), PseudoOtp.encrypt(messageZero, randomOne.toByteArray(), maxLength)); } } }