Search in sources :

Example 56 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project robovm by robovm.

the class GCMParameterSpecTest method testGetIV_Subarray_Success.

public void testGetIV_Subarray_Success() throws Exception {
    GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV, 2, 4);
    assertEquals(Arrays.toString(Arrays.copyOfRange(TEST_IV, 2, 6)), Arrays.toString(spec.getIV()));
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 57 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project jdk8u_jdk by JetBrains.

the class CipherBox method applyExplicitNonce.

/*
     * Applies the explicit nonce/IV to this cipher. This method is used to
     * decrypt an SSL/TLS input record.
     *
     * The returned value is the SecurityParameters.record_iv_length in
     * RFC 4346/5246.  It is the size of explicit IV for CBC mode, and the
     * size of explicit nonce for AEAD mode.
     *
     * @param  authenticator the authenticator to get the additional
     *         authentication data
     * @param  contentType the content type of the input record
     * @param  bb the byte buffer to get the explicit nonce from
     *
     * @return the explicit nonce size of the cipher.
     */
int applyExplicitNonce(Authenticator authenticator, byte contentType, ByteBuffer bb) throws BadPaddingException {
    switch(cipherType) {
        case BLOCK_CIPHER:
            // sanity check length of the ciphertext
            int tagLen = (authenticator instanceof MAC) ? ((MAC) authenticator).MAClen() : 0;
            if (tagLen != 0) {
                if (!sanityCheck(tagLen, bb.remaining())) {
                    throw new BadPaddingException("ciphertext sanity check failed");
                }
            }
            // the SecurityParameters.block_size.
            if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
                return cipher.getBlockSize();
            }
            break;
        case AEAD_CIPHER:
            if (bb.remaining() < (recordIvSize + tagSize)) {
                throw new BadPaddingException("invalid AEAD cipher fragment");
            }
            // initialize the AEAD cipher for the unique IV
            byte[] iv = Arrays.copyOf(fixedIv, fixedIv.length + recordIvSize);
            bb.get(iv, fixedIv.length, recordIvSize);
            bb.position(bb.position() - recordIvSize);
            GCMParameterSpec spec = new GCMParameterSpec(tagSize * 8, iv);
            try {
                cipher.init(mode, key, spec, random);
            } catch (InvalidKeyException | InvalidAlgorithmParameterException ikae) {
                // unlikely to happen
                throw new RuntimeException("invalid key or spec in GCM mode", ikae);
            }
            // update the additional authentication data
            byte[] aad = authenticator.acquireAuthenticationBytes(contentType, bb.remaining() - recordIvSize - tagSize);
            cipher.updateAAD(aad);
            return recordIvSize;
    }
    return 0;
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 58 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project jdk8u_jdk by JetBrains.

the class GCMParameterSpecTest method newGCMParameterSpecFail.

private static void newGCMParameterSpecFail(int tLen, byte[] src, int offset, int len) {
    try {
        new GCMParameterSpec(tLen, src, offset, len);
        new Exception("Didn't Fail as Expected").printStackTrace();
        failed++;
    } catch (IllegalArgumentException e) {
    // swallow
    }
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 59 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project jdk8u_jdk by JetBrains.

the class GCMParameterSpecTest method newGCMParameterSpecPass.

private static void newGCMParameterSpecPass(int tLen, byte[] src, int offset, int len) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src, offset, len);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), Arrays.copyOfRange(src, offset, offset + len))) {
            System.out.println(offset + " " + len);
            System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 60 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project jdk8u_jdk by JetBrains.

the class GCMParameters method engineInit.

protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
    if (!(paramSpec instanceof GCMParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate parameter specification");
    }
    GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
    // need to convert from bits to bytes for ASN.1 encoding
    this.tLen = gps.getTLen() / 8;
    this.iv = gps.getIV();
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)101 Cipher (javax.crypto.Cipher)71 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)31 InvalidKeyException (java.security.InvalidKeyException)30 BadPaddingException (javax.crypto.BadPaddingException)29 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)29 SecretKey (javax.crypto.SecretKey)21 GeneralSecurityException (java.security.GeneralSecurityException)12 AEADBadTagException (javax.crypto.AEADBadTagException)12 Key (java.security.Key)11 ByteBuffer (java.nio.ByteBuffer)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ExcludedTest (com.google.security.wycheproof.WycheproofRunner.ExcludedTest)5 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)5 FileNotFoundException (java.io.FileNotFoundException)5