Java Code Examples for org.apache.tuweni.bytes.Bytes#slice()

The following examples show how to use org.apache.tuweni.bytes.Bytes#slice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CliqueExtraData.java    From besu with Apache License 2.0 6 votes vote down vote up
static CliqueExtraData decodeRaw(final BlockHeader header) {
  final Bytes input = header.getExtraData();
  if (input.size() < EXTRA_VANITY_LENGTH + Signature.BYTES_REQUIRED) {
    throw new IllegalArgumentException(
        "Invalid Bytes supplied - too short to produce a valid Clique Extra Data object.");
  }

  final int validatorByteCount = input.size() - EXTRA_VANITY_LENGTH - Signature.BYTES_REQUIRED;
  if ((validatorByteCount % Address.SIZE) != 0) {
    throw new IllegalArgumentException("Bytes is of invalid size - i.e. contains unused bytes.");
  }

  final Bytes vanityData = input.slice(0, EXTRA_VANITY_LENGTH);
  final List<Address> validators =
      extractValidators(input.slice(EXTRA_VANITY_LENGTH, validatorByteCount));

  final int proposerSealStartIndex = input.size() - Signature.BYTES_REQUIRED;
  final Signature proposerSeal = parseProposerSeal(input.slice(proposerSealStartIndex));

  return new CliqueExtraData(vanityData, proposerSeal, validators, header);
}
 
Example 2
Source File: RPCMessage.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor
 *
 * @param messageBytes the bytes of the encoded message.
 */
public RPCMessage(Bytes messageBytes) {
  rpcFlags = messageBytes.get(0);
  stream = RPCFlag.Stream.STREAM.isApplied(rpcFlags);
  lastMessageOrError = RPCFlag.EndOrError.END.isApplied(rpcFlags);
  if (RPCFlag.BodyType.JSON.isApplied(rpcFlags)) {
    bodyType = RPCFlag.BodyType.JSON;
  } else if (RPCFlag.BodyType.UTF_8_STRING.isApplied(rpcFlags)) {
    bodyType = RPCFlag.BodyType.UTF_8_STRING;
  } else {
    bodyType = RPCFlag.BodyType.BINARY;
  }

  int bodySize = messageBytes.slice(1, 4).toInt();

  requestNumber = messageBytes.slice(5, 4).toInt();

  if (messageBytes.size() < bodySize + 9) {
    throw new IllegalArgumentException(
        "Message body " + (messageBytes.size() - 9) + " is less than body size " + bodySize);
  }

  body = messageBytes.slice(9, bodySize);
}
 
Example 3
Source File: EncryptedMessage.java    From besu with Apache License 2.0 6 votes vote down vote up
/**
 * Decrypts the ciphertext using our private key.
 *
 * @param msgBytes The ciphertext.
 * @param nodeKey Abstraction of this nodes private key & associated cryptographic operations
 * @return The plaintext.
 * @throws InvalidCipherTextException Thrown if decryption failed.
 */
public static Bytes decryptMsgEIP8(final Bytes msgBytes, final NodeKey nodeKey)
    throws InvalidCipherTextException {
  final SECP256K1.PublicKey ephPubKey = SECP256K1.PublicKey.create(msgBytes.slice(3, 64));

  // Strip off the IV to use.
  final Bytes iv = msgBytes.slice(3 + 64, IV_SIZE);

  // Extract the encrypted payload.
  final Bytes encrypted = msgBytes.slice(3 + 64 + IV_SIZE);

  // Perform the decryption.
  final ECIESEncryptionEngine decryptor =
      ECIESEncryptionEngine.forDecryption(nodeKey, ephPubKey, iv);
  return decryptor.decrypt(encrypted, msgBytes.slice(0, 2).toArray());
}
 
