src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/NativeCipher.java

Print this page
7191662: JCE providers should be located via ServiceLoader
   1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38 import javax.crypto.spec.SecretKeySpec;
  39 import javax.crypto.spec.IvParameterSpec;
  40 
  41 /**
  42  * Cipher wrapper class utilizing ucrypto APIs. This class currently supports
  43  * - AES/ECB/NOPADDING
  44  * - AES/CBC/NOPADDING
  45  * - AES/CTR/NOPADDING
  46  * - AES/CFB128/NOPADDING
  47  * (Support for GCM mode is inside the child class NativeGCMCipher)
  48  *
  49  * @since 1.9
  50  */
  51 class NativeCipher extends CipherSpi {
  52 
  53     // public implementation classes
  54     public static final class AesEcbNoPadding extends NativeCipher {
  55         public AesEcbNoPadding() throws NoSuchAlgorithmException {
  56             super(UcryptoMech.CRYPTO_AES_ECB);
  57         }


  58     }

  59     public static final class AesCbcNoPadding extends NativeCipher {
  60         public AesCbcNoPadding() throws NoSuchAlgorithmException {
  61             super(UcryptoMech.CRYPTO_AES_CBC);
  62         }


  63     }

  64     public static final class AesCtrNoPadding extends NativeCipher {
  65         public AesCtrNoPadding() throws NoSuchAlgorithmException {
  66             super(UcryptoMech.CRYPTO_AES_CTR);
  67         }
  68     }
  69     public static final class AesCfb128NoPadding extends NativeCipher {
  70         public AesCfb128NoPadding() throws NoSuchAlgorithmException {
  71             super(UcryptoMech.CRYPTO_AES_CFB128);
  72         }
  73     }
  74 
  75     // public implementation classes with fixed key sizes
  76     public static final class Aes128EcbNoPadding extends NativeCipher {
  77         public Aes128EcbNoPadding() throws NoSuchAlgorithmException {
  78             super(UcryptoMech.CRYPTO_AES_ECB, 16);
  79         }
  80     }
  81     public static final class Aes128CbcNoPadding extends NativeCipher {
  82         public Aes128CbcNoPadding() throws NoSuchAlgorithmException {
  83             super(UcryptoMech.CRYPTO_AES_CBC, 16);
  84         }
  85     }
  86     public static final class Aes192EcbNoPadding extends NativeCipher {
  87         public Aes192EcbNoPadding() throws NoSuchAlgorithmException {
  88             super(UcryptoMech.CRYPTO_AES_ECB, 24);
  89         }
  90     }
  91     public static final class Aes192CbcNoPadding extends NativeCipher {
  92         public Aes192CbcNoPadding() throws NoSuchAlgorithmException {
  93             super(UcryptoMech.CRYPTO_AES_CBC, 24);
  94         }
  95     }
  96     public static final class Aes256EcbNoPadding extends NativeCipher {
  97         public Aes256EcbNoPadding() throws NoSuchAlgorithmException {
  98             super(UcryptoMech.CRYPTO_AES_ECB, 32);
  99         }
 100     }
 101     public static final class Aes256CbcNoPadding extends NativeCipher {
 102         public Aes256CbcNoPadding() throws NoSuchAlgorithmException {
 103             super(UcryptoMech.CRYPTO_AES_CBC, 32);
 104         }
 105     }
 106 
 107     // ok as constants since AES is all we support
 108     public static final int AES_BLOCK_SIZE = 16;
 109     public static final String AES_KEY_ALGO = "AES";
 110 
 111     // fields set in constructor
 112     protected final UcryptoMech mech;
 113     protected String keyAlgo;
 114     protected int blockSize;
 115     protected int fixedKeySize;
 116 
 117     //
 118     // fields (re)set in every init()
 119     //
 120     protected CipherContextRef pCtxt = null;
 121     protected byte[] keyValue = null;
 122     protected byte[] iv = null;
 123     protected boolean initialized = false;
 124     protected boolean encrypt = true;
 125     protected int bytesBuffered = 0;


   1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38 import javax.crypto.spec.SecretKeySpec;
  39 import javax.crypto.spec.IvParameterSpec;
  40 
  41 /**
  42  * Cipher wrapper class utilizing ucrypto APIs. This class currently supports
  43  * - AES/ECB/NOPADDING
  44  * - AES/CBC/NOPADDING
  45  * - AES/CTR/NOPADDING
  46  * - AES/CFB128/NOPADDING
  47  * (Support for GCM mode is inside the child class NativeGCMCipher)
  48  *
  49  * @since 1.9
  50  */
  51 class NativeCipher extends CipherSpi {
  52 
  53     // public implementation classes
  54     public static final class AesEcbNoPadding extends NativeCipher {
  55         public AesEcbNoPadding() throws NoSuchAlgorithmException {
  56             super(UcryptoMech.CRYPTO_AES_ECB); 
  57         }
  58         public AesEcbNoPadding(int keySize) throws NoSuchAlgorithmException {
  59             super(UcryptoMech.CRYPTO_AES_ECB, keySize);
  60         }
  61     }
  62     public static final class AesCbcNoPadding extends NativeCipher {
  63         public AesCbcNoPadding() throws NoSuchAlgorithmException {
  64             super(UcryptoMech.CRYPTO_AES_CBC);
  65         }
  66         public AesCbcNoPadding(int keySize) throws NoSuchAlgorithmException {
  67             super(UcryptoMech.CRYPTO_AES_CBC, keySize);
  68         }
  69     }
  70     public static final class AesCtrNoPadding extends NativeCipher {
  71         public AesCtrNoPadding() throws NoSuchAlgorithmException {
  72             super(UcryptoMech.CRYPTO_AES_CTR);
  73         }
  74     }
  75     public static final class AesCfb128NoPadding extends NativeCipher {
  76         public AesCfb128NoPadding() throws NoSuchAlgorithmException {
  77             super(UcryptoMech.CRYPTO_AES_CFB128);
  78         }
  79     }
































  80 
  81     // ok as constants since AES is all we support
  82     public static final int AES_BLOCK_SIZE = 16;
  83     public static final String AES_KEY_ALGO = "AES";
  84 
  85     // fields set in constructor
  86     protected final UcryptoMech mech;
  87     protected String keyAlgo;
  88     protected int blockSize;
  89     protected int fixedKeySize;
  90 
  91     //
  92     // fields (re)set in every init()
  93     //
  94     protected CipherContextRef pCtxt = null;
  95     protected byte[] keyValue = null;
  96     protected byte[] iv = null;
  97     protected boolean initialized = false;
  98     protected boolean encrypt = true;
  99     protected int bytesBuffered = 0;