sun.security.util.ArrayUtil Java Examples

The following examples show how to use sun.security.util.ArrayUtil. 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: OutputFeedback.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {
    ArrayUtil.blockSizeCheck(plainLen, numBytes);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);

    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        for (int i = 0; i < numBytes; i++) {
            cipher[i + cipherOffset] =
                (byte)(k[i] ^ plain[i + plainOffset]);
        }
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        System.arraycopy(k, 0, register, nShift, numBytes);
    }
    return plainLen;
}
 
Example #2
Source File: OutputFeedback.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs last encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @return the length of the encrypted data
 */
int encryptFinal(byte[] plain, int plainOffset, int plainLen,
                 byte[] cipher, int cipherOffset) {
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);

    int oddBytes = plainLen % numBytes;
    int len = encrypt(plain, plainOffset, (plainLen - oddBytes),
                      cipher, cipherOffset);
    plainOffset += len;
    cipherOffset += len;

    if (oddBytes != 0) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        for (int i = 0; i < oddBytes; i++) {
            cipher[i + cipherOffset] =
                (byte)(k[i] ^ plain[ i + plainOffset]);
        }
    }
    return plainLen;
}
 
Example #3
Source File: PCBC.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset)
{
    ArrayUtil.blockSizeCheck(plainLen, blockSize);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);

    int i;
    int endIndex = plainOffset + plainLen;

    for (; plainOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        for (i = 0; i < blockSize; i++) {
            k[i] ^= plain[i + plainOffset];
        }
        embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return plainLen;
}
 
Example #4
Source File: PCBC.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the block size
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset)
{
    ArrayUtil.blockSizeCheck(cipherLen, blockSize);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);

    int i;
    int endIndex = cipherOffset + cipherLen;

    for (; cipherOffset < endIndex;
         plainOffset += blockSize, cipherOffset += blockSize) {
        embeddedCipher.decryptBlock(cipher, cipherOffset,
                               plain, plainOffset);
        for (i = 0; i < blockSize; i++) {
            plain[i + plainOffset] ^= k[i];
        }
        for (i = 0; i < blockSize; i++) {
            k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]);
        }
    }
    return cipherLen;
}
 
Example #5
Source File: GaloisCounterMode.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>in</code>, starting at <code>inOfs</code>
 * and ending at <code>(inOfs + len - 1)</code>, is encrypted. The result
 * is stored in <code>out</code>, starting at <code>outOfs</code>.
 *
 * @param in the buffer with the input data to be encrypted
 * @param inOfs the offset in <code>in</code>
 * @param len the length of the input data
 * @param out the buffer for the result
 * @param outOfs the offset in <code>out</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the number of bytes placed into the <code>out</code> buffer
 */
int encrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
    ArrayUtil.blockSizeCheck(len, blockSize);

    checkDataLength(processed, len);

    processAAD();

    if (len > 0) {
        ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
        ArrayUtil.nullAndBoundsCheck(out, outOfs, len);

        gctrPAndC.update(in, inOfs, len, out, outOfs);
        processed += len;
        ghashAllToS.update(out, outOfs, len);
    }

    return len;
}
 
Example #6
Source File: CipherFeedback.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>cipherLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset) {

    ArrayUtil.blockSizeCheck(cipherLen, numBytes);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);

    int nShift = blockSize - numBytes;
    int loopCount = cipherLen / numBytes;

    for (; loopCount > 0;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[i + nShift] = cipher[i + cipherOffset];
            plain[i + plainOffset]
                = (byte)(cipher[i + cipherOffset] ^ k[i]);
        }
    }
    return cipherLen;
}
 
Example #7
Source File: CipherFeedback.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>plainLen</code> is not
 * a multiple of the <code>numBytes</code>
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {
    ArrayUtil.blockSizeCheck(plainLen, numBytes);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);

    int nShift = blockSize - numBytes;
    int loopCount = plainLen / numBytes;

    for (; loopCount > 0 ;
         plainOffset += numBytes, cipherOffset += numBytes,
         loopCount--) {
        embeddedCipher.encryptBlock(register, 0, k, 0);
        if (nShift != 0) {
            System.arraycopy(register, numBytes, register, 0, nShift);
        }
        for (int i = 0; i < numBytes; i++) {
            register[nShift + i] = cipher[i + cipherOffset] =
                    (byte)(k[i] ^ plain[i + plainOffset]);
        }
    }
    return plainLen;
}
 
