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