List of usage examples for org.bouncycastle.crypto.modes CCMBlockCipher getOutputSize
public int getOutputSize(int len)
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 ww w .j a va2s.co 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 w ww . j a v a 2 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; }