Java Code Examples for org.bouncycastle.crypto.modes.SICBlockCipher#init()

The following examples show how to use org.bouncycastle.crypto.modes.SICBlockCipher#init() . 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: Framer.java    From besu with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new framer out of the handshake secrets derived during the cryptographic handshake.
 *
 * @param secrets The handshake secrets.
 */
public Framer(final HandshakeSecrets secrets) {
  this.secrets = secrets;

  final KeyParameter aesKey = new KeyParameter(secrets.getAesSecret());
  final KeyParameter macKey = new KeyParameter(secrets.getMacSecret());

  encryptor = new SICBlockCipher(new AESEngine());
  encryptor.init(true, new ParametersWithIV(aesKey, IV));

  decryptor = new SICBlockCipher(new AESEngine());
  decryptor.init(false, new ParametersWithIV(aesKey, IV));

  macEncryptor = new AESEngine();
  macEncryptor.init(true, macKey);
}
 
Example 2
Source File: RLPxConnection.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
RLPxConnection(
    Bytes32 aesSecret,
    Bytes32 macSecret,
    Bytes32 token,
    Bytes egressMac,
    Bytes ingressMac,
    SECP256K1.PublicKey publicKey,
    SECP256K1.PublicKey peerPublicKey) {
  this.aesSecret = aesSecret;
  this.macSecret = macSecret;
  this.token = token;

  KeyParameter macKey = new KeyParameter(macSecret.toArrayUnsafe());
  macEncryptionEngine = new AESEngine();
  macEncryptionEngine.init(true, macKey);

  updateEgress(egressMac);
  updateIngress(ingressMac);
  this.publicKey = publicKey;
  this.peerPublicKey = peerPublicKey;

  KeyParameter aesKey = new KeyParameter(aesSecret.toArrayUnsafe());

  byte[] IV = new byte[16];
  Arrays.fill(IV, (byte) 0);

  decryptionCipher = new SICBlockCipher(new AESEngine());
  decryptionCipher.init(false, new ParametersWithIV(aesKey, IV));

  encryptionCipher = new SICBlockCipher(new AESEngine());
  encryptionCipher.init(true, new ParametersWithIV(aesKey, IV));
}
 
Example 3
Source File: ECKey.java    From javasdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private key as an AES key");
    }


    AESFastEngine engine = new AESFastEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize())
            break;
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}
 
Example 4
Source File: RLPxConnection.java    From cava with Apache License 2.0 4 votes vote down vote up
public RLPxMessage readFrame(Bytes messageFrame) {
  if (messageFrame.size() < 32) {
    return null;
  }

  KeyParameter aesKey = new KeyParameter(aesSecret.toArrayUnsafe());

  byte[] IV = new byte[16];
  Arrays.fill(IV, (byte) 0);

  SICBlockCipher decryptionCipher = new SICBlockCipher(new AESEngine());
  decryptionCipher.init(false, new ParametersWithIV(aesKey, IV));

  Bytes macBytes = messageFrame.slice(16, 16);
  Bytes headerBytes = messageFrame.slice(0, 16);

  Bytes decryptedHeader = Bytes.wrap(new byte[16]);
  decryptionCipher.processBytes(headerBytes.toArrayUnsafe(), 0, 16, decryptedHeader.toArrayUnsafe(), 0);
  int frameSize = decryptedHeader.get(0) & 0xff;
  frameSize = (frameSize << 8) + (decryptedHeader.get(1) & 0xff);
  frameSize = (frameSize << 8) + (decryptedHeader.get(2) & 0xff);
  int pad = frameSize % 16 == 0 ? 0 : 16 - frameSize % 16;

  if (messageFrame.size() < 32 + frameSize + pad + 16) {
    return null;
  }

  Bytes expectedMac = calculateMac(headerBytes, true);

  if (!macBytes.equals(expectedMac)) {
    throw new InvalidMACException(
        String.format(
            "Header MAC did not match expected MAC; expected: %s, received: %s",
            expectedMac.toHexString(),
            macBytes.toHexString()));
  }

  Bytes frameData = messageFrame.slice(32, frameSize);
  Bytes frameMac = messageFrame.slice(32 + frameSize + pad, 16);

  Bytes newFrameMac = Bytes.wrap(new byte[16]);
  Bytes frameMacSeed = updateIngress(messageFrame.slice(32, frameSize + pad));
  macEncryptionEngine.processBlock(frameMacSeed.toArrayUnsafe(), 0, newFrameMac.toArrayUnsafe(), 0);
  Bytes expectedFrameMac = updateIngress(newFrameMac.xor(frameMacSeed.slice(0, 16))).slice(0, 16);
  if (!expectedFrameMac.equals(frameMac)) {
    throw new InvalidMACException(
        String.format(
            "Frame MAC did not match expected MAC; expected: %s, received: %s",
            expectedFrameMac.toHexString(),
            frameMac.toHexString()));
  }

  Bytes decryptedFrameData = Bytes.wrap(new byte[frameData.size()]);
  decryptionCipher
      .processBytes(frameData.toArrayUnsafe(), 0, frameData.size(), decryptedFrameData.toArrayUnsafe(), 0);

  int messageType = RLP.decodeInt(decryptedFrameData.slice(0, 1));

  Bytes messageData = decryptedFrameData.slice(1);
  if (applySnappyCompression) {
    try {
      messageData = Bytes.wrap(Snappy.uncompress(messageData.toArrayUnsafe()));
    } catch (IOException e) {
      throw new IllegalArgumentException(e);
    }
  }

  return new RLPxMessage(messageType, messageData, 32 + frameSize + pad + 16);
}