Home | History | Annotate | Download | only in util
      1 package org.bouncycastle.jcajce.provider.symmetric.util;
      2 
      3 import java.io.IOException;
      4 import java.security.spec.AlgorithmParameterSpec;
      5 import java.security.spec.InvalidParameterSpecException;
      6 
      7 import javax.crypto.spec.IvParameterSpec;
      8 
      9 import org.bouncycastle.asn1.ASN1OctetString;
     10 import org.bouncycastle.asn1.ASN1Primitive;
     11 import org.bouncycastle.asn1.DEROctetString;
     12 import org.bouncycastle.util.Arrays;
     13 
     14 public class IvAlgorithmParameters
     15     extends BaseAlgorithmParameters
     16 {
     17     private byte[] iv;
     18 
     19     protected byte[] engineGetEncoded()
     20         throws IOException
     21     {
     22         return engineGetEncoded("ASN.1");
     23     }
     24 
     25     protected byte[] engineGetEncoded(
     26         String format)
     27         throws IOException
     28     {
     29         if (isASN1FormatString(format))
     30         {
     31             return new DEROctetString(engineGetEncoded("RAW")).getEncoded();
     32         }
     33 
     34         if (format.equals("RAW"))
     35         {
     36             return Arrays.clone(iv);
     37         }
     38 
     39         return null;
     40     }
     41 
     42     protected AlgorithmParameterSpec localEngineGetParameterSpec(
     43         Class paramSpec)
     44         throws InvalidParameterSpecException
     45     {
     46         if (paramSpec == IvParameterSpec.class || paramSpec == AlgorithmParameterSpec.class)
     47         {
     48             return new IvParameterSpec(iv);
     49         }
     50 
     51         throw new InvalidParameterSpecException("unknown parameter spec passed to IV parameters object.");
     52     }
     53 
     54     protected void engineInit(
     55         AlgorithmParameterSpec paramSpec)
     56         throws InvalidParameterSpecException
     57     {
     58         if (!(paramSpec instanceof IvParameterSpec))
     59         {
     60             throw new InvalidParameterSpecException("IvParameterSpec required to initialise a IV parameters algorithm parameters object");
     61         }
     62 
     63         this.iv = ((IvParameterSpec)paramSpec).getIV();
     64     }
     65 
     66     protected void engineInit(
     67         byte[] params)
     68         throws IOException
     69     {
     70         //
     71         // check that we don't have a DER encoded octet string
     72         //
     73         if ((params.length % 8) != 0
     74             && params[0] == 0x04 && params[1] == params.length - 2)
     75         {
     76             ASN1OctetString oct = (ASN1OctetString)ASN1Primitive.fromByteArray(params);
     77 
     78             params = oct.getOctets();
     79         }
     80 
     81         this.iv = Arrays.clone(params);
     82     }
     83 
     84     protected void engineInit(
     85         byte[] params,
     86         String format)
     87         throws IOException
     88     {
     89         if (isASN1FormatString(format))
     90         {
     91             try
     92             {
     93                 ASN1OctetString oct = (ASN1OctetString)ASN1Primitive.fromByteArray(params);
     94 
     95                 engineInit(oct.getOctets());
     96             }
     97             catch (Exception e)
     98             {
     99                 throw new IOException("Exception decoding: " + e);
    100             }
    101 
    102             return;
    103         }
    104 
    105         if (format.equals("RAW"))
    106         {
    107             engineInit(params);
    108             return;
    109         }
    110 
    111         throw new IOException("Unknown parameters format in IV parameters object");
    112     }
    113 
    114     protected String engineToString()
    115     {
    116         return "IV Parameters";
    117     }
    118 }
    119