blob: 8b687987a366c5fe6f105a4ef4424b4dc9f283dd [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package javax.crypto.spec;
19
20import java.security.InvalidKeyException;
21import java.security.spec.KeySpec;
22
23import org.apache.harmony.crypto.internal.nls.Messages;
24
25/**
26 * The key specification for a triple-DES (DES-EDE) key.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080027 */
28public class DESedeKeySpec implements KeySpec {
29
30 /**
31 * The length of a DES-EDE key in bytes.
32 */
33 public static final int DES_EDE_KEY_LEN = 24;
34
35 private final byte[] key;
36
37 /**
38 * Creates a new <code>DESedeKeySpec</code> instance from the first 24 (
39 * {@link #DES_EDE_KEY_LEN}) bytes of the specified key data.
Jesse Wilsonce9ec012009-08-31 15:37:14 -070040 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080041 * @param key
42 * the key data.
43 * @throws InvalidKeyException
44 * if the length of the key data is less than 24.
45 * @throws NullPointerException
46 * if the key data is null.
47 */
48 public DESedeKeySpec(byte[] key)
49 throws InvalidKeyException {
50 if (key == null) {
Elliott Hughesf33eae72010-05-13 12:36:25 -070051 throw new NullPointerException(Messages.getString("crypto.2F"));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080052 }
53 if (key.length < DES_EDE_KEY_LEN) {
54 throw new InvalidKeyException(
Elliott Hughesf33eae72010-05-13 12:36:25 -070055 Messages.getString("crypto.30"));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080056 }
57 this.key = new byte[DES_EDE_KEY_LEN];
58 System.arraycopy(key, 0, this.key, 0, DES_EDE_KEY_LEN);
59 }
60
61 /**
62 * Creates a new <code>DESedeKeySpec</code> instance from the first 24 (
63 * {@link #DES_EDE_KEY_LEN} ) bytes of the specified key data starting at
64 * <code>offset</code>.
Jesse Wilsonce9ec012009-08-31 15:37:14 -070065 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080066 * @param key
67 * the key data
68 * @param offset
69 * the offset to start at.
70 * @throws InvalidKeyException
71 * if the length of the key data starting at offset is less than
72 * 24.
73 * @throws NullPointerException
74 * if the key data is null.
75 */
76 public DESedeKeySpec(byte[] key, int offset)
77 throws InvalidKeyException {
78 if (key == null) {
Elliott Hughesf33eae72010-05-13 12:36:25 -070079 throw new NullPointerException(Messages.getString("crypto.2F"));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080080 }
81 if (key.length - offset < DES_EDE_KEY_LEN) {
82 throw new InvalidKeyException(
Elliott Hughesf33eae72010-05-13 12:36:25 -070083 Messages.getString("crypto.30"));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080084 }
85 this.key = new byte[DES_EDE_KEY_LEN];
86 System.arraycopy(key, offset, this.key, 0, DES_EDE_KEY_LEN);
87 }
88
89 /**
90 * Returns a copy of the key.
Jesse Wilsonce9ec012009-08-31 15:37:14 -070091 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080092 * @return a copy of the key.
93 */
94 public byte[] getKey() {
95 byte[] result = new byte [DES_EDE_KEY_LEN];
96 System.arraycopy(this.key, 0, result, 0, DES_EDE_KEY_LEN);
97 return result;
98 }
99
100 /**
101 * Returns whether the specified key data starting at <code>offset</code> is
102 * <i>parity-adjusted</i>.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700103 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800104 * @param key
105 * the key data.
106 * @param offset
107 * the offset to start checking at.
108 * @return {@code true} if the specified key data is parity-adjusted,
109 * {@code false} otherwise.
110 * @throws InvalidKeyException
111 * if the length of the key data starting at offset is less than
112 * 24.
113 */
114 public static boolean isParityAdjusted(byte[] key, int offset)
115 throws InvalidKeyException {
116 if (key.length - offset < DES_EDE_KEY_LEN) {
117 throw new InvalidKeyException(
Elliott Hughesf33eae72010-05-13 12:36:25 -0700118 Messages.getString("crypto.30"));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800119 }
120 for (int i=offset; i<DES_EDE_KEY_LEN+offset; i++) {
121 int b = key[i];
122 if ((((b & 1) + ((b & 2) >> 1) + ((b & 4) >> 2)
123 + ((b & 8) >> 3) + ((b & 16) >> 4) + ((b & 32) >> 5)
124 + ((b & 64) >> 6)) & 1) == ((b & 128) >> 7)) {
125 return false;
126 }
127 }
128 return true;
129 }
130}
131