Example usage for org.bouncycastle.util Arrays concatenate

List of usage examples for org.bouncycastle.util Arrays concatenate

Introduction

In this page you can find the example usage for org.bouncycastle.util Arrays concatenate.

Prototype

public static int[] concatenate(int[] a, int[] b) 

Source Link

Usage

From source file:org.opendaylight.capwap.dtls.DtlsClient.java

License:Open Source License

public int[] getCipherSuites() {
    return Arrays.concatenate(super.getCipherSuites(),
            new int[] { CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, });
}

From source file:org.opendaylight.capwap.dtls.DtlsServer.java

License:Open Source License

protected int[] getCipherSuites() {
    return Arrays.concatenate(super.getCipherSuites(),
            new int[] { CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, });
}

From source file:org.sejda.sambox.encryption.Algorithm5.java

License:Apache License

@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();/*ww  w.  j  av  a 2  s. c  o  m*/
    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));
}

From source file:org.sejda.sambox.encryption.ConcatenatingAESEngineTest.java

License:Apache License

@Test
public void encryptBytes() {
    byte[] key = new byte[] { -40, -23, -118, -66, -77, -34, 42, 9, 11, 22, 105, 86, -92, 23, 57, 4 };
    byte[] iv = new byte[] { 18, -87, 49, -32, -126, 116, -128, -36, -78, 70, 99, -98, -65, 90, -95, 101 };
    byte[] expected = new byte[] { -125, -84, -39, -13, -125, 92, 23, -82, 68, 81, -78, 105, 34, 21, -70, -14 };
    assertArrayEquals(Arrays.concatenate(iv, expected), victim.encryptBytes("ChuckNorris".getBytes(), key, iv));
}

From source file:org.sejda.sambox.encryption.ConcatenatingAESEngineTest.java

License:Apache License

@Test
public void encryptStream() throws IOException {
    byte[] key = new byte[] { -40, -23, -118, -66, -77, -34, 42, 9, 11, 22, 105, 86, -92, 23, 57, 4 };
    byte[] iv = new byte[] { 18, -87, 49, -32, -126, 116, -128, -36, -78, 70, 99, -98, -65, 90, -95, 101 };
    byte[] expected = new byte[] { -125, -84, -39, -13, -125, 92, 23, -82, 68, 81, -78, 105, 34, 21, -70, -14 };

    InputStream inputStream = victim.encryptStream(new ByteArrayInputStream("ChuckNorris".getBytes()), key, iv);
    assertArrayEquals(Arrays.concatenate(iv, expected), IOUtils.toByteArray(inputStream));

}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenConnection.java

License:Open Source License

private byte[] prepareDsi(byte[] hash, int hashAlgo) throws IOException {
    byte[] dsi;//from w w  w.j a v  a 2s .co m

    Log.i(Constants.TAG, "Hash: " + hashAlgo);
    switch (hashAlgo) {
    case HashAlgorithmTags.SHA1:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 10!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021" // Tag/Length of Sequence, the 0x21 includes all following 33 bytes
                + "3009" // Tag/Length of Sequence, the 0x09 are the following header bytes
                + "0605" + "2B0E03021A" // OID of SHA1
                + "0500" // TLV coding of ZERO
                + "0414"), hash); // 0x14 are 20 hash bytes
        break;
    case HashAlgorithmTags.RIPEMD160:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 20!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021300906052B2403020105000414"), hash);
        break;
    case HashAlgorithmTags.SHA224:
        if (hash.length != 28) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 28!");
        }
        dsi = Arrays.concatenate(Hex.decode("302D300D06096086480165030402040500041C"), hash);
        break;
    case HashAlgorithmTags.SHA256:
        if (hash.length != 32) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 32!");
        }
        dsi = Arrays.concatenate(Hex.decode("3031300D060960864801650304020105000420"), hash);
        break;
    case HashAlgorithmTags.SHA384:
        if (hash.length != 48) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 48!");
        }
        dsi = Arrays.concatenate(Hex.decode("3041300D060960864801650304020205000430"), hash);
        break;
    case HashAlgorithmTags.SHA512:
        if (hash.length != 64) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 64!");
        }
        dsi = Arrays.concatenate(Hex.decode("3051300D060960864801650304020305000440"), hash);
        break;
    default:
        throw new IOException("Not supported hash algo!");
    }
    return dsi;
}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenHelper.java

