/* $Id$ */ package org.noreply.fancydress.type3; import org.noreply.fancydress.type3.routing.*; import org.noreply.fancydress.status.*; import org.noreply.fancydress.crypto.*; import org.noreply.fancydress.misc.*; public class SURB extends SingleLeg { public static final byte[] VERSION = {0, 1}; public static final String ARMOR_VERSION = "0.2"; private byte[] sharedSecret; public SURB (HalfPath path, byte[] secret, RoutingDestination address) throws Mix3BadArgumentsChainTooLongException { super(); byte[] seed; byte[] validateHash; byte[] validate = Util.toOctets("Validate"); Hop[] hops = path.getHops(); do { seed = CryptoPrimitives.rand(20); seed[0] &= 0x7f; validateHash = CryptoPrimitives.hash(seed, secret, validate); } while (validateHash[validateHash.length-1] == 0); byte[] key = Util.slice(CryptoPrimitives.hash(seed, secret, Util.toOctets("Generate")), 0, CryptoPrimitives.KEY_LEN); byte[] stream = CryptoPrimitives.prng(key, CryptoPrimitives.KEY_LEN*(hops.length + 1)); sharedSecret = Util.slice(stream, CryptoPrimitives.KEY_LEN*hops.length, CryptoPrimitives.KEY_LEN); byte[][] sharedKeys = new byte[hops.length][]; for (int i=0; i>24) & 0xff); pos++; result[pos] = (byte) ( (useBy >>16) & 0xff); pos++; result[pos] = (byte) ( (useBy >> 8) & 0xff); pos++; result[pos] = (byte) ( (useBy ) & 0xff); pos++; System.arraycopy(super.asOctets(), 0, result, pos, SINGLE_HEADER_LEN); pos += SINGLE_HEADER_LEN; result[pos] = (byte) ( (routingSize >> 8) & 0xff); pos++; result[pos] = (byte) ( routingSize & 0xff); pos++; result[pos] = (byte) ( (routingType >> 8) & 0xff); pos++; result[pos] = (byte) ( routingType & 0xff); pos++; System.arraycopy(sharedSecret, 0, result, pos, CryptoPrimitives.KEY_LEN); pos += CryptoPrimitives.KEY_LEN; System.arraycopy(routingInformation, 0, result, pos, routingSize); pos += routingSize; if (pos != result.length) throw new Error("did not fill in expected number of bytes"); String[] headers = new String[1]; headers[0] = "Version: "+ARMOR_VERSION; return Util.armor(result, "TYPE III REPLY BLOCK", headers); } }