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/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <skein.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/hmac.h>
|
2013-01-25 17:25:55 +00:00
|
|
|
#include <sha512.h>
|
2013-01-26 10:09:10 +00:00
|
|
|
#include <blake2_digest.h>
|
2012-10-17 18:02:35 +00:00
|
|
|
#include <crypto_aes.h>
|
2012-12-08 08:49:01 +00:00
|
|
|
#include <KeccakNISTInterface.h>
|
2013-01-24 18:40:12 +00:00
|
|
|
#include <utils.h>
|
2013-03-04 16:26:07 +00:00
|
|
|
#include <crypto_xsalsa20.h>
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
#include "crypto_utils.h"
|
2013-01-27 18:17:55 +00:00
|
|
|
#include "sha2_utils.h"
|
|
|
|
#include "sha3_utils.h"
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
#include "old/sha2_utils_old.h"
|
|
|
|
#include "old/sha3_utils_old.h"
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
#define PROVIDER_OPENSSL 0
|
|
|
|
#define PROVIDER_X64_OPT 1
|
|
|
|
|
2013-01-25 17:25:55 +00:00
|
|
|
static void init_sha512(void);
|
2013-01-26 10:09:10 +00:00
|
|
|
static void init_blake2(void);
|
|
|
|
static struct blake2_dispatch bdsp;
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
/*
|
|
|
|
* Checksum properties
|
|
|
|
*/
|
|
|
|
typedef void (*ckinit_func_ptr)(void);
|
|
|
|
static struct {
|
2012-12-27 17:36:48 +00:00
|
|
|
const char *name;
|
2013-01-24 18:40:12 +00:00
|
|
|
const char *desc;
|
2012-10-17 18:02:35 +00:00
|
|
|
cksum_t cksum_id;
|
|
|
|
int bytes, mac_bytes;
|
|
|
|
ckinit_func_ptr init_func;
|
2013-01-26 10:09:10 +00:00
|
|
|
int compatible;
|
2012-10-17 18:02:35 +00:00
|
|
|
} cksum_props[] = {
|
2013-01-26 10:09:10 +00:00
|
|
|
{"CRC64", "Extremely Fast 64-bit CRC from LZMA SDK.",
|
|
|
|
CKSUM_CRC64, 8, 32, NULL, 0},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"SKEIN256", "256-bit SKEIN a NIST SHA3 runners-up (90% faster than Keccak).",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_SKEIN256, 32, 32, NULL, 1},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"SKEIN512", "512-bit SKEIN",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_SKEIN512, 64, 64, NULL, 1},
|
|
|
|
{"SHA256", "SHA512/256 version of Intel's optimized (SSE,AVX) SHA2 for x86.",
|
|
|
|
CKSUM_SHA256, 32, 32, init_sha512, 0},
|
|
|
|
{"SHA512", "SHA512 version of Intel's optimized (SSE,AVX) SHA2 for x86.",
|
|
|
|
CKSUM_SHA512, 64, 64, init_sha512, 0},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"KECCAK256", "Official 256-bit NIST SHA3 optimized implementation.",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_KECCAK256, 32, 32, NULL, 0},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"KECCAK512", "Official 512-bit NIST SHA3 optimized implementation.",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_KECCAK512, 64, 64, NULL, 0},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"BLAKE256", "Very fast 256-bit BLAKE2, derived from the NIST SHA3 runner-up BLAKE.",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_BLAKE256, 32, 32, init_blake2, 0},
|
2013-01-24 18:40:12 +00:00
|
|
|
{"BLAKE512", "Very fast 256-bit BLAKE2, derived from the NIST SHA3 runner-up BLAKE.",
|
2013-01-26 10:09:10 +00:00
|
|
|
CKSUM_BLAKE512, 64, 64, init_blake2, 0}
|
2012-10-17 18:02:35 +00:00
|
|
|
};
|
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
static int cksum_provider = PROVIDER_OPENSSL;
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-12-09 04:45:06 +00:00
|
|
|
extern uint64_t lzma_crc64(const uint8_t *buf, uint64_t size, uint64_t crc);
|
|
|
|
extern uint64_t lzma_crc64_8bchk(const uint8_t *buf, uint64_t size,
|
2012-10-17 18:02:35 +00:00
|
|
|
uint64_t crc, uint64_t *cnt);
|
|
|
|
|
2013-01-04 18:46:15 +00:00
|
|
|
#ifdef __OSSL_OLD__
|
2013-01-27 18:17:55 +00:00
|
|
|
/*
|
|
|
|
* The two functions below fill missing functionality in older versions of OpenSSL.
|
|
|
|
*/
|
2013-01-04 18:46:15 +00:00
|
|
|
int
|
|
|
|
HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
|
|
|
|
{
|
|
|
|
if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
|
|
|
|
return (0);
|
|
|
|
if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
|
|
|
|
return (0);
|
|
|
|
if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
|
|
|
|
dctx->key_length = sctx->key_length;
|
|
|
|
dctx->md = sctx->md;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
|
|
|
|
const unsigned char *salt, int saltlen, int iter,
|
|
|
|
const EVP_MD *digest,
|
|
|
|
int keylen, unsigned char *out)
|
|
|
|
{
|
|
|
|
unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
|
|
|
|
int cplen, j, k, tkeylen, mdlen;
|
|
|
|
unsigned long i = 1;
|
|
|
|
HMAC_CTX hctx;
|
|
|
|
|
|
|
|
mdlen = EVP_MD_size(digest);
|
|
|
|
if (mdlen < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
HMAC_CTX_init(&hctx);
|
|
|
|
p = out;
|
|
|
|
tkeylen = keylen;
|
|
|
|
if(!pass)
|
|
|
|
passlen = 0;
|
|
|
|
else if(passlen == -1)
|
|
|
|
passlen = strlen(pass);
|
|
|
|
while(tkeylen)
|
|
|
|
{
|
|
|
|
if(tkeylen > mdlen)
|
|
|
|
cplen = mdlen;
|
|
|
|
else
|
|
|
|
cplen = tkeylen;
|
|
|
|
/* We are unlikely to ever use more than 256 blocks (5120 bits!)
|
|
|
|
* but just in case...
|
|
|
|
*/
|
|
|
|
itmp[0] = (unsigned char)((i >> 24) & 0xff);
|
|
|
|
itmp[1] = (unsigned char)((i >> 16) & 0xff);
|
|
|
|
itmp[2] = (unsigned char)((i >> 8) & 0xff);
|
|
|
|
itmp[3] = (unsigned char)(i & 0xff);
|
|
|
|
HMAC_Init_ex(&hctx, pass, passlen, digest, NULL);
|
|
|
|
HMAC_Update(&hctx, salt, saltlen);
|
|
|
|
HMAC_Update(&hctx, itmp, 4);
|
|
|
|
HMAC_Final(&hctx, digtmp, NULL);
|
|
|
|
memcpy(p, digtmp, cplen);
|
|
|
|
for(j = 1; j < iter; j++)
|
|
|
|
{
|
|
|
|
HMAC(digest, pass, passlen,
|
|
|
|
digtmp, mdlen, digtmp, NULL);
|
|
|
|
for(k = 0; k < cplen; k++)
|
|
|
|
p[k] ^= digtmp[k];
|
|
|
|
}
|
|
|
|
tkeylen-= cplen;
|
2013-01-20 16:32:26 +00:00
|
|
|
++i;
|
2013-01-04 18:46:15 +00:00
|
|
|
p+= cplen;
|
|
|
|
}
|
|
|
|
HMAC_CTX_cleanup(&hctx);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-04 16:26:07 +00:00
|
|
|
int
|
|
|
|
get_crypto_alg(char *name)
|
|
|
|
{
|
|
|
|
if (name[0] == 0 || name[1] == 0 || name[2] == 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (strncmp(name, "AES", 3) == 0) {
|
|
|
|
return (CRYPTO_ALG_AES);
|
|
|
|
} else {
|
|
|
|
if (name[3] == 0 || name[4] == 0 || name[5] == 0 || name[6] == 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (strncmp(name, "SALSA20", 3) == 0) {
|
|
|
|
return (CRYPTO_ALG_SALSA20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-01-27 18:17:55 +00:00
|
|
|
/*
|
|
|
|
* Compute a digest of the given data segment. The parameter mt indicates whether
|
|
|
|
* to use the parallel(OpenMP) versions. Parallel versions are only used when
|
|
|
|
* a single segment is used to hold the entire file - essentially a single-threaded
|
|
|
|
* compression.
|
|
|
|
* In other cases segments are handled in separate threads any way and we do not
|
|
|
|
* need or want another level of parallelism to cause contention.
|
|
|
|
*/
|
2012-10-17 18:02:35 +00:00
|
|
|
int
|
2013-03-24 17:51:17 +00:00
|
|
|
compute_checksum(uchar_t *cksum_buf, int cksum, uchar_t *buf, uint64_t bytes, int mt, int verbose)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
2012-12-29 18:13:41 +00:00
|
|
|
DEBUG_STAT_EN(double strt, en);
|
|
|
|
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
assert(mt == 0 || mt == 1 || mt == 2);
|
|
|
|
#else
|
|
|
|
assert(mt == 0 || mt == 1);
|
|
|
|
#endif
|
|
|
|
|
2013-03-24 17:51:17 +00:00
|
|
|
DEBUG_STAT_EN(if (verbose) strt = get_wtime_millis());
|
2012-10-17 18:02:35 +00:00
|
|
|
if (cksum == CKSUM_CRC64) {
|
|
|
|
uint64_t *ck = (uint64_t *)cksum_buf;
|
|
|
|
*ck = lzma_crc64(buf, bytes, 0);
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
} else if (cksum == CKSUM_BLAKE256) {
|
2013-01-26 12:58:13 +00:00
|
|
|
if (!mt) {
|
|
|
|
if (bdsp.blake2b(cksum_buf, buf, NULL, 32, bytes, 0) != 0)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if (bdsp.blake2bp(cksum_buf, buf, NULL, 32, bytes, 0) != 0)
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-01-26 10:09:10 +00:00
|
|
|
} else if (cksum == CKSUM_BLAKE512) {
|
2013-01-26 12:58:13 +00:00
|
|
|
if (!mt) {
|
|
|
|
if (bdsp.blake2b(cksum_buf, buf, NULL, 64, bytes, 0) != 0)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if (bdsp.blake2bp(cksum_buf, buf, NULL, 64, bytes, 0) != 0)
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-01-27 18:17:55 +00:00
|
|
|
/*
|
|
|
|
* No parallelism for SKEIN. It is deprecated and retained here only for
|
|
|
|
* backwards compatiblity.
|
|
|
|
*/
|
2012-10-17 18:02:35 +00:00
|
|
|
} else if (cksum == CKSUM_SKEIN256) {
|
|
|
|
Skein_512_Ctxt_t ctx;
|
|
|
|
|
|
|
|
Skein_512_Init(&ctx, 256);
|
|
|
|
Skein_512_Update(&ctx, buf, bytes);
|
|
|
|
Skein_512_Final(&ctx, cksum_buf);
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN512) {
|
|
|
|
Skein_512_Ctxt_t ctx;
|
|
|
|
|
|
|
|
Skein_512_Init(&ctx, 512);
|
|
|
|
Skein_512_Update(&ctx, buf, bytes);
|
|
|
|
Skein_512_Final(&ctx, cksum_buf);
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SHA256) {
|
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt)
|
|
|
|
ossl_SHA256(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
else if (mt == 1)
|
2013-01-27 18:17:55 +00:00
|
|
|
ossl_SHA256_par(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
else
|
|
|
|
ossl_SHA256_par_old(cksum_buf, buf, bytes);
|
|
|
|
#endif
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt)
|
|
|
|
opt_SHA512t256(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
else if (mt == 1)
|
2013-01-27 18:17:55 +00:00
|
|
|
opt_SHA512t256_par(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
else
|
|
|
|
opt_SHA512t256_par_old(cksum_buf, buf, bytes);
|
|
|
|
#endif
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
} else if (cksum == CKSUM_SHA512) {
|
2013-01-25 17:25:55 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt)
|
|
|
|
ossl_SHA512(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
else if (mt == 1)
|
2013-01-27 18:17:55 +00:00
|
|
|
ossl_SHA512_par(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
else
|
|
|
|
ossl_SHA512_par_old(cksum_buf, buf, bytes);
|
|
|
|
#endif
|
2013-01-25 17:25:55 +00:00
|
|
|
} else {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt)
|
|
|
|
opt_SHA512(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
else if (mt == 1)
|
2013-01-27 18:17:55 +00:00
|
|
|
opt_SHA512_par(cksum_buf, buf, bytes);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
else
|
|
|
|
opt_SHA512_par_old(cksum_buf, buf, bytes);
|
|
|
|
#endif
|
2013-01-25 17:25:55 +00:00
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
} else if (cksum == CKSUM_KECCAK256) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt) {
|
|
|
|
if (Keccak256(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
2013-02-24 14:35:16 +00:00
|
|
|
} else if (mt == 1) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (Keccak256_par(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
} else {
|
|
|
|
if (Keccak256_par_old(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
|
|
|
#endif
|
2013-01-27 18:17:55 +00:00
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
} else if (cksum == CKSUM_KECCAK512) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (!mt) {
|
|
|
|
if (Keccak512(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
2013-02-24 14:35:16 +00:00
|
|
|
} else if (mt == 1) {
|
2013-01-27 18:17:55 +00:00
|
|
|
if (Keccak512_par(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
2013-02-24 14:35:16 +00:00
|
|
|
#ifdef __HASH_COMPATIBILITY_
|
|
|
|
} else {
|
|
|
|
if (Keccak512_par_old(cksum_buf, buf, bytes) != 0)
|
|
|
|
return (-1);
|
|
|
|
#endif
|
2013-01-27 18:17:55 +00:00
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-03-24 17:51:17 +00:00
|
|
|
DEBUG_STAT_EN(if (verbose) en = get_wtime_millis());
|
|
|
|
DEBUG_STAT_EN(if (verbose) fprintf(stderr, "Checksum computed at %.3f MB/s\n", get_mb_s(bytes, strt, en)));
|
2012-10-17 18:02:35 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-25 17:25:55 +00:00
|
|
|
init_sha512(void)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
cksum_provider = PROVIDER_OPENSSL;
|
|
|
|
#else
|
|
|
|
#ifdef __x86_64__
|
|
|
|
cksum_provider = PROVIDER_OPENSSL;
|
2013-01-24 18:40:12 +00:00
|
|
|
if (proc_info.proc_type == PROC_X64_INTEL || proc_info.proc_type == PROC_X64_AMD) {
|
2013-01-25 17:25:55 +00:00
|
|
|
if (opt_Init_SHA512(&proc_info) == 0) {
|
2012-10-17 18:02:35 +00:00
|
|
|
cksum_provider = PROVIDER_X64_OPT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
static void
|
|
|
|
init_blake2(void)
|
|
|
|
{
|
|
|
|
blake2_module_init(&bdsp, &proc_info);
|
|
|
|
}
|
|
|
|
|
2013-01-24 18:40:12 +00:00
|
|
|
void
|
|
|
|
list_checksums(FILE *strm, char *pad)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<(sizeof (cksum_props)/sizeof (cksum_props[0])); i++) {
|
2013-01-26 10:09:10 +00:00
|
|
|
if (!cksum_props[i].compatible)
|
|
|
|
fprintf(strm, "%s%10s - %s\n", pad, cksum_props[i].name, cksum_props[i].desc);
|
2013-01-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
/*
|
2012-11-18 14:50:16 +00:00
|
|
|
* Check if either the given checksum name or id is valid and
|
2012-10-17 18:02:35 +00:00
|
|
|
* return it's properties.
|
|
|
|
*/
|
|
|
|
int
|
2013-01-26 10:09:10 +00:00
|
|
|
get_checksum_props(const char *name, int *cksum, int *cksum_bytes,
|
|
|
|
int *mac_bytes, int accept_comptible)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-01-24 18:40:12 +00:00
|
|
|
for (i=0; i<(sizeof (cksum_props)/sizeof (cksum_props[0])); i++) {
|
2012-10-17 18:02:35 +00:00
|
|
|
if ((name != NULL && strcmp(name, cksum_props[i].name) == 0) ||
|
|
|
|
(*cksum != 0 && *cksum == cksum_props[i].cksum_id)) {
|
2013-01-26 10:09:10 +00:00
|
|
|
if (!accept_comptible && cksum_props[i].compatible)
|
|
|
|
break;
|
2012-10-17 18:02:35 +00:00
|
|
|
*cksum = cksum_props[i].cksum_id;
|
|
|
|
*cksum_bytes = cksum_props[i].bytes;
|
|
|
|
*mac_bytes = cksum_props[i].mac_bytes;
|
|
|
|
if (cksum_props[i].init_func)
|
|
|
|
cksum_props[i].init_func();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Endian independent way of storing the checksum bytes. This is actually
|
|
|
|
* storing in little endian format and a copy can be avoided in x86 land.
|
|
|
|
* However unsightly ifdefs are avoided here since this is not so performance
|
|
|
|
* critical.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
serialize_checksum(uchar_t *checksum, uchar_t *buf, int cksum_bytes)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (i=cksum_bytes; i>0; i--) {
|
|
|
|
buf[j] = checksum[i-1];
|
2013-01-20 16:32:26 +00:00
|
|
|
++j;
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
deserialize_checksum(uchar_t *checksum, uchar_t *buf, int cksum_bytes)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (i=cksum_bytes; i>0; i--) {
|
|
|
|
checksum[i-1] = buf[j];
|
2013-01-20 16:32:26 +00:00
|
|
|
++j;
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-24 18:23:07 +00:00
|
|
|
/*
|
2013-01-27 18:17:55 +00:00
|
|
|
* Perform keyed hashing. With Skein/Blake/Keccak, HMAC is not used, rather
|
|
|
|
* their native MAC features are used which are more optimal than HMAC.
|
2012-11-24 18:23:07 +00:00
|
|
|
*/
|
2012-10-17 18:02:35 +00:00
|
|
|
int
|
|
|
|
hmac_init(mac_ctx_t *mctx, int cksum, crypto_ctx_t *cctx)
|
|
|
|
{
|
|
|
|
mctx->mac_cksum = cksum;
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
if (cksum == CKSUM_BLAKE256) {
|
|
|
|
blake2b_state *ctx = (blake2b_state *)malloc(sizeof (blake2b_state));
|
|
|
|
if (!ctx) return (-1);
|
2013-03-04 16:26:07 +00:00
|
|
|
if (bdsp.blake2b_init_key(ctx, 32, cctx->pkey, cctx->keylen) != 0)
|
2013-01-26 10:09:10 +00:00
|
|
|
return (-1);
|
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
ctx = (blake2b_state *)malloc(sizeof (blake2b_state));
|
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (blake2b_state));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_BLAKE512) {
|
|
|
|
blake2b_state *ctx = (blake2b_state *)malloc(sizeof (blake2b_state));
|
|
|
|
if (!ctx) return (-1);
|
2013-03-04 16:26:07 +00:00
|
|
|
if (bdsp.blake2b_init_key(ctx, 64, cctx->pkey, cctx->keylen) != 0)
|
2013-01-26 10:09:10 +00:00
|
|
|
return (-1);
|
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
ctx = (blake2b_state *)malloc(sizeof (blake2b_state));
|
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (blake2b_state));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN256) {
|
2012-12-27 17:36:48 +00:00
|
|
|
Skein_512_Ctxt_t *ctx = (Skein_512_Ctxt_t *)malloc(sizeof (Skein_512_Ctxt_t));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) return (-1);
|
|
|
|
Skein_512_InitExt(ctx, 256, SKEIN_CFG_TREE_INFO_SEQUENTIAL,
|
2013-03-04 16:26:07 +00:00
|
|
|
cctx->pkey, cctx->keylen);
|
2012-10-17 18:02:35 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
2012-12-27 17:36:48 +00:00
|
|
|
ctx = (Skein_512_Ctxt_t *)malloc(sizeof (Skein_512_Ctxt_t));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (Skein_512_Ctxt_t));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN512) {
|
2012-12-27 17:36:48 +00:00
|
|
|
Skein_512_Ctxt_t *ctx = (Skein_512_Ctxt_t *)malloc(sizeof (Skein_512_Ctxt_t));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) return (-1);
|
|
|
|
Skein_512_InitExt(ctx, 512, SKEIN_CFG_TREE_INFO_SEQUENTIAL,
|
2013-03-04 16:26:07 +00:00
|
|
|
cctx->pkey, cctx->keylen);
|
2012-10-17 18:02:35 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
2012-12-27 17:36:48 +00:00
|
|
|
ctx = (Skein_512_Ctxt_t *)malloc(sizeof (Skein_512_Ctxt_t));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (Skein_512_Ctxt_t));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SHA256 || cksum == CKSUM_CRC64) {
|
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2012-12-27 17:36:48 +00:00
|
|
|
HMAC_CTX *ctx = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) return (-1);
|
|
|
|
HMAC_CTX_init(ctx);
|
2013-03-04 16:26:07 +00:00
|
|
|
HMAC_Init_ex(ctx, cctx->pkey, cctx->keylen, EVP_sha256(), NULL);
|
2012-10-17 18:02:35 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
ctx = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));
|
2012-10-17 18:02:35 +00:00
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
if (!HMAC_CTX_copy(ctx, (HMAC_CTX *)(mctx->mac_ctx))) {
|
2012-10-17 18:02:35 +00:00
|
|
|
free(ctx);
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
} else {
|
2013-01-25 17:25:55 +00:00
|
|
|
HMAC_SHA512_Context *ctx = (HMAC_SHA512_Context *)malloc(sizeof (HMAC_SHA512_Context));
|
|
|
|
if (!ctx) return (-1);
|
2013-03-04 16:26:07 +00:00
|
|
|
opt_HMAC_SHA512t256_Init(ctx, cctx->pkey, cctx->keylen);
|
2013-01-25 17:25:55 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
|
|
|
|
ctx = (HMAC_SHA512_Context *)malloc(sizeof (HMAC_SHA512_Context));
|
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (HMAC_SHA512_Context));
|
2012-10-17 18:02:35 +00:00
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
}
|
|
|
|
} else if (cksum == CKSUM_SHA512) {
|
2013-01-25 17:25:55 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
|
|
|
HMAC_CTX *ctx = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));
|
|
|
|
if (!ctx) return (-1);
|
|
|
|
HMAC_CTX_init(ctx);
|
2013-03-04 16:26:07 +00:00
|
|
|
HMAC_Init_ex(ctx, cctx->pkey, cctx->keylen, EVP_sha512(), NULL);
|
2013-01-25 17:25:55 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2013-01-25 17:25:55 +00:00
|
|
|
ctx = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));
|
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (!HMAC_CTX_copy(ctx, (HMAC_CTX *)(mctx->mac_ctx))) {
|
|
|
|
free(ctx);
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
|
|
|
} else {
|
|
|
|
HMAC_SHA512_Context *ctx = (HMAC_SHA512_Context *)malloc(sizeof (HMAC_SHA512_Context));
|
|
|
|
if (!ctx) return (-1);
|
2013-03-04 16:26:07 +00:00
|
|
|
opt_HMAC_SHA512_Init(ctx, cctx->pkey, cctx->keylen);
|
2013-01-25 17:25:55 +00:00
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
|
|
|
|
ctx = (HMAC_SHA512_Context *)malloc(sizeof (HMAC_SHA512_Context));
|
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (HMAC_SHA512_Context));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
|
|
|
|
} else if (cksum == CKSUM_KECCAK256 || cksum == CKSUM_KECCAK512) {
|
2012-12-27 17:36:48 +00:00
|
|
|
hashState *ctx = (hashState *)malloc(sizeof (hashState));
|
2012-12-08 08:49:01 +00:00
|
|
|
if (!ctx) return (-1);
|
|
|
|
|
|
|
|
if (cksum == CKSUM_KECCAK256) {
|
|
|
|
if (Keccak_Init(ctx, 256) != 0)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if (Keccak_Init(ctx, 512) != 0)
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-03-04 16:26:07 +00:00
|
|
|
if (Keccak_Update(ctx, cctx->pkey, cctx->keylen << 3) != 0)
|
2012-12-08 08:49:01 +00:00
|
|
|
return (-1);
|
|
|
|
mctx->mac_ctx = ctx;
|
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
ctx = (hashState *)malloc(sizeof (hashState));
|
2012-12-08 08:49:01 +00:00
|
|
|
if (!ctx) {
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memcpy(ctx, mctx->mac_ctx, sizeof (hashState));
|
|
|
|
mctx->mac_ctx_reinit = ctx;
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
hmac_reinit(mac_ctx_t *mctx)
|
|
|
|
{
|
|
|
|
int cksum = mctx->mac_cksum;
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
if (cksum == CKSUM_BLAKE256 || cksum == CKSUM_BLAKE512) {
|
|
|
|
memcpy(mctx->mac_ctx, mctx->mac_ctx_reinit, sizeof (blake2b_state));
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN256 || cksum == CKSUM_SKEIN512) {
|
2012-10-17 18:02:35 +00:00
|
|
|
memcpy(mctx->mac_ctx, mctx->mac_ctx_reinit, sizeof (Skein_512_Ctxt_t));
|
|
|
|
|
2013-01-25 17:25:55 +00:00
|
|
|
} else if (cksum == CKSUM_SHA256 || cksum == CKSUM_SHA512 || cksum == CKSUM_CRC64) {
|
2012-10-17 18:02:35 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2012-12-27 17:36:48 +00:00
|
|
|
HMAC_CTX_copy((HMAC_CTX *)(mctx->mac_ctx),
|
|
|
|
(HMAC_CTX *)(mctx->mac_ctx_reinit));
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2013-01-25 17:25:55 +00:00
|
|
|
memcpy(mctx->mac_ctx, mctx->mac_ctx_reinit, sizeof (HMAC_SHA512_Context));
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
} else if (cksum == CKSUM_KECCAK256 || cksum == CKSUM_KECCAK512) {
|
|
|
|
memcpy(mctx->mac_ctx, mctx->mac_ctx_reinit, sizeof (hashState));
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
hmac_update(mac_ctx_t *mctx, uchar_t *data, uint64_t len)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
int cksum = mctx->mac_cksum;
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
if (cksum == CKSUM_BLAKE256 || cksum == CKSUM_BLAKE512) {
|
|
|
|
bdsp.blake2b_update((blake2b_state *)(mctx->mac_ctx), (uint8_t *)data, len);
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN256 || cksum == CKSUM_SKEIN512) {
|
2012-12-27 17:36:48 +00:00
|
|
|
Skein_512_Update((Skein_512_Ctxt_t *)(mctx->mac_ctx), data, len);
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SHA256 || cksum == CKSUM_CRC64) {
|
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2013-01-04 18:46:15 +00:00
|
|
|
#ifndef __OSSL_OLD__
|
2012-12-27 17:36:48 +00:00
|
|
|
if (HMAC_Update((HMAC_CTX *)(mctx->mac_ctx), data, len) == 0)
|
2012-10-17 18:02:35 +00:00
|
|
|
return (-1);
|
2013-01-04 18:46:15 +00:00
|
|
|
#else
|
|
|
|
HMAC_Update((HMAC_CTX *)(mctx->mac_ctx), data, len);
|
|
|
|
#endif
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2013-01-25 17:25:55 +00:00
|
|
|
opt_HMAC_SHA512t256_Update((HMAC_SHA512_Context *)(mctx->mac_ctx), data, len);
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
} else if (cksum == CKSUM_SHA512) {
|
2013-01-25 17:25:55 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2013-01-04 18:46:15 +00:00
|
|
|
#ifndef __OSSL_OLD__
|
2013-01-25 17:25:55 +00:00
|
|
|
if (HMAC_Update((HMAC_CTX *)(mctx->mac_ctx), data, len) == 0)
|
|
|
|
return (-1);
|
2013-01-04 18:46:15 +00:00
|
|
|
#else
|
2013-01-25 17:25:55 +00:00
|
|
|
HMAC_Update((HMAC_CTX *)(mctx->mac_ctx), data, len);
|
2013-01-04 18:46:15 +00:00
|
|
|
#endif
|
2013-01-25 17:25:55 +00:00
|
|
|
} else {
|
|
|
|
opt_HMAC_SHA512_Update((HMAC_SHA512_Context *)(mctx->mac_ctx), data, len);
|
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
|
|
|
|
} else if (cksum == CKSUM_KECCAK256 || cksum == CKSUM_KECCAK512) {
|
|
|
|
// Keccak takes data length in bits so we have to scale
|
|
|
|
while (len > KECCAK_MAX_SEG) {
|
|
|
|
uint64_t blen;
|
|
|
|
|
|
|
|
blen = KECCAK_MAX_SEG;
|
2012-12-27 17:36:48 +00:00
|
|
|
if (Keccak_Update((hashState *)(mctx->mac_ctx), data, blen << 3) != 0)
|
2012-12-08 08:49:01 +00:00
|
|
|
return (-1);
|
|
|
|
len -= KECCAK_MAX_SEG;
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
if (Keccak_Update((hashState *)(mctx->mac_ctx), data, len << 3) != 0)
|
2012-12-08 08:49:01 +00:00
|
|
|
return (-1);
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
hmac_final(mac_ctx_t *mctx, uchar_t *hash, unsigned int *len)
|
|
|
|
{
|
|
|
|
int cksum = mctx->mac_cksum;
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
if (cksum == CKSUM_BLAKE256) {
|
|
|
|
bdsp.blake2b_final((blake2b_state *)(mctx->mac_ctx), hash, 32);
|
|
|
|
*len = 32;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_BLAKE512) {
|
|
|
|
bdsp.blake2b_final((blake2b_state *)(mctx->mac_ctx), hash, 64);
|
|
|
|
*len = 64;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN256) {
|
2012-12-27 17:36:48 +00:00
|
|
|
Skein_512_Final((Skein_512_Ctxt_t *)(mctx->mac_ctx), hash);
|
2012-10-17 18:02:35 +00:00
|
|
|
*len = 32;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN512) {
|
2012-12-27 17:36:48 +00:00
|
|
|
Skein_512_Final((Skein_512_Ctxt_t *)(mctx->mac_ctx), hash);
|
2012-10-17 18:02:35 +00:00
|
|
|
*len = 64;
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SHA256 || cksum == CKSUM_CRC64) {
|
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2012-12-27 17:36:48 +00:00
|
|
|
HMAC_Final((HMAC_CTX *)(mctx->mac_ctx), hash, len);
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2013-01-25 17:25:55 +00:00
|
|
|
opt_HMAC_SHA512t256_Final((HMAC_SHA512_Context *)(mctx->mac_ctx), hash);
|
2012-10-17 18:02:35 +00:00
|
|
|
*len = 32;
|
|
|
|
}
|
|
|
|
} else if (cksum == CKSUM_SHA512) {
|
2013-01-25 17:25:55 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
|
|
|
HMAC_Final((HMAC_CTX *)(mctx->mac_ctx), hash, len);
|
|
|
|
} else {
|
|
|
|
opt_HMAC_SHA512_Final((HMAC_SHA512_Context *)(mctx->mac_ctx), hash);
|
|
|
|
*len = 64;
|
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
} else if (cksum == CKSUM_KECCAK256 || cksum == CKSUM_KECCAK512) {
|
2012-12-27 17:36:48 +00:00
|
|
|
if (Keccak_Final((hashState *)(mctx->mac_ctx), hash) != 0)
|
2012-12-08 08:49:01 +00:00
|
|
|
return (-1);
|
|
|
|
if (cksum == CKSUM_KECCAK256)
|
|
|
|
*len = 32;
|
|
|
|
else
|
|
|
|
*len = 64;
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
hmac_cleanup(mac_ctx_t *mctx)
|
|
|
|
{
|
|
|
|
int cksum = mctx->mac_cksum;
|
|
|
|
|
2013-01-26 10:09:10 +00:00
|
|
|
if (cksum == CKSUM_BLAKE256 || cksum == CKSUM_BLAKE512) {
|
|
|
|
memset(mctx->mac_ctx, 0, sizeof (blake2b_state));
|
|
|
|
memset(mctx->mac_ctx_reinit, 0, sizeof (blake2b_state));
|
|
|
|
|
|
|
|
} else if (cksum == CKSUM_SKEIN256 || cksum == CKSUM_SKEIN512) {
|
2012-10-17 18:02:35 +00:00
|
|
|
memset(mctx->mac_ctx, 0, sizeof (Skein_512_Ctxt_t));
|
|
|
|
memset(mctx->mac_ctx_reinit, 0, sizeof (Skein_512_Ctxt_t));
|
|
|
|
|
2013-01-25 17:25:55 +00:00
|
|
|
} else if (cksum == CKSUM_SHA256 || cksum == CKSUM_SHA512 || cksum == CKSUM_CRC64) {
|
2012-10-17 18:02:35 +00:00
|
|
|
if (cksum_provider == PROVIDER_OPENSSL) {
|
2012-12-27 17:36:48 +00:00
|
|
|
HMAC_CTX_cleanup((HMAC_CTX *)(mctx->mac_ctx));
|
|
|
|
HMAC_CTX_cleanup((HMAC_CTX *)(mctx->mac_ctx_reinit));
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2013-01-25 17:25:55 +00:00
|
|
|
memset(mctx->mac_ctx, 0, sizeof (HMAC_SHA512_Context));
|
|
|
|
memset(mctx->mac_ctx_reinit, 0, sizeof (HMAC_SHA512_Context));
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
2012-12-08 08:49:01 +00:00
|
|
|
} else if (cksum == CKSUM_KECCAK256 || cksum == CKSUM_KECCAK512) {
|
|
|
|
memset(mctx->mac_ctx, 0, sizeof (hashState));
|
|
|
|
memset(mctx->mac_ctx_reinit, 0, sizeof (hashState));
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
mctx->mac_cksum = 0;
|
|
|
|
free(mctx->mac_ctx);
|
|
|
|
free(mctx->mac_ctx_reinit);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-01-27 18:17:55 +00:00
|
|
|
/*
|
|
|
|
* Encryption related functions.
|
|
|
|
*/
|
2012-10-17 18:02:35 +00:00
|
|
|
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)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
2013-03-04 16:26:07 +00:00
|
|
|
if (crypto_alg == CRYPTO_ALG_AES || crypto_alg == CRYPTO_ALG_SALSA20) {
|
|
|
|
aes_ctx_t *actx;
|
|
|
|
salsa20_ctx_t *sctx;
|
|
|
|
|
|
|
|
/* Silence compiler warnings */
|
|
|
|
actx = NULL;
|
|
|
|
sctx = NULL;
|
|
|
|
|
|
|
|
if (crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
actx = (aes_ctx_t *)malloc(sizeof (aes_ctx_t));
|
|
|
|
actx->keylen = keylen;
|
|
|
|
cctx->pkey = actx->pkey;
|
|
|
|
aes_module_init(&proc_info);
|
|
|
|
} else {
|
|
|
|
sctx = (salsa20_ctx_t *)malloc(sizeof (salsa20_ctx_t));
|
|
|
|
sctx->keylen = keylen;
|
|
|
|
cctx->pkey = sctx->pkey;
|
|
|
|
}
|
2013-03-03 14:32:14 +00:00
|
|
|
cctx->keylen = keylen;
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
if (enc_dec) {
|
|
|
|
/*
|
|
|
|
* Encryption init.
|
|
|
|
*/
|
2012-12-27 17:36:48 +00:00
|
|
|
cctx->salt = (uchar_t *)malloc(32);
|
2012-10-17 18:02:35 +00:00
|
|
|
salt = cctx->salt;
|
|
|
|
cctx->saltlen = 32;
|
|
|
|
if (RAND_status() != 1 || RAND_bytes(salt, 32) != 1) {
|
2013-03-04 16:26:07 +00:00
|
|
|
if (geturandom_bytes(salt, 32) != 0) {
|
2012-10-17 18:02:35 +00:00
|
|
|
uchar_t sb[64];
|
|
|
|
int b;
|
|
|
|
struct timespec tp;
|
|
|
|
|
|
|
|
b = 0;
|
|
|
|
/* No good random pool is populated/available. What to do ? */
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
|
|
|
|
time((time_t *)&sb[b]);
|
|
|
|
b += 8;
|
|
|
|
} else {
|
|
|
|
uint64_t v;
|
|
|
|
v = tp.tv_sec * 1000UL + tp.tv_nsec;
|
|
|
|
*((uint64_t *)&sb[b]) = v;
|
|
|
|
b += 8;
|
|
|
|
}
|
|
|
|
*((uint32_t *)&sb[b]) = rand();
|
|
|
|
b += 4;
|
|
|
|
*((uint32_t *)&sb[b]) = getpid();
|
|
|
|
b += 4;
|
2013-03-24 17:51:17 +00:00
|
|
|
compute_checksum(&sb[b], CKSUM_SHA256, sb, b, 0, 0);
|
2012-10-17 18:02:35 +00:00
|
|
|
b = 8 + 4;
|
|
|
|
*((uint32_t *)&sb[b]) = rand();
|
2013-03-24 17:51:17 +00:00
|
|
|
compute_checksum(salt, CKSUM_SHA256, &sb[b], 32 + 4, 0, 0);
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Zero nonce (arg #6) since it will be generated.
|
|
|
|
*/
|
2013-03-04 16:26:07 +00:00
|
|
|
if (crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
if (aes_init(actx, salt, 32, pwd, pwd_len, 0, enc_dec) != 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize AES context\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (salsa20_init(sctx, salt, 32, pwd, pwd_len, 0, enc_dec) != 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize SALSA20 context\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Decryption init.
|
|
|
|
* Pass given nonce and salt.
|
|
|
|
*/
|
|
|
|
if (saltlen > MAX_SALTLEN) {
|
|
|
|
fprintf(stderr, "Salt too long. Max allowed length is %d\n",
|
|
|
|
MAX_SALTLEN);
|
2013-02-15 17:23:17 +00:00
|
|
|
free(actx);
|
2012-10-17 18:02:35 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
cctx->salt = (uchar_t *)malloc(saltlen);
|
2012-10-17 18:02:35 +00:00
|
|
|
memcpy(cctx->salt, salt, saltlen);
|
|
|
|
|
2013-03-04 16:26:07 +00:00
|
|
|
if (crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
if (aes_init(actx, cctx->salt, saltlen, pwd, pwd_len, *((uint64_t *)nonce),
|
|
|
|
enc_dec) != 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize AES context\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (salsa20_init(sctx, salt, 32, pwd, pwd_len, nonce, enc_dec) != 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize SALSA20 context\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-04 16:26:07 +00:00
|
|
|
if (crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
cctx->crypto_ctx = actx;
|
|
|
|
} else {
|
|
|
|
cctx->crypto_ctx = sctx;
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
cctx->crypto_alg = crypto_alg;
|
|
|
|
cctx->enc_dec = enc_dec;
|
2013-03-05 15:16:18 +00:00
|
|
|
actx = NULL;
|
|
|
|
sctx = NULL;
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unrecognized algorithm code: %d\n", crypto_alg);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-02 17:26:21 +00:00
|
|
|
crypto_buf(crypto_ctx_t *cctx, uchar_t *from, uchar_t *to, uint64_t bytes, uint64_t id)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
if (cctx->crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
if (cctx->enc_dec == ENCRYPT_FLAG) {
|
2012-12-27 17:36:48 +00:00
|
|
|
return (aes_encrypt((aes_ctx_t *)(cctx->crypto_ctx), from, to, bytes, id));
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
2012-12-27 17:36:48 +00:00
|
|
|
return (aes_decrypt((aes_ctx_t *)(cctx->crypto_ctx), from, to, bytes, id));
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
2013-03-04 16:26:07 +00:00
|
|
|
} else if (cctx->crypto_alg == CRYPTO_ALG_SALSA20) {
|
|
|
|
if (cctx->enc_dec == ENCRYPT_FLAG) {
|
|
|
|
return (salsa20_encrypt((salsa20_ctx_t *)(cctx->crypto_ctx), from, to, bytes, id));
|
|
|
|
} else {
|
|
|
|
return (salsa20_decrypt((salsa20_ctx_t *)(cctx->crypto_ctx), from, to, bytes, id));
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unrecognized algorithm code: %d\n", cctx->crypto_alg);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:26:07 +00:00
|
|
|
uchar_t *
|
2012-10-17 18:02:35 +00:00
|
|
|
crypto_nonce(crypto_ctx_t *cctx)
|
|
|
|
{
|
2013-03-04 16:26:07 +00:00
|
|
|
if (cctx->crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
return (aes_nonce((aes_ctx_t *)(cctx->crypto_ctx)));
|
|
|
|
}
|
|
|
|
return (salsa20_nonce((salsa20_ctx_t *)(cctx->crypto_ctx)));
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
crypto_clean_pkey(crypto_ctx_t *cctx)
|
|
|
|
{
|
2013-03-04 16:26:07 +00:00
|
|
|
if (cctx->crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
aes_clean_pkey((aes_ctx_t *)(cctx->crypto_ctx));
|
|
|
|
} else {
|
|
|
|
salsa20_clean_pkey((salsa20_ctx_t *)(cctx->crypto_ctx));
|
|
|
|
}
|
|
|
|
cctx->pkey = NULL;
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup_crypto(crypto_ctx_t *cctx)
|
|
|
|
{
|
2013-03-04 16:26:07 +00:00
|
|
|
if (cctx->crypto_alg == CRYPTO_ALG_AES) {
|
|
|
|
aes_cleanup((aes_ctx_t *)(cctx->crypto_ctx));
|
|
|
|
} else {
|
|
|
|
salsa20_cleanup((salsa20_ctx_t *)(cctx->crypto_ctx));
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
memset(cctx->salt, 0, 32);
|
|
|
|
free(cctx->salt);
|
|
|
|
free(cctx);
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:26:07 +00:00
|
|
|
int
|
|
|
|
geturandom_bytes(uchar_t *rbytes, int buflen)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t lenread;
|
2012-10-17 18:02:35 +00:00
|
|
|
uchar_t * buf = rbytes;
|
|
|
|
|
2013-03-04 16:26:07 +00:00
|
|
|
/* Open /dev/urandom. Upto 10 retries. */
|
|
|
|
fd = -1;
|
|
|
|
lenread = 1;
|
|
|
|
while (fd == -1 && lenread < 10) {
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) != -1)
|
|
|
|
break;
|
|
|
|
lenread++;
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if (fd == -1)
|
2012-10-17 18:02:35 +00:00
|
|
|
goto err0;
|
|
|
|
|
|
|
|
/* Read bytes until we have filled the buffer. */
|
|
|
|
while (buflen > 0) {
|
|
|
|
if ((lenread = read(fd, buf, buflen)) == -1)
|
|
|
|
goto err1;
|
|
|
|
|
|
|
|
/* The random device should never EOF. */
|
|
|
|
if (lenread == 0)
|
|
|
|
goto err1;
|
|
|
|
|
|
|
|
/* We're partly done. */
|
|
|
|
buf += lenread;
|
|
|
|
buflen -= lenread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the device. */
|
|
|
|
while (close(fd) == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
goto err0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
return (0);
|
|
|
|
err1:
|
|
|
|
close(fd);
|
|
|
|
err0:
|
|
|
|
/* Failure! */
|
|
|
|
return (4);
|
|
|
|
}
|
|
|
|
|
2013-01-27 18:17:55 +00:00
|
|
|
/*
|
|
|
|
* Input password string from terminal without echoing.
|
|
|
|
*/
|
2012-10-17 18:02:35 +00:00
|
|
|
int
|
2012-12-27 17:36:48 +00:00
|
|
|
get_pw_string(uchar_t pw[MAX_PW_LEN], const char *prompt, int twice)
|
2012-10-17 18:02:35 +00:00
|
|
|
{
|
|
|
|
int fd, len;
|
|
|
|
FILE *input, *strm;
|
|
|
|
struct termios oldt, newt;
|
2012-12-27 17:36:48 +00:00
|
|
|
char pw1[MAX_PW_LEN], pw2[MAX_PW_LEN], *s;
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
// Try TTY first
|
|
|
|
fd = open("/dev/tty", O_RDWR | O_NOCTTY);
|
|
|
|
if (fd != -1) {
|
|
|
|
input = fdopen(fd, "w+");
|
|
|
|
strm = input;
|
|
|
|
} else {
|
|
|
|
// Fall back to stdin
|
|
|
|
fd = STDIN_FILENO;
|
|
|
|
input = stdin;
|
|
|
|
strm = stderr;
|
|
|
|
}
|
|
|
|
tcgetattr(fd, &oldt);
|
|
|
|
newt = oldt;
|
|
|
|
newt.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(fd, TCSANOW, &newt);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: ", prompt);
|
|
|
|
fflush(stderr);
|
|
|
|
s = fgets(pw1, MAX_PW_LEN, input);
|
|
|
|
fputs("\n", stderr);
|
|
|
|
|
2013-06-02 15:24:33 +00:00
|
|
|
if (s == NULL || strlen(s) < 2) {
|
2012-10-17 18:02:35 +00:00
|
|
|
tcsetattr(fd, TCSANOW, &oldt);
|
|
|
|
fflush(strm);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
if (twice) {
|
|
|
|
fprintf(stderr, "%s (once more): ", prompt);
|
|
|
|
fflush(stderr);
|
|
|
|
s = fgets(pw2, MAX_PW_LEN, input);
|
|
|
|
tcsetattr(fd, TCSANOW, &oldt);
|
|
|
|
fflush(strm);
|
|
|
|
fputs("\n", stderr);
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
if (strcmp(pw1, pw2) != 0) {
|
|
|
|
fprintf(stderr, "Passwords do not match!\n");
|
|
|
|
memset(pw1, 0, MAX_PW_LEN);
|
|
|
|
memset(pw2, 0, MAX_PW_LEN);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tcsetattr(fd, TCSANOW, &oldt);
|
|
|
|
fflush(strm);
|
|
|
|
fputs("\n", stderr);
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(pw1);
|
|
|
|
pw1[len-1] = '\0';
|
2012-12-27 17:36:48 +00:00
|
|
|
strcpy((char *)pw, (const char *)pw1);
|
2012-10-17 18:02:35 +00:00
|
|
|
memset(pw1, 0, MAX_PW_LEN);
|
|
|
|
memset(pw2, 0, MAX_PW_LEN);
|
|
|
|
return (len);
|
|
|
|
}
|