Example 4
Source File: CallDataLoadOperation.java    From besu with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(final MessageFrame frame) {
  final UInt256 startWord = UInt256.fromBytes(frame.popStackItem());

  // If the start index doesn't fit a int, it comes after anything in data, and so the returned
  // word should be zero.
  if (!startWord.fitsInt()) {
    frame.pushStackItem(Bytes32.ZERO);
    return;
  }

  final int offset = startWord.intValue();
  final Bytes data = frame.getInputData();
  final MutableBytes32 res = MutableBytes32.create();
  if (offset < data.size()) {
    final Bytes toCopy = data.slice(offset, Math.min(Bytes32.SIZE, data.size() - offset));
    toCopy.copyTo(res, 0);
  }
  frame.pushStackItem(res);
}
 
Example 5
Source File: IbftExtraData.java    From besu with Apache License 2.0 6 votes vote down vote up
static IbftExtraData decodeRaw(final Bytes input) {
  checkArgument(
      input.size() > EXTRA_VANITY_LENGTH,
      "Invalid Bytes supplied - too short to produce a valid IBFT Extra Data object.");

  final Bytes vanityData = input.slice(0, EXTRA_VANITY_LENGTH);

  final Bytes rlpData = input.slice(EXTRA_VANITY_LENGTH);
  final RLPInput rlpInput = new BytesValueRLPInput(rlpData, false);

  rlpInput.enterList(); // This accounts for the "root node" which contains IBFT data items.
  final Collection<Address> validators = rlpInput.readList(Address::readFrom);
  final Signature proposerSeal = parseProposerSeal(rlpInput);
  final Collection<Signature> seals = rlpInput.readList(rlp -> Signature.decode(rlp.readBytes()));
  rlpInput.leaveList();

  return new IbftExtraData(vanityData, seals, proposerSeal, validators);
}
 
Example 6
Source File: SecureScuttlebuttHandshakeClient.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the initial message's MAC with our network identifier, and returns the peer ephemeral public key.
 *
 * @param message initial handshake message
 */
public void readHello(Bytes message) {
  if (message.size() != 64) {
    throw new HandshakeException("Invalid handshake message length: " + message.size());
  }
  Bytes hmac = message.slice(0, 32);
  Bytes key = message.slice(32, 32);
  if (!HMACSHA512256.verify(hmac, key, networkIdentifier)) {
    throw new HandshakeException("MAC does not match our network identifier");
  }
  this.serverEphemeralPublicKey = Box.PublicKey.fromBytes(key);

  this.sharedSecret = DiffieHelman.Secret
      .forKeys(
          DiffieHelman.SecretKey.forBoxSecretKey(ephemeralKeyPair.secretKey()),
          DiffieHelman.PublicKey.forBoxPublicKey(serverEphemeralPublicKey));
  this.sharedSecret2 = DiffieHelman.Secret
      .forKeys(
          DiffieHelman.SecretKey.forBoxSecretKey(ephemeralKeyPair.secretKey()),
          DiffieHelman.PublicKey.forSignaturePublicKey(serverLongTermPublicKey));

  this.sharedSecret3 = DiffieHelman.Secret
      .forKeys(
          DiffieHelman.SecretKey.forSignatureSecretKey(longTermKeyPair.secretKey()),
          DiffieHelman.PublicKey.forBoxPublicKey(serverEphemeralPublicKey));
}
 
Example 7
Source File: ResponderHandshakeMessageV1.java    From besu with Apache License 2.0 5 votes vote down vote up
public static ResponderHandshakeMessageV1 decode(final Bytes bytes) {
  checkArgument(bytes.size() == MESSAGE_LENGTH);

  final Bytes pubk = bytes.slice(0, ECIESHandshaker.PUBKEY_LENGTH);
  final SECP256K1.PublicKey ephPubKey = SECP256K1.PublicKey.create(pubk);
  final Bytes32 nonce =
      Bytes32.wrap(bytes.slice(ECIESHandshaker.PUBKEY_LENGTH, ECIESHandshaker.NONCE_LENGTH), 0);
  final boolean token =
      bytes.get(ECIESHandshaker.PUBKEY_LENGTH + ECIESHandshaker.NONCE_LENGTH) == 0x01;
  return new ResponderHandshakeMessageV1(ephPubKey, nonce, token);
}
 
