mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
101
thirdparty/libtomcrypt/stream/chacha/chacha_crypt.c
vendored
Normal file
101
thirdparty/libtomcrypt/stream/chacha/chacha_crypt.c
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
#define QUARTERROUND(a,b,c,d) \
|
||||
x[a] += x[b]; x[d] = ROL(x[d] ^ x[a], 16); \
|
||||
x[c] += x[d]; x[b] = ROL(x[b] ^ x[c], 12); \
|
||||
x[a] += x[b]; x[d] = ROL(x[d] ^ x[a], 8); \
|
||||
x[c] += x[d]; x[b] = ROL(x[b] ^ x[c], 7);
|
||||
|
||||
static void _chacha_block(unsigned char *output, const ulong32 *input, int rounds)
|
||||
{
|
||||
ulong32 x[16];
|
||||
int i;
|
||||
XMEMCPY(x, input, sizeof(x));
|
||||
for (i = rounds; i > 0; i -= 2) {
|
||||
QUARTERROUND(0, 4, 8,12)
|
||||
QUARTERROUND(1, 5, 9,13)
|
||||
QUARTERROUND(2, 6,10,14)
|
||||
QUARTERROUND(3, 7,11,15)
|
||||
QUARTERROUND(0, 5,10,15)
|
||||
QUARTERROUND(1, 6,11,12)
|
||||
QUARTERROUND(2, 7, 8,13)
|
||||
QUARTERROUND(3, 4, 9,14)
|
||||
}
|
||||
for (i = 0; i < 16; ++i) {
|
||||
x[i] += input[i];
|
||||
STORE32L(x[i], output + 4 * i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Encrypt (or decrypt) bytes of ciphertext (or plaintext) with ChaCha
|
||||
@param st The ChaCha state
|
||||
@param in The plaintext (or ciphertext)
|
||||
@param inlen The length of the input (octets)
|
||||
@param out [out] The ciphertext (or plaintext), length inlen
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
unsigned long i, j;
|
||||
|
||||
if (inlen == 0) return CRYPT_OK; /* nothing to do */
|
||||
|
||||
LTC_ARGCHK(st != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(st->ivlen != 0);
|
||||
|
||||
if (st->ksleft > 0) {
|
||||
j = MIN(st->ksleft, inlen);
|
||||
for (i = 0; i < j; ++i, st->ksleft--) out[i] = in[i] ^ st->kstream[64 - st->ksleft];
|
||||
inlen -= j;
|
||||
if (inlen == 0) return CRYPT_OK;
|
||||
out += j;
|
||||
in += j;
|
||||
}
|
||||
for (;;) {
|
||||
_chacha_block(buf, st->input, st->rounds);
|
||||
if (st->ivlen == 8) {
|
||||
/* IV-64bit, increment 64bit counter */
|
||||
if (0 == ++st->input[12] && 0 == ++st->input[13]) return CRYPT_OVERFLOW;
|
||||
}
|
||||
else {
|
||||
/* IV-96bit, increment 32bit counter */
|
||||
if (0 == ++st->input[12]) return CRYPT_OVERFLOW;
|
||||
}
|
||||
if (inlen <= 64) {
|
||||
for (i = 0; i < inlen; ++i) out[i] = in[i] ^ buf[i];
|
||||
st->ksleft = 64 - inlen;
|
||||
for (i = inlen; i < 64; ++i) st->kstream[i] = buf[i];
|
||||
return CRYPT_OK;
|
||||
}
|
||||
for (i = 0; i < 64; ++i) out[i] = in[i] ^ buf[i];
|
||||
inlen -= 64;
|
||||
out += 64;
|
||||
in += 64;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
30
thirdparty/libtomcrypt/stream/chacha/chacha_done.c
vendored
Normal file
30
thirdparty/libtomcrypt/stream/chacha/chacha_done.c
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
/**
|
||||
Terminate and clear ChaCha state
|
||||
@param st The ChaCha state
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int chacha_done(chacha_state *st)
|
||||
{
|
||||
LTC_ARGCHK(st != NULL);
|
||||
XMEMSET(st, 0, sizeof(chacha_state));
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
47
thirdparty/libtomcrypt/stream/chacha/chacha_ivctr32.c
vendored
Normal file
47
thirdparty/libtomcrypt/stream/chacha/chacha_ivctr32.c
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
/**
|
||||
Set IV + counter data to the ChaCha state
|
||||
@param st The ChaCha20 state
|
||||
@param iv The IV data to add
|
||||
@param ivlen The length of the IV (must be 12)
|
||||
@param counter 32bit (unsigned) initial counter value
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int chacha_ivctr32(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong32 counter)
|
||||
{
|
||||
LTC_ARGCHK(st != NULL);
|
||||
LTC_ARGCHK(iv != NULL);
|
||||
/* 96bit IV + 32bit counter */
|
||||
LTC_ARGCHK(ivlen == 12);
|
||||
|
||||
st->input[12] = counter;
|
||||
LOAD32L(st->input[13], iv + 0);
|
||||
LOAD32L(st->input[14], iv + 4);
|
||||
LOAD32L(st->input[15], iv + 8);
|
||||
st->ksleft = 0;
|
||||
st->ivlen = ivlen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
47
thirdparty/libtomcrypt/stream/chacha/chacha_ivctr64.c
vendored
Normal file
47
thirdparty/libtomcrypt/stream/chacha/chacha_ivctr64.c
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
/**
|
||||
Set IV + counter data to the ChaCha state
|
||||
@param st The ChaCha20 state
|
||||
@param iv The IV data to add
|
||||
@param ivlen The length of the IV (must be 8)
|
||||
@param counter 64bit (unsigned) initial counter value
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter)
|
||||
{
|
||||
LTC_ARGCHK(st != NULL);
|
||||
LTC_ARGCHK(iv != NULL);
|
||||
/* 64bit IV + 64bit counter */
|
||||
LTC_ARGCHK(ivlen == 8);
|
||||
|
||||
st->input[12] = (ulong32)(counter & 0xFFFFFFFF);
|
||||
st->input[13] = (ulong32)(counter >> 32);
|
||||
LOAD32L(st->input[14], iv + 0);
|
||||
LOAD32L(st->input[15], iv + 4);
|
||||
st->ksleft = 0;
|
||||
st->ivlen = ivlen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
38
thirdparty/libtomcrypt/stream/chacha/chacha_keystream.c
vendored
Normal file
38
thirdparty/libtomcrypt/stream/chacha/chacha_keystream.c
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
/**
|
||||
Generate a stream of random bytes via ChaCha
|
||||
@param st The ChaCha20 state
|
||||
@param out [out] The output buffer
|
||||
@param outlen The output length
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int chacha_keystream(chacha_state *st, unsigned char *out, unsigned long outlen)
|
||||
{
|
||||
if (outlen == 0) return CRYPT_OK; /* nothing to do */
|
||||
LTC_ARGCHK(out != NULL);
|
||||
XMEMSET(out, 0, outlen);
|
||||
return chacha_crypt(st, out, outlen, out);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
67
thirdparty/libtomcrypt/stream/chacha/chacha_setup.c
vendored
Normal file
67
thirdparty/libtomcrypt/stream/chacha/chacha_setup.c
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
static const char * const sigma = "expand 32-byte k";
|
||||
static const char * const tau = "expand 16-byte k";
|
||||
|
||||
/**
|
||||
Initialize an ChaCha context (only the key)
|
||||
@param st [out] The destination of the ChaCha state
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param rounds Number of rounds (e.g. 20 for ChaCha20)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds)
|
||||
{
|
||||
const char *constants;
|
||||
|
||||
LTC_ARGCHK(st != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(keylen == 32 || keylen == 16);
|
||||
|
||||
if (rounds == 0) rounds = 20;
|
||||
|
||||
LOAD32L(st->input[4], key + 0);
|
||||
LOAD32L(st->input[5], key + 4);
|
||||
LOAD32L(st->input[6], key + 8);
|
||||
LOAD32L(st->input[7], key + 12);
|
||||
if (keylen == 32) { /* 256bit */
|
||||
key += 16;
|
||||
constants = sigma;
|
||||
} else { /* 128bit */
|
||||
constants = tau;
|
||||
}
|
||||
LOAD32L(st->input[8], key + 0);
|
||||
LOAD32L(st->input[9], key + 4);
|
||||
LOAD32L(st->input[10], key + 8);
|
||||
LOAD32L(st->input[11], key + 12);
|
||||
LOAD32L(st->input[0], constants + 0);
|
||||
LOAD32L(st->input[1], constants + 4);
|
||||
LOAD32L(st->input[2], constants + 8);
|
||||
LOAD32L(st->input[3], constants + 12);
|
||||
st->rounds = rounds; /* e.g. 20 for chacha20 */
|
||||
st->ivlen = 0; /* will be set later by chacha_ivctr(32|64) */
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
71
thirdparty/libtomcrypt/stream/chacha/chacha_test.c
vendored
Normal file
71
thirdparty/libtomcrypt/stream/chacha/chacha_test.c
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* The implementation is based on:
|
||||
* chacha-ref.c version 20080118
|
||||
* Public domain from D. J. Bernstein
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
int chacha_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
unsigned long len;
|
||||
unsigned char out[1000];
|
||||
/* https://tools.ietf.org/html/rfc7539#section-2.4.2 */
|
||||
unsigned char k[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
|
||||
unsigned char n[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00 };
|
||||
unsigned char ct[] = { 0x6E, 0x2E, 0x35, 0x9A, 0x25, 0x68, 0xF9, 0x80, 0x41, 0xBA, 0x07, 0x28, 0xDD, 0x0D, 0x69, 0x81,
|
||||
0xE9, 0x7E, 0x7A, 0xEC, 0x1D, 0x43, 0x60, 0xC2, 0x0A, 0x27, 0xAF, 0xCC, 0xFD, 0x9F, 0xAE, 0x0B,
|
||||
0xF9, 0x1B, 0x65, 0xC5, 0x52, 0x47, 0x33, 0xAB, 0x8F, 0x59, 0x3D, 0xAB, 0xCD, 0x62, 0xB3, 0x57,
|
||||
0x16, 0x39, 0xD6, 0x24, 0xE6, 0x51, 0x52, 0xAB, 0x8F, 0x53, 0x0C, 0x35, 0x9F, 0x08, 0x61, 0xD8,
|
||||
0x07, 0xCA, 0x0D, 0xBF, 0x50, 0x0D, 0x6A, 0x61, 0x56, 0xA3, 0x8E, 0x08, 0x8A, 0x22, 0xB6, 0x5E,
|
||||
0x52, 0xBC, 0x51, 0x4D, 0x16, 0xCC, 0xF8, 0x06, 0x81, 0x8C, 0xE9, 0x1A, 0xB7, 0x79, 0x37, 0x36,
|
||||
0x5A, 0xF9, 0x0B, 0xBF, 0x74, 0xA3, 0x5B, 0xE6, 0xB4, 0x0B, 0x8E, 0xED, 0xF2, 0x78, 0x5E, 0x42,
|
||||
0x87, 0x4D };
|
||||
char pt[] = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.";
|
||||
chacha_state st;
|
||||
int err;
|
||||
|
||||
len = strlen(pt);
|
||||
/* crypt piece by piece */
|
||||
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_ivctr32(&st, n, sizeof(n), 1)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt, 35, out)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt + 35, 35, out + 35)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt + 70, 5, out + 70)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt + 75, 5, out + 75)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt + 80, len - 80, out + 80)) != CRYPT_OK) return err;
|
||||
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV1", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
/* crypt in one go */
|
||||
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_ivctr32(&st, n, sizeof(n), 1)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
|
||||
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV2", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
/* crypt in one go - using chacha_ivctr64() */
|
||||
if ((err = chacha_setup(&st, k, sizeof(k), 20)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_ivctr64(&st, n + 4, sizeof(n) - 4, 1)) != CRYPT_OK) return err;
|
||||
if ((err = chacha_crypt(&st, (unsigned char*)pt, len, out)) != CRYPT_OK) return err;
|
||||
if (compare_testvector(out, len, ct, sizeof(ct), "CHACHA-TV3", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
Reference in New Issue
Block a user