View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    * <p>
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * <p>
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.crypto.cipher;
19  
20  import java.nio.ByteBuffer;
21  
22  /**
23   * JNI interface of {@link OpenSsl} implementation. The native method in this
24   * class is defined in OpenSslNative.h (generated by javah).
25   */
26  class OpenSslNative {
27  
28      /**
29       * The private constructor of {@link OpenSslNative}.
30       */
31      private OpenSslNative() {
32      }
33  
34      /**
35       * Declares a native method to initialize JNI field and method IDs.
36       */
37      public native static void initIDs();
38  
39      /**
40       * Declares a native method to initialize the cipher context.
41       *
42       * @param algorithm The algorithm name of cipher
43       * @param padding The padding name of cipher
44       * @return the context address of cipher
45       */
46      public native static long initContext(int algorithm, int padding);
47  
48      /**
49       * Declares a native method to initialize the cipher context.
50       *
51       * @param context The cipher context address
52       * @param mode ENCRYPT_MODE or DECRYPT_MODE
53       * @param alg Algorithm Mode of OpenSsl
54       * @param padding the padding mode of OpenSsl cipher
55       * @param key crypto key
56       * @param iv crypto iv
57       * @return the context address of cipher
58       */
59      public native static long init(long context, int mode, int alg,
60                                     int padding, byte[] key, byte[] iv);
61  
62      /**
63       * Continues a multiple-part encryption/decryption operation. The data is
64       * encrypted or decrypted, depending on how this cipher was initialized.
65       *
66       * @param context The cipher context address
67       * @param input The input byte buffer
68       * @param inputOffset The offset in input where the input starts
69       * @param inputLength The input length
70       * @param output The byte buffer for the result
71       * @param outputOffset The offset in output where the result is stored
72       * @param maxOutputLength The maximum length for output
73       * @return The number of bytes stored in output
74       */
75      public native static int update(long context, ByteBuffer input,
76              int inputOffset, int inputLength, ByteBuffer output,
77              int outputOffset, int maxOutputLength);
78  
79      /**
80       * Continues a multiple-part encryption/decryption operation. The data is
81       * encrypted or decrypted, depending on how this cipher was initialized.
82       *
83       * @param context The cipher context address
84       * @param input The input byte array
85       * @param inputOffset The offset in input where the input starts
86       * @param inputLength The input length
87       * @param output The byte array for the result
88       * @param outputOffset The offset in output where the result is stored
89       * @param maxOutputLength The maximum length for output
90       * @return The number of bytes stored in output
91       */
92      public native static int updateByteArray(long context, byte[] input,
93              int inputOffset, int inputLength, byte[] output, int outputOffset,
94              int maxOutputLength);
95  
96      /**
97       * Continues a multiple-part encryption/decryption operation. The data is
98       * encrypted or decrypted, depending on how this cipher was initialized.
99       *
100      * @param context The cipher context address
101      * @param input The input byte array
102      * @param inputOffset The offset in input where the input starts
103      * @param inputLength The input length
104      * @param output The byte buffer for the result
105      * @param outputOffset The offset in output where the result is stored
106      * @param maxOutputLength The maximum length for output
107      * @return The number of bytes stored in output
108      */
109     public native static int updateByteArrayByteBuffer(long context, byte[] input,
110                                                        int inputOffset, int inputLength,
111                                                        ByteBuffer output, int outputOffset, int maxOutputLength);
112 
113     /**
114      * Finishes a multiple-part operation. The data is encrypted or decrypted,
115      * depending on how this cipher was initialized.
116      *
117      * @param context The cipher context address
118      * @param output The byte buffer for the result
119      * @param offset The offset in output where the result is stored
120      * @param maxOutputLength The maximum length for output
121      * @return The number of bytes stored in output
122      */
123     public native static int doFinal(long context, ByteBuffer output,
124             int offset, int maxOutputLength);
125 
126     /**
127      * Finishes a multiple-part operation. The data is encrypted or decrypted,
128      * depending on how this cipher was initialized.
129      *
130      * @param context The cipher context address
131      * @param output The byte array for the result
132      * @param offset The offset in output where the result is stored
133      * @param maxOutputLength The maximum length for output
134      * @return The number of bytes stored in output
135      */
136     public native static int doFinalByteArray(long context, byte[] output,
137             int offset, int maxOutputLength);
138 
139     /**
140      * allows various cipher specific parameters to be determined and set.
141      *
142      * it will call OpenSSL's API
143      * int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
144      * In OpenSSL, data type of ptr can be char* or long*.  Here, we map java's
145      * byte[] to native void*ptr. Note that the byte order is ByteOrder.nativeOrder.
146      *
147      * @param context The cipher context address
148      * @param type CtrlValues
149      * @param arg argument like a tag length
150      * @param data byte buffer or null
151      * @return return 0 if there is any error, else return 1.
152      */
153     public native static int ctrl(long context, int type, int arg, byte[] data);
154 
155 
156     /**
157      * Cleans the context at native.
158      *
159      * @param context The cipher context address
160      */
161     public native static void clean(long context);
162 }