001: package org.bouncycastle.jce.provider.symmetric;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.misc.CAST5CBCParameters;
005: import org.bouncycastle.crypto.CipherKeyGenerator;
006: import org.bouncycastle.crypto.engines.CAST5Engine;
007: import org.bouncycastle.crypto.modes.CBCBlockCipher;
008: import org.bouncycastle.jce.provider.JCEBlockCipher;
009: import org.bouncycastle.jce.provider.JCEKeyGenerator;
010: import org.bouncycastle.jce.provider.JDKAlgorithmParameterGenerator;
011: import org.bouncycastle.jce.provider.JDKAlgorithmParameters;
012:
013: import javax.crypto.spec.IvParameterSpec;
014: import java.io.IOException;
015: import java.security.AlgorithmParameters;
016: import java.security.InvalidAlgorithmParameterException;
017: import java.security.SecureRandom;
018: import java.security.spec.AlgorithmParameterSpec;
019: import java.security.spec.InvalidParameterSpecException;
020:
021: public final class CAST5 {
022: private CAST5() {
023: }
024:
025: public static class ECB extends JCEBlockCipher {
026: public ECB() {
027: super (new CAST5Engine());
028: }
029: }
030:
031: public static class CBC extends JCEBlockCipher {
032: public CBC() {
033: super (new CBCBlockCipher(new CAST5Engine()));
034: }
035: }
036:
037: public static class KeyGen extends JCEKeyGenerator {
038: public KeyGen() {
039: super ("CAST5", 128, new CipherKeyGenerator());
040: }
041: }
042:
043: public static class AlgParamGen extends
044: JDKAlgorithmParameterGenerator {
045: protected void engineInit(AlgorithmParameterSpec genParamSpec,
046: SecureRandom random)
047: throws InvalidAlgorithmParameterException {
048: throw new InvalidAlgorithmParameterException(
049: "No supported AlgorithmParameterSpec for CAST5 parameter generation.");
050: }
051:
052: protected AlgorithmParameters engineGenerateParameters() {
053: byte[] iv = new byte[8];
054:
055: if (random == null) {
056: random = new SecureRandom();
057: }
058:
059: random.nextBytes(iv);
060:
061: AlgorithmParameters params;
062:
063: try {
064: params = AlgorithmParameters.getInstance("CAST5", "BC");
065: params.init(new IvParameterSpec(iv));
066: } catch (Exception e) {
067: throw new RuntimeException(e.getMessage());
068: }
069:
070: return params;
071: }
072: }
073:
074: public static class AlgParams extends JDKAlgorithmParameters {
075: private byte[] iv;
076: private int keyLength = 128;
077:
078: protected byte[] engineGetEncoded() {
079: byte[] tmp = new byte[iv.length];
080:
081: System.arraycopy(iv, 0, tmp, 0, iv.length);
082: return tmp;
083: }
084:
085: protected byte[] engineGetEncoded(String format)
086: throws IOException {
087: if (isASN1FormatString(format)) {
088: return new CAST5CBCParameters(engineGetEncoded(),
089: keyLength).getEncoded();
090: }
091:
092: if (format.equals("RAW")) {
093: return engineGetEncoded();
094: }
095:
096: return null;
097: }
098:
099: protected AlgorithmParameterSpec localEngineGetParameterSpec(
100: Class paramSpec) throws InvalidParameterSpecException {
101: if (paramSpec == IvParameterSpec.class) {
102: return new IvParameterSpec(iv);
103: }
104:
105: throw new InvalidParameterSpecException(
106: "unknown parameter spec passed to CAST5 parameters object.");
107: }
108:
109: protected void engineInit(AlgorithmParameterSpec paramSpec)
110: throws InvalidParameterSpecException {
111: if (paramSpec instanceof IvParameterSpec) {
112: this .iv = ((IvParameterSpec) paramSpec).getIV();
113: } else {
114: throw new InvalidParameterSpecException(
115: "IvParameterSpec required to initialise a CAST5 parameters algorithm parameters object");
116: }
117: }
118:
119: protected void engineInit(byte[] params) throws IOException {
120: this .iv = new byte[params.length];
121:
122: System.arraycopy(params, 0, iv, 0, iv.length);
123: }
124:
125: protected void engineInit(byte[] params, String format)
126: throws IOException {
127: if (isASN1FormatString(format)) {
128: ASN1InputStream aIn = new ASN1InputStream(params);
129: CAST5CBCParameters p = CAST5CBCParameters
130: .getInstance(aIn.readObject());
131:
132: keyLength = p.getKeyLength();
133:
134: iv = p.getIV();
135:
136: return;
137: }
138:
139: if (format.equals("RAW")) {
140: engineInit(params);
141: return;
142: }
143:
144: throw new IOException(
145: "Unknown parameters format in IV parameters object");
146: }
147:
148: protected String engineToString() {
149: return "CAST5 Parameters";
150: }
151: }
152: }
|