Example 8
Source File: SnappyCompressorTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@Test
public void uncompress_failOnExtraHeader() {
  final Bytes singleByte = compressor.compress(Bytes.of(0x01));
  final Bytes singleByteFrame = singleByte.slice(SNAPPY_HEADER.size());
  final Bytes maliciousPayload = Bytes.concatenate(SNAPPY_HEADER, SNAPPY_HEADER, singleByteFrame);

  List<List<ByteBuf>> testSlices = Utils.generateTestSlices(maliciousPayload);

  for (List<ByteBuf> testSlice : testSlices) {
    Decompressor decompressor = new SnappyFramedCompressor().createDecompressor(1);

    boolean exceptionCaught = false;
    for (ByteBuf byteBuf : testSlice) {
      if (!exceptionCaught) {
        try {
          decompressor.decodeOneMessage(byteBuf);
        } catch (CompressionException e) {
          exceptionCaught = true;
        }
      }
      byteBuf.release();
    }

    assertThat(exceptionCaught).isTrue();
    assertThat(testSlice).allSatisfy(b -> assertThat(b.refCnt()).isEqualTo(0));
  }
}
 
Example 9
Source File: BLSSecretKey.java    From teku with Apache License 2.0 5 votes vote down vote up
public Bytes toBytes() {
  final Bytes bytes = secretKey.toBytes();
  if (bytes.size() == 48) {
    final int paddingLength = 48 - 32;
    if (bytes.slice(0, paddingLength).isZero()) {
      return bytes.slice(paddingLength, 32);
    }
  }
  return bytes;
}
 
Example 10
Source File: TrieIterator.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Node<V> visit(final ExtensionNode<V> node, final Bytes searchPath) {
  Bytes remainingPath = searchPath;
  if (state == State.SEARCHING) {
    final Bytes extensionPath = node.getPath();
    final int commonPathLength = extensionPath.commonPrefixLength(searchPath);
    remainingPath = searchPath.slice(commonPathLength);
  }

  paths.push(node.getPath());
  node.getChild().accept(this, remainingPath);
  paths.pop();
  return node;
}
 
Example 11
Source File: SecureScuttlebuttStream.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
private Bytes decryptMessage(Bytes message, SecretBox.Key key, MutableBytes nonce) {
  if (message.size() < 34) {
    return null;
  }

  SecretBox.Nonce headerNonce = null;
  SecretBox.Nonce bodyNonce = null;
  try {
    MutableBytes snapshotNonce = nonce.mutableCopy();
    headerNonce = SecretBox.Nonce.fromBytes(snapshotNonce);
    bodyNonce = SecretBox.Nonce.fromBytes(snapshotNonce.increment());
    Bytes decryptedHeader = SecretBox.decrypt(message.slice(0, 34), key, headerNonce);

    if (decryptedHeader == null) {
      throw new StreamException("Failed to decrypt message header");
    }

    int bodySize = ((decryptedHeader.get(0) & 0xFF) << 8) + (decryptedHeader.get(1) & 0xFF);
    if (message.size() < bodySize + 34) {
      return null;
    }
    Bytes body = message.slice(34, bodySize);
    Bytes decryptedBody = SecretBox.decrypt(Bytes.concatenate(decryptedHeader.slice(2), body), key, bodyNonce);
    if (decryptedBody == null) {
      throw new StreamException("Failed to decrypt message");
    }
    nonce.increment().increment();
    return decryptedBody;
  } finally {
    destroyIfNonNull(headerNonce);
    destroyIfNonNull(bodyNonce);
  }
}
 
Example 12
Source File: SecureScuttlebuttHandshakeServer.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * Validates the initial message's MAC with our network identifier, and returns the peer ephemeral public key.
 *
 * @param message initial handshake message
 */
