Java Code Examples for javax.crypto.spec.GCMParameterSpec#getTLen()

The following examples show how to use javax.crypto.spec.GCMParameterSpec#getTLen() . 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: GCMParameterSpecTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
    // init a cipher
    Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
    cipher.updateAAD(AAD);
    byte[] cipherText = cipher.doFinal(data);

    // check IVs
    if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
        System.out.println("IV in parameters is incorrect");
        return null;
    }
    if (spec.getTLen() != (cipherText.length - data.length) * 8) {
        System.out.println("Tag length is incorrect");
        return null;
    }
    return cipherText;
}
 
Example 2
Source File: GCMParameterSpecTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
    // init a cipher
    Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
    cipher.updateAAD(AAD);
    byte[] cipherText = cipher.doFinal(data);

    // check IVs
    if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
        System.out.println("IV in parameters is incorrect");
        return null;
    }
    if (spec.getTLen() != (cipherText.length - data.length) * 8) {
        System.out.println("Tag length is incorrect");
        return null;
    }
    return cipherText;
}
 
Example 3
Source File: GCMParameterSpecTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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++;
    }
}
 
Example 4
Source File: GCMParameterSpecTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
    // init a cipher
    Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
    cipher.updateAAD(AAD);
    byte[] cipherText = cipher.doFinal(data);

    // check IVs
    if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
        System.out.println("IV in parameters is incorrect");
        return null;
    }
    if (spec.getTLen() != (cipherText.length - data.length) * 8) {
        System.out.println("Tag length is incorrect");
        return null;
    }
    return cipherText;
}
 
Example 5
Source File: GCMParameterSpecTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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++;
    }
}
 
Example 6
Source File: GCMParameterSpecTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
    // init a cipher
    Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
    cipher.updateAAD(AAD);
    byte[] cipherText = cipher.doFinal(data);

    // check IVs
    if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
        System.out.println("IV in parameters is incorrect");
        return null;
    }
    if (spec.getTLen() != (cipherText.length - data.length) * 8) {
        System.out.println("Tag length is incorrect");
        return null;
    }
    return cipherText;
}
 
Example 7
Source File: GCMParameterSpecTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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++;
    }
}
 
Example 8
Source File: GCMParameterSpecTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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++;
    }
}
 
Example 9
Source File: GCMParameterSpecTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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++;
    }
}
 
Example 10
Source File: GCMParameterSpecTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void newGCMParameterSpecPass(
        int tLen, byte[] src) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), src)) {
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
 
Example 11
Source File: GCMParameterSpecTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void newGCMParameterSpecPass(
        int tLen, byte[] src) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), src)) {
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
 
Example 12
Source File: GCMParameterSpecTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void newGCMParameterSpecPass(
        int tLen, byte[] src) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), src)) {
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
 
Example 13
Source File: GCMParameters.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: GCMParameterSpecTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private boolean doTest() throws Exception {
    GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
    GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
            IVlength);
    byte[] cipherText1 = getCipherTextBySpec(spec1);
    if (cipherText1 == null) {
        return false;
    }
    byte[] cipherText2 = getCipherTextBySpec(spec2);
    if (cipherText2 == null) {
        return false;
    }
    if (!Arrays.equals(cipherText1, cipherText2)) {
        System.out.println("Cipher texts are different");
        return false;
    }
    if (spec1.getTLen() != spec2.getTLen()) {
        System.out.println("Tag lengths are not equal");
        return false;
    }
    byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
    if (recoveredText1 == null) {
        return false;
    }
    byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
    if (recoveredText2 == null) {
        return false;
    }
    if (!Arrays.equals(recoveredText1, recoveredText2)) {
        System.out.println("Recovered texts are different");
        return false;
    }
    if (!Arrays.equals(recoveredText1, data)) {
        System.out.println("Recovered and original texts are not equal");
        return false;
    }

    return true;
}
 
Example 15
Source File: GCMParameterSpecTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void newGCMParameterSpecPass(
        int tLen, byte[] src) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), src)) {
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
 
Example 16
Source File: GCMParameters.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 17
Source File: GCMParameterSpecTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private boolean doTest() throws Exception {
    GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
    GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
            IVlength);
    byte[] cipherText1 = getCipherTextBySpec(spec1);
    if (cipherText1 == null) {
        return false;
    }
    byte[] cipherText2 = getCipherTextBySpec(spec2);
    if (cipherText2 == null) {
        return false;
    }
    if (!Arrays.equals(cipherText1, cipherText2)) {
        System.out.println("Cipher texts are different");
        return false;
    }
    if (spec1.getTLen() != spec2.getTLen()) {
        System.out.println("Tag lengths are not equal");
        return false;
    }
    byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
    if (recoveredText1 == null) {
        return false;
    }
    byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
    if (recoveredText2 == null) {
        return false;
    }
    if (!Arrays.equals(recoveredText1, recoveredText2)) {
        System.out.println("Recovered texts are different");
        return false;
    }
    if (!Arrays.equals(recoveredText1, data)) {
        System.out.println("Recovered and original texts are not equal");
        return false;
    }

    return true;
}
 
Example 18
Source File: GCMParameterSpecTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void newGCMParameterSpecPass(
        int tLen, byte[] src) {
    try {
        GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
        if (gcmps.getTLen() != tLen) {
            throw new Exception("tLen's not equal");
        }
        if (!Arrays.equals(gcmps.getIV(), src)) {
            throw new Exception("IV's not equal");
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed++;
    }
}
 
Example 19
Source File: GCMParameters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 20
Source File: GCMParameterSpecTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean doTest() throws Exception {
    GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
    GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
            IVlength);
    byte[] cipherText1 = getCipherTextBySpec(spec1);
    if (cipherText1 == null) {
        return false;
    }
    byte[] cipherText2 = getCipherTextBySpec(spec2);
    if (cipherText2 == null) {
        return false;
    }
    if (!Arrays.equals(cipherText1, cipherText2)) {
        System.out.println("Cipher texts are different");
        return false;
    }
    if (spec1.getTLen() != spec2.getTLen()) {
        System.out.println("Tag lengths are not equal");
        return false;
    }
    byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
    if (recoveredText1 == null) {
        return false;
    }
    byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
    if (recoveredText2 == null) {
        return false;
    }
    if (!Arrays.equals(recoveredText1, recoveredText2)) {
        System.out.println("Recovered texts are different");
        return false;
    }
    if (!Arrays.equals(recoveredText1, data)) {
        System.out.println("Recovered and original texts are not equal");
        return false;
    }

    return true;
}