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 implementation for OpenSSL 1.x called from {@link OpenSsl}. The native methods in this 24 * class are defined in OpenSslNative.h (generated by javah). 25 */ 26 final class OpenSslNative { 27 28 /** 29 * Cleans the context at native. 30 * 31 * @param context The cipher context address 32 */ 33 public static native void clean(long context); 34 35 /** 36 * Allows various cipher specific parameters to be determined and set. 37 * 38 * it will call OpenSSL's API 39 * int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) 40 * In OpenSSL, data type of ptr can be char* or long*. Here, we map java's 41 * byte[] to native void*ptr. Note that the byte order is ByteOrder.nativeOrder. 42 * 43 * @param context The cipher context address 44 * @param type CtrlValues 45 * @param arg argument like a tag length 46 * @param data byte buffer or null 47 * @return return 0 if there is any error, else return 1. 48 */ 49 public static native int ctrl(long context, int type, int arg, byte[] data); 50 51 /** 52 * Finishes a multiple-part operation. The data is encrypted or decrypted, 53 * depending on how this cipher was initialized. 54 * 55 * @param context The cipher context address 56 * @param output The byte buffer for the result 57 * @param offset The offset in output where the result is stored 58 * @param maxOutputLength The maximum length for output 59 * @return The number of bytes stored in output 60 */ 61 public static native int doFinal(long context, ByteBuffer output, 62 int offset, int maxOutputLength); 63 64 /** 65 * Finishes a multiple-part operation. The data is encrypted or decrypted, 66 * depending on how this cipher was initialized. 67 * 68 * @param context The cipher context address 69 * @param output The byte array for the result 70 * @param offset The offset in output where the result is stored 71 * @param maxOutputLength The maximum length for output 72 * @return The number of bytes stored in output 73 */ 74 public static native int doFinalByteArray(long context, byte[] output, 75 int offset, int maxOutputLength); 76 77 /** 78 * Declares a native method to initialize the cipher context. 79 * 80 * @param context The cipher context address 81 * @param mode ENCRYPT_MODE or DECRYPT_MODE 82 * @param alg Algorithm Mode of OpenSsl 83 * @param padding the padding mode of OpenSsl cipher 84 * @param key crypto key 85 * @param iv crypto iv 86 * @return the context address of cipher 87 */ 88 public static native long init(long context, int mode, int alg, 89 int padding, byte[] key, byte[] iv); 90 91 /** 92 * Declares a native method to initialize the cipher context. 93 * 94 * @param algorithm The algorithm name of cipher 95 * @param padding The padding name of cipher 96 * @return the context address of cipher 97 */ 98 public static native long initContext(int algorithm, int padding); 99 100 /** 101 * Declares a native method to initialize JNI field and method IDs. 102 */ 103 public static native void initIDs(); 104 105 /** 106 * Continues a multiple-part encryption/decryption operation. The data is 107 * encrypted or decrypted, depending on how this cipher was initialized. 108 * 109 * @param context The cipher context address 110 * @param input The input byte buffer 111 * @param inputOffset The offset in input where the input starts 112 * @param inputLength The input length 113 * @param output The byte buffer for the result 114 * @param outputOffset The offset in output where the result is stored 115 * @param maxOutputLength The maximum length for output 116 * @return The number of bytes stored in output 117 */ 118 public static native int update(long context, ByteBuffer input, 119 int inputOffset, int inputLength, ByteBuffer output, 120 int outputOffset, int maxOutputLength); 121 122 /** 123 * Continues a multiple-part encryption/decryption operation. The data is 124 * encrypted or decrypted, depending on how this cipher was initialized. 125 * 126 * @param context The cipher context address 127 * @param input The input byte array 128 * @param inputOffset The offset in input where the input starts 129 * @param inputLength The input length 130 * @param output The byte array for the result 131 * @param outputOffset The offset in output where the result is stored 132 * @param maxOutputLength The maximum length for output 133 * @return The number of bytes stored in output 134 */ 135 public static native int updateByteArray(long context, byte[] input, 136 int inputOffset, int inputLength, byte[] output, int outputOffset, 137 int maxOutputLength); 138 139 /** 140 * Continues a multiple-part encryption/decryption operation. The data is 141 * encrypted or decrypted, depending on how this cipher was initialized. 142 * 143 * @param context The cipher context address 144 * @param input The input byte array 145 * @param inputOffset The offset in input where the input starts 146 * @param inputLength The input length 147 * @param output The byte buffer for the result 148 * @param outputOffset The offset in output where the result is stored 149 * @param maxOutputLength The maximum length for output 150 * @return The number of bytes stored in output 151 */ 152 public static native int updateByteArrayByteBuffer(long context, byte[] input, 153 int inputOffset, int inputLength, 154 ByteBuffer output, int outputOffset, int maxOutputLength); 155 156 /** 157 * Hides this constructor from external access. 158 */ 159 private OpenSslNative() { 160 } 161 }