Home » geronimo-2.2-source-release » org.apache.geronimo.crypto.crypto.params » [javadoc | source]

    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 org.apache.geronimo.crypto.crypto.params;
   19   
   20   public class DESParameters
   21       extends KeyParameter
   22   {
   23       public DESParameters(
   24           byte[]  key)
   25       {
   26           super(key);
   27   
   28           if (isWeakKey(key, 0))
   29           {
   30               throw new IllegalArgumentException("attempt to create weak DES key");
   31           }
   32       }
   33   
   34       /*
   35        * DES Key length in bytes.
   36        */
   37       static public final int DES_KEY_LENGTH = 8;
   38   
   39       /*
   40        * Table of weak and semi-weak keys taken from Schneier pp281
   41        */
   42       static private final int N_DES_WEAK_KEYS = 16;
   43   
   44       static private byte[] DES_weak_keys =
   45       {
   46           /* weak keys */
   47           (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
   48           (byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e,
   49           (byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1,
   50           (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe,
   51   
   52           /* semi-weak keys */
   53           (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe,
   54           (byte)0x1f,(byte)0xe0,(byte)0x1f,(byte)0xe0, (byte)0x0e,(byte)0xf1,(byte)0x0e,(byte)0xf1,
   55           (byte)0x01,(byte)0xe0,(byte)0x01,(byte)0xe0, (byte)0x01,(byte)0xf1,(byte)0x01,(byte)0xf1,
   56           (byte)0x1f,(byte)0xfe,(byte)0x1f,(byte)0xfe, (byte)0x0e,(byte)0xfe,(byte)0x0e,(byte)0xfe,
   57           (byte)0x01,(byte)0x1f,(byte)0x01,(byte)0x1f, (byte)0x01,(byte)0x0e,(byte)0x01,(byte)0x0e,
   58           (byte)0xe0,(byte)0xfe,(byte)0xe0,(byte)0xfe, (byte)0xf1,(byte)0xfe,(byte)0xf1,(byte)0xfe,
   59           (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01,
   60           (byte)0xe0,(byte)0x1f,(byte)0xe0,(byte)0x1f, (byte)0xf1,(byte)0x0e,(byte)0xf1,(byte)0x0e,
   61           (byte)0xe0,(byte)0x01,(byte)0xe0,(byte)0x01, (byte)0xf1,(byte)0x01,(byte)0xf1,(byte)0x01,
   62           (byte)0xfe,(byte)0x1f,(byte)0xfe,(byte)0x1f, (byte)0xfe,(byte)0x0e,(byte)0xfe,(byte)0x0e,
   63           (byte)0x1f,(byte)0x01,(byte)0x1f,(byte)0x01, (byte)0x0e,(byte)0x01,(byte)0x0e,(byte)0x01,
   64           (byte)0xfe,(byte)0xe0,(byte)0xfe,(byte)0xe0, (byte)0xfe,(byte)0xf1,(byte)0xfe,(byte)0xf1
   65       };
   66   
   67       /**
   68        * DES has 16 weak keys.  This method will check
   69        * if the given DES key material is weak or semi-weak.
   70        * Key material that is too short is regarded as weak.
   71        * <p>
   72        * See <a href="http://www.counterpane.com/applied.html">"Applied
   73        * Cryptography"</a> by Bruce Schneier for more information.
   74        *
   75        * @return true if the given DES key material is weak or semi-weak,
   76        *     false otherwise.
   77        */
   78       public static boolean isWeakKey(
   79           byte[] key,
   80           int offset)
   81       {
   82           if (key.length - offset < DES_KEY_LENGTH)
   83           {
   84               throw new IllegalArgumentException("key material too short.");
   85           }
   86   
   87           nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++)
   88           {
   89               for (int j = 0; j < DES_KEY_LENGTH; j++)
   90               {
   91                   if (key[j + offset] != DES_weak_keys[i * DES_KEY_LENGTH + j])
   92                   {
   93                       continue nextkey;
   94                   }
   95               }
   96   
   97               return true;
   98           }
   99           return false;
  100       }
  101   
  102       /**
  103        * DES Keys use the LSB as the odd parity bit.  This can
  104        * be used to check for corrupt keys.
  105        *
  106        * @param bytes the byte array to set the parity on.
  107        */
  108       public static void setOddParity(
  109           byte[] bytes)
  110       {
  111           for (int i = 0; i < bytes.length; i++)
  112           {
  113               int b = bytes[i];
  114               bytes[i] = (byte)((b & 0xfe) |
  115                               ((((b >> 1) ^
  116                               (b >> 2) ^
  117                               (b >> 3) ^
  118                               (b >> 4) ^
  119                               (b >> 5) ^
  120                               (b >> 6) ^
  121                               (b >> 7)) ^ 0x01) & 0x01));
  122           }
  123       }
  124   }

Home » geronimo-2.2-source-release » org.apache.geronimo.crypto.crypto.params » [javadoc | source]