Example #8
Source File: ECDHKeyAgreement.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void validate(ECOperations ops, ECPublicKey key) {

        // ensure that integers are in proper range
        BigInteger x = key.getW().getAffineX();
        BigInteger y = key.getW().getAffineY();

        BigInteger p = ops.getField().getSize();
        validateCoordinate(x, p);
        validateCoordinate(y, p);

        // ensure the point is on the curve
        EllipticCurve curve = key.getParams().getCurve();
        BigInteger rhs = x.modPow(BigInteger.valueOf(3), p).add(curve.getA()
            .multiply(x)).add(curve.getB()).mod(p);
        BigInteger lhs = y.modPow(BigInteger.valueOf(2), p).mod(p);
        if (!rhs.equals(lhs)) {
            throw new ProviderException("point is not on curve");
        }

        // check the order of the point
        ImmutableIntegerModuloP xElem = ops.getField().getElement(x);
        ImmutableIntegerModuloP yElem = ops.getField().getElement(y);
        AffinePoint affP = new AffinePoint(xElem, yElem);
        byte[] order = key.getParams().getOrder().toByteArray();
        ArrayUtil.reverse(order);
        Point product = ops.multiply(affP, order);
        if (!ops.isNeutral(product)) {
            throw new ProviderException("point has incorrect order");
        }

    }
 
Example #9
Source File: ECDHKeyAgreement.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void validate(ECOperations ops, ECPublicKey key) {

        // ensure that integers are in proper range
        BigInteger x = key.getW().getAffineX();
        BigInteger y = key.getW().getAffineY();

        BigInteger p = ops.getField().getSize();
        validateCoordinate(x, p);
        validateCoordinate(y, p);

        // ensure the point is on the curve
        EllipticCurve curve = key.getParams().getCurve();
        BigInteger rhs = x.modPow(BigInteger.valueOf(3), p).add(curve.getA()
            .multiply(x)).add(curve.getB()).mod(p);
        BigInteger lhs = y.modPow(BigInteger.valueOf(2), p).mod(p);
        if (!rhs.equals(lhs)) {
            throw new ProviderException("point is not on curve");
        }

        // check the order of the point
        ImmutableIntegerModuloP xElem = ops.getField().getElement(x);
        ImmutableIntegerModuloP yElem = ops.getField().getElement(y);
        AffinePoint affP = new AffinePoint(xElem, yElem);
        byte[] order = key.getParams().getOrder().toByteArray();
        ArrayUtil.reverse(order);
        Point product = ops.multiply(affP, order);
        if (!ops.isNeutral(product)) {
            throw new ProviderException("point has incorrect order");
        }

    }
 
Example #10
Source File: ECDHKeyAgreement.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void validate(ECOperations ops, ECPublicKey key) {

        // ensure that integers are in proper range
        BigInteger x = key.getW().getAffineX();
        BigInteger y = key.getW().getAffineY();

        BigInteger p = ops.getField().getSize();
        validateCoordinate(x, p);
        validateCoordinate(y, p);

        // ensure the point is on the curve
        EllipticCurve curve = key.getParams().getCurve();
        BigInteger rhs = x.modPow(BigInteger.valueOf(3), p).add(curve.getA()
            .multiply(x)).add(curve.getB()).mod(p);
        BigInteger lhs = y.modPow(BigInteger.valueOf(2), p).mod(p);
        if (!rhs.equals(lhs)) {
            throw new ProviderException("point is not on curve");
        }

        // check the order of the point
        ImmutableIntegerModuloP xElem = ops.getField().getElement(x);
        ImmutableIntegerModuloP yElem = ops.getField().getElement(y);
        AffinePoint affP = new AffinePoint(xElem, yElem);
        byte[] order = key.getParams().getOrder().toByteArray();
        ArrayUtil.reverse(order);
        Point product = ops.multiply(affP, order);
        if (!ops.isNeutral(product)) {
            throw new ProviderException("point has incorrect order");
        }

    }
 
