1 /*
2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.crypto.provider;
27 
28 import java.security.*;
29 import java.security.spec.*;
30 
31 import javax.crypto.*;
32 import javax.crypto.spec.RC2ParameterSpec;
33 
34 /**
35  * JCE CipherSpi for the RC2(tm) algorithm as described in RFC 2268.
36  * The real code is in CipherCore and RC2Crypt.
37  *
38  * @since   1.5
39  * @author  Andreas Sterbenz
40  */
41 public final class RC2Cipher extends CipherSpi {
42 
43     // internal CipherCore & RC2Crypt objects which do the real work.
44     private final CipherCore core;
45     private final RC2Crypt embeddedCipher;
46 
RC2Cipher()47     public RC2Cipher() {
48         embeddedCipher = new RC2Crypt();
49         core = new CipherCore(embeddedCipher, 8);
50     }
51 
engineSetMode(String mode)52     protected void engineSetMode(String mode)
53             throws NoSuchAlgorithmException {
54         core.setMode(mode);
55     }
56 
engineSetPadding(String paddingScheme)57     protected void engineSetPadding(String paddingScheme)
58             throws NoSuchPaddingException {
59         core.setPadding(paddingScheme);
60     }
61 
engineGetBlockSize()62     protected int engineGetBlockSize() {
63         return 8;
64     }
65 
engineGetOutputSize(int inputLen)66     protected int engineGetOutputSize(int inputLen) {
67         return core.getOutputSize(inputLen);
68     }
69 
engineGetIV()70     protected byte[] engineGetIV() {
71         return core.getIV();
72     }
73 
engineGetParameters()74     protected AlgorithmParameters engineGetParameters() {
75         return core.getParameters("RC2");
76     }
77 
engineInit(int opmode, Key key, SecureRandom random)78     protected void engineInit(int opmode, Key key, SecureRandom random)
79             throws InvalidKeyException {
80         embeddedCipher.initEffectiveKeyBits(0);
81         core.init(opmode, key, random);
82     }
83 
engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)84     protected void engineInit(int opmode, Key key,
85             AlgorithmParameterSpec params, SecureRandom random)
86             throws InvalidKeyException, InvalidAlgorithmParameterException {
87         if (params != null && params instanceof RC2ParameterSpec) {
88             embeddedCipher.initEffectiveKeyBits
89                 (((RC2ParameterSpec)params).getEffectiveKeyBits());
90         } else {
91             embeddedCipher.initEffectiveKeyBits(0);
92         }
93         core.init(opmode, key, params, random);
94     }
95 
engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)96     protected void engineInit(int opmode, Key key,
97             AlgorithmParameters params, SecureRandom random)
98             throws InvalidKeyException, InvalidAlgorithmParameterException {
99         if (params != null && params.getAlgorithm().equals("RC2")) {
100             try {
101                 RC2ParameterSpec rc2Params =
102                         params.getParameterSpec(RC2ParameterSpec.class);
103                 engineInit(opmode, key, rc2Params, random);
104             } catch (InvalidParameterSpecException ipse) {
105                 throw new InvalidAlgorithmParameterException
106                             ("Wrong parameter type: RC2 expected");
107             }
108         } else {
109             embeddedCipher.initEffectiveKeyBits(0);
110             core.init(opmode, key, params, random);
111         }
112     }
113 
engineUpdate(byte[] in, int inOfs, int inLen)114     protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
115         return core.update(in, inOfs, inLen);
116     }
117 
engineUpdate(byte[] in, int inOfs, int inLen, byte[] out, int outOfs)118     protected int engineUpdate(byte[] in, int inOfs, int inLen,
119             byte[] out, int outOfs) throws ShortBufferException {
120         return core.update(in, inOfs, inLen, out, outOfs);
121     }
122 
engineDoFinal(byte[] in, int inOfs, int inLen)123     protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
124             throws IllegalBlockSizeException, BadPaddingException {
125         return core.doFinal(in, inOfs, inLen);
126     }
127 
engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out, int outOfs)128     protected int engineDoFinal(byte[] in, int inOfs, int inLen,
129             byte[] out, int outOfs) throws IllegalBlockSizeException,
130             ShortBufferException, BadPaddingException {
131         return core.doFinal(in, inOfs, inLen, out, outOfs);
132     }
133 
engineGetKeySize(Key key)134     protected int engineGetKeySize(Key key) throws InvalidKeyException {
135         byte[] keyBytes = CipherCore.getKeyBytes(key);
136         RC2Crypt.checkKey(key.getAlgorithm(), keyBytes.length);
137         return keyBytes.length << 3;
138     }
139 
engineWrap(Key key)140     protected byte[] engineWrap(Key key)
141             throws IllegalBlockSizeException, InvalidKeyException {
142         return core.wrap(key);
143     }
144 
engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)145     protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
146             int wrappedKeyType) throws InvalidKeyException,
147             NoSuchAlgorithmException {
148         return core.unwrap(wrappedKey, wrappedKeyAlgorithm, wrappedKeyType);
149     }
150 
151 }
152