Showing the changes to java/pkg/com/sun/crypto/provider/BlockCipherParamsCore.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,127 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2002, 2007, 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.util.*; | |
| 29 | +import java.io.*; | |
| 30 | +import sun.security.util.*; | |
| 31 | +import sun.misc.HexDumpEncoder; | |
| 32 | +import java.security.spec.AlgorithmParameterSpec; | |
| 33 | +import java.security.spec.InvalidParameterSpecException; | |
| 34 | +import javax.crypto.spec.IvParameterSpec; | |
| 35 | + | |
| 36 | +/** | |
| 37 | + * This class implements the parameter (IV) used with Block Ciphers | |
| 38 | + * in feedback-mode. IV is defined in the standards as follows: | |
| 39 | + * | |
| 40 | + * <pre> | |
| 41 | + * IV ::= OCTET STRING -- length depends on the block size of the | |
| 42 | + * block ciphers | |
| 43 | + * </pre> | |
| 44 | + * | |
| 45 | + * @author Valerie Peng | |
| 46 | + * | |
| 47 | + */ | |
| 48 | +final class BlockCipherParamsCore { | |
| 49 | + private int block_size = 0; | |
| 50 | + private byte[] iv = null; | |
| 51 | + | |
| 52 | + BlockCipherParamsCore(int blksize) { | |
| 53 | + block_size = blksize; | |
| 54 | + } | |
| 55 | + | |
| 56 | + void init(AlgorithmParameterSpec paramSpec) | |
| 57 | + throws InvalidParameterSpecException { | |
| 58 | + if (!(paramSpec instanceof IvParameterSpec)) { | |
| 59 | + throw new InvalidParameterSpecException | |
| 60 | + ("Inappropriate parameter specification"); | |
| 61 | + } | |
| 62 | + byte[] tmpIv = ((IvParameterSpec)paramSpec).getIV(); | |
| 63 | + if (tmpIv.length != block_size) { | |
| 64 | + throw new InvalidParameterSpecException("IV not " + | |
| 65 | + block_size + " bytes long"); | |
| 66 | + } | |
| 67 | + iv = (byte[]) tmpIv.clone(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + void init(byte[] encoded) throws IOException { | |
| 71 | + DerInputStream der = new DerInputStream(encoded); | |
| 72 | + | |
| 73 | + byte[] tmpIv = der.getOctetString(); | |
| 74 | + if (der.available() != 0) { | |
| 75 | + throw new IOException("IV parsing error: extra data"); | |
| 76 | + } | |
| 77 | + if (tmpIv.length != block_size) { | |
| 78 | + throw new IOException("IV not " + block_size + | |
| 79 | + " bytes long"); | |
| 80 | + } | |
| 81 | + iv = tmpIv; | |
| 82 | + } | |
| 83 | + | |
| 84 | + void init(byte[] encoded, String decodingMethod) | |
| 85 | + throws IOException { | |
| 86 | + if ((decodingMethod != null) && | |
| 87 | + (!decodingMethod.equalsIgnoreCase("ASN.1"))) { | |
| 88 | + throw new IllegalArgumentException("Only support ASN.1 format"); | |
| 89 | + } | |
| 90 | + init(encoded); | |
| 91 | + } | |
| 92 | + | |
| 93 | + AlgorithmParameterSpec getParameterSpec(Class paramSpec) | |
| 94 | + throws InvalidParameterSpecException | |
| 95 | + { | |
| 96 | + if (IvParameterSpec.class.isAssignableFrom(paramSpec)) { | |
| 97 | + return new IvParameterSpec(this.iv); | |
| 98 | + } else { | |
| 99 | + throw new InvalidParameterSpecException | |
| 100 | + ("Inappropriate parameter specification"); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + byte[] getEncoded() throws IOException { | |
| 105 | + DerOutputStream out = new DerOutputStream(); | |
| 106 | + out.putOctetString(this.iv); | |
| 107 | + return out.toByteArray(); | |
| 108 | + } | |
| 109 | + | |
| 110 | + byte[] getEncoded(String encodingMethod) | |
| 111 | + throws IOException { | |
| 112 | + return getEncoded(); | |
| 113 | + } | |
| 114 | + | |
| 115 | + /* | |
| 116 | + * Returns a formatted string describing the parameters. | |
| 117 | + */ | |
| 118 | + public String toString() { | |
| 119 | + String LINE_SEP = System.getProperty("line.separator"); | |
| 120 | + | |
| 121 | + String ivString = LINE_SEP + " iv:" + LINE_SEP + "["; | |
| 122 | + HexDumpEncoder encoder = new HexDumpEncoder(); | |
| 123 | + ivString += encoder.encodeBuffer(this.iv); | |
| 124 | + ivString += "]" + LINE_SEP; | |
| 125 | + return ivString; | |
| 126 | + } | |
| 127 | +} | |