public void setKey(int i, ClientCHK key) { byte[] r = key.getRoutingKey(); System.arraycopy(r, 0, routingKeys, i * NodeCHK.KEY_LENGTH, NodeCHK.KEY_LENGTH); if(decryptKeys != null) { byte[] c = key.getCryptoKey(); System.arraycopy(c, 0, decryptKeys, i * ClientCHK.CRYPTO_KEY_LENGTH, ClientCHK.CRYPTO_KEY_LENGTH); } if(extraBytesForKeys != null) { byte[] e = key.getExtra(); System.arraycopy(e, 0, extraBytesForKeys, i * EXTRA_BYTES_LENGTH, EXTRA_BYTES_LENGTH); } }
private ClientCHKBlock encodeBlock(ClientCHK key, byte[] data) { try { return ClientCHKBlock.encodeSplitfileBlock(data, key.getCryptoKey(), key.getCryptoAlgorithm()); } catch (CHKEncodeException e) { return null; } }
public void readKeys(DataInputStream dis, boolean check) throws IOException { int count = check ? checkBlocks : dataBlocks; int offset = check ? dataBlocks : 0; if(commonDecryptKey != null) { int rkOffset = offset * NodeCHK.KEY_LENGTH; for(int i=0;i<count;i++) { dis.readFully(routingKeys, rkOffset, NodeCHK.KEY_LENGTH); rkOffset += NodeCHK.KEY_LENGTH; } } else { int rkOffset = offset * NodeCHK.KEY_LENGTH; int extraOffset = offset * EXTRA_BYTES_LENGTH; assert(NodeCHK.KEY_LENGTH == ClientCHK.CRYPTO_KEY_LENGTH); for(int i=0;i<count;i++) { ClientCHK key = ClientCHK.readRawBinaryKey(dis); byte[] r = key.getRoutingKey(); System.arraycopy(r, 0, routingKeys, rkOffset, NodeCHK.KEY_LENGTH); byte[] c = key.getCryptoKey(); System.arraycopy(c, 0, decryptKeys, rkOffset, NodeCHK.KEY_LENGTH); rkOffset += NodeCHK.KEY_LENGTH; byte[] e = key.getExtra(); System.arraycopy(e, 0, extraBytesForKeys, extraOffset, EXTRA_BYTES_LENGTH); extraOffset += EXTRA_BYTES_LENGTH; } } }
private void queueHeal(int blockNumber, byte[] data) throws IOException { byte[] cryptoKey; byte cryptoAlgorithm; if(parent.splitfileSingleCryptoKey != null) { cryptoKey = parent.splitfileSingleCryptoKey; cryptoAlgorithm = parent.splitfileSingleCryptoAlgorithm; } else { ClientCHK key = getSegmentKeys().getKey(blockNumber, null, false); cryptoKey = key.getCryptoKey(); cryptoAlgorithm = key.getCryptoAlgorithm(); } parent.fetcher.queueHeal(data, cryptoKey, cryptoAlgorithm); }
if(ckey == null) ckey = key.getCryptoKey(); assert(ClientCHK.CRYPTO_KEY_LENGTH == NodeCHK.KEY_LENGTH);
private boolean checkEncodedDataBlocks(byte[][] checkBlocks, boolean[] checkBlocksPresent, SplitFileSegmentKeys keys, boolean capturingBinaryBlob) { for(int i=0;i<checkBlocks.length;i++) { if(checkBlocksPresent[i]) continue; ClientCHK decodeKey = keys.getKey(i+blocksForDecode(), null, false); // Encode it to check whether the key is the same. ClientCHKBlock block; try { block = ClientCHKBlock.encodeSplitfileBlock(checkBlocks[i], decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(!actualKey.equals(decodeKey)) { Logger.error(this, "Splitfile check block "+i+" does not encode to expected key for "+this+" for "+parent); return false; } if(capturingBinaryBlob) parent.fetcher.maybeAddToBinaryBlob(block); } catch (CHKEncodeException e) { // Impossible! parent.fail(new FetchException(FetchExceptionMode.INTERNAL_ERROR, "Decoded block could not be encoded")); Logger.error(this, "Impossible: Decoded block could not be encoded"); return false; } } return true; }
public synchronized byte[] checkAndGetBlockData(int blockNum) throws IOException { if(!blockChooser.hasSucceeded(blockNum)) return null; ClientCHK key = getKey(blockNum); if(key == null) return null; for(int i=0;i<blocksFetched.length;i++) { if(blocksFetched[i] == blockNum) { byte[] buf = readBlock(i); try { ClientCHKBlock block = ClientCHKBlock.encodeSplitfileBlock(buf, key.getCryptoKey(), key.getCryptoAlgorithm()); if(!(block.getClientKey().equals(key))) { Logger.error(this, "Block "+blockNum+" in blocksFound["+i+"] is not valid!"); blockChooser.onUnSuccess(blockNum); succeeded = false; finished = false; } else { return buf; } } catch (CHKEncodeException e) { // Should not be possible. Logger.error(this, "Impossible: "+e); return null; } } } Logger.error(this, "Block "+blockNum+" in blocksFound but not in blocksFetched on "+this); return null; }
block = ClientCHKBlock.encodeSplitfileBlock(dataBlocks[i], decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(!actualKey.equals(decodeKey)) {
if((redirectedKey instanceof ClientCHK) && !((ClientCHK)redirectedKey).isMetadata()) { rcb.onBlockSetFinished(this, context); byte [] redirectedCryptoKey = ((ClientCHK)redirectedKey).getCryptoKey(); if (key instanceof ClientCHK && !Arrays.equals( ((ClientCHK)key).getCryptoKey(), redirectedCryptoKey)) redirectedCryptoKey = null;
ClientCHKBlock.encodeSplitfileBlock(buf, decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(decodeKey == null || !decodeKey.equals(actualKey)) {