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