jdk/src/share/classes/sun/security/krb5/internal/crypto/dk/Des3DkCrypto.java
changeset 2917 fcd464c4e52c
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
2916:f8f76a2c1728 2917:fcd464c4e52c
     1 /*
     1 /*
     2  * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2004-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
   167         result[7] = last;
   167         result[7] = last;
   168         setParityBit(result);
   168         setParityBit(result);
   169         return result;
   169         return result;
   170     }
   170     }
   171 
   171 
   172     /* Mask used to check for parity adjustment */
       
   173     private static final byte[] PARITY_BIT_MASK = {
       
   174         (byte)0x80, (byte)0x40, (byte)0x20, (byte)0x10,
       
   175         (byte)0x08, (byte)0x04, (byte)0x02
       
   176     };
       
   177 
       
   178     /**
   172     /**
   179      * Sets the parity bit (0th bit) in each byte so that each byte
   173      * Sets the parity bit (0th bit) in each byte so that each byte
   180      * contains an odd number of 1's.
   174      * contains an odd number of 1's.
   181      */
   175      */
   182     private static void setParityBit(byte[] key) {
   176     private static void setParityBit(byte[] key) {
   183         for (int i = 0; i < key.length; i++) {
   177         for (int i = 0; i < key.length; i++) {
   184             int bitCount = 0;
   178             int b = key[i] & 0xfe;
   185             for (int maskIndex = 0;
   179             b |= (Integer.bitCount(b) & 1) ^ 1;
   186                  maskIndex < PARITY_BIT_MASK.length; maskIndex++) {
   180             key[i] = (byte) b;
   187                 if ((key[i] & PARITY_BIT_MASK[maskIndex])
       
   188                     == PARITY_BIT_MASK[maskIndex]) {
       
   189                     bitCount++;
       
   190                 }
       
   191             }
       
   192             if ((bitCount & 0x01) == 1) {
       
   193                 // Odd number of 1 bits in the top 7 bits. Set parity bit to 0
       
   194                 key[i] = (byte)(key[i] & (byte)0xfe);
       
   195             } else {
       
   196                 // Even number of 1 bits in the top 7 bits. Set parity bit to 1
       
   197                 key[i] = (byte)(key[i] | 1);
       
   198             }
       
   199         }
   181         }
   200     }
   182     }
   201 
   183 
   202     protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
   184     protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
   203         throws GeneralSecurityException {
   185         throws GeneralSecurityException {