2012-10-17 18:02:35 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
2013-03-07 14:56:48 +00:00
|
|
|
* Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
|
2012-10-17 18:02:35 +00:00
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
2013-03-07 14:56:48 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-10-17 18:02:35 +00:00
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
2014-07-26 09:58:40 +00:00
|
|
|
*
|
2012-10-17 18:02:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CRYPTO_UTILS_H
|
|
|
|
#define _CRYPTO_UTILS_H
|
|
|
|
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-03-20 17:17:03 +00:00
|
|
|
#include <utils.h>
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_PW_LEN 16
|
2013-01-25 17:25:55 +00:00
|
|
|
#define CKSUM_MASK 0x700
|
2012-10-17 18:02:35 +00:00
|
|
|
#define CKSUM_MAX_BYTES 64
|
2013-01-26 12:10:23 +00:00
|
|
|
#define DEFAULT_CKSUM "BLAKE256"
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2013-03-03 14:32:14 +00:00
|
|
|
/*
|
|
|
|
* Default key length for Encryption and Decryption
|
|
|
|
*/
|
|
|
|
#ifndef DEFAULT_KEYLEN
|
|
|
|
#define DEFAULT_KEYLEN 32
|
|
|
|
#define MAX_KEYLEN 32
|
|
|
|
#else
|
|
|
|
#define MAX_KEYLEN DEFAULT_KEYLEN
|
|
|
|
#endif
|
|
|
|
|
2013-03-04 17:05:33 +00:00
|
|
|
#define OLD_KEYLEN 16
|
|
|
|
#define ENCRYPT_FLAG 1
|
|
|
|
#define DECRYPT_FLAG 0
|
2013-03-04 16:26:07 +00:00
|
|
|
#define CRYPTO_ALG_AES 0x10
|
|
|
|
#define CRYPTO_ALG_SALSA20 0x20
|
|
|
|
#define MAX_SALTLEN 64
|
|
|
|
#define MAX_NONCE 32
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-12-08 08:49:01 +00:00
|
|
|
#define KECCAK_MAX_SEG (2305843009213693950ULL)
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *crypto_ctx;
|
|
|
|
int crypto_alg;
|
|
|
|
int enc_dec;
|
|
|
|
uchar_t *salt;
|
2013-03-04 16:26:07 +00:00
|
|
|
uchar_t *pkey;
|
2012-10-17 18:02:35 +00:00
|
|
|
int saltlen;
|
2013-03-03 14:32:14 +00:00
|
|
|
int keylen;
|
2012-10-17 18:02:35 +00:00
|
|
|
} crypto_ctx_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *mac_ctx;
|
|
|
|
void *mac_ctx_reinit;
|
|
|
|
int mac_cksum;
|
|
|
|
} mac_ctx_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic message digest functions.
|
|
|
|
*/
|
2013-03-24 17:51:17 +00:00
|
|
|
int compute_checksum(uchar_t *cksum_buf, int cksum, uchar_t *buf, uint64_t bytes, int mt, int verbose);
|
2013-01-24 18:40:12 +00:00
|
|
|
void list_checksums(FILE *strm, char *pad);
|
2013-01-26 10:09:10 +00:00
|
|
|
int get_checksum_props(const char *name, int *cksum, int *cksum_bytes,
|
|
|
|
int *mac_bytes, int accept_compatible);
|
2012-10-17 18:02:35 +00:00
|
|
|
void serialize_checksum(uchar_t *checksum, uchar_t *buf, int cksum_bytes);
|
|
|
|
void deserialize_checksum(uchar_t *checksum, uchar_t *buf, int cksum_bytes);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encryption related functions.
|
|
|
|
*/
|
|
|
|
int init_crypto(crypto_ctx_t *cctx, uchar_t *pwd, int pwd_len, int crypto_alg,
|
2013-03-04 16:26:07 +00:00
|
|
|
uchar_t *salt, int saltlen, int keylen, uchar_t *nonce, int enc_dec);
|
2013-01-02 17:26:21 +00:00
|
|
|
int crypto_buf(crypto_ctx_t *cctx, uchar_t *from, uchar_t *to, uint64_t bytes, uint64_t id);
|
2013-03-04 16:26:07 +00:00
|
|
|
uchar_t *crypto_nonce(crypto_ctx_t *cctx);
|
2012-10-17 18:02:35 +00:00
|
|
|
void crypto_clean_pkey(crypto_ctx_t *cctx);
|
|
|
|
void cleanup_crypto(crypto_ctx_t *cctx);
|
2012-12-27 17:36:48 +00:00
|
|
|
int get_pw_string(uchar_t pw[MAX_PW_LEN], const char *prompt, int twice);
|
2013-03-04 16:26:07 +00:00
|
|
|
int get_crypto_alg(char *name);
|
|
|
|
int geturandom_bytes(uchar_t *rbytes, int nbytes);
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HMAC functions.
|
|
|
|
*/
|
|
|
|
int hmac_init(mac_ctx_t *mctx, int cksum, crypto_ctx_t *cctx);
|
|
|
|
int hmac_reinit(mac_ctx_t *mctx);
|
2012-12-09 04:45:06 +00:00
|
|
|
int hmac_update(mac_ctx_t *mctx, uchar_t *data, uint64_t len);
|
2012-12-27 17:36:48 +00:00
|
|
|
int hmac_final(mac_ctx_t *mctx, uchar_t *hash, unsigned int *len);
|
2012-10-17 18:02:35 +00:00
|
|
|
int hmac_cleanup(mac_ctx_t *mctx);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 09:58:40 +00:00
|
|
|
#endif
|