List of usage examples for org.bouncycastle.crypto.modes CCMBlockCipher CCMBlockCipher
public CCMBlockCipher(BlockCipher c)
From source file:COSE.EncryptCommon.java
private void AES_CCM_Decrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException, IllegalStateException, InvalidCipherTextException { CCMBlockCipher cipher = new CCMBlockCipher(new AESFastEngine()); KeyParameter ContentKey;/*from www. java2 s.c o m*/ int cbIV = 0; switch (alg) { case AES_CCM_16_64_128: case AES_CCM_16_64_256: case AES_CCM_16_128_128: case AES_CCM_16_128_256: cbIV = 15 - 2; break; case AES_CCM_64_64_128: case AES_CCM_64_64_256: case AES_CCM_64_128_256: case AES_CCM_64_128_128: cbIV = 15 - 8; break; } // The requirements from JWA CBORObject cn = FindAttribute(HeaderKeys.IV); if (cn == null) throw new CoseException("Missing IV during decryption"); if (cn.getType() != CBORType.ByteString) throw new CoseException("IV is incorrectly formed"); if (cn.GetByteString().length != cbIV) throw new CoseException("IV size is incorrect"); byte[] IV = cn.GetByteString(); if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Missing IV during decryption"); ContentKey = new KeyParameter(rgbKey); // Build the object to be hashed AEADParameters parameters = new AEADParameters(ContentKey, alg.getTagSize(), IV, getAADBytes()); cipher.init(false, parameters); byte[] C = new byte[cipher.getOutputSize(rgbEncrypt.length)]; int len = cipher.processBytes(rgbEncrypt, 0, rgbEncrypt.length, C, 0); len += cipher.doFinal(C, len); rgbContent = C; }
From source file:COSE.EncryptCommon.java
private byte[] AES_CCM_Encrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException, IllegalStateException, InvalidCipherTextException { CCMBlockCipher cipher = new CCMBlockCipher(new AESFastEngine()); KeyParameter ContentKey;//from www. ja v a2 s. c o m int cbIV; switch (alg) { case AES_CCM_16_64_128: case AES_CCM_16_64_256: case AES_CCM_16_128_128: case AES_CCM_16_128_256: cbIV = 15 - 2; break; case AES_CCM_64_64_128: case AES_CCM_64_64_256: case AES_CCM_64_128_256: case AES_CCM_64_128_128: cbIV = 15 - 8; break; default: throw new CoseException("Unsupported algorithm: " + alg); } // The requirements from JWA byte[] IV = new byte[cbIV]; CBORObject cbor = FindAttribute(HeaderKeys.IV); if (cbor != null) { if (cbor.getType() != CBORType.ByteString) throw new CoseException("IV is incorreclty formed."); if (cbor.GetByteString().length > cbIV) throw new CoseException("IV is too long."); IV = cbor.GetByteString(); } else { random.nextBytes(IV); AddUnprotected(HeaderKeys.IV, CBORObject.FromObject(IV)); } if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Key Size is incorrect"); ContentKey = new KeyParameter(rgbKey); // Build the object to be hashed AEADParameters parameters = new AEADParameters(ContentKey, alg.getTagSize(), IV, getAADBytes()); cipher.init(true, parameters); byte[] C = new byte[cipher.getOutputSize(rgbContent.length)]; int len = cipher.processBytes(rgbContent, 0, rgbContent.length, C, 0); len += cipher.doFinal(C, len); return C; }
From source file:org.cryptacular.spec.AEADBlockCipherSpec.java
License:Open Source License
/** * Creates a new AEAD block cipher from the specification in this instance. * * @return New AEAD block cipher instance. *//*from ww w .j a v a2s. c om*/ @Override public AEADBlockCipher newInstance() { final BlockCipher blockCipher = new BlockCipherSpec(algorithm).newInstance(); AEADBlockCipher aeadBlockCipher; switch (mode) { case "GCM": aeadBlockCipher = new GCMBlockCipher(blockCipher); break; case "CCM": aeadBlockCipher = new CCMBlockCipher(blockCipher); break; case "OCB": aeadBlockCipher = new OCBBlockCipher(blockCipher, new BlockCipherSpec(algorithm).newInstance()); break; case "EAX": aeadBlockCipher = new EAXBlockCipher(blockCipher); break; default: throw new IllegalStateException("Unsupported mode " + mode); } return aeadBlockCipher; }
From source file:org.cryptacular.util.CipherUtilTest.java
License:Open Source License
@DataProvider(name = "aead-block-cipher") public Object[][] getAeadBlockCipherData() { return new Object[][] { new Object[] { // Plaintext is NOT multiple of block size "I never picked cotton like my mother did", new GCMBlockCipher(new AESEngine()), }, new Object[] { // Plaintext is multiple of block size "Cogito ergo sum.", new GCMBlockCipher(new AESEngine()), }, // CCM new Object[] { "Thousands of candles can be lit from a single candle and the life " + "of the candle will not be shortened.", new CCMBlockCipher(new TwofishEngine()), }, // OCB new Object[] { "I slept and dreamt life was joy. I awoke and saw that life was " + "service. I acted and behold: service was joy.", new OCBBlockCipher(new AESEngine(), new AESEngine()), }, }; }