Example #11
Source File: ECDHKeyAgreement.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void validate(ECOperations ops, ECPublicKey key) {

        // ensure that integers are in proper range
        BigInteger x = key.getW().getAffineX();
        BigInteger y = key.getW().getAffineY();

        BigInteger p = ops.getField().getSize();
        validateCoordinate(x, p);
        validateCoordinate(y, p);

        // ensure the point is on the curve
        EllipticCurve curve = key.getParams().getCurve();
        BigInteger rhs = x.modPow(BigInteger.valueOf(3), p).add(curve.getA()
            .multiply(x)).add(curve.getB()).mod(p);
        BigInteger lhs = y.modPow(BigInteger.valueOf(2), p).mod(p);
        if (!rhs.equals(lhs)) {
            throw new ProviderException("point is not on curve");
        }

        // check the order of the point
        ImmutableIntegerModuloP xElem = ops.getField().getElement(x);
        ImmutableIntegerModuloP yElem = ops.getField().getElement(y);
        AffinePoint affP = new AffinePoint(xElem, yElem);
        byte[] order = key.getParams().getOrder().toByteArray();
        ArrayUtil.reverse(order);
        Point product = ops.multiply(affP, order);
        if (!ops.isNeutral(product)) {
            throw new ProviderException("point has incorrect order");
        }

    }
 
Example #12
Source File: CounterMode.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Do the actual encryption/decryption operation.
 * Essentially we XOR the input plaintext/ciphertext stream with a
 * keystream generated by encrypting the counter values. Counter values
 * are encrypted on demand.
 */
private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
    if (len == 0) {
        return 0;
    }

    ArrayUtil.nullAndBoundsCheck(in, inOff, len);
    ArrayUtil.nullAndBoundsCheck(out, outOff, len);
    return implCrypt(in, inOff, len, out, outOff);
}
 
Example #13
Source File: GaloisCounterMode.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Performs encryption operation for the last time.
 *
 * @param in the input buffer with the data to be encrypted
 * @param inOfs the offset in <code>in</code>
 * @param len the length of the input data
 * @param out the buffer for the encryption result
 * @param outOfs the offset in <code>out</code>
 * @return the number of bytes placed into the <code>out</code> buffer
 */
int encryptFinal(byte[] in, int inOfs, int len, byte[] out, int outOfs)
    throws IllegalBlockSizeException, ShortBufferException {
    if (len > MAX_BUF_SIZE - tagLenBytes) {
        throw new ShortBufferException
            ("Can't fit both data and tag into one buffer");
    }
    try {
        ArrayUtil.nullAndBoundsCheck(out, outOfs,
            (len + tagLenBytes));
    } catch (ArrayIndexOutOfBoundsException aiobe) {
        throw new ShortBufferException("Output buffer too small");
    }

    checkDataLength(processed, len);

    processAAD();
    if (len > 0) {
        ArrayUtil.nullAndBoundsCheck(in, inOfs, len);

        doLastBlock(in, inOfs, len, out, outOfs, true);
    }

    byte[] lengthBlock =
        getLengthBlock(sizeOfAAD, processed);
    ghashAllToS.update(lengthBlock);
    byte[] s = ghashAllToS.digest();
    byte[] sOut = new byte[s.length];
    GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
    gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);

    System.arraycopy(sOut, 0, out, (outOfs + len), tagLenBytes);
    return (len + tagLenBytes);
}
 
Example #14
Source File: ECDHKeyAgreement.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static
Optional<byte[]> deriveKeyImpl(ECPrivateKey priv, ECPublicKey pubKey) {

    ECParameterSpec ecSpec = priv.getParams();
    EllipticCurve curve = ecSpec.getCurve();
    Optional<ECOperations> opsOpt = ECOperations.forParameters(ecSpec);
    if (!opsOpt.isPresent()) {
        return Optional.empty();
    }
    ECOperations ops = opsOpt.get();
    if (! (priv instanceof ECPrivateKeyImpl)) {
        return Optional.empty();
    }
    ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) priv;
    byte[] sArr = privImpl.getArrayS();

    // to match the native implementation, validate the public key here
    // and throw ProviderException if it is invalid
    validate(ops, pubKey);

    IntegerFieldModuloP field = ops.getField();
    // convert s array into field element and multiply by the cofactor
    MutableIntegerModuloP scalar = field.getElement(sArr).mutable();
    SmallValue cofactor =
        field.getSmallValue(priv.getParams().getCofactor());
    scalar.setProduct(cofactor);
    int keySize = (curve.getField().getFieldSize() + 7) / 8;
    byte[] privArr = scalar.asByteArray(keySize);

    ImmutableIntegerModuloP x =
        field.getElement(pubKey.getW().getAffineX());
    ImmutableIntegerModuloP y =
        field.getElement(pubKey.getW().getAffineY());
    AffinePoint affPub = new AffinePoint(x, y);
    Point product = ops.multiply(affPub, privArr);
    if (ops.isNeutral(product)) {
        throw new ProviderException("Product is zero");
    }
    AffinePoint affProduct = product.asAffine();

    byte[] result = affProduct.getX().asByteArray(keySize);
    ArrayUtil.reverse(result);

    return Optional.of(result);
}
 