public void readHello(Bytes message) {
  if (message.size() != 64) {
    throw new HandshakeException("Invalid handshake message length: " + message.size());
  }
  Bytes hmac = message.slice(0, 32);
  Bytes key = message.slice(32, 32);
  if (!HMACSHA512256.verify(hmac, key, networkIdentifier)) {
    throw new HandshakeException("MAC does not match our network identifier");
  }
  this.clientEphemeralPublicKey = Box.PublicKey.fromBytes(key);
  computeSharedSecrets();
}
 
Example 13
Source File: DefaultPrivacyController.java    From besu with Apache License 2.0 5 votes vote down vote up
private List<String> getParticipantsFromParameter(final Bytes input) {
  final List<String> participants = new ArrayList<>();
  final Bytes mungedParticipants = input.slice(4 + 32 + 32 + 32);
  for (int i = 0; i <= mungedParticipants.size() - 32; i += 32) {
    participants.add(mungedParticipants.slice(i, 32).toBase64String());
  }
  return participants;
}
 
Example 14
Source File: CliqueBlockHashing.java    From besu with Apache License 2.0 4 votes vote down vote up
private static Bytes encodeExtraDataWithoutProposerSeal(final CliqueExtraData cliqueExtraData) {
  final Bytes extraDataBytes = cliqueExtraData.encode();
  // Always trim off final 65 bytes (which maybe zeros)
  return extraDataBytes.slice(0, extraDataBytes.size() - Signature.BYTES_REQUIRED);
}
 
Example 15
Source File: VertxIntegrationTest.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Test
void connectToServer(@VertxInstance Vertx vertx) throws Exception {
  Signature.KeyPair serverKeyPair = Signature.KeyPair.random();
  Bytes32 networkIdentifier = Bytes32.random();
  AtomicReference<MyServerHandler> serverHandlerRef = new AtomicReference<>();
  SecureScuttlebuttVertxServer server = new SecureScuttlebuttVertxServer(
      vertx,
      new InetSocketAddress("localhost", 20000),
      serverKeyPair,
      networkIdentifier,
      (streamServer, fn) -> {
        serverHandlerRef.set(new MyServerHandler());
        return serverHandlerRef.get();
      });

  server.start().join();

  SecureScuttlebuttVertxClient client =
      new SecureScuttlebuttVertxClient(vertx, Signature.KeyPair.random(), networkIdentifier);
  MyClientHandler handler =
      client.connectTo(20000, "localhost", serverKeyPair.publicKey(), MyClientHandler::new).get();

  Thread.sleep(1000);
  assertNotNull(handler);

  String rpcRequestBody = "{\"name\": [\"whoami\"],\"type\": \"async\",\"args\":[]}";
  Bytes rpcRequest = RPCCodec.encodeRequest(rpcRequestBody, RPCFlag.BodyType.JSON);

  handler.sendMessage(rpcRequest);

  Thread.sleep(1000);
  MyServerHandler serverHandler = serverHandlerRef.get();

  Bytes receivedBytes = serverHandler.received;
  Bytes receivedBody = receivedBytes.slice(9);

  Bytes requestBody = rpcRequest.slice(9);

  assertEquals(requestBody, receivedBody);

  handler.closeStream();
  Thread.sleep(1000);
  assertTrue(serverHandler.closed);

  client.stop();
  server.stop();

}
 
Example 16
Source File: TracingUtils.java    From besu with Apache License 2.0 4 votes vote down vote up
private static Bytes trimTrailingZeros(final Bytes value) {
  final int toTrim = trailingZeros(value);
  return value.slice(0, value.size() - toTrim + 1);
}
 
Example 17
Source File: SecureScuttlebuttVertxServer.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
private int getBodyLength(Bytes rpcHeader) {
  Bytes size = rpcHeader.slice(1, 4);
  return size.toInt();
}
 
