Source Code Cross Referenced for Des3DkCrypto.java in  » 6.0-JDK-Modules-sun » security » sun » security » krb5 » internal » crypto » dk » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules sun » security » sun.security.krb5.internal.crypto.dk 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package sun.security.krb5.internal.crypto.dk;
027:
028:        import javax.crypto.Cipher;
029:        import javax.crypto.Mac;
030:        import javax.crypto.SecretKeyFactory;
031:        import javax.crypto.SecretKey;
032:        import javax.crypto.spec.SecretKeySpec;
033:        import javax.crypto.spec.DESKeySpec;
034:        import javax.crypto.spec.DESedeKeySpec;
035:        import javax.crypto.spec.IvParameterSpec;
036:        import java.security.spec.KeySpec;
037:        import java.security.GeneralSecurityException;
038:        import java.security.InvalidKeyException;
039:        import java.util.Arrays;
040:
041:        public class Des3DkCrypto extends DkCrypto {
042:
043:            private static final byte[] ZERO_IV = new byte[] { 0, 0, 0, 0, 0,
044:                    0, 0, 0 };
045:
046:            public Des3DkCrypto() {
047:            }
048:
049:            protected int getKeySeedLength() {
050:                return 168; // bits; 3DES key material has 21 bytes
051:            }
052:
053:            public byte[] stringToKey(char[] salt)
054:                    throws GeneralSecurityException {
055:                byte[] saltUtf8 = null;
056:                try {
057:                    saltUtf8 = charToUtf8(salt);
058:                    return stringToKey(saltUtf8, null);
059:                } finally {
060:                    if (saltUtf8 != null) {
061:                        Arrays.fill(saltUtf8, (byte) 0);
062:                    }
063:                    // Caller responsible for clearing its own salt
064:                }
065:            }
066:
067:            private byte[] stringToKey(byte[] secretAndSalt, byte[] opaque)
068:                    throws GeneralSecurityException {
069:
070:                if (opaque != null && opaque.length > 0) {
071:                    throw new RuntimeException(
072:                            "Invalid parameter to stringToKey");
073:                }
074:
075:                byte[] tmpKey = randomToKey(nfold(secretAndSalt,
076:                        getKeySeedLength()));
077:                return dk(tmpKey, KERBEROS_CONSTANT);
078:            }
079:
080:            public byte[] parityFix(byte[] value)
081:                    throws GeneralSecurityException {
082:                // fix key parity
083:                setParityBit(value);
084:                return value;
085:            }
086:
087:            /*
088:             * From RFC 3961.
089:             *
090:             * The 168 bits of random key data are converted to a protocol key value
091:             * as follows.  First, the 168 bits are divided into three groups of 56
092:             * bits, which are expanded individually into 64 bits as in des3Expand().
093:             * Result is a 24 byte (192-bit) key.
094:             */
095:            protected byte[] randomToKey(byte[] in) {
096:                if (in.length != 21) {
097:                    throw new IllegalArgumentException("input must be 168 bits");
098:                }
099:
100:                byte[] one = keyCorrection(des3Expand(in, 0, 7));
101:                byte[] two = keyCorrection(des3Expand(in, 7, 14));
102:                byte[] three = keyCorrection(des3Expand(in, 14, 21));
103:
104:                byte[] key = new byte[24];
105:                System.arraycopy(one, 0, key, 0, 8);
106:                System.arraycopy(two, 0, key, 8, 8);
107:                System.arraycopy(three, 0, key, 16, 8);
108:
109:                return key;
110:            }
111:
112:            private static byte[] keyCorrection(byte[] key) {
113:                // check for weak key
114:                try {
115:                    if (DESKeySpec.isWeak(key, 0)) {
116:                        key[7] = (byte) (key[7] ^ 0xF0);
117:                    }
118:                } catch (InvalidKeyException ex) {
119:                    // swallow, since it should never happen
120:                }
121:                return key;
122:            }
123:
124:            /**
125:             * From RFC 3961.
126:             *
127:             * Expands a 7-byte array into an 8-byte array that contains parity bits.
128:             * The 56 bits are expanded into 64 bits as follows:
129:             *   1  2  3  4  5  6  7  p
130:             *   9 10 11 12 13 14 15  p
131:             *   17 18 19 20 21 22 23  p
132:             *   25 26 27 28 29 30 31  p
133:             *   33 34 35 36 37 38 39  p
134:             *   41 42 43 44 45 46 47  p
135:             *   49 50 51 52 53 54 55  p
136:             *   56 48 40 32 24 16  8  p
137:             *
138:             * (PI,P2,...,P8) are reserved for parity bits computed on the preceding 
139:             * seven independent bits and set so that the parity of the octet is odd, 
140:             * i.e., there is an odd number of "1" bits in the octet.
141:             *
142:             * @param start index of starting byte (inclusive)
143:             * @param end index of ending byte (exclusive)
144:             */
145:            private static byte[] des3Expand(byte[] input, int start, int end) {
146:                if ((end - start) != 7)
147:                    throw new IllegalArgumentException(
148:                            "Invalid length of DES Key Value:" + start + ","
149:                                    + end);
150:
151:                byte[] result = new byte[8];
152:                byte last = 0;
153:                System.arraycopy(input, start, result, 0, 7);
154:                byte posn = 0;
155:
156:                // Fill in last row
157:                for (int i = start; i < end; i++) {
158:                    byte bit = (byte) (input[i] & 0x01);
159:                    if (debug) {
160:                        System.out.println(i + ": "
161:                                + Integer.toHexString(input[i]) + " bit= "
162:                                + Integer.toHexString(bit));
163:                    }
164:                    ++posn;
165:                    if (bit != 0) {
166:                        last |= (bit << posn);
167:                    }
168:                }
169:
170:                if (debug) {
171:                    System.out.println("last: " + Integer.toHexString(last));
172:                }
173:                result[7] = last;
174:                setParityBit(result);
175:                return result;
176:            }
177:
178:            /* Mask used to check for parity adjustment */
179:            private static final byte[] PARITY_BIT_MASK = { (byte) 0x80,
180:                    (byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,
181:                    (byte) 0x04, (byte) 0x02 };
182:
183:            /**
184:             * Sets the parity bit (0th bit) in each byte so that each byte
185:             * contains an odd number of 1's.
186:             */
187:            private static void setParityBit(byte[] key) {
188:                for (int i = 0; i < key.length; i++) {
189:                    int bitCount = 0;
190:                    for (int maskIndex = 0; maskIndex < PARITY_BIT_MASK.length; maskIndex++) {
191:                        if ((key[i] & PARITY_BIT_MASK[maskIndex]) == PARITY_BIT_MASK[maskIndex]) {
192:                            bitCount++;
193:                        }
194:                    }
195:                    if ((bitCount & 0x01) == 1) {
196:                        // Odd number of 1 bits in the top 7 bits. Set parity bit to 0
197:                        key[i] = (byte) (key[i] & (byte) 0xfe);
198:                    } else {
199:                        // Even number of 1 bits in the top 7 bits. Set parity bit to 1
200:                        key[i] = (byte) (key[i] | 1);
201:                    }
202:                }
203:            }
204:
205:            protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
206:                    throws GeneralSecurityException {
207:                // NoSuchAlgorithException
208:                SecretKeyFactory factory = SecretKeyFactory
209:                        .getInstance("desede");
210:
211:                // InvalidKeyException
212:                KeySpec spec = new DESedeKeySpec(key, 0);
213:
214:                // InvalidKeySpecException
215:                SecretKey secretKey = factory.generateSecret(spec);
216:
217:                // IV
218:                if (ivec == null) {
219:                    ivec = ZERO_IV;
220:                }
221:
222:                // NoSuchAlgorithmException, NoSuchPaddingException
223:                // NoSuchProviderException
224:                Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
225:                IvParameterSpec encIv = new IvParameterSpec(ivec, 0,
226:                        ivec.length);
227:
228:                // InvalidKeyException, InvalidAlgorithParameterException
229:                cipher.init(mode, secretKey, encIv);
230:
231:                return cipher;
232:            }
233:
234:            public int getChecksumLength() {
235:                return 20; // bytes
236:            }
237:
238:            protected byte[] getHmac(byte[] key, byte[] msg)
239:                    throws GeneralSecurityException {
240:
241:                SecretKey keyKi = new SecretKeySpec(key, "HmacSHA1");
242:                Mac m = Mac.getInstance("HmacSHA1");
243:                m.init(keyKi);
244:                return m.doFinal(msg);
245:            }
246:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.