Source Code Cross Referenced for Cipher.java in  » Apache-Harmony-Java-SE » javax-package » javax » crypto » 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 » Apache Harmony Java SE » javax package » javax.crypto 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Boris V. Kuznetsov
020:         * @version $Revision$
021:         */package javax.crypto;
022:
023:        import java.nio.ByteBuffer;
024:        import java.security.AlgorithmParameters;
025:        import java.security.InvalidAlgorithmParameterException;
026:        import java.security.InvalidKeyException;
027:        import java.security.InvalidParameterException;
028:        import java.security.Key;
029:        import java.security.NoSuchAlgorithmException;
030:        import java.security.NoSuchProviderException;
031:        import java.security.Provider;
032:        import java.security.SecureRandom;
033:        import java.security.Security;
034:        import java.security.cert.Certificate;
035:        import java.security.cert.X509Certificate;
036:        import java.security.spec.AlgorithmParameterSpec;
037:        import java.util.Set;
038:        import java.util.StringTokenizer;
039:
040:        import org.apache.harmony.crypto.internal.NullCipherSpi;
041:        import org.apache.harmony.crypto.internal.nls.Messages;
042:        import org.apache.harmony.security.fortress.Engine;
043:
044:        /**
045:         * @com.intel.drl.spec_ref
046:         * 
047:         */
048:        public class Cipher {
049:
050:            /**
051:             * @com.intel.drl.spec_ref
052:             *  
053:             */
054:            public static final int DECRYPT_MODE = 2;
055:
056:            /**
057:             * @com.intel.drl.spec_ref
058:             *  
059:             */
060:            public static final int ENCRYPT_MODE = 1;
061:
062:            /**
063:             * @com.intel.drl.spec_ref
064:             *  
065:             */
066:            public static final int PRIVATE_KEY = 2;
067:
068:            /**
069:             * @com.intel.drl.spec_ref
070:             *  
071:             */
072:            public static final int PUBLIC_KEY = 1;
073:
074:            /**
075:             * @com.intel.drl.spec_ref
076:             *  
077:             */
078:            public static final int SECRET_KEY = 3;
079:
080:            /**
081:             * @com.intel.drl.spec_ref
082:             *  
083:             */
084:            public static final int UNWRAP_MODE = 4;
085:
086:            /**
087:             * @com.intel.drl.spec_ref
088:             *  
089:             */
090:            public static final int WRAP_MODE = 3;
091:
092:            private int mode;
093:
094:            /**
095:             * The service name.
096:             */
097:            private static final String SERVICE = "Cipher"; //$NON-NLS-1$
098:
099:            /**
100:             * Used to access common engine functionality
101:             */
102:            private static final Engine engine = new Engine(SERVICE);
103:
104:            /**
105:             * The provider
106:             */
107:            private Provider provider;
108:
109:            /**
110:             * The SPI implementation.
111:             */
112:            private CipherSpi spiImpl;
113:
114:            /**
115:             * The transformation.
116:             */
117:            private String transformation;
118:
119:            private static SecureRandom sec_rand;
120:
121:            /**
122:             * @com.intel.drl.spec_ref
123:             *  
124:             */
125:            protected Cipher(CipherSpi cipherSpi, Provider provider,
126:                    String transformation) {
127:                if (cipherSpi == null) {
128:                    throw new NullPointerException();
129:                }
130:                if (!(cipherSpi instanceof  NullCipherSpi) && provider == null) {
131:                    throw new NullPointerException();
132:                }
133:                this .provider = provider;
134:                this .transformation = transformation;
135:                this .spiImpl = cipherSpi;
136:            }
137:
138:            /**
139:             * @com.intel.drl.spec_ref
140:             *  
141:             */
142:            public static final Cipher getInstance(String transformation)
143:                    throws NoSuchAlgorithmException, NoSuchPaddingException {
144:                return getCipher(transformation, null);
145:            }
146:
147:            /**
148:             * @com.intel.drl.spec_ref
149:             *  
150:             */
151:            public static final Cipher getInstance(String transformation,
152:                    String provider) throws NoSuchAlgorithmException,
153:                    NoSuchProviderException, NoSuchPaddingException {
154:
155:                if (provider == null) {
156:                    throw new IllegalArgumentException(Messages
157:                            .getString("crypto.04")); //$NON-NLS-1$
158:                }
159:
160:                Provider p = Security.getProvider(provider);
161:                if (p == null) {
162:                    throw new NoSuchProviderException(Messages.getString(
163:                            "crypto.16", provider)); //$NON-NLS-1$
164:                }
165:                return getInstance(transformation, p);
166:            }
167:
168:            /**
169:             * @com.intel.drl.spec_ref
170:             *  
171:             */
172:            public static final Cipher getInstance(String transformation,
173:                    Provider provider) throws NoSuchAlgorithmException,
174:                    NoSuchPaddingException {
175:                if (provider == null) {
176:                    throw new IllegalArgumentException(Messages
177:                            .getString("crypto.04")); //$NON-NLS-1$
178:                }
179:                Cipher c = getCipher(transformation, provider);
180:                return c;
181:            }
182:
183:            /**
184:             * Find appropriate Cipher according the specification rules
185:             * 
186:             * @param transformation
187:             * @param provider
188:             * @return
189:             * @throws NoSuchAlgorithmException
190:             * @throws NoSuchPaddingException
191:             */
192:            private static synchronized Cipher getCipher(String transformation,
193:                    Provider provider) throws NoSuchAlgorithmException,
194:                    NoSuchPaddingException {
195:
196:                if (transformation == null || "".equals(transformation)) { //$NON-NLS-1$
197:                    throw new NoSuchAlgorithmException(Messages.getString(
198:                            "crypto.17", //$NON-NLS-1$
199:                            transformation));
200:                }
201:
202:                String[] transf = checkTransformation(transformation);
203:
204:                boolean needSetPadding = false;
205:                boolean needSetMode = false;
206:                if (transf[1] == null && transf[2] == null) { // "algorithm"
207:                    if (provider == null) {
208:                        engine.getInstance(transf[0], null);
209:                    } else {
210:                        engine.getInstance(transf[0], provider, null);
211:                    }
212:                } else {
213:                    String[] searhOrder = {
214:                            transf[0] + "/" + transf[1] + "/" + transf[2], // "algorithm/mode/padding" //$NON-NLS-1$ //$NON-NLS-2$
215:                            transf[0] + "/" + transf[1], // "algorithm/mode" //$NON-NLS-1$
216:                            transf[0] + "//" + transf[2], // "algorithm//padding" //$NON-NLS-1$
217:                            transf[0] // "algorithm"
218:                    };
219:                    int i;
220:                    for (i = 0; i < searhOrder.length; i++) {
221:                        try {
222:                            if (provider == null) {
223:                                engine.getInstance(searhOrder[i], null);
224:                            } else {
225:                                engine.getInstance(searhOrder[i], provider,
226:                                        null);
227:                            }
228:                            break;
229:                        } catch (NoSuchAlgorithmException e) {
230:                            if (i == searhOrder.length - 1) {
231:                                throw new NoSuchAlgorithmException(
232:                                        transformation);
233:                            }
234:                        }
235:                    }
236:                    switch (i) {
237:                    case 1: // "algorithm/mode"
238:                        needSetPadding = true;
239:                        break;
240:                    case 2: // "algorithm//padding"
241:                        needSetMode = true;
242:                        break;
243:                    case 3: // "algorithm"
244:                        needSetPadding = true;
245:                        needSetMode = true;
246:                    }
247:                }
248:                CipherSpi cspi;
249:                try {
250:                    cspi = (CipherSpi) engine.spi;
251:                } catch (ClassCastException e) {
252:                    throw new NoSuchAlgorithmException(e);
253:                }
254:                Cipher c = new Cipher(cspi, engine.provider, transformation);
255:                if (needSetMode) {
256:                    c.spiImpl.engineSetMode(transf[1]);
257:                }
258:                if (needSetPadding) {
259:                    c.spiImpl.engineSetPadding(transf[2]);
260:                }
261:                return c;
262:            }
263:
264:            private static String[] checkTransformation(String transformation)
265:                    throws NoSuchAlgorithmException {
266:                String[] transf = { null, null, null };
267:                StringTokenizer st;
268:                int i = 0;
269:                for (st = new StringTokenizer(transformation, "/"); st //$NON-NLS-1$
270:                        .hasMoreElements();) {
271:                    if (i > 2) {
272:                        throw new NoSuchAlgorithmException(Messages.getString(
273:                                "crypto.17", //$NON-NLS-1$
274:                                transformation));
275:                    }
276:                    transf[i] = st.nextToken();
277:                    if (transf[i] != null) {
278:                        transf[i] = transf[i].trim();
279:                        if ("".equals(transf[i])) { //$NON-NLS-1$
280:                            transf[i] = null;
281:                        }
282:                        i++;
283:                    }
284:                }
285:                if (transf[0] == null) {
286:                    throw new NoSuchAlgorithmException(Messages.getString(
287:                            "crypto.17", //$NON-NLS-1$
288:                            transformation));
289:                }
290:                if (!(transf[1] == null && transf[2] == null)
291:                        && (transf[1] == null || transf[2] == null)) {
292:                    throw new NoSuchAlgorithmException(Messages.getString(
293:                            "crypto.17", //$NON-NLS-1$
294:                            transformation));
295:                }
296:                return transf;
297:            }
298:
299:            /**
300:             * @com.intel.drl.spec_ref
301:             *  
302:             */
303:            public final Provider getProvider() {
304:                return provider;
305:            }
306:
307:            /**
308:             * @com.intel.drl.spec_ref
309:             *  
310:             */
311:            public final String getAlgorithm() {
312:                return transformation;
313:            }
314:
315:            /**
316:             * @com.intel.drl.spec_ref
317:             *  
318:             */
319:            public final int getBlockSize() {
320:                return spiImpl.engineGetBlockSize();
321:            }
322:
323:            /**
324:             * @com.intel.drl.spec_ref
325:             *  
326:             */
327:            public final int getOutputSize(int inputLen) {
328:                if (mode == 0) {
329:                    throw new IllegalStateException(Messages
330:                            .getString("crypto.18")); //$NON-NLS-1$
331:                }
332:                return spiImpl.engineGetOutputSize(inputLen);
333:            }
334:
335:            /**
336:             * @com.intel.drl.spec_ref
337:             *  
338:             */
339:            public final byte[] getIV() {
340:                return spiImpl.engineGetIV();
341:            }
342:
343:            /**
344:             * @com.intel.drl.spec_ref
345:             *  
346:             */
347:            public final AlgorithmParameters getParameters() {
348:                return spiImpl.engineGetParameters();
349:            }
350:
351:            /**
352:             * @com.intel.drl.spec_ref
353:             *  
354:             */
355:            public final ExemptionMechanism getExemptionMechanism() {
356:                //FIXME implement getExemptionMechanism
357:
358:                //        try {
359:                //            return ExemptionMechanism.getInstance(transformation, provider);
360:                //        } catch (NoSuchAlgorithmException e) {
361:                return null;
362:                //        }
363:
364:            }
365:
366:            /**
367:             * @com.intel.drl.spec_ref
368:             *  
369:             */
370:            public final void init(int opmode, Key key)
371:                    throws InvalidKeyException {
372:                if (sec_rand == null) {
373:                    // In theory it might be thread-unsafe but in the given case it's OK
374:                    // since it does not matter which SecureRandom instance is passed
375:                    // to the init()
376:                    sec_rand = new SecureRandom();
377:                }
378:                init(opmode, key, sec_rand);
379:            }
380:
381:            /**
382:             * @com.intel.drl.spec_ref
383:             *  
384:             */
385:            public final void init(int opmode, Key key, SecureRandom random)
386:                    throws InvalidKeyException {
387:                if (opmode != ENCRYPT_MODE && opmode != DECRYPT_MODE
388:                        && opmode != UNWRAP_MODE && opmode != WRAP_MODE) {
389:                    throw new InvalidParameterException(Messages
390:                            .getString("crypto.19")); //$NON-NLS-1$
391:                }
392:                //        FIXME InvalidKeyException
393:                //        if keysize exceeds the maximum allowable keysize
394:                //        (jurisdiction policy files)
395:                spiImpl.engineInit(opmode, key, random);
396:                mode = opmode;
397:            }
398:
399:            /**
400:             * @com.intel.drl.spec_ref
401:             *  
402:             */
403:            public final void init(int opmode, Key key,
404:                    AlgorithmParameterSpec params) throws InvalidKeyException,
405:                    InvalidAlgorithmParameterException {
406:                if (sec_rand == null) {
407:                    sec_rand = new SecureRandom();
408:                }
409:                init(opmode, key, params, sec_rand);
410:            }
411:
412:            /**
413:             * @com.intel.drl.spec_ref
414:             *  
415:             */
416:            public final void init(int opmode, Key key,
417:                    AlgorithmParameterSpec params, SecureRandom random)
418:                    throws InvalidKeyException,
419:                    InvalidAlgorithmParameterException {
420:                if (opmode != ENCRYPT_MODE && opmode != DECRYPT_MODE
421:                        && opmode != UNWRAP_MODE && opmode != WRAP_MODE) {
422:                    throw new InvalidParameterException(Messages
423:                            .getString("crypto.19")); //$NON-NLS-1$
424:                }
425:                //        FIXME InvalidKeyException
426:                //        if keysize exceeds the maximum allowable keysize
427:                //        (jurisdiction policy files)
428:                //        FIXME InvalidAlgorithmParameterException
429:                //        cryptographic strength exceed the legal limits
430:                //        (jurisdiction policy files)
431:                spiImpl.engineInit(opmode, key, params, random);
432:                mode = opmode;
433:            }
434:
435:            /**
436:             * @com.intel.drl.spec_ref
437:             *  
438:             */
439:            public final void init(int opmode, Key key,
440:                    AlgorithmParameters params) throws InvalidKeyException,
441:                    InvalidAlgorithmParameterException {
442:                if (sec_rand == null) {
443:                    sec_rand = new SecureRandom();
444:                }
445:                init(opmode, key, params, sec_rand);
446:            }
447:
448:            /**
449:             * @com.intel.drl.spec_ref
450:             *  
451:             */
452:            public final void init(int opmode, Key key,
453:                    AlgorithmParameters params, SecureRandom random)
454:                    throws InvalidKeyException,
455:                    InvalidAlgorithmParameterException {
456:                if (opmode != ENCRYPT_MODE && opmode != DECRYPT_MODE
457:                        && opmode != UNWRAP_MODE && opmode != WRAP_MODE) {
458:                    throw new InvalidParameterException(Messages
459:                            .getString("crypto.19")); //$NON-NLS-1$
460:                }
461:                //        FIXME InvalidKeyException
462:                //        if keysize exceeds the maximum allowable keysize
463:                //        (jurisdiction policy files)
464:                //        FIXME InvalidAlgorithmParameterException
465:                //        cryptographic strength exceed the legal limits
466:                //        (jurisdiction policy files)
467:                spiImpl.engineInit(opmode, key, params, random);
468:                mode = opmode;
469:            }
470:
471:            /**
472:             * @com.intel.drl.spec_ref
473:             *  
474:             */
475:            public final void init(int opmode, Certificate certificate)
476:                    throws InvalidKeyException {
477:                if (sec_rand == null) {
478:                    sec_rand = new SecureRandom();
479:                }
480:                init(opmode, certificate, sec_rand);
481:            }
482:
483:            /**
484:             * @com.intel.drl.spec_ref
485:             *  
486:             */
487:            public final void init(int opmode, Certificate certificate,
488:                    SecureRandom random) throws InvalidKeyException {
489:                if (opmode != ENCRYPT_MODE && opmode != DECRYPT_MODE
490:                        && opmode != UNWRAP_MODE && opmode != WRAP_MODE) {
491:                    throw new InvalidParameterException(Messages
492:                            .getString("crypto.19")); //$NON-NLS-1$
493:                }
494:                if (certificate instanceof  X509Certificate) {
495:                    Set<String> ce = ((X509Certificate) certificate)
496:                            .getCriticalExtensionOIDs();
497:                    boolean critical = false;
498:                    if (ce != null && !ce.isEmpty()) {
499:                        for (String oid : ce) {
500:                            if (oid.equals("2.5.29.15")) { //KeyUsage OID = 2.5.29.15 //$NON-NLS-1$
501:                                critical = true;
502:                                break;
503:                            }
504:                        }
505:                        if (critical) {
506:                            boolean[] keyUsage = ((X509Certificate) certificate)
507:                                    .getKeyUsage();
508:                            // As specified in RFC 3280 -
509:                            // Internet X.509 Public Key Infrastructure
510:                            // Certificate and Certificate Revocation List (CRL) Profile.
511:                            // (http://www.ietf.org/rfc/rfc3280.txt)
512:                            //
513:                            // KeyUsage ::= BIT STRING {digitalSignature (0),
514:                            //                          ...
515:                            //                          encipherOnly (7),
516:                            //                          decipherOnly (8) }
517:                            if (keyUsage != null) {
518:                                if (opmode == ENCRYPT_MODE && (!keyUsage[7])) {
519:                                    throw new InvalidKeyException(Messages
520:                                            .getString("crypto.1A")); //$NON-NLS-1$
521:                                } else if (opmode == DECRYPT_MODE
522:                                        && (!keyUsage[8])) {
523:                                    throw new InvalidKeyException(Messages
524:                                            .getString("crypto.1B")); //$NON-NLS-1$
525:                                }
526:                            }
527:                        }
528:                    }
529:                }
530:                //        FIXME InvalidKeyException
531:                //        if keysize exceeds the maximum allowable keysize
532:                //        (jurisdiction policy files)
533:                spiImpl.engineInit(opmode, certificate.getPublicKey(), random);
534:                mode = opmode;
535:            }
536:
537:            /**
538:             * @com.intel.drl.spec_ref
539:             *  
540:             */
541:            public final byte[] update(byte[] input) {
542:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
543:                    throw new IllegalStateException(Messages
544:                            .getString("crypto.1C")); //$NON-NLS-1$
545:                }
546:                if (input == null) {
547:                    throw new IllegalArgumentException(Messages
548:                            .getString("crypto.1D")); //$NON-NLS-1$
549:                }
550:                if (input.length == 0) {
551:                    return null;
552:                }
553:                return spiImpl.engineUpdate(input, 0, input.length);
554:            }
555:
556:            /**
557:             * @com.intel.drl.spec_ref
558:             *  
559:             */
560:            public final byte[] update(byte[] input, int inputOffset,
561:                    int inputLen) {
562:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
563:                    throw new IllegalStateException(Messages
564:                            .getString("crypto.1C")); //$NON-NLS-1$
565:                }
566:                if (input == null) {
567:                    throw new IllegalArgumentException(Messages
568:                            .getString("crypto.1D")); //$NON-NLS-1$
569:                }
570:                if (inputOffset < 0 || inputLen < 0 || inputLen > input.length
571:                        || inputOffset > input.length - inputLen) {
572:                    throw new IllegalArgumentException(Messages
573:                            .getString("crypto.1E")); //$NON-NLS-1$
574:                }
575:                if (input.length == 0) {
576:                    return null;
577:                }
578:                return spiImpl.engineUpdate(input, inputOffset, inputLen);
579:            }
580:
581:            /**
582:             * @com.intel.drl.spec_ref
583:             *  
584:             */
585:            public final int update(byte[] input, int inputOffset,
586:                    int inputLen, byte[] output) throws ShortBufferException {
587:                return update(input, inputOffset, inputLen, output, 0);
588:            }
589:
590:            /**
591:             * @com.intel.drl.spec_ref
592:             *  
593:             */
594:            public final int update(byte[] input, int inputOffset,
595:                    int inputLen, byte[] output, int outputOffset)
596:                    throws ShortBufferException {
597:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
598:                    throw new IllegalStateException(Messages
599:                            .getString("crypto.1C")); //$NON-NLS-1$
600:                }
601:                if (input == null) {
602:                    throw new IllegalArgumentException(Messages
603:                            .getString("crypto.1D")); //$NON-NLS-1$
604:                }
605:                if (output == null) {
606:                    throw new IllegalArgumentException(Messages
607:                            .getString("crypto.1F")); //$NON-NLS-1$
608:                }
609:                if (outputOffset < 0) {
610:                    throw new IllegalArgumentException(Messages
611:                            .getString("crypto.20")); //$NON-NLS-1$
612:                }
613:                if (inputOffset < 0 || inputLen < 0 || inputLen > input.length
614:                        || inputOffset > input.length - inputLen) {
615:                    throw new IllegalArgumentException(Messages
616:                            .getString("crypto.21")); //$NON-NLS-1$
617:                }
618:                if (input.length == 0) {
619:                    return 0;
620:                }
621:                return spiImpl.engineUpdate(input, inputOffset, inputLen,
622:                        output, outputOffset);
623:            }
624:
625:            /**
626:             * @com.intel.drl.spec_ref
627:             *  
628:             */
629:            public final int update(ByteBuffer input, ByteBuffer output)
630:                    throws ShortBufferException {
631:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
632:                    throw new IllegalStateException(Messages
633:                            .getString("crypto.1C")); //$NON-NLS-1$
634:                }
635:                if (input == output) {
636:                    throw new IllegalArgumentException(Messages
637:                            .getString("crypto.22")); //$NON-NLS-1$
638:                }
639:                return spiImpl.engineUpdate(input, output);
640:            }
641:
642:            /**
643:             * @com.intel.drl.spec_ref
644:             *  
645:             */
646:            public final byte[] doFinal() throws IllegalBlockSizeException,
647:                    BadPaddingException {
648:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
649:                    throw new IllegalStateException(Messages
650:                            .getString("crypto.1C")); //$NON-NLS-1$
651:                }
652:                return spiImpl.engineDoFinal(null, 0, 0);
653:            }
654:
655:            /**
656:             * @com.intel.drl.spec_ref
657:             *  
658:             */
659:            public final int doFinal(byte[] output, int outputOffset)
660:                    throws IllegalBlockSizeException, ShortBufferException,
661:                    BadPaddingException {
662:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
663:                    throw new IllegalStateException(Messages
664:                            .getString("crypto.1C")); //$NON-NLS-1$
665:                }
666:                if (outputOffset < 0) {
667:                    throw new IllegalArgumentException(Messages
668:                            .getString("crypto.20")); //$NON-NLS-1$
669:                }
670:                return spiImpl.engineDoFinal(null, 0, 0, output, outputOffset);
671:            }
672:
673:            /**
674:             * @com.intel.drl.spec_ref
675:             *  
676:             */
677:            public final byte[] doFinal(byte[] input)
678:                    throws IllegalBlockSizeException, BadPaddingException {
679:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
680:                    throw new IllegalStateException(Messages
681:                            .getString("crypto.1C")); //$NON-NLS-1$
682:                }
683:                return spiImpl.engineDoFinal(input, 0, input.length);
684:            }
685:
686:            /**
687:             * @com.intel.drl.spec_ref
688:             *  
689:             */
690:            public final byte[] doFinal(byte[] input, int inputOffset,
691:                    int inputLen) throws IllegalBlockSizeException,
692:                    BadPaddingException {
693:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
694:                    throw new IllegalStateException(Messages
695:                            .getString("crypto.1C")); //$NON-NLS-1$
696:                }
697:                if (inputOffset < 0 || inputLen < 0
698:                        || inputOffset + inputLen > input.length) {
699:                    throw new IllegalArgumentException(Messages
700:                            .getString("crypto.1E")); //$NON-NLS-1$
701:                }
702:                return spiImpl.engineDoFinal(input, inputOffset, inputLen);
703:            }
704:
705:            /**
706:             * @com.intel.drl.spec_ref
707:             *  
708:             */
709:            public final int doFinal(byte[] input, int inputOffset,
710:                    int inputLen, byte[] output) throws ShortBufferException,
711:                    IllegalBlockSizeException, BadPaddingException {
712:                return doFinal(input, inputOffset, inputLen, output, 0);
713:            }
714:
715:            /**
716:             * @com.intel.drl.spec_ref
717:             *  
718:             */
719:            public final int doFinal(byte[] input, int inputOffset,
720:                    int inputLen, byte[] output, int outputOffset)
721:                    throws ShortBufferException, IllegalBlockSizeException,
722:                    BadPaddingException {
723:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
724:                    throw new IllegalStateException(Messages
725:                            .getString("crypto.1C")); //$NON-NLS-1$
726:                }
727:                if (inputOffset < 0 || inputLen < 0
728:                        || inputOffset + inputLen > input.length) {
729:                    throw new IllegalArgumentException(Messages
730:                            .getString("crypto.1E")); //$NON-NLS-1$
731:                }
732:                return spiImpl.engineDoFinal(input, inputOffset, inputLen,
733:                        output, outputOffset);
734:            }
735:
736:            /**
737:             * @com.intel.drl.spec_ref
738:             *  
739:             */
740:            public final int doFinal(ByteBuffer input, ByteBuffer output)
741:                    throws ShortBufferException, IllegalBlockSizeException,
742:                    BadPaddingException {
743:                if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
744:                    throw new IllegalStateException(Messages
745:                            .getString("crypto.1C")); //$NON-NLS-1$
746:                }
747:                if (input == output) {
748:                    throw new IllegalArgumentException(Messages
749:                            .getString("crypto.2E")); //$NON-NLS-1$
750:                }
751:                return spiImpl.engineDoFinal(input, output);
752:            }
753:
754:            /**
755:             * @com.intel.drl.spec_ref
756:             *  
757:             */
758:            public final byte[] wrap(Key key) throws IllegalBlockSizeException,
759:                    InvalidKeyException {
760:                if (mode != WRAP_MODE) {
761:                    throw new IllegalStateException(Messages
762:                            .getString("crypto.1C")); //$NON-NLS-1$
763:                }
764:                return spiImpl.engineWrap(key);
765:            }
766:
767:            /**
768:             * @com.intel.drl.spec_ref
769:             *  
770:             */
771:            public final Key unwrap(byte[] wrappedKey,
772:                    String wrappedKeyAlgorithm, int wrappedKeyType)
773:                    throws InvalidKeyException, NoSuchAlgorithmException {
774:                if (mode != UNWRAP_MODE) {
775:                    throw new IllegalStateException(Messages
776:                            .getString("crypto.1C")); //$NON-NLS-1$
777:                }
778:                return spiImpl.engineUnwrap(wrappedKey, wrappedKeyAlgorithm,
779:                        wrappedKeyType);
780:            }
781:
782:            /**
783:             * @com.intel.drl.spec_ref
784:             *  
785:             */
786:            public static final int getMaxAllowedKeyLength(String transformation)
787:                    throws NoSuchAlgorithmException {
788:                if (transformation == null) {
789:                    throw new NullPointerException();
790:                }
791:                checkTransformation(transformation);
792:                //FIXME jurisdiction policy files
793:                return Integer.MAX_VALUE;
794:            }
795:
796:            /**
797:             * @com.intel.drl.spec_ref
798:             *  
799:             */
800:            public static final AlgorithmParameterSpec getMaxAllowedParameterSpec(
801:                    String transformation) throws NoSuchAlgorithmException {
802:                if (transformation == null) {
803:                    throw new NullPointerException();
804:                }
805:                checkTransformation(transformation);
806:                //FIXME jurisdiction policy files
807:                return null;
808:            }
809:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.