Example #15
Source File: GaloisCounterMode.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Performs decryption operation for the last time.
 *
 * <p>NOTE: For cipher feedback modes which does not perform
 * special handling for the last few blocks, this is essentially
 * the same as <code>encrypt(...)</code>. Given most modes do
 * not do special handling, the default impl for this method is
 * to simply call <code>decrypt(...)</code>.
 *
 * @param in the input buffer with the data to be decrypted
 * @param inOfs the offset in <code>cipher</code>
 * @param len the length of the input data
 * @param out the buffer for the decryption result
 * @param outOfs the offset in <code>plain</code>
 * @return the number of bytes placed into the <code>out</code> buffer
 */
int decryptFinal(byte[] in, int inOfs, int len,
                 byte[] out, int outOfs)
    throws IllegalBlockSizeException, AEADBadTagException,
    ShortBufferException {
    if (len < tagLenBytes) {
        throw new AEADBadTagException("Input too short - need tag");
    }

    // do this check here can also catch the potential integer overflow
    // scenario for the subsequent output buffer capacity check.
    checkDataLength(ibuffer.size(), (len - tagLenBytes));

    try {
        ArrayUtil.nullAndBoundsCheck(out, outOfs,
            (ibuffer.size() + len) - tagLenBytes);
    } catch (ArrayIndexOutOfBoundsException aiobe) {
        throw new ShortBufferException("Output buffer too small");
    }

    processAAD();

    ArrayUtil.nullAndBoundsCheck(in, inOfs, len);

    // get the trailing tag bytes from 'in'
    byte[] tag = new byte[tagLenBytes];
    System.arraycopy(in, inOfs + len - tagLenBytes, tag, 0, tagLenBytes);
    len -= tagLenBytes;

    // If decryption is in-place or there is buffered "ibuffer" data, copy
    // the "in" byte array into the ibuffer before proceeding.
    if (in == out || ibuffer.size() > 0) {
        if (len > 0) {
            ibuffer.write(in, inOfs, len);
        }

        // refresh 'in' to all buffered-up bytes
        in = ibuffer.toByteArray();
        inOfs = 0;
        len = in.length;
        ibuffer.reset();
    }

    if (len > 0) {
        doLastBlock(in, inOfs, len, out, outOfs, false);
    }

    byte[] lengthBlock =
        getLengthBlock(sizeOfAAD, processed);
    ghashAllToS.update(lengthBlock);

    byte[] s = ghashAllToS.digest();
    byte[] sOut = new byte[s.length];
    GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
    gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);

    // check entire authentication tag for time-consistency
    int mismatch = 0;
    for (int i = 0; i < tagLenBytes; i++) {
        mismatch |= tag[i] ^ sOut[i];
    }

    if (mismatch != 0) {
        throw new AEADBadTagException("Tag mismatch!");
    }

    return len;
}
 
Example #16
Source File: GaloisCounterMode.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>in</code>, starting at
 * <code>inOfs</code> and ending at <code>(inOfs + len - 1)</code>,
 * is decrypted. The result is stored in <code>out</code>, starting at
 * <code>outOfs</code>.
 *
 * @param in the buffer with the input data to be decrypted
 * @param inOfs the offset in <code>in</code>
 * @param len the length of the input data
 * @param out the buffer for the result
 * @param outOfs the offset in <code>out</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the number of bytes placed into the <code>out</code> buffer
 */
int decrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
    ArrayUtil.blockSizeCheck(len, blockSize);

    checkDataLength(ibuffer.size(), len);

    processAAD();

    if (len > 0) {
        // store internally until decryptFinal is called because
        // spec mentioned that only return recovered data after tag
        // is successfully verified
        ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
        ibuffer.write(in, inOfs, len);
    }
    return 0;
}
 