License:Open Source License

/**
 * Modifies the user's PW1 or PW3. Before sending, the new PIN will be validated for
 * conformance to the token's requirements for key length.
 *
 * @param pw     For PW1, this is 0x81. For PW3 (Admin PIN), mode is 0x83.
 * @param newPin The new PW1 or PW3.//from  w  w  w. ja  v a2s.  co m
 */
public void modifyPin(int pw, byte[] newPin) throws IOException {
    final int MAX_PW1_LENGTH_INDEX = 1;
    final int MAX_PW3_LENGTH_INDEX = 3;

    byte[] pwStatusBytes = getPwStatusBytes();

    if (pw == 0x81) {
        if (newPin.length < 6 || newPin.length > pwStatusBytes[MAX_PW1_LENGTH_INDEX]) {
            throw new IOException("Invalid PIN length");
        }
    } else if (pw == 0x83) {
        if (newPin.length < 8 || newPin.length > pwStatusBytes[MAX_PW3_LENGTH_INDEX]) {
            throw new IOException("Invalid PIN length");
        }
    } else {
        throw new IOException("Invalid PW index for modify PIN operation");
    }

    byte[] pin;
    if (pw == 0x83) {
        pin = mAdminPin.toStringUnsafe().getBytes();
    } else {
        pin = mPin.toStringUnsafe().getBytes();
    }

    // Command APDU for CHANGE REFERENCE DATA command (page 32)
    CommandAPDU changePin = new CommandAPDU(0x00, 0x24, 0x00, pw, Arrays.concatenate(pin, newPin));
    ResponseAPDU response = communicate(changePin);

    if (response.getSW() != APDU_SW_SUCCESS) {
        throw new CardException("Failed to change PIN", response.getSW());
    }
}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenHelper.java

License:Open Source License

/**
 * Call COMPUTE DIGITAL SIGNATURE command and returns the MPI value
 *
 * @param hash the hash for signing/*w ww .  j a va2s .  c  o  m*/
 * @return a big integer representing the MPI for the given hash
 */
public byte[] calculateSignature(byte[] hash, int hashAlgo) throws IOException {
    if (!mPw1ValidatedForSignature) {
        verifyPin(0x81); // (Verify PW1 with mode 81 for signing)
    }

    byte[] dsi;

    Log.i(Constants.TAG, "Hash: " + hashAlgo);
    switch (hashAlgo) {
    case HashAlgorithmTags.SHA1:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 10!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021" // Tag/Length of Sequence, the 0x21 includes all following 33 bytes
                + "3009" // Tag/Length of Sequence, the 0x09 are the following header bytes
                + "0605" + "2B0E03021A" // OID of SHA1
                + "0500" // TLV coding of ZERO
                + "0414"), hash); // 0x14 are 20 hash bytes
        break;
    case HashAlgorithmTags.RIPEMD160:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 20!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021300906052B2403020105000414"), hash);
        break;
    case HashAlgorithmTags.SHA224:
        if (hash.length != 28) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 28!");
        }
        dsi = Arrays.concatenate(Hex.decode("302D300D06096086480165030402040500041C"), hash);
        break;
    case HashAlgorithmTags.SHA256:
        if (hash.length != 32) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 32!");
        }
        dsi = Arrays.concatenate(Hex.decode("3031300D060960864801650304020105000420"), hash);
        break;
    case HashAlgorithmTags.SHA384:
        if (hash.length != 48) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 48!");
        }
        dsi = Arrays.concatenate(Hex.decode("3041300D060960864801650304020205000430"), hash);
        break;
    case HashAlgorithmTags.SHA512:
        if (hash.length != 64) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 64!");
        }
        dsi = Arrays.concatenate(Hex.decode("3051300D060960864801650304020305000440"), hash);
        break;
    default:
        throw new IOException("Not supported hash algo!");
    }

    // Command APDU for PERFORM SECURITY OPERATION: COMPUTE DIGITAL SIGNATURE (page 37)
    CommandAPDU command = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A, dsi, MAX_APDU_NE_EXT);
    ResponseAPDU response = communicate(command);

    if (response.getSW() != APDU_SW_SUCCESS) {
        throw new CardException("Failed to sign", response.getSW());
    }

    if (!mOpenPgpCapabilities.isPw1ValidForMultipleSignatures()) {
        mPw1ValidatedForSignature = false;
    }

    byte[] signature = response.getData();

    // Make sure the signature we received is actually the expected number of bytes long!
    if (signature.length != 128 && signature.length != 256 && signature.length != 384
            && signature.length != 512) {
        throw new IOException("Bad signature length! Expected 128/256/384/512 bytes, got " + signature.length);
    }

    return signature;
}

