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()));
}
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;
}
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
}
}
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++;
}
}
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();
}
Aggregations