Example 18
Source File: Memory.java    From besu with Apache License 2.0 4 votes vote down vote up
/**
 * Copy the bytes from the provided number of bytes from the provided value to memory from the
 * provided offset.
 *
 * <p>Note that this method will extend memory to accommodate the location assigned and bytes
 * copied and so never fails.
 *
 * @param memOffset the location in memory at which to start copying the bytes of {@code value}.
 * @param sourceOffset the location in the source to start copying.
 * @param numBytes the number of bytes to set in memory. Note that this value may differ from
 *     {@code value.size()}: if {@code numBytes < value.size()} bytes, only {@code numBytes} will
 *     be copied from {@code value}; if {@code numBytes < value.size()}, then only the bytes in
 *     {@code value} will be copied, but the memory will be expanded if necessary to cover {@code
 *     numBytes} (in other words, {@link #getActiveWords()} will return a value consistent with
 *     having set {@code numBytes} bytes, even if less than that have been concretely set due to
 *     {@code value} being smaller).
 * @param bytes the bytes to copy to memory from {@code location}.
 */
public void setBytes(
    final UInt256 memOffset,
    final UInt256 sourceOffset,
    final UInt256 numBytes,
    final Bytes bytes) {
  final int offset = sourceOffset.fitsInt() ? sourceOffset.intValue() : Integer.MAX_VALUE;
  final int length = numBytes.fitsInt() ? numBytes.intValue() : Integer.MAX_VALUE;

  if (offset >= bytes.size()) {
    clearBytes(memOffset, numBytes);
    return;
  }

  final Bytes toCopy = bytes.slice(offset, Math.min(length, bytes.size() - offset));
  setBytes(memOffset, numBytes, toCopy);
}
 
Example 19
Source File: SnappyCompressorTest.java    From teku with Apache License 2.0 4 votes vote down vote up
@Test
public void uncompress_maliciousBytes() {
  // The number of underlying uncompressed bytes encoded
  final int uncompressedByteCount = 4;

  // Build a set of compressed data with a snappy header, and one frame for each uncompressed byte
  final Bytes singleByte = compressor.compress(Bytes.of(0x01));
  final Bytes singleByteFrame = singleByte.slice(SNAPPY_HEADER.size());
  final Bytes[] headerAndFrames = new Bytes[uncompressedByteCount + 1];
  headerAndFrames[0] = SNAPPY_HEADER;
  for (int i = 0; i < uncompressedByteCount; i++) {
    headerAndFrames[i + 1] = singleByteFrame;
  }
  final Bytes maliciousPayload = Bytes.concatenate(headerAndFrames);

  // Check assumptions - we want to build a set of bytes with valid frames that
  // exceeds the maximum expected compressed size given the underlying data
  final int maxExpectedCompressedBytes = compressor.getMaxCompressedLength(uncompressedByteCount);
  assertThat(maliciousPayload.size()).isGreaterThan(maxExpectedCompressedBytes);

  List<List<ByteBuf>> testSlices = Utils.generateTestSlices(maliciousPayload);

  for (List<ByteBuf> testSlice : testSlices) {
    Decompressor decompressor =
        new SnappyFramedCompressor().createDecompressor(uncompressedByteCount);

    boolean exceptionCaught = false;
    for (ByteBuf byteBuf : testSlice) {
      if (!exceptionCaught) {
        try {
          decompressor.decodeOneMessage(byteBuf);
        } catch (CompressionException e) {
          exceptionCaught = true;
        }
      }
      byteBuf.release();
    }

    assertThat(exceptionCaught).isTrue();
    assertThat(testSlice).allSatisfy(b -> assertThat(b.refCnt()).isEqualTo(0));
  }
}
 
Example 20
Source File: RLPxConnectionFactory.java    From incubator-tuweni with Apache License 2.0 2 votes vote down vote up
/**
 * Identify the size of a handshake message based on elements of the common MAC.
 *
 * @param msgBytes the bytes of the message
 * @return the size of the message, including MAC, key and IV
 */
public static int messageSize(Bytes msgBytes) {
  Bytes commonMac = msgBytes.slice(0, 2);
  int size = (commonMac.get(1) & 0xFF) + ((commonMac.get(0) & 0xFF) << 8);
  return size + 2;
}