From source file:org.sufficientlysecure.keychain.securitytoken.usb.CcidTransceiver.java

License:Open Source License

/**
 * Transmits XfrBlock/*  w ww . ja  v  a  2s  .  c o  m*/
 * 6.1.4 PC_to_RDR_XfrBlock
 * @param payload payload to transmit
 * @throws UsbTransportException
 */
public void sendXfrBlock(byte[] payload) throws UsbTransportException {
    int l = payload.length;
    byte[] data = Arrays.concatenate(new byte[] { 0x6f, (byte) l, (byte) (l >> 8), (byte) (l >> 16),
            (byte) (l >> 24), 0x00, mCounter++, 0x00, 0x00, 0x00 }, payload);

    int send = 0;
    while (send < data.length) {
        final int len = Math.min(mBulkIn.getMaxPacketSize(), data.length - send);
        sendRaw(Arrays.copyOfRange(data, send, send + len));
        send += len;
    }
}

From source file:org.sufficientlysecure.keychain.securitytoken.usb.tpdu.T1TpduProtocol.java

License:Open Source License

public byte[] transceive(@NonNull byte[] apdu) throws UsbTransportException {
    int start = 0;

    if (apdu.length == 0) {
        throw new UsbTransportException("Cant transcive zero-length apdu(tpdu)");
    }//ww  w.  j  a v a 2  s .c o m

    Block responseBlock = null;
    while (apdu.length - start > 0) {
        boolean hasMore = start + MAX_FRAME_LEN < apdu.length;
        int len = Math.min(MAX_FRAME_LEN, apdu.length - start);

        // Send next frame
        Block block = newIBlock(mCounter++, hasMore, Arrays.copyOfRange(apdu, start, start + len));

        mTransceiver.sendXfrBlock(block.getRawData());

        // Receive I or R block
        responseBlock = getBlockFromResponse(mTransceiver.receiveRaw());

        start += len;

        if (responseBlock instanceof SBlock) {
            Log.d(Constants.TAG, "S-Block received " + responseBlock.toString());
            // just ignore
        } else if (responseBlock instanceof RBlock) {
            Log.d(Constants.TAG, "R-Block received " + responseBlock.toString());
            if (((RBlock) responseBlock).getError() != RBlock.RError.NO_ERROR) {
                throw new UsbTransportException("R-Block reports error " + ((RBlock) responseBlock).getError());
            }
        } else { // I block
            if (start != apdu.length) {
                throw new UsbTransportException("T1 frame response underflow");
            }
            break;
        }
    }

    // Receive
    if (responseBlock == null || !(responseBlock instanceof IBlock))
        throw new UsbTransportException("Invalid tpdu sequence state");

    byte[] responseApdu = responseBlock.getApdu();

    while (((IBlock) responseBlock).getChaining()) {
        Block ackBlock = newRBlock((byte) (((IBlock) responseBlock).getSequence() + 1));
        mTransceiver.sendXfrBlock(ackBlock.getRawData());

        responseBlock = getBlockFromResponse(mTransceiver.receiveRaw());

        if (responseBlock instanceof IBlock) {
            responseApdu = Arrays.concatenate(responseApdu, responseBlock.getApdu());
        } else {
            Log.d(Constants.TAG, "Response block received " + responseBlock.toString());
            throw new UsbTransportException("Response: invalid state - invalid block received");
        }
    }

    return responseApdu;
}