Source Code Cross Referenced for TlsBlockCipherCipherSuite.java in  » Security » Bouncy-Castle » org » bouncycastle » crypto » tls » 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 » Security » Bouncy Castle » org.bouncycastle.crypto.tls 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.crypto.tls;
002:
003:        import org.bouncycastle.crypto.BlockCipher;
004:        import org.bouncycastle.crypto.Digest;
005:        import org.bouncycastle.crypto.params.KeyParameter;
006:        import org.bouncycastle.crypto.params.ParametersWithIV;
007:
008:        import java.io.IOException;
009:
010:        /**
011:         * A generic TLS 1.0 block cipher suite. This can be used for AES or 3DES for
012:         * example.
013:         */
014:        public class TlsBlockCipherCipherSuite extends TlsCipherSuite {
015:
016:            private BlockCipher encryptCipher;
017:
018:            private BlockCipher decryptCipher;
019:
020:            private Digest writeDigest;
021:
022:            private Digest readDigest;
023:
024:            private int cipherKeySize;
025:
026:            private short keyExchange;
027:
028:            private TlsMac writeMac;
029:
030:            private TlsMac readMac;
031:
032:            protected TlsBlockCipherCipherSuite(BlockCipher encrypt,
033:                    BlockCipher decrypt, Digest writeDigest, Digest readDigest,
034:                    int cipherKeySize, short keyExchange) {
035:                this .encryptCipher = encrypt;
036:                this .decryptCipher = decrypt;
037:                this .writeDigest = writeDigest;
038:                this .readDigest = readDigest;
039:                this .cipherKeySize = cipherKeySize;
040:                this .keyExchange = keyExchange;
041:            }
042:
043:            protected void init(byte[] ms, byte[] cr, byte[] sr) {
044:                int prfSize = (2 * cipherKeySize)
045:                        + (2 * writeDigest.getDigestSize())
046:                        + (2 * encryptCipher.getBlockSize());
047:                byte[] key_block = new byte[prfSize];
048:                byte[] random = new byte[cr.length + sr.length];
049:                System.arraycopy(cr, 0, random, sr.length, cr.length);
050:                System.arraycopy(sr, 0, random, 0, sr.length);
051:                TlsUtils.PRF(ms, TlsUtils.toByteArray("key expansion"), random,
052:                        key_block);
053:
054:                int offset = 0;
055:
056:                // Init MACs
057:                writeMac = new TlsMac(writeDigest, key_block, offset,
058:                        writeDigest.getDigestSize());
059:                offset += writeDigest.getDigestSize();
060:                readMac = new TlsMac(readDigest, key_block, offset, readDigest
061:                        .getDigestSize());
062:                offset += readDigest.getDigestSize();
063:
064:                // Init Ciphers
065:                this .initCipher(true, encryptCipher, key_block, cipherKeySize,
066:                        offset, offset + (cipherKeySize * 2));
067:                offset += cipherKeySize;
068:                this .initCipher(false, decryptCipher, key_block, cipherKeySize,
069:                        offset, offset + cipherKeySize
070:                                + decryptCipher.getBlockSize());
071:            }
072:
073:            private void initCipher(boolean forEncryption, BlockCipher cipher,
074:                    byte[] key_block, int key_size, int key_offset,
075:                    int iv_offset) {
076:                KeyParameter key_parameter = new KeyParameter(key_block,
077:                        key_offset, key_size);
078:                ParametersWithIV parameters_with_iv = new ParametersWithIV(
079:                        key_parameter, key_block, iv_offset, cipher
080:                                .getBlockSize());
081:                cipher.init(forEncryption, parameters_with_iv);
082:            }
083:
084:            protected byte[] encodePlaintext(short type, byte[] plaintext,
085:                    int offset, int len) {
086:                int blocksize = encryptCipher.getBlockSize();
087:                int paddingsize = blocksize
088:                        - ((len + writeMac.getSize() + 1) % blocksize);
089:                int totalsize = len + writeMac.getSize() + paddingsize + 1;
090:                byte[] outbuf = new byte[totalsize];
091:                System.arraycopy(plaintext, offset, outbuf, 0, len);
092:                byte[] mac = writeMac
093:                        .calculateMac(type, plaintext, offset, len);
094:                System.arraycopy(mac, 0, outbuf, len, mac.length);
095:                int paddoffset = len + mac.length;
096:                for (int i = 0; i <= paddingsize; i++) {
097:                    outbuf[i + paddoffset] = (byte) paddingsize;
098:                }
099:                for (int i = 0; i < totalsize; i += blocksize) {
100:                    encryptCipher.processBlock(outbuf, i, outbuf, i);
101:                }
102:                return outbuf;
103:
104:            }
105:
106:            protected byte[] decodeCiphertext(short type, byte[] ciphertext,
107:                    int offset, int len, TlsProtocolHandler handler)
108:                    throws IOException {
109:                int blocksize = decryptCipher.getBlockSize();
110:                boolean decrypterror = false;
111:
112:                /*
113:                 * Decrypt all the ciphertext using the blockcipher
114:                 */
115:                for (int i = 0; i < len; i += blocksize) {
116:                    decryptCipher.processBlock(ciphertext, i + offset,
117:                            ciphertext, i + offset);
118:                }
119:
120:                /*
121:                 * Check if padding is correct
122:                 */
123:                int paddingsize = ciphertext[offset + len - 1];
124:                if (offset + len - 1 - paddingsize < 0) {
125:                    /*
126:                     * This would lead to an negativ array index, so this padding
127:                     * must be incorrect!
128:                     */
129:                    decrypterror = true;
130:                    paddingsize = 0;
131:                } else {
132:                    /*
133:                     * Now, check all the padding-bytes.
134:                     */
135:                    for (int i = 0; i <= paddingsize; i++) {
136:                        if (ciphertext[offset + len - 1 - i] != paddingsize) {
137:                            /* Wrong padding */
138:                            decrypterror = true;
139:                        }
140:                    }
141:                }
142:
143:                /*
144:                 * We now don't care if padding verification has failed or not,
145:                 * we will calculate the mac to give an attacker no kind of timing
146:                 * profile he can use to find out if mac verification failed or
147:                 * padding verification failed.
148:                 */
149:                int plaintextlength = len - readMac.getSize() - paddingsize - 1;
150:                byte[] calculatedMac = readMac.calculateMac(type, ciphertext,
151:                        offset, plaintextlength);
152:
153:                /*
154:                 * Check all bytes in the mac.
155:                 */
156:                for (int i = 0; i < calculatedMac.length; i++) {
157:                    if (ciphertext[offset + plaintextlength + i] != calculatedMac[i]) {
158:                        decrypterror = true;
159:                    }
160:                }
161:
162:                /*
163:                 * Now, it is save to fail.
164:                 */
165:                if (decrypterror) {
166:                    handler.failWithError(TlsProtocolHandler.AL_fatal,
167:                            TlsProtocolHandler.AP_bad_record_mac);
168:                }
169:                byte[] plaintext = new byte[plaintextlength];
170:                System.arraycopy(ciphertext, offset, plaintext, 0,
171:                        plaintextlength);
172:                return plaintext;
173:
174:            }
175:
176:            protected short getKeyExchangeAlgorithm() {
177:                return this.keyExchange;
178:            }
179:
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.