Example #17
Source File: ECDHKeyAgreement.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static
Optional<byte[]> deriveKeyImpl(ECPrivateKey priv, ECPublicKey pubKey) {

    ECParameterSpec ecSpec = priv.getParams();
    EllipticCurve curve = ecSpec.getCurve();
    Optional<ECOperations> opsOpt = ECOperations.forParameters(ecSpec);
    if (!opsOpt.isPresent()) {
        return Optional.empty();
    }
    ECOperations ops = opsOpt.get();
    if (! (priv instanceof ECPrivateKeyImpl)) {
        return Optional.empty();
    }
    ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) priv;
    byte[] sArr = privImpl.getArrayS();

    // to match the native implementation, validate the public key here
    // and throw ProviderException if it is invalid
    validate(ops, pubKey);

    IntegerFieldModuloP field = ops.getField();
    // convert s array into field element and multiply by the cofactor
    MutableIntegerModuloP scalar = field.getElement(sArr).mutable();
    SmallValue cofactor =
        field.getSmallValue(priv.getParams().getCofactor());
    scalar.setProduct(cofactor);
    int keySize = (curve.getField().getFieldSize() + 7) / 8;
    byte[] privArr = scalar.asByteArray(keySize);

    ImmutableIntegerModuloP x =
        field.getElement(pubKey.getW().getAffineX());
    ImmutableIntegerModuloP y =
        field.getElement(pubKey.getW().getAffineY());
    AffinePoint affPub = new AffinePoint(x, y);
    Point product = ops.multiply(affPub, privArr);
    if (ops.isNeutral(product)) {
        throw new ProviderException("Product is zero");
    }
    AffinePoint affProduct = product.asAffine();

    byte[] result = affProduct.getX().asByteArray(keySize);
    ArrayUtil.reverse(result);

    return Optional.of(result);
}
 
Example #18
Source File: ECDHKeyAgreement.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static
Optional<byte[]> deriveKeyImpl(ECPrivateKey priv, ECPublicKey pubKey) {

    ECParameterSpec ecSpec = priv.getParams();
    EllipticCurve curve = ecSpec.getCurve();
    Optional<ECOperations> opsOpt = ECOperations.forParameters(ecSpec);
    if (!opsOpt.isPresent()) {
        return Optional.empty();
    }
    ECOperations ops = opsOpt.get();
    if (! (priv instanceof ECPrivateKeyImpl)) {
        return Optional.empty();
    }
    ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) priv;
    byte[] sArr = privImpl.getArrayS();

    // to match the native implementation, validate the public key here
    // and throw ProviderException if it is invalid
    validate(ops, pubKey);

    IntegerFieldModuloP field = ops.getField();
    // convert s array into field element and multiply by the cofactor
    MutableIntegerModuloP scalar = field.getElement(sArr).mutable();
    SmallValue cofactor =
        field.getSmallValue(priv.getParams().getCofactor());
    scalar.setProduct(cofactor);
    int keySize = (curve.getField().getFieldSize() + 7) / 8;
    byte[] privArr = scalar.asByteArray(keySize);

    ImmutableIntegerModuloP x =
        field.getElement(pubKey.getW().getAffineX());
    ImmutableIntegerModuloP y =
        field.getElement(pubKey.getW().getAffineY());
    AffinePoint affPub = new AffinePoint(x, y);
    Point product = ops.multiply(affPub, privArr);
    if (ops.isNeutral(product)) {
        throw new ProviderException("Product is zero");
    }
    AffinePoint affProduct = product.asAffine();

    byte[] result = affProduct.getX().asByteArray(keySize);
    ArrayUtil.reverse(result);

    return Optional.of(result);
}
 
Example #19
Source File: ECDHKeyAgreement.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static
Optional<byte[]> deriveKeyImpl(ECPrivateKey priv, ECPublicKey pubKey) {

    ECParameterSpec ecSpec = priv.getParams();
    EllipticCurve curve = ecSpec.getCurve();
    Optional<ECOperations> opsOpt = ECOperations.forParameters(ecSpec);
    if (!opsOpt.isPresent()) {
        return Optional.empty();
    }
    ECOperations ops = opsOpt.get();
    if (! (priv instanceof ECPrivateKeyImpl)) {
        return Optional.empty();
    }
    ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) priv;
    byte[] sArr = privImpl.getArrayS();

    // to match the native implementation, validate the public key here
    // and throw ProviderException if it is invalid
    validate(ops, pubKey);

    IntegerFieldModuloP field = ops.getField();
    // convert s array into field element and multiply by the cofactor
    MutableIntegerModuloP scalar = field.getElement(sArr).mutable();
    SmallValue cofactor =
        field.getSmallValue(priv.getParams().getCofactor());
    scalar.setProduct(cofactor);
    int keySize = (curve.getField().getFieldSize() + 7) / 8;
    byte[] privArr = scalar.asByteArray(keySize);

    ImmutableIntegerModuloP x =
        field.getElement(pubKey.getW().getAffineX());
    ImmutableIntegerModuloP y =
        field.getElement(pubKey.getW().getAffineY());
    AffinePoint affPub = new AffinePoint(x, y);
    Point product = ops.multiply(affPub, privArr);
    if (ops.isNeutral(product)) {
        throw new ProviderException("Product is zero");
    }
    AffinePoint affProduct = product.asAffine();

    byte[] result = affProduct.getX().asByteArray(keySize);
    ArrayUtil.reverse(result);

    return Optional.of(result);
}
 
