Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief TP level crypto functions
0003  *
0004  * @file tpcrypto.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <ndrx_config.h>
0035 #include <string.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <errno.h>
0040 #include <sys/sem.h>
0041 #include <signal.h>
0042 
0043 #include <atmi.h>
0044 #include <atmi_shm.h>
0045 #include <ndrstandard.h>
0046 #include <ndebug.h>
0047 #include <ndrxdcmn.h>
0048 #include <userlog.h>
0049 
0050 /* shm_* stuff, and mmap() */
0051 #include <sys/mman.h>
0052 #include <sys/types.h>
0053 /* exit() etc */
0054 #include <unistd.h>
0055 #include <fcntl.h>
0056 #include <sys/stat.h>
0057 #include <sys/ipc.h>
0058 #include <excrypto.h>
0059 
0060 #include <nstd_shm.h>
0061 #include <cpm.h>
0062 /*---------------------------Externs------------------------------------*/
0063 /*---------------------------Macros-------------------------------------*/
0064 /*---------------------------Enums--------------------------------------*/
0065 /*---------------------------Typedefs-----------------------------------*/
0066 /*---------------------------Globals------------------------------------*/
0067 /*---------------------------Statics------------------------------------*/
0068 /*---------------------------Prototypes---------------------------------*/
0069 
0070 /**
0071  * Encrypt data block.
0072  * Currently AES-128 is used, CBC mode
0073  * In string mode (TPEX_STRING) - input is 0x0 terminated string, on output base64
0074  * encoded data (output buffer size is checked, but no len provided)
0075  * @param input input input data
0076  * @param ilen input string len (not used for TPEX_STRING)
0077  * @param output output buffer
0078  * @param olen output buffer len
0079  * @param flags TPEX_STRING - input & output data is string
0080  * @return EXSUCCEED/EXFAIL tperror loaded
0081  */
0082 expublic int tpencrypt_int(char *input, long ilen, char *output, long *olen, long flags)
0083 {
0084     int ret = EXSUCCEED;
0085     
0086     NDRX_LOG(log_debug, "%s - flags=%lx", __func__, flags);
0087     
0088     if (flags & TPEX_STRING)
0089     {
0090         if (EXSUCCEED!=ndrx_crypto_enc_string(input, output, olen))
0091         {
0092             EXFAIL_OUT(ret);
0093         }
0094     }
0095     else
0096     {
0097         if (EXSUCCEED!=ndrx_crypto_enc(input, ilen, output, olen))
0098         {
0099             EXFAIL_OUT(ret);
0100         }
0101     }
0102     
0103 out:
0104     if (EXSUCCEED!=ret)   
0105     {
0106         /* map the errors... */
0107         ndrx_TPset_error_nstd();
0108     }
0109     
0110     NDRX_LOG(log_debug, "%s return %d", __func__, ret);
0111 
0112     return ret;
0113 }
0114 
0115 /**
0116  * Decrypt data block.
0117  * Currently AES-128 is used, CBC mode
0118  * In string mode (TPEX_STRING) - input is 0x0 terminated string with base64 data,
0119  * on output 0x0 terminate string is provided. No len is provided on output
0120  * but output buffer size is tested.
0121  * @param input input data buffer, string or binary data
0122  * @param ilen input len (only for binary)
0123  * @param output output buffer
0124  * @param olen output buffer size, on output len provided (only for binary mode)
0125  * @param flags TPEX_STRING - input & output data is string
0126  * @return EXSUCCEED/EXFAIL
0127  */
0128 expublic int tpdecrypt_int(char *input, long ilen, char *output, long *olen, long flags)
0129 {
0130     int ret = EXSUCCEED;
0131     
0132     NDRX_LOG(log_debug, "%s - flags=%lx", __func__, flags);
0133     
0134     if (flags & TPEX_STRING)
0135     {
0136         if (EXSUCCEED!=ndrx_crypto_dec_string(input, output, olen))
0137         {
0138             EXFAIL_OUT(ret);
0139         }
0140     }
0141     else
0142     {
0143         if (EXSUCCEED!=ndrx_crypto_dec(input, ilen, output, olen))
0144         {
0145             EXFAIL_OUT(ret);
0146         }
0147     }
0148     
0149 out:
0150     if (EXSUCCEED!=ret)   
0151     {
0152         /* map the errors... */
0153         ndrx_TPset_error_nstd();
0154     }
0155 
0156     NDRX_LOG(log_debug, "%s return %d", __func__, ret);
0157     
0158     return ret;
0159 }
0160 
0161 /* vim: set ts=4 sw=4 et smartindent: */