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