Example #20
Source File: CipherBlockChaining.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>plain</code>, starting at
 * <code>plainOffset</code> and ending at
 * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 * The result is stored in <code>cipher</code>, starting at
 * <code>cipherOffset</code>.
 *
 * @param plain the buffer with the input data to be encrypted
 * @param plainOffset the offset in <code>plain</code>
 * @param plainLen the length of the input data
 * @param cipher the buffer for the result
 * @param cipherOffset the offset in <code>cipher</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] plain, int plainOffset, int plainLen,
            byte[] cipher, int cipherOffset) {
    if (plainLen <= 0) {
        return plainLen;
    }
    ArrayUtil.blockSizeCheck(plainLen, blockSize);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
    return implEncrypt(plain, plainOffset, plainLen,
                       cipher, cipherOffset);
}
 
Example #21
Source File: CipherBlockChaining.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Performs decryption operation.
 *
 * <p>The input cipher text <code>cipher</code>, starting at
 * <code>cipherOffset</code> and ending at
 * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 * The result is stored in <code>plain</code>, starting at
 * <code>plainOffset</code>.
 *
 * <p>It is also the application's responsibility to make sure that
 * <code>init</code> has been called before this method is called.
 * (This check is omitted here, to avoid double checking.)
 *
 * @param cipher the buffer with the input data to be decrypted
 * @param cipherOffset the offset in <code>cipherOffset</code>
 * @param cipherLen the length of the input data
 * @param plain the buffer for the result
 * @param plainOffset the offset in <code>plain</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the length of the decrypted data
 */
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
            byte[] plain, int plainOffset) {
    if (cipherLen <= 0) {
        return cipherLen;
    }
    ArrayUtil.blockSizeCheck(cipherLen, blockSize);
    ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
    ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);
    return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
}
 
Example #22
Source File: ElectronicCodeBook.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Performs encryption operation.
 *
 * <p>The input plain text <code>in</code>, starting at
 * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
 * is encrypted. The result is stored in <code>out</code>, starting at
 * <code>outOff</code>.
 *
 * @param in the buffer with the input data to be encrypted
 * @param inOff the offset in <code>plain</code>
 * @param len the length of the input data
 * @param out the buffer for the result
 * @param outOff the offset in <code>cipher</code>
 * @exception ProviderException if <code>len</code> is not
 * a multiple of the block size
 * @return the length of the encrypted data
 */
int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
    ArrayUtil.blockSizeCheck(len, blockSize);
    ArrayUtil.nullAndBoundsCheck(in, inOff, len);
    ArrayUtil.nullAndBoundsCheck(out, outOff, len);
    return implECBEncrypt(in, inOff, len, out, outOff);
}
 
Example #23
Source File: ElectronicCodeBook.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
  * Performs decryption operation.
  *
  * <p>The input cipher text <code>in</code>, starting at
  * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
  * is decrypted.The result is stored in <code>out</code>, starting at
  * <code>outOff</code>.
  *
  * @param in the buffer with the input data to be decrypted
  * @param inOff the offset in <code>cipherOffset</code>
  * @param len the length of the input data
  * @param out the buffer for the result
  * @param outOff the offset in <code>plain</code>
  * @exception ProviderException if <code>len</code> is not
  * a multiple of the block size
  * @return the length of the decrypted data
  */
 int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
     ArrayUtil.blockSizeCheck(len, blockSize);
     ArrayUtil.nullAndBoundsCheck(in, inOff, len);
     ArrayUtil.nullAndBoundsCheck(out, outOff, len);
     return implECBDecrypt(in, inOff, len, out, outOff);
}