Java Code Examples for org.bouncycastle.util.Arrays#copyOf()

The following examples show how to use org.bouncycastle.util.Arrays#copyOf() . 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: GCMDataB.java    From InflatableDonkey with MIT License 7 votes vote down vote up
public static byte[] decrypt(byte[] key, byte[] data) {
    // TODO utilize GCMAES#decrypt method
    try {
        if (data.length < NONCE_LENGTH + TAG_LENGTH) {
            throw new IllegalArgumentException("data packet too short");
        }

        int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH;

        byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH);

        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce);
        cipher.init(false, parameters);

        byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)];

        int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0);
        pos += cipher.doFinal(out, pos);

        return Arrays.copyOf(out, pos);

    } catch (IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 2
Source File: Algorithm5.java    From sambox with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] computePassword(EncryptionContext context)
{
    context.security.encryption.revision.requireAtLeast(StandardSecurityHandlerRevision.R3,
            "Algorithm 5 requires a security handler of revision 3 or greater");
    digest.reset();
    digest.update(ENCRYPT_PADDING);
    byte[] encrypted = engine.encryptBytes(
            Arrays.copyOf(digest.digest(context.documentId()), 16), context.key());
    byte[] iterationKey = new byte[context.key().length];
    for (int i = 1; i < 20; i++)
    {
        iterationKey = Arrays.copyOf(context.key(), context.key().length);
        for (int j = 0; j < iterationKey.length; j++)
        {
            iterationKey[j] = (byte) (iterationKey[j] ^ (byte) i);
        }
        encrypted = engine.encryptBytes(encrypted, iterationKey);
    }
    return Arrays.concatenate(Arrays.copyOf(encrypted, 16), Arrays.copyOf(ENCRYPT_PADDING, 16));
}
 
Example 3
Source File: Curve25519.java    From LiquidDonkey with MIT License 5 votes vote down vote up
byte[] clampPrivateKey(byte[] privateKey) {
    byte[] copy = Arrays.copyOf(privateKey, privateKey.length);
    copy[0] &= 0xF8;
    copy[31] &= 0x7F;
    copy[31] |= 0x40;
    return copy;
}
 
Example 4
Source File: Algorithm3.java    From sambox with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] computePassword(EncryptionContext context)
{
    byte[] ownerBytes = context.security.getOwnerPassword();
    byte[] userBytes = context.security.getUserPassword();
    byte[] padded = padOrTruncate(
            of(ownerBytes).filter(p -> p.length > 0).orElseGet(() -> userBytes));
    byte[] paddedUser = padOrTruncate(userBytes);
    digest.reset();
    byte[] arc4Key = digest.digest(padded);
    if (StandardSecurityHandlerRevision.R3.compareTo(context.security.encryption.revision) <= 0)
    {
        for (int i = 0; i < 50; ++i)
        {
            digest.update(arc4Key, 0, context.security.encryption.revision.length);
            arc4Key = Arrays.copyOf(digest.digest(),
                    context.security.encryption.revision.length);
        }
        byte[] encrypted = engine.encryptBytes(paddedUser, arc4Key);
        byte[] iterationKey = new byte[arc4Key.length];
        for (int i = 1; i < 20; i++)
        {
            iterationKey = Arrays.copyOf(arc4Key, arc4Key.length);
            for (int j = 0; j < iterationKey.length; j++)
            {
                iterationKey[j] = (byte) (iterationKey[j] ^ (byte) i);
            }
            encrypted = engine.encryptBytes(encrypted, iterationKey);
        }
        return encrypted;
    }
    return engine.encryptBytes(paddedUser, arc4Key);
}
 
Example 5
Source File: BlobA4.java    From InflatableDonkey with MIT License 5 votes vote down vote up
BlobA4(int x, byte[] tag, byte[] uid, byte[] salt, byte[] ephemeralKey) {
    if (tag.length != 0x10) {
        throw new IllegalArgumentException("bad tag 0x" + Hex.toHexString(tag));
    }

    this.x = x;
    this.tag = Arrays.copyOf(tag, 0x10);
    this.uid = Arrays.copyOf(uid, uid.length);
    this.salt = Arrays.copyOf(salt, salt.length);
    this.ephemeralKey = Arrays.copyOf(ephemeralKey, ephemeralKey.length);
}
 
Example 6
Source File: CablePairingData.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
/**
 * @param cableData
 * @param sessionKeyPair
 * @return
 */
