The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package javax.crypto.spec; |
| 19 | |
| 20 | import java.security.InvalidKeyException; |
| 21 | import java.security.spec.KeySpec; |
| 22 | |
| 23 | import org.apache.harmony.crypto.internal.nls.Messages; |
| 24 | |
| 25 | /** |
| 26 | * The key specification for a triple-DES (DES-EDE) key. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | */ |
| 28 | public 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 Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 40 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | * @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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 51 | throw new NullPointerException(Messages.getString("crypto.2F")); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 52 | } |
| 53 | if (key.length < DES_EDE_KEY_LEN) { |
| 54 | throw new InvalidKeyException( |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 55 | Messages.getString("crypto.30")); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | } |
| 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 Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 65 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 66 | * @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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 79 | throw new NullPointerException(Messages.getString("crypto.2F")); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | } |
| 81 | if (key.length - offset < DES_EDE_KEY_LEN) { |
| 82 | throw new InvalidKeyException( |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 83 | Messages.getString("crypto.30")); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 84 | } |
| 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 Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 91 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 92 | * @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 Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 103 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 104 | * @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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 118 | Messages.getString("crypto.30")); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | } |
| 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 | |