1 package org.bouncycastle.jcajce.spec; 2 3 import javax.crypto.spec.IvParameterSpec; 4 5 import org.bouncycastle.util.Arrays; 6 7 /** 8 * ParameterSpec for AEAD modes which allows associated data to be added via an algorithm parameter spec.In normal 9 * circumstances you would only want to use this if you had to work with the pre-JDK1.7 Cipher class as associated 10 * data is ignored for the purposes of returning a Cipher's parameters. 11 */ 12 public class AEADParameterSpec 13 extends IvParameterSpec 14 { 15 private final byte[] associatedData; 16 private final int macSizeInBits; 17 18 /** 19 * Base constructor. 20 * 21 * @param nonce nonce/iv to be used 22 * @param macSizeInBits macSize in bits 23 */ AEADParameterSpec(byte[] nonce, int macSizeInBits)24 public AEADParameterSpec(byte[] nonce, int macSizeInBits) 25 { 26 this(nonce, macSizeInBits, null); 27 } 28 29 /** 30 * Base constructor with prepended associated data. 31 * 32 * @param nonce nonce/iv to be used 33 * @param macSizeInBits macSize in bits 34 * @param associatedData associated data to be prepended to the cipher stream. 35 */ AEADParameterSpec(byte[] nonce, int macSizeInBits, byte[] associatedData)36 public AEADParameterSpec(byte[] nonce, int macSizeInBits, byte[] associatedData) 37 { 38 super(nonce); 39 40 this.macSizeInBits = macSizeInBits; 41 this.associatedData = Arrays.clone(associatedData); 42 } 43 44 /** 45 * Return the size of the MAC associated with this parameter spec. 46 * 47 * @return the MAC size in bits. 48 */ getMacSizeInBits()49 public int getMacSizeInBits() 50 { 51 return macSizeInBits; 52 } 53 54 /** 55 * Return the associated data associated with this parameter spec. 56 * 57 * @return the associated data, null if there isn't any. 58 */ getAssociatedData()59 public byte[] getAssociatedData() 60 { 61 return Arrays.clone(associatedData); 62 } 63 64 /** 65 * Return the nonce (same as IV) associated with this parameter spec. 66 * 67 * @return the nonce/IV. 68 */ getNonce()69 public byte[] getNonce() 70 { 71 return getIV(); 72 } 73 } 74