public static CablePairingData generatePairingData(CableRegistrationData cableData,
    KeyPair sessionKeyPair) {
  byte[] sharedSecret = Crypto.getS(sessionKeyPair.getPrivate(), cableData.publicKey);

  byte[] info = "FIDO caBLE v1 pairing data".getBytes(StandardCharsets.US_ASCII);
  byte[] version = ByteBuffer.allocate(4).putInt(cableData.versions.get(0)).array();

  byte[] result = Crypto.hkdfSha256(sharedSecret, Crypto.sha256Digest(Bytes.concat(version,
      Crypto.compressECPublicKey((ECPublicKey) sessionKeyPair.getPublic()), cableData.publicKey)),
      info, HKDF_SHA_LENGTH);

  return new CablePairingData(cableData.versions.get(0), Arrays.copyOf(result, K_LENGTH),
      Arrays.copyOfRange(result, K_LENGTH, 2 * K_LENGTH));
}
 
Example 7
Source File: PZKeyUnwrap.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public PZKeyUnwrap(byte[] label, int keyLength) {
    this.label = Arrays.copyOf(label, label.length);
    this.keyLength = keyLength;
}
 
Example 8
Source File: DiskChunk.java    From InflatableDonkey with MIT License 4 votes vote down vote up
@Override
public byte[] checksum() {
    return Arrays.copyOf(checksum, checksum.length);
}
 
Example 9
Source File: ExtendedKey.java    From bop-bitcoin-client with Apache License 2.0 4 votes vote down vote up
public static ExtendedKey parse (String serialized) throws ValidationException
{
	byte[] data = ByteUtils.fromBase58WithChecksum (serialized);
	if ( data.length != 78 )
	{
		throw new ValidationException ("invalid extended key");
	}
	byte[] type = Arrays.copyOf (data, 4);
	boolean hasPrivate;
	if ( Arrays.areEqual (type, xprv) || Arrays.areEqual (type, tprv) )
	{
		hasPrivate = true;
	}
	else if ( Arrays.areEqual (type, xpub) || Arrays.areEqual (type, tpub) )
	{
		hasPrivate = false;
	}
	else
	{
		throw new ValidationException ("invalid magic number for an extended key");
	}

	int depth = data[4] & 0xff;

	int parent = data[5] & 0xff;
	parent <<= 8;
	parent |= data[6] & 0xff;
	parent <<= 8;
	parent |= data[7] & 0xff;
	parent <<= 8;
	parent |= data[8] & 0xff;

	int sequence = data[9] & 0xff;
	sequence <<= 8;
	sequence |= data[10] & 0xff;
	sequence <<= 8;
	sequence |= data[11] & 0xff;
	sequence <<= 8;
	sequence |= data[12] & 0xff;

	byte[] chainCode = Arrays.copyOfRange (data, 13, 13 + 32);
	byte[] pubOrPriv = Arrays.copyOfRange (data, 13 + 32, data.length);
	Key key;
	if ( hasPrivate )
	{
		key = new ECKeyPair (new BigInteger (1, pubOrPriv), true);
	}
	else
	{
		key = new ECPublicKey (pubOrPriv, true);
	}
	return new ExtendedKey (key, chainCode, depth, parent, sequence);
}
 
Example 10
Source File: ProxyMessage.java    From xipki with Apache License 2.0 4 votes vote down vote up
public byte[] getKeyValue() {
  return Arrays.copyOf(keyValue, keyValue.length);
}
 
Example 11
Source File: Algorithm2BExtensionLevel3.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] computeHash(byte[] input, byte[] password)
{
    return Arrays.copyOf(digest.digest(input), 32);
}
 
Example 12
Source File: TagLengthValue.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public byte[] value() {
    return Arrays.copyOf(value, value.length);
}
 
Example 13
Source File: BlobA4.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public byte[] salt() {
    return Arrays.copyOf(salt, salt.length);
}
 
Example 14
Source File: BlobA4.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public byte[] uid() {
    return Arrays.copyOf(uid, uid.length);
}
 
Example 15
Source File: BlobA4.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public byte[] tag() {
    return Arrays.copyOf(tag, tag.length);
}
 
Example 16
Source File: BlobA6.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public byte[] tag() {
    return Arrays.copyOf(tag, tag.length);
}
 
Example 17
Source File: ProxyMessage.java    From xipki with Apache License 2.0 4 votes vote down vote up
public byte[] getOjectId() {
  return objectId == null ? null : Arrays.copyOf(objectId, objectId.length);
}
 
Example 18
Source File: EncryptedKey.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public EncryptedKey(NOS masterKey, byte[] wrappedKey, Optional<Integer> flags) {
    this.masterKey = Objects.requireNonNull(masterKey, "masterKey");
    this.wrappedKey = Arrays.copyOf(wrappedKey, wrappedKey.length);
    this.flags = Objects.requireNonNull(flags, "flags");
}
 
Example 19
Source File: NtlmContext.java    From jcifs-ng with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void verifyMIC ( byte[] data, byte[] mic ) throws CIFSException {
    byte[] sk = this.verifyKey;
    if ( sk == null ) {
        throw new CIFSException("Signing is not initialized");
    }

    int ver = SMBUtil.readInt4(mic, 0);
    if ( ver != 1 ) {
        throw new SmbUnsupportedOperationException("Invalid signature version");
    }

    MessageDigest mac = Crypto.getHMACT64(sk);
    int seq = SMBUtil.readInt4(mic, 12);
    mac.update(mic, 12, 4); // sequence
    byte[] dgst = mac.digest(data); // data
    byte[] trunc = Arrays.copyOf(dgst, 8);

    if ( log.isDebugEnabled() ) {
        log.debug("Digest " + Hexdump.toHexString(dgst));
        log.debug("Truncated " + Hexdump.toHexString(trunc));
    }

    boolean encrypted = ( this.ntlmsspFlags & NtlmFlags.NTLMSSP_NEGOTIATE_KEY_EXCH ) != 0;
    if ( encrypted ) {
        try {
            trunc = this.sealServerHandle.doFinal(trunc);
            if ( log.isDebugEnabled() ) {
                log.debug("Decrypted " + Hexdump.toHexString(trunc));
            }
        }
        catch ( GeneralSecurityException e ) {
            throw new CIFSException("Failed to decrypt MIC", e);
        }
    }

    int expectSeq = this.verifySequence.getAndIncrement();
    if ( expectSeq != seq ) {
        throw new CIFSException(String.format("Invalid MIC sequence, expect %d have %d", expectSeq, seq));
    }

    byte[] verify = new byte[8];
    System.arraycopy(mic, 4, verify, 0, 8);
    if ( !MessageDigest.isEqual(trunc, verify) ) {
        if ( log.isDebugEnabled() ) {
            log.debug(String.format("Seq = %d ver = %d encrypted = %s", seq, ver, encrypted));
            log.debug(String.format("Expected MIC %s != %s", Hexdump.toHexString(trunc), Hexdump.toHexString(verify)));
        }
        throw new CIFSException("Invalid MIC");
    }

}
 
Example 20
Source File: NtlmContext.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void verifyMIC ( byte[] data, byte[] mic ) throws CIFSException {
    byte[] sk = this.verifyKey;
    if ( sk == null ) {
        throw new CIFSException("Signing is not initialized");
    }

    int ver = SMBUtil.readInt4(mic, 0);
    if ( ver != 1 ) {
        throw new SmbUnsupportedOperationException("Invalid signature version");
    }

    MessageDigest mac = Crypto.getHMACT64(sk);
    int seq = SMBUtil.readInt4(mic, 12);
    mac.update(mic, 12, 4); // sequence
    byte[] dgst = mac.digest(data); // data
    byte[] trunc = Arrays.copyOf(dgst, 8);

    if ( log.isDebugEnabled() ) {
        log.debug("Digest " + Hexdump.toHexString(dgst));
        log.debug("Truncated " + Hexdump.toHexString(trunc));
    }

    boolean encrypted = ( this.ntlmsspFlags & NtlmFlags.NTLMSSP_NEGOTIATE_KEY_EXCH ) != 0;
    if ( encrypted ) {
        try {
            trunc = this.sealServerHandle.doFinal(trunc);
            if ( log.isDebugEnabled() ) {
                log.debug("Decrypted " + Hexdump.toHexString(trunc));
            }
        }
        catch ( GeneralSecurityException e ) {
            throw new CIFSException("Failed to decrypt MIC", e);
        }
    }

    int expectSeq = this.verifySequence.getAndIncrement();
    if ( expectSeq != seq ) {
        throw new CIFSException(String.format("Invalid MIC sequence, expect %d have %d", expectSeq, seq));
    }

    byte[] verify = new byte[8];
    System.arraycopy(mic, 4, verify, 0, 8);
    if ( !MessageDigest.isEqual(trunc, verify) ) {
        if ( log.isDebugEnabled() ) {
            log.debug(String.format("Seq = %d ver = %d encrypted = %s", seq, ver, encrypted));
            log.debug(String.format("Expected MIC %s != %s", Hexdump.toHexString(trunc), Hexdump.toHexString(verify)));
        }
        throw new CIFSException("Invalid MIC");
    }

}