release
This commit is contained in:
commit
cd5ffe60a5
BIN
Release/TeknoMW3C.dll
Normal file
BIN
Release/TeknoMW3C.dll
Normal file
Binary file not shown.
BIN
Release/ultimateteknotool.v2.dll
Normal file
BIN
Release/ultimateteknotool.v2.dll
Normal file
Binary file not shown.
BIN
Release/ultimateteknotool.v2.iobj
Normal file
BIN
Release/ultimateteknotool.v2.iobj
Normal file
Binary file not shown.
BIN
Release/ultimateteknotool.v2.ipdb
Normal file
BIN
Release/ultimateteknotool.v2.ipdb
Normal file
Binary file not shown.
BIN
Release/ultimateteknotool.v2.pdb
Normal file
BIN
Release/ultimateteknotool.v2.pdb
Normal file
Binary file not shown.
103
libtom/libtomcrypt/include/tomcrypt.h
Normal file
103
libtom/libtomcrypt/include/tomcrypt.h
Normal file
@ -0,0 +1,103 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#ifndef TOMCRYPT_H_
|
||||
#define TOMCRYPT_H_
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
/* use configuration data */
|
||||
#include <tomcrypt_custom.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* version */
|
||||
#define CRYPT 0x0118
|
||||
#define SCRYPT "1.18.0"
|
||||
|
||||
/* max size of either a cipher/hash block or symmetric key [largest of the two] */
|
||||
#define MAXBLOCKSIZE 128
|
||||
|
||||
/* descriptor table size */
|
||||
#define TAB_SIZE 32
|
||||
|
||||
/* error codes [will be expanded in future releases] */
|
||||
enum {
|
||||
CRYPT_OK=0, /* Result OK */
|
||||
CRYPT_ERROR, /* Generic Error */
|
||||
CRYPT_NOP, /* Not a failure but no operation was performed */
|
||||
|
||||
CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
|
||||
CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
|
||||
CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
|
||||
|
||||
CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
|
||||
CRYPT_INVALID_PACKET, /* Invalid input packet given */
|
||||
|
||||
CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
|
||||
CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
|
||||
|
||||
CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
|
||||
CRYPT_INVALID_HASH, /* Invalid hash specified */
|
||||
CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
|
||||
|
||||
CRYPT_MEM, /* Out of memory */
|
||||
|
||||
CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
|
||||
CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
|
||||
|
||||
CRYPT_INVALID_ARG, /* Generic invalid argument */
|
||||
CRYPT_FILE_NOTFOUND, /* File Not Found */
|
||||
|
||||
CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
|
||||
|
||||
CRYPT_OVERFLOW, /* An overflow of a value was detected/prevented */
|
||||
|
||||
CRYPT_UNUSED1, /* UNUSED1 */
|
||||
|
||||
CRYPT_INPUT_TOO_LONG, /* The input was longer than expected. */
|
||||
|
||||
CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
|
||||
|
||||
CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
|
||||
CRYPT_PK_INVALID_PADDING, /* Invalid padding on input */
|
||||
|
||||
CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */
|
||||
};
|
||||
|
||||
#include <tomcrypt_cfg.h>
|
||||
#include <tomcrypt_macros.h>
|
||||
#include <tomcrypt_cipher.h>
|
||||
#include <tomcrypt_hash.h>
|
||||
#include <tomcrypt_mac.h>
|
||||
#include <tomcrypt_prng.h>
|
||||
#include <tomcrypt_pk.h>
|
||||
#include <tomcrypt_math.h>
|
||||
#include <tomcrypt_misc.h>
|
||||
#include <tomcrypt_argchk.h>
|
||||
#include <tomcrypt_pkcs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TOMCRYPT_H_ */
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
53
libtom/libtomcrypt/include/tomcrypt_argchk.h
Normal file
53
libtom/libtomcrypt/include/tomcrypt_argchk.h
Normal file
@ -0,0 +1,53 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* Defines the LTC_ARGCHK macro used within the library */
|
||||
/* ARGTYPE is defined in tomcrypt_cfg.h */
|
||||
#if ARGTYPE == 0
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
/* this is the default LibTomCrypt macro */
|
||||
#if defined(__clang__) || defined(__GNUC_MINOR__)
|
||||
#define NORETURN __attribute__ ((noreturn))
|
||||
#else
|
||||
#define NORETURN
|
||||
#endif
|
||||
|
||||
void crypt_argchk(const char *v, const char *s, int d) NORETURN;
|
||||
#define LTC_ARGCHK(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0)
|
||||
#define LTC_ARGCHKVD(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0)
|
||||
|
||||
#elif ARGTYPE == 1
|
||||
|
||||
/* fatal type of error */
|
||||
#define LTC_ARGCHK(x) assert((x))
|
||||
#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
|
||||
|
||||
#elif ARGTYPE == 2
|
||||
|
||||
#define LTC_ARGCHK(x) if (!(x)) { fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); }
|
||||
#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
|
||||
|
||||
#elif ARGTYPE == 3
|
||||
|
||||
#define LTC_ARGCHK(x)
|
||||
#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
|
||||
|
||||
#elif ARGTYPE == 4
|
||||
|
||||
#define LTC_ARGCHK(x) if (!(x)) return CRYPT_INVALID_ARG;
|
||||
#define LTC_ARGCHKVD(x) if (!(x)) return;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
283
libtom/libtomcrypt/include/tomcrypt_cfg.h
Normal file
283
libtom/libtomcrypt/include/tomcrypt_cfg.h
Normal file
@ -0,0 +1,283 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* This is the build config file.
|
||||
*
|
||||
* With this you can setup what to inlcude/exclude automatically during any build. Just comment
|
||||
* out the line that #define's the word for the thing you want to remove. phew!
|
||||
*/
|
||||
|
||||
#ifndef TOMCRYPT_CFG_H
|
||||
#define TOMCRYPT_CFG_H
|
||||
|
||||
#if defined(_WIN32) || defined(_MSC_VER)
|
||||
#define LTC_CALL __cdecl
|
||||
#elif !defined(LTC_CALL)
|
||||
#define LTC_CALL
|
||||
#endif
|
||||
|
||||
#ifndef LTC_EXPORT
|
||||
#define LTC_EXPORT
|
||||
#endif
|
||||
|
||||
/* certain platforms use macros for these, making the prototypes broken */
|
||||
#ifndef LTC_NO_PROTOTYPES
|
||||
|
||||
/* you can change how memory allocation works ... */
|
||||
LTC_EXPORT void * LTC_CALL XMALLOC(size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XREALLOC(void *p, size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XCALLOC(size_t n, size_t s);
|
||||
LTC_EXPORT void LTC_CALL XFREE(void *p);
|
||||
|
||||
LTC_EXPORT void LTC_CALL XQSORT(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
|
||||
|
||||
|
||||
/* change the clock function too */
|
||||
LTC_EXPORT clock_t LTC_CALL XCLOCK(void);
|
||||
|
||||
/* various other functions */
|
||||
LTC_EXPORT void * LTC_CALL XMEMCPY(void *dest, const void *src, size_t n);
|
||||
LTC_EXPORT int LTC_CALL XMEMCMP(const void *s1, const void *s2, size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XMEMSET(void *s, int c, size_t n);
|
||||
|
||||
LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
|
||||
|
||||
#endif
|
||||
|
||||
/* some compilers do not like "inline" (or maybe "static inline"), namely: HP cc, IBM xlc */
|
||||
#if defined(__HP_cc) || defined(__xlc__)
|
||||
#define LTC_INLINE
|
||||
#elif defined(_MSC_VER)
|
||||
#define LTC_INLINE __inline
|
||||
#else
|
||||
#define LTC_INLINE inline
|
||||
#endif
|
||||
|
||||
/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
|
||||
#ifndef ARGTYPE
|
||||
#define ARGTYPE 0
|
||||
#endif
|
||||
|
||||
#undef LTC_ENCRYPT
|
||||
#define LTC_ENCRYPT 0
|
||||
#undef LTC_DECRYPT
|
||||
#define LTC_DECRYPT 1
|
||||
|
||||
/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower] code
|
||||
*
|
||||
* Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit read/writes.
|
||||
* The x86 platforms allow this but some others [ARM for instance] do not. On those platforms you **MUST**
|
||||
* use the portable [slower] macros.
|
||||
*/
|
||||
/* detect x86/i386 32bit */
|
||||
#if defined(__i386__) || defined(__i386) || defined(_M_IX86)
|
||||
#define ENDIAN_LITTLE
|
||||
#define ENDIAN_32BITWORD
|
||||
#define LTC_FAST
|
||||
#endif
|
||||
|
||||
/* detect amd64/x64 */
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
|
||||
#define ENDIAN_LITTLE
|
||||
#define ENDIAN_64BITWORD
|
||||
#define LTC_FAST
|
||||
#endif
|
||||
|
||||
/* detect PPC32 */
|
||||
#if defined(LTC_PPC32)
|
||||
#define ENDIAN_BIG
|
||||
#define ENDIAN_32BITWORD
|
||||
#define LTC_FAST
|
||||
#endif
|
||||
|
||||
/* detects MIPS R5900 processors (PS2) */
|
||||
#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips))
|
||||
#define ENDIAN_64BITWORD
|
||||
#if defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
|
||||
#define ENDIAN_BIG
|
||||
#endif
|
||||
#define ENDIAN_LITTLE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect AIX */
|
||||
#if defined(_AIX) && defined(_BIG_ENDIAN)
|
||||
#define ENDIAN_BIG
|
||||
#if defined(__LP64__) || defined(_ARCH_PPC64)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect HP-UX */
|
||||
#if defined(__hpux) || defined(__hpux__)
|
||||
#define ENDIAN_BIG
|
||||
#if defined(__ia64) || defined(__ia64__) || defined(__LP64__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect Apple OS X */
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if defined(__LITTLE_ENDIAN__) || defined(__x86_64__)
|
||||
#define ENDIAN_LITTLE
|
||||
#else
|
||||
#define ENDIAN_BIG
|
||||
#endif
|
||||
#if defined(__LP64__) || defined(__x86_64__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect SPARC and SPARC64 */
|
||||
#if defined(__sparc__) || defined(__sparc)
|
||||
#define ENDIAN_BIG
|
||||
#if defined(__arch64__) || defined(__sparcv9) || defined(__sparc_v9__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect IBM S390(x) */
|
||||
#if defined(__s390x__) || defined(__s390__)
|
||||
#define ENDIAN_BIG
|
||||
#if defined(__s390x__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* detect PPC64 */
|
||||
#if defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define ENDIAN_BIG
|
||||
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define ENDIAN_LITTLE
|
||||
#endif
|
||||
#define LTC_FAST
|
||||
#endif
|
||||
|
||||
/* endianness fallback */
|
||||
#if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
|
||||
#if defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN || \
|
||||
defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
||||
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
|
||||
defined(__BIG_ENDIAN__) || \
|
||||
defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
|
||||
defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
|
||||
#define ENDIAN_BIG
|
||||
#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN || \
|
||||
defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
||||
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
|
||||
defined(__LITTLE_ENDIAN__) || \
|
||||
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
|
||||
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
||||
#define ENDIAN_LITTLE
|
||||
#else
|
||||
#error Cannot detect endianness
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* ulong64: 64-bit data type */
|
||||
#ifdef _MSC_VER
|
||||
#define CONST64(n) n ## ui64
|
||||
typedef unsigned __int64 ulong64;
|
||||
#else
|
||||
#define CONST64(n) n ## ULL
|
||||
typedef unsigned long long ulong64;
|
||||
#endif
|
||||
|
||||
/* ulong32: "32-bit at least" data type */
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
|
||||
defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
|
||||
defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
|
||||
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
|
||||
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
|
||||
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
|
||||
typedef unsigned ulong32;
|
||||
#if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
|
||||
#define ENDIAN_64BITWORD
|
||||
#endif
|
||||
#else
|
||||
typedef unsigned long ulong32;
|
||||
#if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(ENDIAN_64BITWORD) && !defined(_MSC_VER)
|
||||
typedef unsigned long long ltc_mp_digit;
|
||||
#else
|
||||
typedef unsigned long ltc_mp_digit;
|
||||
#endif
|
||||
|
||||
/* No asm is a quick way to disable anything "not portable" */
|
||||
#ifdef LTC_NO_ASM
|
||||
#define ENDIAN_NEUTRAL
|
||||
#undef ENDIAN_32BITWORD
|
||||
#undef ENDIAN_64BITWORD
|
||||
#undef LTC_FAST
|
||||
#define LTC_NO_ROLC
|
||||
#define LTC_NO_BSWAP
|
||||
#endif
|
||||
|
||||
/* No LTC_FAST if: explicitly disabled OR non-gcc/non-clang compiler OR old gcc OR using -ansi -std=c99 */
|
||||
#if defined(LTC_NO_FAST) || (__GNUC__ < 4) || defined(__STRICT_ANSI__)
|
||||
#undef LTC_FAST
|
||||
#endif
|
||||
|
||||
#ifdef LTC_FAST
|
||||
#define LTC_FAST_TYPE_PTR_CAST(x) ((LTC_FAST_TYPE*)(void*)(x))
|
||||
#ifdef ENDIAN_64BITWORD
|
||||
typedef ulong64 __attribute__((__may_alias__)) LTC_FAST_TYPE;
|
||||
#else
|
||||
typedef ulong32 __attribute__((__may_alias__)) LTC_FAST_TYPE;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(ENDIAN_NEUTRAL) && (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
|
||||
#error You must specify a word size as well as endianess in tomcrypt_cfg.h
|
||||
#endif
|
||||
|
||||
#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
|
||||
#define ENDIAN_NEUTRAL
|
||||
#endif
|
||||
|
||||
#if (defined(ENDIAN_32BITWORD) && defined(ENDIAN_64BITWORD))
|
||||
#error Cannot be 32 and 64 bit words...
|
||||
#endif
|
||||
|
||||
/* gcc 4.3 and up has a bswap builtin; detect it by gcc version.
|
||||
* clang also supports the bswap builtin, and although clang pretends
|
||||
* to be gcc (macro-wise, anyway), clang pretends to be a version
|
||||
* prior to gcc 4.3, so we can't detect bswap that way. Instead,
|
||||
* clang has a __has_builtin mechanism that can be used to check
|
||||
* for builtins:
|
||||
* http://clang.llvm.org/docs/LanguageExtensions.html#feature_check */
|
||||
#ifndef __has_builtin
|
||||
#define __has_builtin(x) 0
|
||||
#endif
|
||||
#if !defined(LTC_NO_BSWAP) && defined(__GNUC__) && \
|
||||
((__GNUC__ * 100 + __GNUC_MINOR__ >= 403) || \
|
||||
(__has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)))
|
||||
#define LTC_HAVE_BSWAP_BUILTIN
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
1008
libtom/libtomcrypt/include/tomcrypt_cipher.h
Normal file
1008
libtom/libtomcrypt/include/tomcrypt_cipher.h
Normal file
File diff suppressed because it is too large
Load Diff
585
libtom/libtomcrypt/include/tomcrypt_custom.h
Normal file
585
libtom/libtomcrypt/include/tomcrypt_custom.h
Normal file
@ -0,0 +1,585 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#ifndef TOMCRYPT_CUSTOM_H_
|
||||
#define TOMCRYPT_CUSTOM_H_
|
||||
|
||||
#define LTM_DESC
|
||||
|
||||
|
||||
/* macros for various libc functions you can change for embedded targets */
|
||||
#ifndef XMALLOC
|
||||
#define XMALLOC malloc
|
||||
#endif
|
||||
#ifndef XREALLOC
|
||||
#define XREALLOC realloc
|
||||
#endif
|
||||
#ifndef XCALLOC
|
||||
#define XCALLOC calloc
|
||||
#endif
|
||||
#ifndef XFREE
|
||||
#define XFREE free
|
||||
#endif
|
||||
|
||||
#ifndef XMEMSET
|
||||
#define XMEMSET memset
|
||||
#endif
|
||||
#ifndef XMEMCPY
|
||||
#define XMEMCPY memcpy
|
||||
#endif
|
||||
#ifndef XMEMMOVE
|
||||
#define XMEMMOVE memmove
|
||||
#endif
|
||||
#ifndef XMEMCMP
|
||||
#define XMEMCMP memcmp
|
||||
#endif
|
||||
/* A memory compare function that has to run in constant time,
|
||||
* c.f. mem_neq() API summary.
|
||||
*/
|
||||
#ifndef XMEM_NEQ
|
||||
#define XMEM_NEQ mem_neq
|
||||
#endif
|
||||
#ifndef XSTRCMP
|
||||
#define XSTRCMP strcmp
|
||||
#endif
|
||||
|
||||
#ifndef XCLOCK
|
||||
#define XCLOCK clock
|
||||
#endif
|
||||
|
||||
#ifndef XQSORT
|
||||
#define XQSORT qsort
|
||||
#endif
|
||||
|
||||
/*#if ( defined(malloc) || defined(realloc) || defined(calloc) || defined(free) || \
|
||||
defined(memset) || defined(memcpy) || defined(memcmp) || defined(strcmp) || \
|
||||
defined(clock) || defined(qsort) ) && !defined(LTC_NO_PROTOTYPES)
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif*/
|
||||
#define LTC_NO_PROTOTYPES
|
||||
|
||||
/* shortcut to disable automatic inclusion */
|
||||
#if defined LTC_NOTHING && !defined LTC_EASY
|
||||
#define LTC_NO_CIPHERS
|
||||
#define LTC_NO_MODES
|
||||
#define LTC_NO_HASHES
|
||||
#define LTC_NO_MACS
|
||||
#define LTC_NO_PRNGS
|
||||
#define LTC_NO_PK
|
||||
#define LTC_NO_PKCS
|
||||
#define LTC_NO_MISC
|
||||
#endif /* LTC_NOTHING */
|
||||
|
||||
/* Easy button? */
|
||||
#ifdef LTC_EASY
|
||||
#define LTC_NO_CIPHERS
|
||||
#define LTC_RIJNDAEL
|
||||
#define LTC_BLOWFISH
|
||||
#define LTC_DES
|
||||
#define LTC_CAST5
|
||||
|
||||
#define LTC_NO_MODES
|
||||
#define LTC_ECB_MODE
|
||||
#define LTC_CBC_MODE
|
||||
#define LTC_CTR_MODE
|
||||
|
||||
#define LTC_NO_HASHES
|
||||
#define LTC_SHA1
|
||||
#define LTC_SHA3
|
||||
#define LTC_SHA512
|
||||
#define LTC_SHA384
|
||||
#define LTC_SHA256
|
||||
#define LTC_SHA224
|
||||
#define LTC_HASH_HELPERS
|
||||
|
||||
#define LTC_NO_MACS
|
||||
#define LTC_HMAC
|
||||
#define LTC_OMAC
|
||||
#define LTC_CCM_MODE
|
||||
|
||||
#define LTC_NO_PRNGS
|
||||
#define LTC_SPRNG
|
||||
#define LTC_YARROW
|
||||
#define LTC_DEVRANDOM
|
||||
#define LTC_TRY_URANDOM_FIRST
|
||||
#define LTC_RNG_GET_BYTES
|
||||
#define LTC_RNG_MAKE_PRNG
|
||||
|
||||
#define LTC_NO_PK
|
||||
#define LTC_MRSA
|
||||
#define LTC_MECC
|
||||
|
||||
#define LTC_NO_MISC
|
||||
#define LTC_BASE64
|
||||
#endif
|
||||
|
||||
/* The minimal set of functionality to run the tests */
|
||||
#ifdef LTC_MINIMAL
|
||||
#define LTC_RIJNDAEL
|
||||
#define LTC_SHA256
|
||||
#define LTC_YARROW
|
||||
#define LTC_CTR_MODE
|
||||
|
||||
#define LTC_RNG_MAKE_PRNG
|
||||
#define LTC_RNG_GET_BYTES
|
||||
#define LTC_DEVRANDOM
|
||||
#define LTC_TRY_URANDOM_FIRST
|
||||
|
||||
#undef LTC_NO_FILE
|
||||
#endif
|
||||
|
||||
/* Enable self-test test vector checking */
|
||||
#ifndef LTC_NO_TEST
|
||||
#define LTC_TEST
|
||||
#endif
|
||||
/* Enable extended self-tests */
|
||||
/* #define LTC_TEST_EXT */
|
||||
|
||||
/* Use small code where possible */
|
||||
/* #define LTC_SMALL_CODE */
|
||||
|
||||
/* clean the stack of functions which put private information on stack */
|
||||
/* #define LTC_CLEAN_STACK */
|
||||
|
||||
/* disable all file related functions */
|
||||
/* #define LTC_NO_FILE */
|
||||
|
||||
/* disable all forms of ASM */
|
||||
/* #define LTC_NO_ASM */
|
||||
|
||||
/* disable FAST mode */
|
||||
/* #define LTC_NO_FAST */
|
||||
|
||||
/* disable BSWAP on x86 */
|
||||
/* #define LTC_NO_BSWAP */
|
||||
|
||||
/* ---> math provider? <--- */
|
||||
#ifndef LTC_NO_MATH
|
||||
|
||||
/* LibTomMath */
|
||||
/* #define LTM_DESC */
|
||||
|
||||
/* TomsFastMath */
|
||||
/* #define TFM_DESC */
|
||||
|
||||
/* GNU Multiple Precision Arithmetic Library */
|
||||
/* #define GMP_DESC */
|
||||
|
||||
#endif /* LTC_NO_MATH */
|
||||
|
||||
/* ---> Symmetric Block Ciphers <--- */
|
||||
#ifndef LTC_NO_CIPHERS
|
||||
|
||||
#define LTC_BLOWFISH
|
||||
#define LTC_RC2
|
||||
#define LTC_RC5
|
||||
#define LTC_RC6
|
||||
#define LTC_SAFERP
|
||||
#define LTC_RIJNDAEL
|
||||
#define LTC_XTEA
|
||||
/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
|
||||
* (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
|
||||
#define LTC_TWOFISH
|
||||
#ifndef LTC_NO_TABLES
|
||||
#define LTC_TWOFISH_TABLES
|
||||
/* #define LTC_TWOFISH_ALL_TABLES */
|
||||
#else
|
||||
#define LTC_TWOFISH_SMALL
|
||||
#endif
|
||||
/* #define LTC_TWOFISH_SMALL */
|
||||
/* LTC_DES includes EDE triple-DES */
|
||||
#define LTC_DES
|
||||
#define LTC_CAST5
|
||||
#define LTC_NOEKEON
|
||||
#define LTC_SKIPJACK
|
||||
#define LTC_SAFER
|
||||
#define LTC_KHAZAD
|
||||
#define LTC_ANUBIS
|
||||
#define LTC_ANUBIS_TWEAK
|
||||
#define LTC_KSEED
|
||||
#define LTC_KASUMI
|
||||
#define LTC_MULTI2
|
||||
#define LTC_CAMELLIA
|
||||
|
||||
/* stream ciphers */
|
||||
#define LTC_CHACHA
|
||||
#define LTC_RC4_STREAM
|
||||
#define LTC_SOBER128_STREAM
|
||||
|
||||
#endif /* LTC_NO_CIPHERS */
|
||||
|
||||
|
||||
/* ---> Block Cipher Modes of Operation <--- */
|
||||
#ifndef LTC_NO_MODES
|
||||
|
||||
#define LTC_CFB_MODE
|
||||
#define LTC_OFB_MODE
|
||||
#define LTC_ECB_MODE
|
||||
#define LTC_CBC_MODE
|
||||
#define LTC_CTR_MODE
|
||||
|
||||
/* F8 chaining mode */
|
||||
#define LTC_F8_MODE
|
||||
|
||||
/* LRW mode */
|
||||
#define LTC_LRW_MODE
|
||||
#ifndef LTC_NO_TABLES
|
||||
/* like GCM mode this will enable 16 8x128 tables [64KB] that make
|
||||
* seeking very fast.
|
||||
*/
|
||||
#define LTC_LRW_TABLES
|
||||
#endif
|
||||
|
||||
/* XTS mode */
|
||||
#define LTC_XTS_MODE
|
||||
|
||||
#endif /* LTC_NO_MODES */
|
||||
|
||||
/* ---> One-Way Hash Functions <--- */
|
||||
#ifndef LTC_NO_HASHES
|
||||
|
||||
#define LTC_CHC_HASH
|
||||
#define LTC_WHIRLPOOL
|
||||
#define LTC_SHA3
|
||||
#define LTC_SHA512
|
||||
#define LTC_SHA512_256
|
||||
#define LTC_SHA512_224
|
||||
#define LTC_SHA384
|
||||
#define LTC_SHA256
|
||||
#define LTC_SHA224
|
||||
#define LTC_TIGER
|
||||
#define LTC_SHA1
|
||||
#define LTC_MD5
|
||||
#define LTC_MD4
|
||||
#define LTC_MD2
|
||||
#define LTC_RIPEMD128
|
||||
#define LTC_RIPEMD160
|
||||
#define LTC_RIPEMD256
|
||||
#define LTC_RIPEMD320
|
||||
#define LTC_BLAKE2S
|
||||
#define LTC_BLAKE2B
|
||||
|
||||
#define LTC_HASH_HELPERS
|
||||
|
||||
#endif /* LTC_NO_HASHES */
|
||||
|
||||
|
||||
/* ---> MAC functions <--- */
|
||||
#ifndef LTC_NO_MACS
|
||||
|
||||
#define LTC_HMAC
|
||||
#define LTC_OMAC
|
||||
#define LTC_PMAC
|
||||
#define LTC_XCBC
|
||||
#define LTC_F9_MODE
|
||||
#define LTC_PELICAN
|
||||
#define LTC_POLY1305
|
||||
#define LTC_BLAKE2SMAC
|
||||
#define LTC_BLAKE2BMAC
|
||||
|
||||
/* ---> Encrypt + Authenticate Modes <--- */
|
||||
|
||||
#define LTC_EAX_MODE
|
||||
|
||||
#define LTC_OCB_MODE
|
||||
#define LTC_OCB3_MODE
|
||||
#define LTC_CCM_MODE
|
||||
#define LTC_GCM_MODE
|
||||
#define LTC_CHACHA20POLY1305_MODE
|
||||
|
||||
/* Use 64KiB tables */
|
||||
#ifndef LTC_NO_TABLES
|
||||
#define LTC_GCM_TABLES
|
||||
#endif
|
||||
|
||||
/* USE SSE2? requires GCC works on x86_32 and x86_64*/
|
||||
#ifdef LTC_GCM_TABLES
|
||||
/* #define LTC_GCM_TABLES_SSE2 */
|
||||
#endif
|
||||
|
||||
#endif /* LTC_NO_MACS */
|
||||
|
||||
|
||||
/* --> Pseudo Random Number Generators <--- */
|
||||
#ifndef LTC_NO_PRNGS
|
||||
|
||||
/* Yarrow */
|
||||
#define LTC_YARROW
|
||||
|
||||
/* a PRNG that simply reads from an available system source */
|
||||
#define LTC_SPRNG
|
||||
|
||||
/* The RC4 stream cipher based PRNG */
|
||||
#define LTC_RC4
|
||||
|
||||
/* The ChaCha20 stream cipher based PRNG */
|
||||
#define LTC_CHACHA20_PRNG
|
||||
|
||||
/* Fortuna PRNG */
|
||||
#define LTC_FORTUNA
|
||||
|
||||
/* Greg's SOBER128 stream cipher based PRNG */
|
||||
#define LTC_SOBER128
|
||||
|
||||
/* the *nix style /dev/random device */
|
||||
#define LTC_DEVRANDOM
|
||||
/* try /dev/urandom before trying /dev/random
|
||||
* are you sure you want to disable this? http://www.2uo.de/myths-about-urandom/ */
|
||||
#define LTC_TRY_URANDOM_FIRST
|
||||
/* rng_get_bytes() */
|
||||
#define LTC_RNG_GET_BYTES
|
||||
/* rng_make_prng() */
|
||||
#define LTC_RNG_MAKE_PRNG
|
||||
|
||||
/* enable the ltc_rng hook to integrate e.g. embedded hardware RNG's easily */
|
||||
/* #define LTC_PRNG_ENABLE_LTC_RNG */
|
||||
|
||||
#endif /* LTC_NO_PRNGS */
|
||||
|
||||
#ifdef LTC_YARROW
|
||||
|
||||
/* which descriptor of AES to use? */
|
||||
/* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */
|
||||
#ifdef ENCRYPT_ONLY
|
||||
#define LTC_YARROW_AES 0
|
||||
#else
|
||||
#define LTC_YARROW_AES 2
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
|
||||
#ifndef LTC_FORTUNA_WD
|
||||
/* reseed every N calls to the read function */
|
||||
#define LTC_FORTUNA_WD 10
|
||||
#endif
|
||||
|
||||
#ifndef LTC_FORTUNA_POOLS
|
||||
/* number of pools (4..32) can save a bit of ram by lowering the count */
|
||||
#define LTC_FORTUNA_POOLS 32
|
||||
#endif
|
||||
|
||||
#endif /* LTC_FORTUNA */
|
||||
|
||||
|
||||
/* ---> Public Key Crypto <--- */
|
||||
#ifndef LTC_NO_PK
|
||||
|
||||
/* Include RSA support */
|
||||
#define LTC_MRSA
|
||||
|
||||
/* Include Diffie-Hellman support */
|
||||
/* is_prime fails for GMP */
|
||||
#define LTC_MDH
|
||||
/* Supported Key Sizes */
|
||||
#define LTC_DH768
|
||||
#define LTC_DH1024
|
||||
#define LTC_DH1536
|
||||
#define LTC_DH2048
|
||||
|
||||
#ifndef TFM_DESC
|
||||
/* tfm has a problem in fp_isprime for larger key sizes */
|
||||
#define LTC_DH3072
|
||||
#define LTC_DH4096
|
||||
#define LTC_DH6144
|
||||
#define LTC_DH8192
|
||||
#endif
|
||||
|
||||
/* Include Katja (a Rabin variant like RSA) */
|
||||
/* #define LTC_MKAT */
|
||||
|
||||
/* Digital Signature Algorithm */
|
||||
#define LTC_MDSA
|
||||
|
||||
/* ECC */
|
||||
#define LTC_MECC
|
||||
|
||||
/* use Shamir's trick for point mul (speeds up signature verification) */
|
||||
#define LTC_ECC_SHAMIR
|
||||
|
||||
#if defined(TFM_DESC) && defined(LTC_MECC)
|
||||
#define LTC_MECC_ACCEL
|
||||
#endif
|
||||
|
||||
/* do we want fixed point ECC */
|
||||
/* #define LTC_MECC_FP */
|
||||
|
||||
#endif /* LTC_NO_PK */
|
||||
|
||||
#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_BLINDING)
|
||||
/* Enable RSA blinding when doing private key operations by default */
|
||||
#define LTC_RSA_BLINDING
|
||||
#endif /* LTC_NO_RSA_BLINDING */
|
||||
|
||||
#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_CRT_HARDENING)
|
||||
/* Enable RSA CRT hardening when doing private key operations by default */
|
||||
#define LTC_RSA_CRT_HARDENING
|
||||
#endif /* LTC_NO_RSA_CRT_HARDENING */
|
||||
|
||||
#if defined(LTC_MECC) && !defined(LTC_NO_ECC_TIMING_RESISTANT)
|
||||
/* Enable ECC timing resistant version by default */
|
||||
#define LTC_ECC_TIMING_RESISTANT
|
||||
#endif
|
||||
|
||||
/* PKCS #1 (RSA) and #5 (Password Handling) stuff */
|
||||
#ifndef LTC_NO_PKCS
|
||||
|
||||
#define LTC_PKCS_1
|
||||
#define LTC_PKCS_5
|
||||
|
||||
/* Include ASN.1 DER (required by DSA/RSA) */
|
||||
#define LTC_DER
|
||||
|
||||
#endif /* LTC_NO_PKCS */
|
||||
|
||||
/* misc stuff */
|
||||
#ifndef LTC_NO_MISC
|
||||
|
||||
/* Various tidbits of modern neatoness */
|
||||
#define LTC_BASE64
|
||||
/* ... and it's URL safe version */
|
||||
#define LTC_BASE64_URL
|
||||
|
||||
/* Keep LTC_NO_HKDF for compatibility reasons
|
||||
* superseeded by LTC_NO_MISC*/
|
||||
#ifndef LTC_NO_HKDF
|
||||
/* HKDF Key Derivation/Expansion stuff */
|
||||
#define LTC_HKDF
|
||||
#endif /* LTC_NO_HKDF */
|
||||
|
||||
#define LTC_ADLER32
|
||||
|
||||
#define LTC_CRC32
|
||||
|
||||
#endif /* LTC_NO_MISC */
|
||||
|
||||
/* cleanup */
|
||||
|
||||
#ifdef LTC_MECC
|
||||
/* Supported ECC Key Sizes */
|
||||
#ifndef LTC_NO_CURVES
|
||||
#define LTC_ECC112
|
||||
#define LTC_ECC128
|
||||
#define LTC_ECC160
|
||||
#define LTC_ECC192
|
||||
#define LTC_ECC224
|
||||
#define LTC_ECC256
|
||||
#define LTC_ECC384
|
||||
#define LTC_ECC521
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT)
|
||||
/* Include the MPI functionality? (required by the PK algorithms) */
|
||||
#define LTC_MPI
|
||||
|
||||
#ifndef LTC_PK_MAX_RETRIES
|
||||
/* iterations limit for retry-loops */
|
||||
#define LTC_PK_MAX_RETRIES 20
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MRSA
|
||||
#define LTC_PKCS_1
|
||||
#endif
|
||||
|
||||
#if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
|
||||
#error Pelican-MAC requires LTC_RIJNDAEL
|
||||
#endif
|
||||
|
||||
#if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC))
|
||||
#error LTC_EAX_MODE requires CTR and LTC_OMAC mode
|
||||
#endif
|
||||
|
||||
#if defined(LTC_YARROW) && !defined(LTC_CTR_MODE)
|
||||
#error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined!
|
||||
#endif
|
||||
|
||||
#if defined(LTC_DER) && !defined(LTC_MPI)
|
||||
#error ASN.1 DER requires MPI functionality
|
||||
#endif
|
||||
|
||||
#if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(LTC_MKAT)) && !defined(LTC_DER)
|
||||
#error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
|
||||
#endif
|
||||
|
||||
#if defined(LTC_CHACHA20POLY1305_MODE) && (!defined(LTC_CHACHA) || !defined(LTC_POLY1305))
|
||||
#error LTC_CHACHA20POLY1305_MODE requires LTC_CHACHA + LTC_POLY1305
|
||||
#endif
|
||||
|
||||
#if defined(LTC_CHACHA20_PRNG) && !defined(LTC_CHACHA)
|
||||
#error LTC_CHACHA20_PRNG requires LTC_CHACHA
|
||||
#endif
|
||||
|
||||
#if defined(LTC_RC4) && !defined(LTC_RC4_STREAM)
|
||||
#error LTC_RC4 requires LTC_RC4_STREAM
|
||||
#endif
|
||||
|
||||
#if defined(LTC_SOBER128) && !defined(LTC_SOBER128_STREAM)
|
||||
#error LTC_SOBER128 requires LTC_SOBER128_STREAM
|
||||
#endif
|
||||
|
||||
#if defined(LTC_BLAKE2SMAC) && !defined(LTC_BLAKE2S)
|
||||
#error LTC_BLAKE2SMAC requires LTC_BLAKE2S
|
||||
#endif
|
||||
|
||||
#if defined(LTC_BLAKE2BMAC) && !defined(LTC_BLAKE2B)
|
||||
#error LTC_BLAKE2BMAC requires LTC_BLAKE2B
|
||||
#endif
|
||||
|
||||
#if defined(LTC_SPRNG) && !defined(LTC_RNG_GET_BYTES)
|
||||
#error LTC_SPRNG requires LTC_RNG_GET_BYTES
|
||||
#endif
|
||||
|
||||
#if defined(LTC_NO_MATH) && (defined(LTM_DESC) || defined(TFM_DESC) || defined(GMP_DESC))
|
||||
#error LTC_NO_MATH defined, but also a math descriptor
|
||||
#endif
|
||||
|
||||
/* THREAD management */
|
||||
#ifdef LTC_PTHREAD
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x;
|
||||
#define LTC_MUTEX_TYPE(x) pthread_mutex_t x;
|
||||
#define LTC_MUTEX_INIT(x) LTC_ARGCHK(pthread_mutex_init(x, NULL) == 0);
|
||||
#define LTC_MUTEX_LOCK(x) LTC_ARGCHK(pthread_mutex_lock(x) == 0);
|
||||
#define LTC_MUTEX_UNLOCK(x) LTC_ARGCHK(pthread_mutex_unlock(x) == 0);
|
||||
|
||||
#else
|
||||
|
||||
/* default no functions */
|
||||
#define LTC_MUTEX_GLOBAL(x)
|
||||
#define LTC_MUTEX_PROTO(x)
|
||||
#define LTC_MUTEX_TYPE(x)
|
||||
#define LTC_MUTEX_INIT(x)
|
||||
#define LTC_MUTEX_LOCK(x)
|
||||
#define LTC_MUTEX_UNLOCK(x)
|
||||
|
||||
#endif
|
||||
|
||||
/* Debuggers */
|
||||
|
||||
/* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and RC4 work (see the code) */
|
||||
/* #define LTC_VALGRIND */
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef LTC_NO_FILE
|
||||
/* buffer size for reading from a file via fread(..) */
|
||||
#ifndef LTC_FILE_READ_BUFSIZE
|
||||
#define LTC_FILE_READ_BUFSIZE 8192
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
531
libtom/libtomcrypt/include/tomcrypt_hash.h
Normal file
531
libtom/libtomcrypt/include/tomcrypt_hash.h
Normal file
@ -0,0 +1,531 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* ---- HASH FUNCTIONS ---- */
|
||||
#ifdef LTC_SHA3
|
||||
struct sha3_state {
|
||||
ulong64 saved; /* the portion of the input message that we didn't consume yet */
|
||||
ulong64 s[25];
|
||||
unsigned char sb[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */
|
||||
unsigned short byte_index; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */
|
||||
unsigned short word_index; /* 0..24--the next word to integrate input (starts from 0) */
|
||||
unsigned short capacity_words; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */
|
||||
unsigned short xof_flag;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA512
|
||||
struct sha512_state {
|
||||
ulong64 length, state[8];
|
||||
unsigned long curlen;
|
||||
unsigned char buf[128];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA256
|
||||
struct sha256_state {
|
||||
ulong64 length;
|
||||
ulong32 state[8], curlen;
|
||||
unsigned char buf[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA1
|
||||
struct sha1_state {
|
||||
ulong64 length;
|
||||
ulong32 state[5], curlen;
|
||||
unsigned char buf[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD5
|
||||
struct md5_state {
|
||||
ulong64 length;
|
||||
ulong32 state[4], curlen;
|
||||
unsigned char buf[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD4
|
||||
struct md4_state {
|
||||
ulong64 length;
|
||||
ulong32 state[4], curlen;
|
||||
unsigned char buf[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_TIGER
|
||||
struct tiger_state {
|
||||
ulong64 state[3], length;
|
||||
unsigned long curlen;
|
||||
unsigned char buf[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD2
|
||||
struct md2_state {
|
||||
unsigned char chksum[16], X[48], buf[16];
|
||||
unsigned long curlen;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD128
|
||||
struct rmd128_state {
|
||||
ulong64 length;
|
||||
unsigned char buf[64];
|
||||
ulong32 curlen, state[4];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD160
|
||||
struct rmd160_state {
|
||||
ulong64 length;
|
||||
unsigned char buf[64];
|
||||
ulong32 curlen, state[5];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD256
|
||||
struct rmd256_state {
|
||||
ulong64 length;
|
||||
unsigned char buf[64];
|
||||
ulong32 curlen, state[8];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD320
|
||||
struct rmd320_state {
|
||||
ulong64 length;
|
||||
unsigned char buf[64];
|
||||
ulong32 curlen, state[10];
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_WHIRLPOOL
|
||||
struct whirlpool_state {
|
||||
ulong64 length, state[8];
|
||||
unsigned char buf[64];
|
||||
ulong32 curlen;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHC_HASH
|
||||
struct chc_state {
|
||||
ulong64 length;
|
||||
unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
|
||||
ulong32 curlen;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_BLAKE2S
|
||||
struct blake2s_state {
|
||||
ulong32 h[8];
|
||||
ulong32 t[2];
|
||||
ulong32 f[2];
|
||||
unsigned char buf[64];
|
||||
unsigned long curlen;
|
||||
unsigned long outlen;
|
||||
unsigned char last_node;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_BLAKE2B
|
||||
struct blake2b_state {
|
||||
ulong64 h[8];
|
||||
ulong64 t[2];
|
||||
ulong64 f[2];
|
||||
unsigned char buf[128];
|
||||
unsigned long curlen;
|
||||
unsigned long outlen;
|
||||
unsigned char last_node;
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef union Hash_state {
|
||||
char dummy[1];
|
||||
#ifdef LTC_CHC_HASH
|
||||
struct chc_state chc;
|
||||
#endif
|
||||
#ifdef LTC_WHIRLPOOL
|
||||
struct whirlpool_state whirlpool;
|
||||
#endif
|
||||
#ifdef LTC_SHA3
|
||||
struct sha3_state sha3;
|
||||
#endif
|
||||
#ifdef LTC_SHA512
|
||||
struct sha512_state sha512;
|
||||
#endif
|
||||
#ifdef LTC_SHA256
|
||||
struct sha256_state sha256;
|
||||
#endif
|
||||
#ifdef LTC_SHA1
|
||||
struct sha1_state sha1;
|
||||
#endif
|
||||
#ifdef LTC_MD5
|
||||
struct md5_state md5;
|
||||
#endif
|
||||
#ifdef LTC_MD4
|
||||
struct md4_state md4;
|
||||
#endif
|
||||
#ifdef LTC_MD2
|
||||
struct md2_state md2;
|
||||
#endif
|
||||
#ifdef LTC_TIGER
|
||||
struct tiger_state tiger;
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD128
|
||||
struct rmd128_state rmd128;
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD160
|
||||
struct rmd160_state rmd160;
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD256
|
||||
struct rmd256_state rmd256;
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD320
|
||||
struct rmd320_state rmd320;
|
||||
#endif
|
||||
#ifdef LTC_BLAKE2S
|
||||
struct blake2s_state blake2s;
|
||||
#endif
|
||||
#ifdef LTC_BLAKE2B
|
||||
struct blake2b_state blake2b;
|
||||
#endif
|
||||
|
||||
void *data;
|
||||
} hash_state;
|
||||
|
||||
/** hash descriptor */
|
||||
extern struct ltc_hash_descriptor {
|
||||
/** name of hash */
|
||||
const char *name;
|
||||
/** internal ID */
|
||||
unsigned char ID;
|
||||
/** Size of digest in octets */
|
||||
unsigned long hashsize;
|
||||
/** Input block size in octets */
|
||||
unsigned long blocksize;
|
||||
/** ASN.1 OID */
|
||||
unsigned long OID[16];
|
||||
/** Length of DER encoding */
|
||||
unsigned long OIDlen;
|
||||
|
||||
/** Init a hash state
|
||||
@param hash The hash to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*init)(hash_state *hash);
|
||||
/** Process a block of data
|
||||
@param hash The hash state
|
||||
@param in The data to hash
|
||||
@param inlen The length of the data (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
|
||||
/** Produce the digest and store it
|
||||
@param hash The hash state
|
||||
@param out [out] The destination of the digest
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*done)(hash_state *hash, unsigned char *out);
|
||||
/** Self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
|
||||
*/
|
||||
int (*test)(void);
|
||||
|
||||
/* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
|
||||
int (*hmac_block)(const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
} hash_descriptor[];
|
||||
|
||||
#ifdef LTC_CHC_HASH
|
||||
int chc_register(int cipher);
|
||||
int chc_init(hash_state * md);
|
||||
int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int chc_done(hash_state * md, unsigned char *hash);
|
||||
int chc_test(void);
|
||||
extern const struct ltc_hash_descriptor chc_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_WHIRLPOOL
|
||||
int whirlpool_init(hash_state * md);
|
||||
int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int whirlpool_done(hash_state * md, unsigned char *hash);
|
||||
int whirlpool_test(void);
|
||||
extern const struct ltc_hash_descriptor whirlpool_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA3
|
||||
int sha3_512_init(hash_state * md);
|
||||
int sha3_512_test(void);
|
||||
extern const struct ltc_hash_descriptor sha3_512_desc;
|
||||
int sha3_384_init(hash_state * md);
|
||||
int sha3_384_test(void);
|
||||
extern const struct ltc_hash_descriptor sha3_384_desc;
|
||||
int sha3_256_init(hash_state * md);
|
||||
int sha3_256_test(void);
|
||||
extern const struct ltc_hash_descriptor sha3_256_desc;
|
||||
int sha3_224_init(hash_state * md);
|
||||
int sha3_224_test(void);
|
||||
extern const struct ltc_hash_descriptor sha3_224_desc;
|
||||
/* process + done are the same for all variants */
|
||||
int sha3_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int sha3_done(hash_state *md, unsigned char *hash);
|
||||
/* SHAKE128 + SHAKE256 */
|
||||
int sha3_shake_init(hash_state *md, int num);
|
||||
#define sha3_shake_process(a,b,c) sha3_process(a,b,c)
|
||||
int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen);
|
||||
int sha3_shake_test(void);
|
||||
int sha3_shake_memory(int num, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA512
|
||||
int sha512_init(hash_state * md);
|
||||
int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int sha512_done(hash_state * md, unsigned char *hash);
|
||||
int sha512_test(void);
|
||||
extern const struct ltc_hash_descriptor sha512_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA384
|
||||
#ifndef LTC_SHA512
|
||||
#error LTC_SHA512 is required for LTC_SHA384
|
||||
#endif
|
||||
int sha384_init(hash_state * md);
|
||||
#define sha384_process sha512_process
|
||||
int sha384_done(hash_state * md, unsigned char *hash);
|
||||
int sha384_test(void);
|
||||
extern const struct ltc_hash_descriptor sha384_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA512_256
|
||||
#ifndef LTC_SHA512
|
||||
#error LTC_SHA512 is required for LTC_SHA512_256
|
||||
#endif
|
||||
int sha512_256_init(hash_state * md);
|
||||
#define sha512_256_process sha512_process
|
||||
int sha512_256_done(hash_state * md, unsigned char *hash);
|
||||
int sha512_256_test(void);
|
||||
extern const struct ltc_hash_descriptor sha512_256_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA512_224
|
||||
#ifndef LTC_SHA512
|
||||
#error LTC_SHA512 is required for LTC_SHA512_224
|
||||
#endif
|
||||
int sha512_224_init(hash_state * md);
|
||||
#define sha512_224_process sha512_process
|
||||
int sha512_224_done(hash_state * md, unsigned char *hash);
|
||||
int sha512_224_test(void);
|
||||
extern const struct ltc_hash_descriptor sha512_224_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA256
|
||||
int sha256_init(hash_state * md);
|
||||
int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int sha256_done(hash_state * md, unsigned char *hash);
|
||||
int sha256_test(void);
|
||||
extern const struct ltc_hash_descriptor sha256_desc;
|
||||
|
||||
#ifdef LTC_SHA224
|
||||
#ifndef LTC_SHA256
|
||||
#error LTC_SHA256 is required for LTC_SHA224
|
||||
#endif
|
||||
int sha224_init(hash_state * md);
|
||||
#define sha224_process sha256_process
|
||||
int sha224_done(hash_state * md, unsigned char *hash);
|
||||
int sha224_test(void);
|
||||
extern const struct ltc_hash_descriptor sha224_desc;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SHA1
|
||||
int sha1_init(hash_state * md);
|
||||
int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int sha1_done(hash_state * md, unsigned char *hash);
|
||||
int sha1_test(void);
|
||||
extern const struct ltc_hash_descriptor sha1_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_BLAKE2S
|
||||
extern const struct ltc_hash_descriptor blake2s_256_desc;
|
||||
int blake2s_256_init(hash_state * md);
|
||||
int blake2s_256_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2s_224_desc;
|
||||
int blake2s_224_init(hash_state * md);
|
||||
int blake2s_224_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2s_160_desc;
|
||||
int blake2s_160_init(hash_state * md);
|
||||
int blake2s_160_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2s_128_desc;
|
||||
int blake2s_128_init(hash_state * md);
|
||||
int blake2s_128_test(void);
|
||||
|
||||
int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
|
||||
int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int blake2s_done(hash_state * md, unsigned char *hash);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_BLAKE2B
|
||||
extern const struct ltc_hash_descriptor blake2b_512_desc;
|
||||
int blake2b_512_init(hash_state * md);
|
||||
int blake2b_512_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2b_384_desc;
|
||||
int blake2b_384_init(hash_state * md);
|
||||
int blake2b_384_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2b_256_desc;
|
||||
int blake2b_256_init(hash_state * md);
|
||||
int blake2b_256_test(void);
|
||||
|
||||
extern const struct ltc_hash_descriptor blake2b_160_desc;
|
||||
int blake2b_160_init(hash_state * md);
|
||||
int blake2b_160_test(void);
|
||||
|
||||
int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
|
||||
int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int blake2b_done(hash_state * md, unsigned char *hash);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD5
|
||||
int md5_init(hash_state * md);
|
||||
int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int md5_done(hash_state * md, unsigned char *hash);
|
||||
int md5_test(void);
|
||||
extern const struct ltc_hash_descriptor md5_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD4
|
||||
int md4_init(hash_state * md);
|
||||
int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int md4_done(hash_state * md, unsigned char *hash);
|
||||
int md4_test(void);
|
||||
extern const struct ltc_hash_descriptor md4_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MD2
|
||||
int md2_init(hash_state * md);
|
||||
int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int md2_done(hash_state * md, unsigned char *hash);
|
||||
int md2_test(void);
|
||||
extern const struct ltc_hash_descriptor md2_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_TIGER
|
||||
int tiger_init(hash_state * md);
|
||||
int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int tiger_done(hash_state * md, unsigned char *hash);
|
||||
int tiger_test(void);
|
||||
extern const struct ltc_hash_descriptor tiger_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD128
|
||||
int rmd128_init(hash_state * md);
|
||||
int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int rmd128_done(hash_state * md, unsigned char *hash);
|
||||
int rmd128_test(void);
|
||||
extern const struct ltc_hash_descriptor rmd128_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD160
|
||||
int rmd160_init(hash_state * md);
|
||||
int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int rmd160_done(hash_state * md, unsigned char *hash);
|
||||
int rmd160_test(void);
|
||||
extern const struct ltc_hash_descriptor rmd160_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD256
|
||||
int rmd256_init(hash_state * md);
|
||||
int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int rmd256_done(hash_state * md, unsigned char *hash);
|
||||
int rmd256_test(void);
|
||||
extern const struct ltc_hash_descriptor rmd256_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RIPEMD320
|
||||
int rmd320_init(hash_state * md);
|
||||
int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen);
|
||||
int rmd320_done(hash_state * md, unsigned char *hash);
|
||||
int rmd320_test(void);
|
||||
extern const struct ltc_hash_descriptor rmd320_desc;
|
||||
#endif
|
||||
|
||||
|
||||
int find_hash(const char *name);
|
||||
int find_hash_id(unsigned char ID);
|
||||
int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
|
||||
int find_hash_any(const char *name, int digestlen);
|
||||
int register_hash(const struct ltc_hash_descriptor *hash);
|
||||
int unregister_hash(const struct ltc_hash_descriptor *hash);
|
||||
int register_all_hashes(void);
|
||||
int hash_is_valid(int idx);
|
||||
|
||||
LTC_MUTEX_PROTO(ltc_hash_mutex)
|
||||
|
||||
int hash_memory(int hash,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
|
||||
#ifndef LTC_NO_FILE
|
||||
int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
|
||||
int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
/* a simple macro for making hash "process" functions */
|
||||
#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
|
||||
int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
|
||||
{ \
|
||||
unsigned long n; \
|
||||
int err; \
|
||||
LTC_ARGCHK(md != NULL); \
|
||||
LTC_ARGCHK(in != NULL); \
|
||||
if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
|
||||
return CRYPT_INVALID_ARG; \
|
||||
} \
|
||||
if ((md-> state_var .length + inlen) < md-> state_var .length) { \
|
||||
return CRYPT_HASH_OVERFLOW; \
|
||||
} \
|
||||
while (inlen > 0) { \
|
||||
if (md-> state_var .curlen == 0 && inlen >= block_size) { \
|
||||
if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
|
||||
return err; \
|
||||
} \
|
||||
md-> state_var .length += block_size * 8; \
|
||||
in += block_size; \
|
||||
inlen -= block_size; \
|
||||
} else { \
|
||||
n = MIN(inlen, (block_size - md-> state_var .curlen)); \
|
||||
XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
|
||||
md-> state_var .curlen += n; \
|
||||
in += n; \
|
||||
inlen -= n; \
|
||||
if (md-> state_var .curlen == block_size) { \
|
||||
if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
|
||||
return err; \
|
||||
} \
|
||||
md-> state_var .length += 8*block_size; \
|
||||
md-> state_var .curlen = 0; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
return CRYPT_OK; \
|
||||
}
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
568
libtom/libtomcrypt/include/tomcrypt_mac.h
Normal file
568
libtom/libtomcrypt/include/tomcrypt_mac.h
Normal file
@ -0,0 +1,568 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
typedef struct Hmac_state {
|
||||
hash_state md;
|
||||
int hash;
|
||||
hash_state hashstate;
|
||||
unsigned char *key;
|
||||
} hmac_state;
|
||||
|
||||
int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen);
|
||||
int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen);
|
||||
int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen);
|
||||
int hmac_test(void);
|
||||
int hmac_memory(int hash,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int hmac_memory_multi(int hash,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
int hmac_file(int hash, const char *fname, const unsigned char *key,
|
||||
unsigned long keylen,
|
||||
unsigned char *dst, unsigned long *dstlen);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_OMAC
|
||||
|
||||
typedef struct {
|
||||
int cipher_idx,
|
||||
buflen,
|
||||
blklen;
|
||||
unsigned char block[MAXBLOCKSIZE],
|
||||
prev[MAXBLOCKSIZE],
|
||||
Lu[2][MAXBLOCKSIZE];
|
||||
symmetric_key key;
|
||||
} omac_state;
|
||||
|
||||
int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
|
||||
int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen);
|
||||
int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen);
|
||||
int omac_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int omac_memory_multi(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
int omac_file(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const char *filename,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int omac_test(void);
|
||||
#endif /* LTC_OMAC */
|
||||
|
||||
#ifdef LTC_PMAC
|
||||
|
||||
typedef struct {
|
||||
unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
|
||||
Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
|
||||
Lr[MAXBLOCKSIZE], /* L * x^-1 */
|
||||
block[MAXBLOCKSIZE], /* currently accumulated block */
|
||||
checksum[MAXBLOCKSIZE]; /* current checksum */
|
||||
|
||||
symmetric_key key; /* scheduled key for cipher */
|
||||
unsigned long block_index; /* index # for current block */
|
||||
int cipher_idx, /* cipher idx */
|
||||
block_len, /* length of block */
|
||||
buflen; /* number of bytes in the buffer */
|
||||
} pmac_state;
|
||||
|
||||
int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen);
|
||||
int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen);
|
||||
int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pmac_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *msg, unsigned long msglen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pmac_memory_multi(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
|
||||
int pmac_file(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const char *filename,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pmac_test(void);
|
||||
|
||||
/* internal functions */
|
||||
int pmac_ntz(unsigned long x);
|
||||
void pmac_shift_xor(pmac_state *pmac);
|
||||
|
||||
#endif /* PMAC */
|
||||
|
||||
#ifdef LTC_POLY1305
|
||||
typedef struct {
|
||||
ulong32 r[5];
|
||||
ulong32 h[5];
|
||||
ulong32 pad[4];
|
||||
unsigned long leftover;
|
||||
unsigned char buffer[16];
|
||||
int final;
|
||||
} poly1305_state;
|
||||
|
||||
int poly1305_init(poly1305_state *st, const unsigned char *key, unsigned long keylen);
|
||||
int poly1305_process(poly1305_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int poly1305_done(poly1305_state *st, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_test(void);
|
||||
int poly1305_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
|
||||
int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_test(void);
|
||||
#endif /* LTC_POLY1305 */
|
||||
|
||||
#ifdef LTC_BLAKE2SMAC
|
||||
typedef hash_state blake2smac_state;
|
||||
int blake2smac_init(blake2smac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen);
|
||||
int blake2smac_process(blake2smac_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int blake2smac_done(blake2smac_state *st, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2smac_test(void);
|
||||
int blake2smac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2smac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
|
||||
int blake2smac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2smac_test(void);
|
||||
#endif /* LTC_BLAKE2SMAC */
|
||||
|
||||
#ifdef LTC_BLAKE2BMAC
|
||||
typedef hash_state blake2bmac_state;
|
||||
int blake2bmac_init(blake2bmac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen);
|
||||
int blake2bmac_process(blake2bmac_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int blake2bmac_done(blake2bmac_state *st, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2bmac_test(void);
|
||||
int blake2bmac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2bmac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
|
||||
int blake2bmac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
|
||||
int blake2bmac_test(void);
|
||||
#endif /* LTC_BLAKE2BMAC */
|
||||
|
||||
#ifdef LTC_EAX_MODE
|
||||
|
||||
#if !(defined(LTC_OMAC) && defined(LTC_CTR_MODE))
|
||||
#error LTC_EAX_MODE requires LTC_OMAC and CTR
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
unsigned char N[MAXBLOCKSIZE];
|
||||
symmetric_CTR ctr;
|
||||
omac_state headeromac, ctomac;
|
||||
} eax_state;
|
||||
|
||||
int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *header, unsigned long headerlen);
|
||||
|
||||
int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length);
|
||||
int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length);
|
||||
int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length);
|
||||
int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int eax_encrypt_authenticate_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *header, unsigned long headerlen,
|
||||
const unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int eax_decrypt_verify_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *header, unsigned long headerlen,
|
||||
const unsigned char *ct, unsigned long ctlen,
|
||||
unsigned char *pt,
|
||||
unsigned char *tag, unsigned long taglen,
|
||||
int *stat);
|
||||
|
||||
int eax_test(void);
|
||||
#endif /* EAX MODE */
|
||||
|
||||
#ifdef LTC_OCB_MODE
|
||||
typedef struct {
|
||||
unsigned char L[MAXBLOCKSIZE], /* L value */
|
||||
Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
|
||||
Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
|
||||
Lr[MAXBLOCKSIZE], /* L * x^-1 */
|
||||
R[MAXBLOCKSIZE], /* R value */
|
||||
checksum[MAXBLOCKSIZE]; /* current checksum */
|
||||
|
||||
symmetric_key key; /* scheduled key for cipher */
|
||||
unsigned long block_index; /* index # for current block */
|
||||
int cipher, /* cipher idx */
|
||||
block_len; /* length of block */
|
||||
} ocb_state;
|
||||
|
||||
int ocb_init(ocb_state *ocb, int cipher,
|
||||
const unsigned char *key, unsigned long keylen, const unsigned char *nonce);
|
||||
|
||||
int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct);
|
||||
int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt);
|
||||
|
||||
int ocb_done_encrypt(ocb_state *ocb,
|
||||
const unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int ocb_done_decrypt(ocb_state *ocb,
|
||||
const unsigned char *ct, unsigned long ctlen,
|
||||
unsigned char *pt,
|
||||
const unsigned char *tag, unsigned long taglen, int *stat);
|
||||
|
||||
int ocb_encrypt_authenticate_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce,
|
||||
const unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int ocb_decrypt_verify_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce,
|
||||
const unsigned char *ct, unsigned long ctlen,
|
||||
unsigned char *pt,
|
||||
const unsigned char *tag, unsigned long taglen,
|
||||
int *stat);
|
||||
|
||||
int ocb_test(void);
|
||||
|
||||
/* internal functions */
|
||||
void ocb_shift_xor(ocb_state *ocb, unsigned char *Z);
|
||||
int ocb_ntz(unsigned long x);
|
||||
int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode);
|
||||
|
||||
#endif /* LTC_OCB_MODE */
|
||||
|
||||
#ifdef LTC_OCB3_MODE
|
||||
typedef struct {
|
||||
unsigned char Offset_0[MAXBLOCKSIZE], /* Offset_0 value */
|
||||
Offset_current[MAXBLOCKSIZE], /* Offset_{current_block_index} value */
|
||||
L_dollar[MAXBLOCKSIZE], /* L_$ value */
|
||||
L_star[MAXBLOCKSIZE], /* L_* value */
|
||||
L_[32][MAXBLOCKSIZE], /* L_{i} values */
|
||||
tag_part[MAXBLOCKSIZE], /* intermediate result of tag calculation */
|
||||
checksum[MAXBLOCKSIZE]; /* current checksum */
|
||||
|
||||
/* AAD related members */
|
||||
unsigned char aSum_current[MAXBLOCKSIZE], /* AAD related helper variable */
|
||||
aOffset_current[MAXBLOCKSIZE], /* AAD related helper variable */
|
||||
adata_buffer[MAXBLOCKSIZE]; /* AAD buffer */
|
||||
int adata_buffer_bytes; /* bytes in AAD buffer */
|
||||
unsigned long ablock_index; /* index # for current adata (AAD) block */
|
||||
|
||||
symmetric_key key; /* scheduled key for cipher */
|
||||
unsigned long block_index; /* index # for current data block */
|
||||
int cipher, /* cipher idx */
|
||||
tag_len, /* length of tag */
|
||||
block_len; /* length of block */
|
||||
} ocb3_state;
|
||||
|
||||
int ocb3_init(ocb3_state *ocb, int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
unsigned long taglen);
|
||||
|
||||
int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct);
|
||||
int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, unsigned char *pt);
|
||||
int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct);
|
||||
int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, unsigned char *pt);
|
||||
int ocb3_add_aad(ocb3_state *ocb, const unsigned char *aad, unsigned long aadlen);
|
||||
int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int ocb3_encrypt_authenticate_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *adata, unsigned long adatalen,
|
||||
const unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int ocb3_decrypt_verify_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *adata, unsigned long adatalen,
|
||||
const unsigned char *ct, unsigned long ctlen,
|
||||
unsigned char *pt,
|
||||
const unsigned char *tag, unsigned long taglen,
|
||||
int *stat);
|
||||
|
||||
int ocb3_test(void);
|
||||
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int ocb3_int_ntz(unsigned long x);
|
||||
void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
|
||||
#endif /* LTC_SOURCE */
|
||||
|
||||
#endif /* LTC_OCB3_MODE */
|
||||
|
||||
#ifdef LTC_CCM_MODE
|
||||
|
||||
#define CCM_ENCRYPT LTC_ENCRYPT
|
||||
#define CCM_DECRYPT LTC_DECRYPT
|
||||
|
||||
typedef struct {
|
||||
symmetric_key K;
|
||||
int cipher, /* which cipher */
|
||||
taglen, /* length of the tag */
|
||||
x; /* index in PAD */
|
||||
|
||||
unsigned long L, /* L value */
|
||||
ptlen, /* length that will be enc / dec */
|
||||
current_ptlen, /* current processed length */
|
||||
aadlen, /* length of the aad */
|
||||
current_aadlen, /* length of the currently provided add */
|
||||
noncelen; /* length of the nonce */
|
||||
|
||||
unsigned char PAD[16],
|
||||
ctr[16],
|
||||
CTRPAD[16],
|
||||
CTRlen;
|
||||
} ccm_state;
|
||||
|
||||
int ccm_init(ccm_state *ccm, int cipher,
|
||||
const unsigned char *key, int keylen, int ptlen, int taglen, int aad_len);
|
||||
|
||||
int ccm_reset(ccm_state *ccm);
|
||||
|
||||
int ccm_add_nonce(ccm_state *ccm,
|
||||
const unsigned char *nonce, unsigned long noncelen);
|
||||
|
||||
int ccm_add_aad(ccm_state *ccm,
|
||||
const unsigned char *adata, unsigned long adatalen);
|
||||
|
||||
int ccm_process(ccm_state *ccm,
|
||||
unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
int direction);
|
||||
|
||||
int ccm_done(ccm_state *ccm,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int ccm_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
symmetric_key *uskey,
|
||||
const unsigned char *nonce, unsigned long noncelen,
|
||||
const unsigned char *header, unsigned long headerlen,
|
||||
unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen,
|
||||
int direction);
|
||||
|
||||
int ccm_test(void);
|
||||
|
||||
#endif /* LTC_CCM_MODE */
|
||||
|
||||
#if defined(LRW_MODE) || defined(LTC_GCM_MODE)
|
||||
void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c);
|
||||
#endif
|
||||
|
||||
|
||||
/* table shared between GCM and LRW */
|
||||
#if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
|
||||
extern const unsigned char gcm_shift_table[];
|
||||
#endif
|
||||
|
||||
#ifdef LTC_GCM_MODE
|
||||
|
||||
#define GCM_ENCRYPT LTC_ENCRYPT
|
||||
#define GCM_DECRYPT LTC_DECRYPT
|
||||
|
||||
#define LTC_GCM_MODE_IV 0
|
||||
#define LTC_GCM_MODE_AAD 1
|
||||
#define LTC_GCM_MODE_TEXT 2
|
||||
|
||||
typedef struct {
|
||||
symmetric_key K;
|
||||
unsigned char H[16], /* multiplier */
|
||||
X[16], /* accumulator */
|
||||
Y[16], /* counter */
|
||||
Y_0[16], /* initial counter */
|
||||
buf[16]; /* buffer for stuff */
|
||||
|
||||
int cipher, /* which cipher */
|
||||
ivmode, /* Which mode is the IV in? */
|
||||
mode, /* mode the GCM code is in */
|
||||
buflen; /* length of data in buf */
|
||||
|
||||
ulong64 totlen, /* 64-bit counter used for IV and AAD */
|
||||
pttotlen; /* 64-bit counter for the PT */
|
||||
|
||||
#ifdef LTC_GCM_TABLES
|
||||
unsigned char PC[16][256][16] /* 16 tables of 8x128 */
|
||||
#ifdef LTC_GCM_TABLES_SSE2
|
||||
__attribute__ ((aligned (16)))
|
||||
#endif
|
||||
;
|
||||
#endif
|
||||
} gcm_state;
|
||||
|
||||
void gcm_mult_h(gcm_state *gcm, unsigned char *I);
|
||||
|
||||
int gcm_init(gcm_state *gcm, int cipher,
|
||||
const unsigned char *key, int keylen);
|
||||
|
||||
int gcm_reset(gcm_state *gcm);
|
||||
|
||||
int gcm_add_iv(gcm_state *gcm,
|
||||
const unsigned char *IV, unsigned long IVlen);
|
||||
|
||||
int gcm_add_aad(gcm_state *gcm,
|
||||
const unsigned char *adata, unsigned long adatalen);
|
||||
|
||||
int gcm_process(gcm_state *gcm,
|
||||
unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
int direction);
|
||||
|
||||
int gcm_done(gcm_state *gcm,
|
||||
unsigned char *tag, unsigned long *taglen);
|
||||
|
||||
int gcm_memory( int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *IV, unsigned long IVlen,
|
||||
const unsigned char *adata, unsigned long adatalen,
|
||||
unsigned char *pt, unsigned long ptlen,
|
||||
unsigned char *ct,
|
||||
unsigned char *tag, unsigned long *taglen,
|
||||
int direction);
|
||||
int gcm_test(void);
|
||||
|
||||
#endif /* LTC_GCM_MODE */
|
||||
|
||||
#ifdef LTC_PELICAN
|
||||
|
||||
typedef struct pelican_state
|
||||
{
|
||||
symmetric_key K;
|
||||
unsigned char state[16];
|
||||
int buflen;
|
||||
} pelican_state;
|
||||
|
||||
int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen);
|
||||
int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen);
|
||||
int pelican_done(pelican_state *pelmac, unsigned char *out);
|
||||
int pelican_test(void);
|
||||
|
||||
int pelican_memory(const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_XCBC
|
||||
|
||||
/* add this to "keylen" to xcbc_init to use a pure three-key XCBC MAC */
|
||||
#define LTC_XCBC_PURE 0x8000UL
|
||||
|
||||
typedef struct {
|
||||
unsigned char K[3][MAXBLOCKSIZE],
|
||||
IV[MAXBLOCKSIZE];
|
||||
|
||||
symmetric_key key;
|
||||
|
||||
int cipher,
|
||||
buflen,
|
||||
blocksize;
|
||||
} xcbc_state;
|
||||
|
||||
int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen);
|
||||
int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen);
|
||||
int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen);
|
||||
int xcbc_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int xcbc_memory_multi(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
int xcbc_file(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const char *filename,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int xcbc_test(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_F9_MODE
|
||||
|
||||
typedef struct {
|
||||
unsigned char akey[MAXBLOCKSIZE],
|
||||
ACC[MAXBLOCKSIZE],
|
||||
IV[MAXBLOCKSIZE];
|
||||
|
||||
symmetric_key key;
|
||||
|
||||
int cipher,
|
||||
buflen,
|
||||
keylen,
|
||||
blocksize;
|
||||
} f9_state;
|
||||
|
||||
int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen);
|
||||
int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen);
|
||||
int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen);
|
||||
int f9_memory(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int f9_memory_multi(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *in, unsigned long inlen, ...);
|
||||
int f9_file(int cipher,
|
||||
const unsigned char *key, unsigned long keylen,
|
||||
const char *filename,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int f9_test(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA20POLY1305_MODE
|
||||
|
||||
typedef struct {
|
||||
poly1305_state poly;
|
||||
chacha_state chacha;
|
||||
ulong64 aadlen;
|
||||
ulong64 ctlen;
|
||||
int aadflg;
|
||||
} chacha20poly1305_state;
|
||||
|
||||
#define CHCHA20POLY1305_ENCRYPT LTC_ENCRYPT
|
||||
#define CHCHA20POLY1305_DECRYPT LTC_DECRYPT
|
||||
|
||||
int chacha20poly1305_init(chacha20poly1305_state *st, const unsigned char *key, unsigned long keylen);
|
||||
int chacha20poly1305_setiv(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen);
|
||||
int chacha20poly1305_setiv_rfc7905(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 sequence_number);
|
||||
int chacha20poly1305_add_aad(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int chacha20poly1305_encrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
|
||||
int chacha20poly1305_decrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
|
||||
int chacha20poly1305_done(chacha20poly1305_state *st, unsigned char *tag, unsigned long *taglen);
|
||||
int chacha20poly1305_memory(const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *iv, unsigned long ivlen,
|
||||
const unsigned char *aad, unsigned long aadlen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out,
|
||||
unsigned char *tag, unsigned long *taglen,
|
||||
int direction);
|
||||
int chacha20poly1305_test(void);
|
||||
|
||||
#endif /* LTC_CHACHA20POLY1305_MODE */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
446
libtom/libtomcrypt/include/tomcrypt_macros.h
Normal file
446
libtom/libtomcrypt/include/tomcrypt_macros.h
Normal file
@ -0,0 +1,446 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* ---- HELPER MACROS ---- */
|
||||
#ifdef ENDIAN_NEUTRAL
|
||||
|
||||
#define STORE32L(x, y) \
|
||||
do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD32L(x, y) \
|
||||
do { x = ((ulong32)((y)[3] & 255)<<24) | \
|
||||
((ulong32)((y)[2] & 255)<<16) | \
|
||||
((ulong32)((y)[1] & 255)<<8) | \
|
||||
((ulong32)((y)[0] & 255)); } while(0)
|
||||
|
||||
#define STORE64L(x, y) \
|
||||
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64L(x, y) \
|
||||
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
|
||||
(((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
|
||||
(((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
|
||||
(((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
do { x = ((ulong32)((y)[0] & 255)<<24) | \
|
||||
((ulong32)((y)[1] & 255)<<16) | \
|
||||
((ulong32)((y)[2] & 255)<<8) | \
|
||||
((ulong32)((y)[3] & 255)); } while(0)
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
|
||||
(((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
|
||||
(((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
|
||||
(((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
|
||||
|
||||
|
||||
#elif defined(ENDIAN_LITTLE)
|
||||
|
||||
#ifdef LTC_HAVE_BSWAP_BUILTIN
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
do { ulong32 __t = __builtin_bswap32 ((x)); \
|
||||
XMEMCPY ((y), &__t, 4); } while(0)
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
do { XMEMCPY (&(x), (y), 4); \
|
||||
(x) = __builtin_bswap32 ((x)); } while(0)
|
||||
|
||||
#elif !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
asm __volatile__ ( \
|
||||
"bswapl %0 \n\t" \
|
||||
"movl %0,(%1)\n\t" \
|
||||
"bswapl %0 \n\t" \
|
||||
::"r"(x), "r"(y));
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
asm __volatile__ ( \
|
||||
"movl (%1),%0\n\t" \
|
||||
"bswapl %0\n\t" \
|
||||
:"=r"(x): "r"(y));
|
||||
|
||||
#else
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
do { x = ((ulong32)((y)[0] & 255)<<24) | \
|
||||
((ulong32)((y)[1] & 255)<<16) | \
|
||||
((ulong32)((y)[2] & 255)<<8) | \
|
||||
((ulong32)((y)[3] & 255)); } while(0)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_HAVE_BSWAP_BUILTIN
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
do { ulong64 __t = __builtin_bswap64 ((x)); \
|
||||
XMEMCPY ((y), &__t, 8); } while(0)
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
do { XMEMCPY (&(x), (y), 8); \
|
||||
(x) = __builtin_bswap64 ((x)); } while(0)
|
||||
|
||||
/* x86_64 processor */
|
||||
#elif !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
asm __volatile__ ( \
|
||||
"bswapq %0 \n\t" \
|
||||
"movq %0,(%1)\n\t" \
|
||||
"bswapq %0 \n\t" \
|
||||
::"r"(x), "r"(y): "memory");
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
asm __volatile__ ( \
|
||||
"movq (%1),%0\n\t" \
|
||||
"bswapq %0\n\t" \
|
||||
:"=r"(x): "r"(y): "memory");
|
||||
|
||||
#else
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
|
||||
(((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
|
||||
(((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
|
||||
(((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef ENDIAN_32BITWORD
|
||||
|
||||
#define STORE32L(x, y) \
|
||||
do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
|
||||
|
||||
#define LOAD32L(x, y) \
|
||||
do { XMEMCPY(&(x), y, 4); } while(0)
|
||||
|
||||
#define STORE64L(x, y) \
|
||||
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64L(x, y) \
|
||||
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
|
||||
(((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
|
||||
(((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
|
||||
(((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
|
||||
|
||||
#else /* 64-bit words then */
|
||||
|
||||
#define STORE32L(x, y) \
|
||||
do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
|
||||
|
||||
#define LOAD32L(x, y) \
|
||||
do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
|
||||
|
||||
#define STORE64L(x, y) \
|
||||
do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
|
||||
|
||||
#define LOAD64L(x, y) \
|
||||
do { XMEMCPY(&(x), y, 8); } while(0)
|
||||
|
||||
#endif /* ENDIAN_64BITWORD */
|
||||
|
||||
#elif defined(ENDIAN_BIG)
|
||||
|
||||
#define STORE32L(x, y) \
|
||||
do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD32L(x, y) \
|
||||
do { x = ((ulong32)((y)[3] & 255)<<24) | \
|
||||
((ulong32)((y)[2] & 255)<<16) | \
|
||||
((ulong32)((y)[1] & 255)<<8) | \
|
||||
((ulong32)((y)[0] & 255)); } while(0)
|
||||
|
||||
#define STORE64L(x, y) \
|
||||
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64L(x, y) \
|
||||
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
|
||||
(((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
|
||||
(((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
|
||||
(((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
|
||||
|
||||
#ifdef ENDIAN_32BITWORD
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
do { XMEMCPY(&(x), y, 4); } while(0)
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
|
||||
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
|
||||
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
|
||||
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
|
||||
(((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
|
||||
(((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
|
||||
(((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } while(0)
|
||||
|
||||
#else /* 64-bit words then */
|
||||
|
||||
#define STORE32H(x, y) \
|
||||
do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
|
||||
|
||||
#define LOAD32H(x, y) \
|
||||
do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
|
||||
|
||||
#define STORE64H(x, y) \
|
||||
do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
|
||||
|
||||
#define LOAD64H(x, y) \
|
||||
do { XMEMCPY(&(x), y, 8); } while(0)
|
||||
|
||||
#endif /* ENDIAN_64BITWORD */
|
||||
#endif /* ENDIAN_BIG */
|
||||
|
||||
#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
|
||||
((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
|
||||
|
||||
|
||||
/* 32-bit Rotates */
|
||||
#if defined(_MSC_VER)
|
||||
#define LTC_ROx_ASM
|
||||
|
||||
/* instrinsic rotate */
|
||||
#include <stdlib.h>
|
||||
#pragma intrinsic(_lrotr,_lrotl)
|
||||
#define ROR(x,n) _lrotr(x,n)
|
||||
#define ROL(x,n) _lrotl(x,n)
|
||||
#define RORc(x,n) _lrotr(x,n)
|
||||
#define ROLc(x,n) _lrotl(x,n)
|
||||
|
||||
#elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
|
||||
#define LTC_ROx_ASM
|
||||
|
||||
static inline ulong32 ROL(ulong32 word, int i)
|
||||
{
|
||||
asm ("roll %%cl,%0"
|
||||
:"=r" (word)
|
||||
:"0" (word),"c" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
static inline ulong32 ROR(ulong32 word, int i)
|
||||
{
|
||||
asm ("rorl %%cl,%0"
|
||||
:"=r" (word)
|
||||
:"0" (word),"c" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
#ifndef LTC_NO_ROLC
|
||||
|
||||
#define ROLc(word,i) ({ \
|
||||
ulong32 __ROLc_tmp = (word); \
|
||||
__asm__ ("roll %2, %0" : \
|
||||
"=r" (__ROLc_tmp) : \
|
||||
"0" (__ROLc_tmp), \
|
||||
"I" (i)); \
|
||||
__ROLc_tmp; \
|
||||
})
|
||||
#define RORc(word,i) ({ \
|
||||
ulong32 __RORc_tmp = (word); \
|
||||
__asm__ ("rorl %2, %0" : \
|
||||
"=r" (__RORc_tmp) : \
|
||||
"0" (__RORc_tmp), \
|
||||
"I" (i)); \
|
||||
__RORc_tmp; \
|
||||
})
|
||||
|
||||
#else
|
||||
|
||||
#define ROLc ROL
|
||||
#define RORc ROR
|
||||
|
||||
#endif
|
||||
|
||||
#elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
|
||||
#define LTC_ROx_ASM
|
||||
|
||||
static inline ulong32 ROL(ulong32 word, int i)
|
||||
{
|
||||
asm ("rotlw %0,%0,%2"
|
||||
:"=r" (word)
|
||||
:"0" (word),"r" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
static inline ulong32 ROR(ulong32 word, int i)
|
||||
{
|
||||
asm ("rotlw %0,%0,%2"
|
||||
:"=r" (word)
|
||||
:"0" (word),"r" (32-i));
|
||||
return word;
|
||||
}
|
||||
|
||||
#ifndef LTC_NO_ROLC
|
||||
|
||||
static inline ulong32 ROLc(ulong32 word, const int i)
|
||||
{
|
||||
asm ("rotlwi %0,%0,%2"
|
||||
:"=r" (word)
|
||||
:"0" (word),"I" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
static inline ulong32 RORc(ulong32 word, const int i)
|
||||
{
|
||||
asm ("rotrwi %0,%0,%2"
|
||||
:"=r" (word)
|
||||
:"0" (word),"I" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define ROLc ROL
|
||||
#define RORc ROR
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#else
|
||||
|
||||
/* rotates the hard way */
|
||||
#define ROL(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
|
||||
#define ROR(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
|
||||
#define ROLc(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
|
||||
#define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* 64-bit Rotates */
|
||||
#if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(_WIN64) && !defined(LTC_NO_ASM)
|
||||
|
||||
static inline ulong64 ROL64(ulong64 word, int i)
|
||||
{
|
||||
asm("rolq %%cl,%0"
|
||||
:"=r" (word)
|
||||
:"0" (word),"c" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
static inline ulong64 ROR64(ulong64 word, int i)
|
||||
{
|
||||
asm("rorq %%cl,%0"
|
||||
:"=r" (word)
|
||||
:"0" (word),"c" (i));
|
||||
return word;
|
||||
}
|
||||
|
||||
#ifndef LTC_NO_ROLC
|
||||
|
||||
#define ROL64c(word,i) ({ \
|
||||
ulong64 __ROL64c_tmp = word; \
|
||||
__asm__ ("rolq %2, %0" : \
|
||||
"=r" (__ROL64c_tmp) : \
|
||||
"0" (__ROL64c_tmp), \
|
||||
"J" (i)); \
|
||||
__ROL64c_tmp; \
|
||||
})
|
||||
#define ROR64c(word,i) ({ \
|
||||
ulong64 __ROR64c_tmp = word; \
|
||||
__asm__ ("rorq %2, %0" : \
|
||||
"=r" (__ROR64c_tmp) : \
|
||||
"0" (__ROR64c_tmp), \
|
||||
"J" (i)); \
|
||||
__ROR64c_tmp; \
|
||||
})
|
||||
|
||||
#else /* LTC_NO_ROLC */
|
||||
|
||||
#define ROL64c ROL64
|
||||
#define ROR64c ROR64
|
||||
|
||||
#endif
|
||||
|
||||
#else /* Not x86_64 */
|
||||
|
||||
#define ROL64(x, y) \
|
||||
( (((x)<<((ulong64)(y)&63)) | \
|
||||
(((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
|
||||
|
||||
#define ROR64(x, y) \
|
||||
( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
|
||||
((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
|
||||
|
||||
#define ROL64c(x, y) \
|
||||
( (((x)<<((ulong64)(y)&63)) | \
|
||||
(((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
|
||||
|
||||
#define ROR64c(x, y) \
|
||||
( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
|
||||
((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ( ((x)>(y))?(x):(y) )
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) ( ((x)<(y))?(x):(y) )
|
||||
#endif
|
||||
|
||||
#ifndef LTC_UNUSED_PARAM
|
||||
#define LTC_UNUSED_PARAM(x) (void)(x)
|
||||
#endif
|
||||
|
||||
/* extract a byte portably */
|
||||
#ifdef _MSC_VER
|
||||
#define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
|
||||
#else
|
||||
#define byte(x, n) (((x) >> (8 * (n))) & 255)
|
||||
#endif
|
||||
|
||||
/* there is no snprintf before Visual C++ 2015 */
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
585
libtom/libtomcrypt/include/tomcrypt_math.h
Normal file
585
libtom/libtomcrypt/include/tomcrypt_math.h
Normal file
@ -0,0 +1,585 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/** math functions **/
|
||||
|
||||
#pragma comment (lib, "tomcrypt.lib")
|
||||
|
||||
#define LTC_MP_LT -1
|
||||
#define LTC_MP_EQ 0
|
||||
#define LTC_MP_GT 1
|
||||
|
||||
#define LTC_MP_NO 0
|
||||
#define LTC_MP_YES 1
|
||||
|
||||
#ifndef LTC_MECC
|
||||
typedef void ecc_point;
|
||||
#endif
|
||||
|
||||
#ifndef LTC_MRSA
|
||||
typedef void rsa_key;
|
||||
#endif
|
||||
|
||||
#ifndef LTC_MILLER_RABIN_REPS
|
||||
/* Number of rounds of the Miller-Rabin test
|
||||
* "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p()
|
||||
* As of https://security.stackexchange.com/a/4546 we should use 40 rounds */
|
||||
#define LTC_MILLER_RABIN_REPS 40
|
||||
#endif
|
||||
|
||||
int radix_to_bin(const void *in, int radix, void *out, unsigned long *len);
|
||||
|
||||
/** math descriptor */
|
||||
typedef struct {
|
||||
/** Name of the math provider */
|
||||
const char *name;
|
||||
|
||||
/** Bits per digit, amount of bits must fit in an unsigned long */
|
||||
int bits_per_digit;
|
||||
|
||||
/* ---- init/deinit functions ---- */
|
||||
|
||||
/** initialize a bignum
|
||||
@param a The number to initialize
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*init)(void **a);
|
||||
|
||||
/** init copy
|
||||
@param dst The number to initialize and write to
|
||||
@param src The number to copy from
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*init_copy)(void **dst, void *src);
|
||||
|
||||
/** deinit
|
||||
@param a The number to free
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
void (*deinit)(void *a);
|
||||
|
||||
/* ---- data movement ---- */
|
||||
|
||||
/** negate
|
||||
@param src The number to negate
|
||||
@param dst The destination
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*neg)(void *src, void *dst);
|
||||
|
||||
/** copy
|
||||
@param src The number to copy from
|
||||
@param dst The number to write to
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*copy)(void *src, void *dst);
|
||||
|
||||
/* ---- trivial low level functions ---- */
|
||||
|
||||
/** set small constant
|
||||
@param a Number to write to
|
||||
@param n Source upto bits_per_digit (actually meant for very small constants)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*set_int)(void *a, ltc_mp_digit n);
|
||||
|
||||
/** get small constant
|
||||
@param a Small number to read,
|
||||
only fetches up to bits_per_digit from the number
|
||||
@return The lower bits_per_digit of the integer (unsigned)
|
||||
*/
|
||||
unsigned long (*get_int)(void *a);
|
||||
|
||||
/** get digit n
|
||||
@param a The number to read from
|
||||
@param n The number of the digit to fetch
|
||||
@return The bits_per_digit sized n'th digit of a
|
||||
*/
|
||||
ltc_mp_digit (*get_digit)(void *a, int n);
|
||||
|
||||
/** Get the number of digits that represent the number
|
||||
@param a The number to count
|
||||
@return The number of digits used to represent the number
|
||||
*/
|
||||
int (*get_digit_count)(void *a);
|
||||
|
||||
/** compare two integers
|
||||
@param a The left side integer
|
||||
@param b The right side integer
|
||||
@return LTC_MP_LT if a < b,
|
||||
LTC_MP_GT if a > b and
|
||||
LTC_MP_EQ otherwise. (signed comparison)
|
||||
*/
|
||||
int (*compare)(void *a, void *b);
|
||||
|
||||
/** compare against int
|
||||
@param a The left side integer
|
||||
@param b The right side integer (upto bits_per_digit)
|
||||
@return LTC_MP_LT if a < b,
|
||||
LTC_MP_GT if a > b and
|
||||
LTC_MP_EQ otherwise. (signed comparison)
|
||||
*/
|
||||
int (*compare_d)(void *a, ltc_mp_digit n);
|
||||
|
||||
/** Count the number of bits used to represent the integer
|
||||
@param a The integer to count
|
||||
@return The number of bits required to represent the integer
|
||||
*/
|
||||
int (*count_bits)(void * a);
|
||||
|
||||
/** Count the number of LSB bits which are zero
|
||||
@param a The integer to count
|
||||
@return The number of contiguous zero LSB bits
|
||||
*/
|
||||
int (*count_lsb_bits)(void *a);
|
||||
|
||||
/** Compute a power of two
|
||||
@param a The integer to store the power in
|
||||
@param n The power of two you want to store (a = 2^n)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*twoexpt)(void *a , int n);
|
||||
|
||||
/* ---- radix conversions ---- */
|
||||
|
||||
/** read ascii string
|
||||
@param a The integer to store into
|
||||
@param str The string to read
|
||||
@param radix The radix the integer has been represented in (2-64)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*read_radix)(void *a, const char *str, int radix);
|
||||
|
||||
/** write number to string
|
||||
@param a The integer to store
|
||||
@param str The destination for the string
|
||||
@param radix The radix the integer is to be represented in (2-64)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*write_radix)(void *a, char *str, int radix);
|
||||
|
||||
/** get size as unsigned char string
|
||||
@param a The integer to get the size (when stored in array of octets)
|
||||
@return The length of the integer in octets
|
||||
*/
|
||||
unsigned long (*unsigned_size)(void *a);
|
||||
|
||||
/** store an integer as an array of octets
|
||||
@param src The integer to store
|
||||
@param dst The buffer to store the integer in
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*unsigned_write)(void *src, unsigned char *dst);
|
||||
|
||||
/** read an array of octets and store as integer
|
||||
@param dst The integer to load
|
||||
@param src The array of octets
|
||||
@param len The number of octets
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*unsigned_read)( void *dst,
|
||||
unsigned char *src,
|
||||
unsigned long len);
|
||||
|
||||
/* ---- basic math ---- */
|
||||
|
||||
/** add two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
@param c The destination of "a + b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*add)(void *a, void *b, void *c);
|
||||
|
||||
/** add two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
(single digit of upto bits_per_digit in length)
|
||||
@param c The destination of "a + b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*addi)(void *a, ltc_mp_digit b, void *c);
|
||||
|
||||
/** subtract two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
@param c The destination of "a - b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*sub)(void *a, void *b, void *c);
|
||||
|
||||
/** subtract two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
(single digit of upto bits_per_digit in length)
|
||||
@param c The destination of "a - b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*subi)(void *a, ltc_mp_digit b, void *c);
|
||||
|
||||
/** multiply two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
(single digit of upto bits_per_digit in length)
|
||||
@param c The destination of "a * b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*mul)(void *a, void *b, void *c);
|
||||
|
||||
/** multiply two integers
|
||||
@param a The first source integer
|
||||
@param b The second source integer
|
||||
(single digit of upto bits_per_digit in length)
|
||||
@param c The destination of "a * b"
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*muli)(void *a, ltc_mp_digit b, void *c);
|
||||
|
||||
/** Square an integer
|
||||
@param a The integer to square
|
||||
@param b The destination
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*sqr)(void *a, void *b);
|
||||
|
||||
/** Divide an integer
|
||||
@param a The dividend
|
||||
@param b The divisor
|
||||
@param c The quotient (can be NULL to signify don't care)
|
||||
@param d The remainder (can be NULL to signify don't care)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*mpdiv)(void *a, void *b, void *c, void *d);
|
||||
|
||||
/** divide by two
|
||||
@param a The integer to divide (shift right)
|
||||
@param b The destination
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*div_2)(void *a, void *b);
|
||||
|
||||
/** Get remainder (small value)
|
||||
@param a The integer to reduce
|
||||
@param b The modulus (upto bits_per_digit in length)
|
||||
@param c The destination for the residue
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*modi)(void *a, ltc_mp_digit b, ltc_mp_digit *c);
|
||||
|
||||
/** gcd
|
||||
@param a The first integer
|
||||
@param b The second integer
|
||||
@param c The destination for (a, b)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*gcd)(void *a, void *b, void *c);
|
||||
|
||||
/** lcm
|
||||
@param a The first integer
|
||||
@param b The second integer
|
||||
@param c The destination for [a, b]
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*lcm)(void *a, void *b, void *c);
|
||||
|
||||
/** Modular multiplication
|
||||
@param a The first source
|
||||
@param b The second source
|
||||
@param c The modulus
|
||||
@param d The destination (a*b mod c)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*mulmod)(void *a, void *b, void *c, void *d);
|
||||
|
||||
/** Modular squaring
|
||||
@param a The first source
|
||||
@param b The modulus
|
||||
@param c The destination (a*a mod b)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*sqrmod)(void *a, void *b, void *c);
|
||||
|
||||
/** Modular inversion
|
||||
@param a The value to invert
|
||||
@param b The modulus
|
||||
@param c The destination (1/a mod b)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*invmod)(void *, void *, void *);
|
||||
|
||||
/* ---- reduction ---- */
|
||||
|
||||
/** setup Montgomery
|
||||
@param a The modulus
|
||||
@param b The destination for the reduction digit
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*montgomery_setup)(void *a, void **b);
|
||||
|
||||
/** get normalization value
|
||||
@param a The destination for the normalization value
|
||||
@param b The modulus
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*montgomery_normalization)(void *a, void *b);
|
||||
|
||||
/** reduce a number
|
||||
@param a The number [and dest] to reduce
|
||||
@param b The modulus
|
||||
@param c The value "b" from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*montgomery_reduce)(void *a, void *b, void *c);
|
||||
|
||||
/** clean up (frees memory)
|
||||
@param a The value "b" from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
void (*montgomery_deinit)(void *a);
|
||||
|
||||
/* ---- exponentiation ---- */
|
||||
|
||||
/** Modular exponentiation
|
||||
@param a The base integer
|
||||
@param b The power (can be negative) integer
|
||||
@param c The modulus integer
|
||||
@param d The destination
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*exptmod)(void *a, void *b, void *c, void *d);
|
||||
|
||||
/** Primality testing
|
||||
@param a The integer to test
|
||||
@param b The number of Miller-Rabin tests that shall be executed
|
||||
@param c The destination of the result (FP_YES if prime)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*isprime)(void *a, int b, int *c);
|
||||
|
||||
/* ---- (optional) ecc point math ---- */
|
||||
|
||||
/** ECC GF(p) point multiplication (from the NIST curves)
|
||||
@param k The integer to multiply the point by
|
||||
@param G The point to multiply
|
||||
@param R The destination for kG
|
||||
@param modulus The modulus for the field
|
||||
@param map Boolean indicated whether to map back to affine or not
|
||||
(can be ignored if you work in affine only)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*ecc_ptmul)( void *k,
|
||||
ecc_point *G,
|
||||
ecc_point *R,
|
||||
void *modulus,
|
||||
int map);
|
||||
|
||||
/** ECC GF(p) point addition
|
||||
@param P The first point
|
||||
@param Q The second point
|
||||
@param R The destination of P + Q
|
||||
@param modulus The modulus
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*ecc_ptadd)(ecc_point *P,
|
||||
ecc_point *Q,
|
||||
ecc_point *R,
|
||||
void *modulus,
|
||||
void *mp);
|
||||
|
||||
/** ECC GF(p) point double
|
||||
@param P The first point
|
||||
@param R The destination of 2P
|
||||
@param modulus The modulus
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*ecc_ptdbl)(ecc_point *P,
|
||||
ecc_point *R,
|
||||
void *modulus,
|
||||
void *mp);
|
||||
|
||||
/** ECC mapping from projective to affine,
|
||||
currently uses (x,y,z) => (x/z^2, y/z^3, 1)
|
||||
@param P The point to map
|
||||
@param modulus The modulus
|
||||
@param mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
@remark The mapping can be different but keep in mind a
|
||||
ecc_point only has three integers (x,y,z) so if
|
||||
you use a different mapping you have to make it fit.
|
||||
*/
|
||||
int (*ecc_map)(ecc_point *P, void *modulus, void *mp);
|
||||
|
||||
/** Computes kA*A + kB*B = C using Shamir's Trick
|
||||
@param A First point to multiply
|
||||
@param kA What to multiple A by
|
||||
@param B Second point to multiply
|
||||
@param kB What to multiple B by
|
||||
@param C [out] Destination point (can overlap with A or B)
|
||||
@param modulus Modulus for curve
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*ecc_mul2add)(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *modulus);
|
||||
|
||||
/* ---- (optional) rsa optimized math (for internal CRT) ---- */
|
||||
|
||||
/** RSA Key Generation
|
||||
@param prng An active PRNG state
|
||||
@param wprng The index of the PRNG desired
|
||||
@param size The size of the key in octets
|
||||
@param e The "e" value (public key).
|
||||
e==65537 is a good choice
|
||||
@param key [out] Destination of a newly created private key pair
|
||||
@return CRYPT_OK if successful, upon error all allocated ram is freed
|
||||
*/
|
||||
int (*rsa_keygen)(prng_state *prng,
|
||||
int wprng,
|
||||
int size,
|
||||
long e,
|
||||
rsa_key *key);
|
||||
|
||||
/** RSA exponentiation
|
||||
@param in The octet array representing the base
|
||||
@param inlen The length of the input
|
||||
@param out The destination (to be stored in an octet array format)
|
||||
@param outlen The length of the output buffer and the resulting size
|
||||
(zero padded to the size of the modulus)
|
||||
@param which PK_PUBLIC for public RSA and PK_PRIVATE for private RSA
|
||||
@param key The RSA key to use
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*rsa_me)(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int which,
|
||||
rsa_key *key);
|
||||
|
||||
/* ---- basic math continued ---- */
|
||||
|
||||
/** Modular addition
|
||||
@param a The first source
|
||||
@param b The second source
|
||||
@param c The modulus
|
||||
@param d The destination (a + b mod c)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*addmod)(void *a, void *b, void *c, void *d);
|
||||
|
||||
/** Modular substraction
|
||||
@param a The first source
|
||||
@param b The second source
|
||||
@param c The modulus
|
||||
@param d The destination (a - b mod c)
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*submod)(void *a, void *b, void *c, void *d);
|
||||
|
||||
/* ---- misc stuff ---- */
|
||||
|
||||
/** Make a pseudo-random mpi
|
||||
@param a The mpi to make random
|
||||
@param size The desired length
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
int (*rand)(void *a, int size);
|
||||
} ltc_math_descriptor;
|
||||
|
||||
extern ltc_math_descriptor ltc_mp;
|
||||
|
||||
int ltc_init_multi(void **a, ...);
|
||||
void ltc_deinit_multi(void *a, ...);
|
||||
void ltc_cleanup_multi(void **a, ...);
|
||||
|
||||
#ifdef LTM_DESC
|
||||
extern const ltc_math_descriptor ltm_desc;
|
||||
#endif
|
||||
|
||||
#ifdef TFM_DESC
|
||||
extern const ltc_math_descriptor tfm_desc;
|
||||
#endif
|
||||
|
||||
#ifdef GMP_DESC
|
||||
extern const ltc_math_descriptor gmp_desc;
|
||||
#endif
|
||||
|
||||
#if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE)
|
||||
|
||||
#define MP_DIGIT_BIT ltc_mp.bits_per_digit
|
||||
|
||||
/* some handy macros */
|
||||
#define mp_init(a) ltc_mp.init(a)
|
||||
#define mp_init_multi ltc_init_multi
|
||||
#define mp_clear(a) ltc_mp.deinit(a)
|
||||
#define mp_clear_multi ltc_deinit_multi
|
||||
#define mp_cleanup_multi ltc_cleanup_multi
|
||||
#define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
|
||||
|
||||
#define mp_neg(a, b) ltc_mp.neg(a, b)
|
||||
#define mp_copy(a, b) ltc_mp.copy(a, b)
|
||||
|
||||
#define mp_set(a, b) ltc_mp.set_int(a, b)
|
||||
#define mp_set_int(a, b) ltc_mp.set_int(a, b)
|
||||
#define mp_get_int(a) ltc_mp.get_int(a)
|
||||
#define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
|
||||
#define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
|
||||
#define mp_cmp(a, b) ltc_mp.compare(a, b)
|
||||
#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
|
||||
#define mp_count_bits(a) ltc_mp.count_bits(a)
|
||||
#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
|
||||
#define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
|
||||
|
||||
#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
|
||||
#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
|
||||
#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
|
||||
#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
|
||||
#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
|
||||
|
||||
#define mp_add(a, b, c) ltc_mp.add(a, b, c)
|
||||
#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
|
||||
#define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
|
||||
#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
|
||||
#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
|
||||
#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
|
||||
#define mp_sqr(a, b) ltc_mp.sqr(a, b)
|
||||
#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
|
||||
#define mp_div_2(a, b) ltc_mp.div_2(a, b)
|
||||
#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
|
||||
#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
|
||||
#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
|
||||
#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
|
||||
|
||||
#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
|
||||
#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
|
||||
#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
|
||||
#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
|
||||
#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
|
||||
|
||||
#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
|
||||
#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
|
||||
#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
|
||||
#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
|
||||
|
||||
#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
|
||||
#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
|
||||
|
||||
#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
|
||||
#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
|
||||
#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
|
||||
|
||||
#define mp_tohex(a, b) mp_toradix(a, b, 16)
|
||||
|
||||
#define mp_rand(a, b) ltc_mp.rand(a, b)
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
113
libtom/libtomcrypt/include/tomcrypt_misc.h
Normal file
113
libtom/libtomcrypt/include/tomcrypt_misc.h
Normal file
@ -0,0 +1,113 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* ---- LTC_BASE64 Routines ---- */
|
||||
#ifdef LTC_BASE64
|
||||
int base64_encode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int base64_decode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int base64_strict_decode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_BASE64_URL
|
||||
int base64url_encode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int base64url_decode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int base64url_strict_decode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
/* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */
|
||||
#ifdef LTC_HKDF
|
||||
|
||||
int hkdf_test(void);
|
||||
|
||||
int hkdf_extract(int hash_idx,
|
||||
const unsigned char *salt, unsigned long saltlen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int hkdf_expand(int hash_idx,
|
||||
const unsigned char *info, unsigned long infolen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long outlen);
|
||||
|
||||
int hkdf(int hash_idx,
|
||||
const unsigned char *salt, unsigned long saltlen,
|
||||
const unsigned char *info, unsigned long infolen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long outlen);
|
||||
|
||||
#endif /* LTC_HKDF */
|
||||
|
||||
/* ---- MEM routines ---- */
|
||||
int mem_neq(const void *a, const void *b, size_t len);
|
||||
void zeromem(volatile void *dst, size_t len);
|
||||
void burn_stack(unsigned long len);
|
||||
|
||||
const char *error_to_string(int err);
|
||||
|
||||
extern const char *crypt_build_settings;
|
||||
|
||||
/* ---- HMM ---- */
|
||||
int crypt_fsa(void *mp, ...);
|
||||
|
||||
/* ---- Dynamic language support ---- */
|
||||
int crypt_get_constant(const char* namein, int *valueout);
|
||||
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size);
|
||||
|
||||
int crypt_get_size(const char* namein, unsigned int *sizeout);
|
||||
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
|
||||
|
||||
#ifdef LTM_DESC
|
||||
void init_LTM(void);
|
||||
#endif
|
||||
#ifdef TFM_DESC
|
||||
void init_TFM(void);
|
||||
#endif
|
||||
#ifdef GMP_DESC
|
||||
void init_GMP(void);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_ADLER32
|
||||
typedef struct adler32_state_s
|
||||
{
|
||||
unsigned short s[2];
|
||||
} adler32_state;
|
||||
|
||||
void adler32_init(adler32_state *ctx);
|
||||
void adler32_update(adler32_state *ctx, const unsigned char *input, unsigned long length);
|
||||
void adler32_finish(adler32_state *ctx, void *hash, unsigned long size);
|
||||
int adler32_test(void);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CRC32
|
||||
typedef struct crc32_state_s
|
||||
{
|
||||
ulong32 crc;
|
||||
} crc32_state;
|
||||
|
||||
void crc32_init(crc32_state *ctx);
|
||||
void crc32_update(crc32_state *ctx, const unsigned char *input, unsigned long length);
|
||||
void crc32_finish(crc32_state *ctx, void *hash, unsigned long size);
|
||||
int crc32_test(void);
|
||||
#endif
|
||||
|
||||
int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
747
libtom/libtomcrypt/include/tomcrypt_pk.h
Normal file
747
libtom/libtomcrypt/include/tomcrypt_pk.h
Normal file
@ -0,0 +1,747 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* ---- NUMBER THEORY ---- */
|
||||
|
||||
enum {
|
||||
PK_PUBLIC=0,
|
||||
PK_PRIVATE=1
|
||||
};
|
||||
|
||||
/* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
|
||||
#define PK_STD 0x1000
|
||||
|
||||
int rand_prime(void *N, long len, prng_state *prng, int wprng);
|
||||
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
|
||||
int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
|
||||
|
||||
enum public_key_algorithms {
|
||||
PKA_RSA,
|
||||
PKA_DSA
|
||||
};
|
||||
|
||||
typedef struct Oid {
|
||||
unsigned long OID[16];
|
||||
/** Length of DER encoding */
|
||||
unsigned long OIDlen;
|
||||
} oid_st;
|
||||
|
||||
int pk_get_oid(int pk, oid_st *st);
|
||||
#endif /* LTC_SOURCE */
|
||||
|
||||
/* ---- RSA ---- */
|
||||
#ifdef LTC_MRSA
|
||||
|
||||
/** RSA PKCS style key */
|
||||
typedef struct Rsa_key {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
/** The public exponent */
|
||||
void *e;
|
||||
/** The private exponent */
|
||||
void *d;
|
||||
/** The modulus */
|
||||
void *N;
|
||||
/** The p factor of N */
|
||||
void *p;
|
||||
/** The q factor of N */
|
||||
void *q;
|
||||
/** The 1/q mod p CRT param */
|
||||
void *qP;
|
||||
/** The d mod (p - 1) CRT param */
|
||||
void *dP;
|
||||
/** The d mod (q - 1) CRT param */
|
||||
void *dQ;
|
||||
} rsa_key;
|
||||
|
||||
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
|
||||
|
||||
int rsa_get_size(rsa_key *key);
|
||||
|
||||
int rsa_exptmod(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int which,
|
||||
rsa_key *key);
|
||||
|
||||
void rsa_free(rsa_key *key);
|
||||
|
||||
/* These use PKCS #1 v2.0 padding */
|
||||
#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
|
||||
rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
|
||||
|
||||
#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
|
||||
rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
|
||||
|
||||
#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
|
||||
rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
|
||||
|
||||
#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
|
||||
rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
|
||||
|
||||
#define rsa_sign_saltlen_get_max(_hash_idx, _key) \
|
||||
rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
|
||||
|
||||
/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
|
||||
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
|
||||
|
||||
int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
int hash_idx, int padding,
|
||||
int *stat, rsa_key *key);
|
||||
|
||||
int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
int padding,
|
||||
prng_state *prng, int prng_idx,
|
||||
int hash_idx, unsigned long saltlen,
|
||||
rsa_key *key);
|
||||
|
||||
int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int padding,
|
||||
int hash_idx, unsigned long saltlen,
|
||||
int *stat, rsa_key *key);
|
||||
|
||||
int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
|
||||
|
||||
/* PKCS #1 import/export */
|
||||
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
|
||||
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
|
||||
|
||||
int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
|
||||
int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
|
||||
const void *passwd, unsigned long passwdlen, rsa_key *key);
|
||||
|
||||
int rsa_set_key(const unsigned char *N, unsigned long Nlen,
|
||||
const unsigned char *e, unsigned long elen,
|
||||
const unsigned char *d, unsigned long dlen,
|
||||
rsa_key *key);
|
||||
int rsa_set_factors(const unsigned char *p, unsigned long plen,
|
||||
const unsigned char *q, unsigned long qlen,
|
||||
rsa_key *key);
|
||||
int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
|
||||
const unsigned char *dQ, unsigned long dQlen,
|
||||
const unsigned char *qP, unsigned long qPlen,
|
||||
rsa_key *key);
|
||||
#endif
|
||||
|
||||
/* ---- Katja ---- */
|
||||
#ifdef LTC_MKAT
|
||||
|
||||
/* Min and Max KAT key sizes (in bits) */
|
||||
#define MIN_KAT_SIZE 1024
|
||||
#define MAX_KAT_SIZE 4096
|
||||
|
||||
/** Katja PKCS style key */
|
||||
typedef struct KAT_key {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
/** The private exponent */
|
||||
void *d;
|
||||
/** The modulus */
|
||||
void *N;
|
||||
/** The p factor of N */
|
||||
void *p;
|
||||
/** The q factor of N */
|
||||
void *q;
|
||||
/** The 1/q mod p CRT param */
|
||||
void *qP;
|
||||
/** The d mod (p - 1) CRT param */
|
||||
void *dP;
|
||||
/** The d mod (q - 1) CRT param */
|
||||
void *dQ;
|
||||
/** The pq param */
|
||||
void *pq;
|
||||
} katja_key;
|
||||
|
||||
int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
|
||||
|
||||
int katja_exptmod(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int which,
|
||||
katja_key *key);
|
||||
|
||||
void katja_free(katja_key *key);
|
||||
|
||||
/* These use PKCS #1 v2.0 padding */
|
||||
int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
|
||||
|
||||
int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
int hash_idx, int *stat,
|
||||
katja_key *key);
|
||||
|
||||
/* PKCS #1 import/export */
|
||||
int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
|
||||
int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
|
||||
|
||||
#endif
|
||||
|
||||
/* ---- DH Routines ---- */
|
||||
#ifdef LTC_MDH
|
||||
|
||||
typedef struct {
|
||||
int type;
|
||||
void *x;
|
||||
void *y;
|
||||
void *base;
|
||||
void *prime;
|
||||
} dh_key;
|
||||
|
||||
int dh_get_groupsize(dh_key *key);
|
||||
|
||||
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
|
||||
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
|
||||
|
||||
int dh_set_pg(const unsigned char *p, unsigned long plen,
|
||||
const unsigned char *g, unsigned long glen,
|
||||
dh_key *key);
|
||||
int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
|
||||
int dh_set_pg_groupsize(int groupsize, dh_key *key);
|
||||
|
||||
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
|
||||
int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
|
||||
|
||||
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
void dh_free(dh_key *key);
|
||||
|
||||
int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
|
||||
|
||||
#ifdef LTC_SOURCE
|
||||
typedef struct {
|
||||
int size;
|
||||
const char *name, *base, *prime;
|
||||
} ltc_dh_set_type;
|
||||
|
||||
extern const ltc_dh_set_type ltc_dh_sets[];
|
||||
|
||||
/* internal helper functions */
|
||||
int dh_check_pubkey(dh_key *key);
|
||||
#endif
|
||||
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
|
||||
/* ---- ECC Routines ---- */
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/* size of our temp buffers for exported keys */
|
||||
#define ECC_BUF_SIZE 256
|
||||
|
||||
/* max private key size */
|
||||
#define ECC_MAXSIZE 66
|
||||
|
||||
/** Structure defines a NIST GF(p) curve */
|
||||
typedef struct {
|
||||
/** The size of the curve in octets */
|
||||
int size;
|
||||
|
||||
/** name of curve */
|
||||
const char *name;
|
||||
|
||||
/** The prime that defines the field the curve is in (encoded in hex) */
|
||||
const char *prime;
|
||||
|
||||
/** The fields B param (hex) */
|
||||
const char *B;
|
||||
|
||||
/** The order of the curve (hex) */
|
||||
const char *order;
|
||||
|
||||
/** The x co-ordinate of the base point on the curve (hex) */
|
||||
const char *Gx;
|
||||
|
||||
/** The y co-ordinate of the base point on the curve (hex) */
|
||||
const char *Gy;
|
||||
} ltc_ecc_set_type;
|
||||
|
||||
/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
|
||||
typedef struct {
|
||||
/** The x co-ordinate */
|
||||
void *x;
|
||||
|
||||
/** The y co-ordinate */
|
||||
void *y;
|
||||
|
||||
/** The z co-ordinate */
|
||||
void *z;
|
||||
} ecc_point;
|
||||
|
||||
/** An ECC key */
|
||||
typedef struct {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
|
||||
/** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
|
||||
int idx;
|
||||
|
||||
/** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
|
||||
const ltc_ecc_set_type *dp;
|
||||
|
||||
/** The public key */
|
||||
ecc_point pubkey;
|
||||
|
||||
/** The private key */
|
||||
void *k;
|
||||
} ecc_key;
|
||||
|
||||
/** the ECC params provided */
|
||||
extern const ltc_ecc_set_type ltc_ecc_sets[];
|
||||
|
||||
int ecc_test(void);
|
||||
void ecc_sizes(int *low, int *high);
|
||||
int ecc_get_size(ecc_key *key);
|
||||
|
||||
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
|
||||
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
|
||||
void ecc_free(ecc_key *key);
|
||||
|
||||
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
|
||||
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
|
||||
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
|
||||
|
||||
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
|
||||
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
|
||||
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
|
||||
|
||||
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
ecc_key *key);
|
||||
|
||||
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
ecc_key *key);
|
||||
|
||||
int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key);
|
||||
|
||||
int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key);
|
||||
|
||||
int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key);
|
||||
|
||||
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key);
|
||||
|
||||
/* low level functions */
|
||||
ecc_point *ltc_ecc_new_point(void);
|
||||
void ltc_ecc_del_point(ecc_point *p);
|
||||
int ltc_ecc_is_valid_idx(int n);
|
||||
|
||||
/* point ops (mp == montgomery digit) */
|
||||
#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
|
||||
/* R = 2P */
|
||||
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
|
||||
|
||||
/* R = P + Q */
|
||||
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
|
||||
#endif
|
||||
|
||||
#if defined(LTC_MECC_FP)
|
||||
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
|
||||
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
|
||||
|
||||
/* functions for saving/loading/freeing/adding to fixed point cache */
|
||||
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
|
||||
int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
|
||||
void ltc_ecc_fp_free(void);
|
||||
int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
|
||||
|
||||
/* lock/unlock all points currently in fixed point cache */
|
||||
void ltc_ecc_fp_tablelock(int lock);
|
||||
#endif
|
||||
|
||||
/* R = kG */
|
||||
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
|
||||
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
/* kA*A + kB*B = C */
|
||||
int ltc_ecc_mul2add(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *modulus);
|
||||
|
||||
#ifdef LTC_MECC_FP
|
||||
/* Shamir's trick with optimized point multiplication using fixed point cache */
|
||||
int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C, void *modulus);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* map P to affine from projective */
|
||||
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/* Max diff between group and modulus size in bytes */
|
||||
#define LTC_MDSA_DELTA 512
|
||||
|
||||
/* Max DSA group size in bytes (default allows 4k-bit groups) */
|
||||
#define LTC_MDSA_MAX_GROUP 512
|
||||
|
||||
/** DSA key structure */
|
||||
typedef struct {
|
||||
/** The key type, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
|
||||
/** The order of the sub-group used in octets */
|
||||
int qord;
|
||||
|
||||
/** The generator */
|
||||
void *g;
|
||||
|
||||
/** The prime used to generate the sub-group */
|
||||
void *q;
|
||||
|
||||
/** The large prime that generats the field the contains the sub-group */
|
||||
void *p;
|
||||
|
||||
/** The private key */
|
||||
void *x;
|
||||
|
||||
/** The public key */
|
||||
void *y;
|
||||
} dsa_key;
|
||||
|
||||
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
|
||||
|
||||
int dsa_set_pqg(const unsigned char *p, unsigned long plen,
|
||||
const unsigned char *q, unsigned long qlen,
|
||||
const unsigned char *g, unsigned long glen,
|
||||
dsa_key *key);
|
||||
int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
|
||||
int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
|
||||
|
||||
int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
|
||||
int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
|
||||
|
||||
void dsa_free(dsa_key *key);
|
||||
|
||||
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
|
||||
void *r, void *s,
|
||||
prng_state *prng, int wprng, dsa_key *key);
|
||||
|
||||
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, dsa_key *key);
|
||||
|
||||
int dsa_verify_hash_raw( void *r, void *s,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key);
|
||||
|
||||
int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key);
|
||||
|
||||
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
dsa_key *key);
|
||||
|
||||
int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
dsa_key *key);
|
||||
|
||||
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
|
||||
int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
|
||||
int dsa_verify_key(dsa_key *key, int *stat);
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int dsa_int_validate_xy(dsa_key *key, int *stat);
|
||||
int dsa_int_validate_pqg(dsa_key *key, int *stat);
|
||||
int dsa_int_validate_primes(dsa_key *key, int *stat);
|
||||
#endif
|
||||
int dsa_shared_secret(void *private_key, void *base,
|
||||
dsa_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_DER
|
||||
/* DER handling */
|
||||
|
||||
typedef enum ltc_asn1_type_ {
|
||||
/* 0 */
|
||||
LTC_ASN1_EOL,
|
||||
LTC_ASN1_BOOLEAN,
|
||||
LTC_ASN1_INTEGER,
|
||||
LTC_ASN1_SHORT_INTEGER,
|
||||
LTC_ASN1_BIT_STRING,
|
||||
/* 5 */
|
||||
LTC_ASN1_OCTET_STRING,
|
||||
LTC_ASN1_NULL,
|
||||
LTC_ASN1_OBJECT_IDENTIFIER,
|
||||
LTC_ASN1_IA5_STRING,
|
||||
LTC_ASN1_PRINTABLE_STRING,
|
||||
/* 10 */
|
||||
LTC_ASN1_UTF8_STRING,
|
||||
LTC_ASN1_UTCTIME,
|
||||
LTC_ASN1_CHOICE,
|
||||
LTC_ASN1_SEQUENCE,
|
||||
LTC_ASN1_SET,
|
||||
/* 15 */
|
||||
LTC_ASN1_SETOF,
|
||||
LTC_ASN1_RAW_BIT_STRING,
|
||||
LTC_ASN1_TELETEX_STRING,
|
||||
LTC_ASN1_CONSTRUCTED,
|
||||
LTC_ASN1_CONTEXT_SPECIFIC,
|
||||
/* 20 */
|
||||
LTC_ASN1_GENERALIZEDTIME,
|
||||
} ltc_asn1_type;
|
||||
|
||||
/** A LTC ASN.1 list type */
|
||||
typedef struct ltc_asn1_list_ {
|
||||
/** The LTC ASN.1 enumerated type identifier */
|
||||
ltc_asn1_type type;
|
||||
/** The data to encode or place for decoding */
|
||||
void *data;
|
||||
/** The size of the input or resulting output */
|
||||
unsigned long size;
|
||||
/** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
|
||||
int used;
|
||||
/** prev/next entry in the list */
|
||||
struct ltc_asn1_list_ *prev, *next, *child, *parent;
|
||||
} ltc_asn1_list;
|
||||
|
||||
#define LTC_SET_ASN1(list, index, Type, Data, Size) \
|
||||
do { \
|
||||
int LTC_MACRO_temp = (index); \
|
||||
ltc_asn1_list *LTC_MACRO_list = (list); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
|
||||
} while (0)
|
||||
|
||||
/* SEQUENCE */
|
||||
int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int type_of);
|
||||
|
||||
#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
|
||||
|
||||
int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen, int ordered);
|
||||
|
||||
#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
|
||||
|
||||
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen);
|
||||
|
||||
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen, unsigned long *payloadlen);
|
||||
/* SUBJECT PUBLIC KEY INFO */
|
||||
int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long public_key_len,
|
||||
unsigned long parameters_type, void* parameters, unsigned long parameters_len);
|
||||
|
||||
int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
|
||||
unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
|
||||
#endif /* LTC_SOURCE */
|
||||
|
||||
/* SET */
|
||||
#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
|
||||
#define der_length_set der_length_sequence
|
||||
int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
/* VA list handy helpers with triplets of <type, size, data> */
|
||||
int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
|
||||
int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
|
||||
|
||||
/* FLEXI DECODER handle unknown list decoder */
|
||||
int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
|
||||
#define der_free_sequence_flexi der_sequence_free
|
||||
void der_sequence_free(ltc_asn1_list *in);
|
||||
void der_sequence_shrink(ltc_asn1_list *in);
|
||||
|
||||
/* BOOLEAN */
|
||||
int der_length_boolean(unsigned long *outlen);
|
||||
int der_encode_boolean(int in,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_boolean(const unsigned char *in, unsigned long inlen,
|
||||
int *out);
|
||||
/* INTEGER */
|
||||
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
|
||||
int der_length_integer(void *num, unsigned long *len);
|
||||
|
||||
/* INTEGER -- handy for 0..2^32-1 values */
|
||||
int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
|
||||
int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
|
||||
int der_length_short_integer(unsigned long num, unsigned long *outlen);
|
||||
|
||||
/* BIT STRING */
|
||||
int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
|
||||
|
||||
/* OCTET STRING */
|
||||
int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
/* OBJECT IDENTIFIER */
|
||||
int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
|
||||
unsigned long *words, unsigned long *outlen);
|
||||
int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
|
||||
unsigned long der_object_identifier_bits(unsigned long x);
|
||||
|
||||
/* IA5 STRING */
|
||||
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
int der_ia5_char_encode(int c);
|
||||
int der_ia5_value_decode(int v);
|
||||
|
||||
/* TELETEX STRING */
|
||||
int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int der_teletex_char_encode(int c);
|
||||
int der_teletex_value_decode(int v);
|
||||
#endif /* LTC_SOURCE */
|
||||
|
||||
|
||||
/* PRINTABLE STRING */
|
||||
int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
int der_printable_char_encode(int c);
|
||||
int der_printable_value_decode(int v);
|
||||
|
||||
/* UTF-8 */
|
||||
#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
|
||||
#include <wchar.h>
|
||||
#if defined(__WCHAR_MAX__)
|
||||
#define LTC_WCHAR_MAX __WCHAR_MAX__
|
||||
#elif defined(WCHAR_MAX)
|
||||
#define LTC_WCHAR_MAX WCHAR_MAX
|
||||
#endif
|
||||
/* please note that it might happen that LTC_WCHAR_MAX is undefined */
|
||||
#else
|
||||
typedef ulong32 wchar_t;
|
||||
#define LTC_WCHAR_MAX 0xFFFFFFFF
|
||||
#endif
|
||||
|
||||
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
||||
wchar_t *out, unsigned long *outlen);
|
||||
unsigned long der_utf8_charsize(const wchar_t c);
|
||||
#ifdef LTC_SOURCE
|
||||
/* internal helper functions */
|
||||
int der_utf8_valid_char(const wchar_t c);
|
||||
#endif /* LTC_SOURCE */
|
||||
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
|
||||
/* CHOICE */
|
||||
int der_decode_choice(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen);
|
||||
|
||||
/* UTCTime */
|
||||
typedef struct {
|
||||
unsigned YY, /* year */
|
||||
MM, /* month */
|
||||
DD, /* day */
|
||||
hh, /* hour */
|
||||
mm, /* minute */
|
||||
ss, /* second */
|
||||
off_dir, /* timezone offset direction 0 == +, 1 == - */
|
||||
off_hh, /* timezone offset hours */
|
||||
off_mm; /* timezone offset minutes */
|
||||
} ltc_utctime;
|
||||
|
||||
int der_encode_utctime(ltc_utctime *utctime,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_utctime *out);
|
||||
|
||||
int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
|
||||
|
||||
/* GeneralizedTime */
|
||||
typedef struct {
|
||||
unsigned YYYY, /* year */
|
||||
MM, /* month */
|
||||
DD, /* day */
|
||||
hh, /* hour */
|
||||
mm, /* minute */
|
||||
ss, /* second */
|
||||
fs, /* fractional seconds */
|
||||
off_dir, /* timezone offset direction 0 == +, 1 == - */
|
||||
off_hh, /* timezone offset hours */
|
||||
off_mm; /* timezone offset minutes */
|
||||
} ltc_generalizedtime;
|
||||
|
||||
int der_encode_generalizedtime(ltc_generalizedtime *gtime,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_generalizedtime *out);
|
||||
|
||||
int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
108
libtom/libtomcrypt/include/tomcrypt_pkcs.h
Normal file
108
libtom/libtomcrypt/include/tomcrypt_pkcs.h
Normal file
@ -0,0 +1,108 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* PKCS Header Info */
|
||||
|
||||
/* ===> PKCS #1 -- RSA Cryptography <=== */
|
||||
#ifdef LTC_PKCS_1
|
||||
|
||||
enum ltc_pkcs_1_v1_5_blocks
|
||||
{
|
||||
LTC_PKCS_1_EMSA = 1, /* Block type 1 (PKCS #1 v1.5 signature padding) */
|
||||
LTC_PKCS_1_EME = 2 /* Block type 2 (PKCS #1 v1.5 encryption padding) */
|
||||
};
|
||||
|
||||
enum ltc_pkcs_1_paddings
|
||||
{
|
||||
LTC_PKCS_1_V1_5 = 1, /* PKCS #1 v1.5 padding (\sa ltc_pkcs_1_v1_5_blocks) */
|
||||
LTC_PKCS_1_OAEP = 2, /* PKCS #1 v2.0 encryption padding */
|
||||
LTC_PKCS_1_PSS = 3, /* PKCS #1 v2.1 signature padding */
|
||||
LTC_PKCS_1_V1_5_NA1 = 4 /* PKCS #1 v1.5 padding - No ASN.1 (\sa ltc_pkcs_1_v1_5_blocks) */
|
||||
};
|
||||
|
||||
int pkcs_1_mgf1( int hash_idx,
|
||||
const unsigned char *seed, unsigned long seedlen,
|
||||
unsigned char *mask, unsigned long masklen);
|
||||
|
||||
int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out);
|
||||
int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen);
|
||||
|
||||
/* *** v1.5 padding */
|
||||
int pkcs_1_v1_5_encode(const unsigned char *msg,
|
||||
unsigned long msglen,
|
||||
int block_type,
|
||||
unsigned long modulus_bitlen,
|
||||
prng_state *prng,
|
||||
int prng_idx,
|
||||
unsigned char *out,
|
||||
unsigned long *outlen);
|
||||
|
||||
int pkcs_1_v1_5_decode(const unsigned char *msg,
|
||||
unsigned long msglen,
|
||||
int block_type,
|
||||
unsigned long modulus_bitlen,
|
||||
unsigned char *out,
|
||||
unsigned long *outlen,
|
||||
int *is_valid);
|
||||
|
||||
/* *** v2.1 padding */
|
||||
int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
unsigned long modulus_bitlen, prng_state *prng,
|
||||
int prng_idx, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
unsigned long modulus_bitlen, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
int *res);
|
||||
|
||||
int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
|
||||
unsigned long saltlen, prng_state *prng,
|
||||
int prng_idx, int hash_idx,
|
||||
unsigned long modulus_bitlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
|
||||
const unsigned char *sig, unsigned long siglen,
|
||||
unsigned long saltlen, int hash_idx,
|
||||
unsigned long modulus_bitlen, int *res);
|
||||
|
||||
#endif /* LTC_PKCS_1 */
|
||||
|
||||
/* ===> PKCS #5 -- Password Based Cryptography <=== */
|
||||
#ifdef LTC_PKCS_5
|
||||
|
||||
/* Algorithm #1 (PBKDF1) */
|
||||
int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
|
||||
const unsigned char *salt,
|
||||
int iteration_count, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
/* Algorithm #1 (PBKDF1) - OpenSSL-compatible variant for arbitrarily-long keys.
|
||||
Compatible with EVP_BytesToKey() */
|
||||
int pkcs_5_alg1_openssl(const unsigned char *password,
|
||||
unsigned long password_len,
|
||||
const unsigned char *salt,
|
||||
int iteration_count, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
/* Algorithm #2 (PBKDF2) */
|
||||
int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
|
||||
const unsigned char *salt, unsigned long salt_len,
|
||||
int iteration_count, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pkcs_5_test (void);
|
||||
#endif /* LTC_PKCS_5 */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
457
libtom/libtomcrypt/include/tomcrypt_private.h
Normal file
457
libtom/libtomcrypt/include/tomcrypt_private.h
Normal file
@ -0,0 +1,457 @@
|
||||
/* 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"
|
||||
|
||||
/*
|
||||
* Internal Macros
|
||||
*/
|
||||
|
||||
#define LTC_PAD_MASK (0xF000U)
|
||||
|
||||
/*
|
||||
* Internal Enums
|
||||
*/
|
||||
|
||||
enum ltc_oid_id {
|
||||
PKA_RSA,
|
||||
PKA_DSA,
|
||||
PKA_EC,
|
||||
PKA_EC_PRIMEF,
|
||||
PKA_X25519,
|
||||
PKA_ED25519,
|
||||
};
|
||||
|
||||
/*
|
||||
* Internal Types
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
const char *name, *base, *prime;
|
||||
} ltc_dh_set_type;
|
||||
|
||||
|
||||
typedef int (*fn_kdf_t)(const unsigned char *password, unsigned long password_len,
|
||||
const unsigned char *salt, unsigned long salt_len,
|
||||
int iteration_count, int hash_idx,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
typedef struct {
|
||||
/* KDF */
|
||||
fn_kdf_t kdf;
|
||||
/* Hash or HMAC */
|
||||
const char* h;
|
||||
/* cipher */
|
||||
const char* c;
|
||||
unsigned long keylen;
|
||||
/* not used for pbkdf2 */
|
||||
unsigned long blocklen;
|
||||
} pbes_properties;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
pbes_properties type;
|
||||
const void *pwd;
|
||||
unsigned long pwdlen;
|
||||
ltc_asn1_list *enc_data;
|
||||
ltc_asn1_list *salt;
|
||||
ltc_asn1_list *iv;
|
||||
unsigned long iterations;
|
||||
/* only used for RC2 */
|
||||
unsigned long key_bits;
|
||||
} pbes_arg;
|
||||
|
||||
/*
|
||||
* Internal functions
|
||||
*/
|
||||
|
||||
|
||||
/* tomcrypt_cipher.h */
|
||||
|
||||
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
|
||||
int blowfish_expand(const unsigned char *key, int keylen,
|
||||
const unsigned char *data, int datalen,
|
||||
symmetric_key *skey);
|
||||
int blowfish_setup_with_data(const unsigned char *key, int keylen,
|
||||
const unsigned char *data, int datalen,
|
||||
symmetric_key *skey);
|
||||
|
||||
/* tomcrypt_hash.h */
|
||||
|
||||
/* a simple macro for making hash "process" functions */
|
||||
#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
|
||||
int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
|
||||
{ \
|
||||
unsigned long n; \
|
||||
int err; \
|
||||
LTC_ARGCHK(md != NULL); \
|
||||
LTC_ARGCHK(in != NULL); \
|
||||
if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
|
||||
return CRYPT_INVALID_ARG; \
|
||||
} \
|
||||
if ((md-> state_var .length + inlen) < md-> state_var .length) { \
|
||||
return CRYPT_HASH_OVERFLOW; \
|
||||
} \
|
||||
while (inlen > 0) { \
|
||||
if (md-> state_var .curlen == 0 && inlen >= block_size) { \
|
||||
if ((err = compress_name (md, in)) != CRYPT_OK) { \
|
||||
return err; \
|
||||
} \
|
||||
md-> state_var .length += block_size * 8; \
|
||||
in += block_size; \
|
||||
inlen -= block_size; \
|
||||
} else { \
|
||||
n = MIN(inlen, (block_size - md-> state_var .curlen)); \
|
||||
XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
|
||||
md-> state_var .curlen += n; \
|
||||
in += n; \
|
||||
inlen -= n; \
|
||||
if (md-> state_var .curlen == block_size) { \
|
||||
if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
|
||||
return err; \
|
||||
} \
|
||||
md-> state_var .length += 8*block_size; \
|
||||
md-> state_var .curlen = 0; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
return CRYPT_OK; \
|
||||
}
|
||||
|
||||
|
||||
/* tomcrypt_mac.h */
|
||||
|
||||
int ocb3_int_ntz(unsigned long x);
|
||||
void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
|
||||
|
||||
|
||||
/* tomcrypt_math.h */
|
||||
|
||||
#if !defined(DESC_DEF_ONLY)
|
||||
|
||||
#define MP_DIGIT_BIT ltc_mp.bits_per_digit
|
||||
|
||||
/* some handy macros */
|
||||
#define mp_init(a) ltc_mp.init(a)
|
||||
#define mp_init_multi ltc_init_multi
|
||||
#define mp_clear(a) ltc_mp.deinit(a)
|
||||
#define mp_clear_multi ltc_deinit_multi
|
||||
#define mp_cleanup_multi ltc_cleanup_multi
|
||||
#define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
|
||||
|
||||
#define mp_neg(a, b) ltc_mp.neg(a, b)
|
||||
#define mp_copy(a, b) ltc_mp.copy(a, b)
|
||||
|
||||
#define mp_set(a, b) ltc_mp.set_int(a, b)
|
||||
#define mp_set_int(a, b) ltc_mp.set_int(a, b)
|
||||
#define mp_get_int(a) ltc_mp.get_int(a)
|
||||
#define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
|
||||
#define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
|
||||
#define mp_cmp(a, b) ltc_mp.compare(a, b)
|
||||
#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
|
||||
#define mp_count_bits(a) ltc_mp.count_bits(a)
|
||||
#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
|
||||
#define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
|
||||
|
||||
#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
|
||||
#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
|
||||
#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
|
||||
#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
|
||||
#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
|
||||
|
||||
#define mp_add(a, b, c) ltc_mp.add(a, b, c)
|
||||
#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
|
||||
#define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
|
||||
#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
|
||||
#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
|
||||
#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
|
||||
#define mp_sqr(a, b) ltc_mp.sqr(a, b)
|
||||
#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
|
||||
#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
|
||||
#define mp_div_2(a, b) ltc_mp.div_2(a, b)
|
||||
#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
|
||||
#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
|
||||
#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
|
||||
#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
|
||||
|
||||
#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
|
||||
#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
|
||||
#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
|
||||
#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
|
||||
#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
|
||||
|
||||
#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
|
||||
#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
|
||||
#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
|
||||
#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
|
||||
|
||||
#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
|
||||
#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
|
||||
|
||||
#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
|
||||
#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
|
||||
#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
|
||||
|
||||
#define mp_tohex(a, b) mp_toradix(a, b, 16)
|
||||
|
||||
#define mp_rand(a, b) ltc_mp.rand(a, b)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* tomcrypt_misc.h */
|
||||
|
||||
void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
|
||||
|
||||
int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *dec_size);
|
||||
|
||||
int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res);
|
||||
int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res);
|
||||
|
||||
|
||||
/* tomcrypt_pk.h */
|
||||
|
||||
int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
|
||||
int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
|
||||
|
||||
int pk_get_oid(enum ltc_oid_id id, const char **st);
|
||||
int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
|
||||
int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
|
||||
|
||||
/* ---- DH Routines ---- */
|
||||
#ifdef LTC_MRSA
|
||||
int rsa_init(rsa_key *key);
|
||||
void rsa_shrink_key(rsa_key *key);
|
||||
#endif /* LTC_MRSA */
|
||||
|
||||
/* ---- DH Routines ---- */
|
||||
#ifdef LTC_MDH
|
||||
extern const ltc_dh_set_type ltc_dh_sets[];
|
||||
|
||||
int dh_check_pubkey(const dh_key *key);
|
||||
#endif /* LTC_MDH */
|
||||
|
||||
/* ---- ECC Routines ---- */
|
||||
#ifdef LTC_MECC
|
||||
int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
|
||||
int ecc_copy_curve(const ecc_key *srckey, ecc_key *key);
|
||||
int ecc_set_curve_by_size(int size, ecc_key *key);
|
||||
int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
|
||||
|
||||
#ifdef LTC_SSH
|
||||
int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
|
||||
#endif
|
||||
|
||||
/* low level functions */
|
||||
ecc_point *ltc_ecc_new_point(void);
|
||||
void ltc_ecc_del_point(ecc_point *p);
|
||||
int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
|
||||
int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
|
||||
int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
|
||||
int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
|
||||
int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
|
||||
int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
|
||||
int ltc_ecc_verify_key(const ecc_key *key);
|
||||
|
||||
/* point ops (mp == montgomery digit) */
|
||||
#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
|
||||
/* R = 2P */
|
||||
int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *ma, void *modulus, void *mp);
|
||||
|
||||
/* R = P + Q */
|
||||
int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *ma, void *modulus, void *mp);
|
||||
#endif
|
||||
|
||||
#if defined(LTC_MECC_FP)
|
||||
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
|
||||
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
|
||||
|
||||
/* functions for saving/loading/freeing/adding to fixed point cache */
|
||||
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
|
||||
int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
|
||||
void ltc_ecc_fp_free(void);
|
||||
int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
|
||||
|
||||
/* lock/unlock all points currently in fixed point cache */
|
||||
void ltc_ecc_fp_tablelock(int lock);
|
||||
#endif
|
||||
|
||||
/* R = kG */
|
||||
int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
|
||||
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
/* kA*A + kB*B = C */
|
||||
int ltc_ecc_mul2add(const ecc_point *A, void *kA,
|
||||
const ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *ma,
|
||||
void *modulus);
|
||||
|
||||
#ifdef LTC_MECC_FP
|
||||
/* Shamir's trick with optimized point multiplication using fixed point cache */
|
||||
int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
|
||||
const ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *ma,
|
||||
void *modulus);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* map P to affine from projective */
|
||||
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
|
||||
#endif /* LTC_MECC */
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
int dsa_int_validate_xy(const dsa_key *key, int *stat);
|
||||
int dsa_int_validate_pqg(const dsa_key *key, int *stat);
|
||||
int dsa_int_validate_primes(const dsa_key *key, int *stat);
|
||||
#endif /* LTC_MDSA */
|
||||
|
||||
|
||||
#ifdef LTC_CURVE25519
|
||||
|
||||
int tweetnacl_crypto_sign(
|
||||
unsigned char *sm,unsigned long long *smlen,
|
||||
const unsigned char *m,unsigned long long mlen,
|
||||
const unsigned char *sk, const unsigned char *pk);
|
||||
int tweetnacl_crypto_sign_open(
|
||||
int *stat,
|
||||
unsigned char *m,unsigned long long *mlen,
|
||||
const unsigned char *sm,unsigned long long smlen,
|
||||
const unsigned char *pk);
|
||||
int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
|
||||
int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
|
||||
int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
|
||||
int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
|
||||
|
||||
typedef int (*sk_to_pk)(unsigned char *pk ,const unsigned char *sk);
|
||||
int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
|
||||
const void *pwd, unsigned long pwdlen,
|
||||
enum ltc_oid_id id, sk_to_pk fp,
|
||||
curve25519_key *key);
|
||||
int ec25519_export( unsigned char *out, unsigned long *outlen,
|
||||
int which,
|
||||
const curve25519_key *key);
|
||||
#endif /* LTC_CURVE25519 */
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
#define LTC_ASN1_IS_TYPE(e, t) (((e) != NULL) && ((e)->type == (t)))
|
||||
|
||||
/* DER handling */
|
||||
int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
|
||||
ltc_asn1_list *root,
|
||||
ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
|
||||
|
||||
int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
|
||||
int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
|
||||
|
||||
int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
|
||||
int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen);
|
||||
int der_length_asn1_length(unsigned long len, unsigned long *outlen);
|
||||
|
||||
int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen, unsigned long *payloadlen);
|
||||
|
||||
extern const ltc_asn1_type der_asn1_tag_to_type_map[];
|
||||
extern const unsigned long der_asn1_tag_to_type_map_sz;
|
||||
|
||||
extern const int der_asn1_type_to_identifier_map[];
|
||||
extern const unsigned long der_asn1_type_to_identifier_map_sz;
|
||||
|
||||
int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...);
|
||||
|
||||
int der_teletex_char_encode(int c);
|
||||
int der_teletex_value_decode(int v);
|
||||
|
||||
int der_utf8_valid_char(const wchar_t c);
|
||||
|
||||
typedef int (*public_key_decode_cb)(const unsigned char *in, unsigned long inlen, void *ctx);
|
||||
|
||||
int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
|
||||
enum ltc_oid_id algorithm, ltc_asn1_type param_type,
|
||||
ltc_asn1_list* parameters, unsigned long *parameters_len,
|
||||
public_key_decode_cb callback, void *ctx);
|
||||
|
||||
/* SUBJECT PUBLIC KEY INFO */
|
||||
int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
|
||||
unsigned int algorithm, const void* public_key, unsigned long public_key_len,
|
||||
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
|
||||
|
||||
int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
|
||||
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
|
||||
|
||||
int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size);
|
||||
int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
|
||||
|
||||
#endif /* LTC_DER */
|
||||
|
||||
/* tomcrypt_pkcs.h */
|
||||
|
||||
#ifdef LTC_PKCS_8
|
||||
|
||||
int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
|
||||
const void *pwd, unsigned long pwdlen,
|
||||
ltc_asn1_list **decoded_list);
|
||||
|
||||
#endif /* LTC_PKCS_8 */
|
||||
|
||||
|
||||
#ifdef LTC_PKCS_12
|
||||
|
||||
int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int pkcs12_kdf( int hash_id,
|
||||
const unsigned char *pw, unsigned long pwlen,
|
||||
const unsigned char *salt, unsigned long saltlen,
|
||||
unsigned int iterations, unsigned char purpose,
|
||||
unsigned char *out, unsigned long outlen);
|
||||
|
||||
#endif /* LTC_PKCS_12 */
|
||||
|
||||
/* tomcrypt_prng.h */
|
||||
|
||||
#define _LTC_PRNG_EXPORT(which) \
|
||||
int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
|
||||
{ \
|
||||
unsigned long len = which ## _desc.export_size; \
|
||||
\
|
||||
LTC_ARGCHK(prng != NULL); \
|
||||
LTC_ARGCHK(out != NULL); \
|
||||
LTC_ARGCHK(outlen != NULL); \
|
||||
\
|
||||
if (*outlen < len) { \
|
||||
*outlen = len; \
|
||||
return CRYPT_BUFFER_OVERFLOW; \
|
||||
} \
|
||||
\
|
||||
if (which ## _read(out, len, prng) != len) { \
|
||||
return CRYPT_ERROR_READPRNG; \
|
||||
} \
|
||||
\
|
||||
*outlen = len; \
|
||||
return CRYPT_OK; \
|
||||
}
|
||||
|
||||
/* extract a byte portably */
|
||||
#ifdef _MSC_VER
|
||||
#define LTC_BYTE(x, n) ((unsigned char)((x) >> (8 * (n))))
|
||||
#else
|
||||
#define LTC_BYTE(x, n) (((x) >> (8 * (n))) & 255)
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> develop */
|
||||
/* git commit: 0c30412a669d37451341ec871c08974da2451eca */
|
||||
/* commit time: 2019-10-21 11:44:04 +0200 */
|
232
libtom/libtomcrypt/include/tomcrypt_prng.h
Normal file
232
libtom/libtomcrypt/include/tomcrypt_prng.h
Normal file
@ -0,0 +1,232 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* ---- PRNG Stuff ---- */
|
||||
#ifdef LTC_YARROW
|
||||
struct yarrow_prng {
|
||||
int cipher, hash;
|
||||
unsigned char pool[MAXBLOCKSIZE];
|
||||
symmetric_CTR ctr;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RC4
|
||||
struct rc4_prng {
|
||||
rc4_state s;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA20_PRNG
|
||||
struct chacha20_prng {
|
||||
chacha_state s; /* chacha state */
|
||||
unsigned char ent[40]; /* entropy buffer */
|
||||
unsigned long idx; /* entropy counter */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
struct fortuna_prng {
|
||||
hash_state pool[LTC_FORTUNA_POOLS]; /* the pools */
|
||||
|
||||
symmetric_key skey;
|
||||
|
||||
unsigned char K[32], /* the current key */
|
||||
IV[16]; /* IV for CTR mode */
|
||||
|
||||
unsigned long pool_idx, /* current pool we will add to */
|
||||
pool0_len, /* length of 0'th pool */
|
||||
wd;
|
||||
|
||||
ulong64 reset_cnt; /* number of times we have reset */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SOBER128
|
||||
struct sober128_prng {
|
||||
sober128_state s; /* sober128 state */
|
||||
unsigned char ent[40]; /* entropy buffer */
|
||||
unsigned long idx; /* entropy counter */
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
char dummy[1];
|
||||
#ifdef LTC_YARROW
|
||||
struct yarrow_prng yarrow;
|
||||
#endif
|
||||
#ifdef LTC_RC4
|
||||
struct rc4_prng rc4;
|
||||
#endif
|
||||
#ifdef LTC_CHACHA20_PRNG
|
||||
struct chacha20_prng chacha;
|
||||
#endif
|
||||
#ifdef LTC_FORTUNA
|
||||
struct fortuna_prng fortuna;
|
||||
#endif
|
||||
#ifdef LTC_SOBER128
|
||||
struct sober128_prng sober128;
|
||||
#endif
|
||||
};
|
||||
short ready; /* ready flag 0-1 */
|
||||
LTC_MUTEX_TYPE(lock) /* lock */
|
||||
} prng_state;
|
||||
|
||||
/** PRNG descriptor */
|
||||
extern struct ltc_prng_descriptor {
|
||||
/** Name of the PRNG */
|
||||
const char *name;
|
||||
/** size in bytes of exported state */
|
||||
int export_size;
|
||||
/** Start a PRNG state
|
||||
@param prng [out] The state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*start)(prng_state *prng);
|
||||
/** Add entropy to the PRNG
|
||||
@param in The entropy
|
||||
@param inlen Length of the entropy (octets)\
|
||||
@param prng The PRNG state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
/** Ready a PRNG state to read from
|
||||
@param prng The PRNG state to ready
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*ready)(prng_state *prng);
|
||||
/** Read from the PRNG
|
||||
@param out [out] Where to store the data
|
||||
@param outlen Length of data desired (octets)
|
||||
@param prng The PRNG state to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
/** Terminate a PRNG state
|
||||
@param prng The PRNG state to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*done)(prng_state *prng);
|
||||
/** Export a PRNG state
|
||||
@param out [out] The destination for the state
|
||||
@param outlen [in/out] The max size and resulting size of the PRNG state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
/** Import a PRNG state
|
||||
@param in The data to import
|
||||
@param inlen The length of the data to import (octets)
|
||||
@param prng The PRNG to initialize/import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
/** Self-test the PRNG
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int (*test)(void);
|
||||
} prng_descriptor[];
|
||||
|
||||
#ifdef LTC_YARROW
|
||||
int yarrow_start(prng_state *prng);
|
||||
int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int yarrow_ready(prng_state *prng);
|
||||
unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int yarrow_done(prng_state *prng);
|
||||
int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int yarrow_test(void);
|
||||
extern const struct ltc_prng_descriptor yarrow_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
int fortuna_start(prng_state *prng);
|
||||
int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int fortuna_ready(prng_state *prng);
|
||||
unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int fortuna_done(prng_state *prng);
|
||||
int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int fortuna_test(void);
|
||||
extern const struct ltc_prng_descriptor fortuna_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_RC4
|
||||
int rc4_start(prng_state *prng);
|
||||
int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int rc4_ready(prng_state *prng);
|
||||
unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int rc4_done(prng_state *prng);
|
||||
int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int rc4_test(void);
|
||||
extern const struct ltc_prng_descriptor rc4_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA20_PRNG
|
||||
int chacha20_prng_start(prng_state *prng);
|
||||
int chacha20_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int chacha20_prng_ready(prng_state *prng);
|
||||
unsigned long chacha20_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int chacha20_prng_done(prng_state *prng);
|
||||
int chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int chacha20_prng_test(void);
|
||||
extern const struct ltc_prng_descriptor chacha20_prng_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SPRNG
|
||||
int sprng_start(prng_state *prng);
|
||||
int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int sprng_ready(prng_state *prng);
|
||||
unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int sprng_done(prng_state *prng);
|
||||
int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int sprng_test(void);
|
||||
extern const struct ltc_prng_descriptor sprng_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SOBER128
|
||||
int sober128_start(prng_state *prng);
|
||||
int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int sober128_ready(prng_state *prng);
|
||||
unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int sober128_done(prng_state *prng);
|
||||
int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int sober128_test(void);
|
||||
extern const struct ltc_prng_descriptor sober128_desc;
|
||||
#endif
|
||||
|
||||
int find_prng(const char *name);
|
||||
int register_prng(const struct ltc_prng_descriptor *prng);
|
||||
int unregister_prng(const struct ltc_prng_descriptor *prng);
|
||||
int register_all_prngs(void);
|
||||
int prng_is_valid(int idx);
|
||||
LTC_MUTEX_PROTO(ltc_prng_mutex)
|
||||
|
||||
/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
|
||||
* might not work on all platforms as planned
|
||||
*/
|
||||
unsigned long rng_get_bytes(unsigned char *out,
|
||||
unsigned long outlen,
|
||||
void (*callback)(void));
|
||||
|
||||
int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
|
||||
|
||||
#ifdef LTC_PRNG_ENABLE_LTC_RNG
|
||||
extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,
|
||||
void (*callback)(void));
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.0 */
|
||||
/* git commit: 0676c9aec7299f5c398d96cbbb64f7e38f67d73f */
|
||||
/* commit time: 2017-10-10 15:51:36 +0200 */
|
BIN
libtom/libtomcrypt/lib/tomcrypt.lib
Normal file
BIN
libtom/libtomcrypt/lib/tomcrypt.lib
Normal file
Binary file not shown.
781
libtom/libtommath/include/tommath.h
Normal file
781
libtom/libtommath/include/tommath.h
Normal file
@ -0,0 +1,781 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#ifndef BN_H_
|
||||
#define BN_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef LTM_NO_FILE
|
||||
# warning LTM_NO_FILE has been deprecated, use MP_NO_FILE.
|
||||
# define MP_NO_FILE
|
||||
#endif
|
||||
|
||||
#ifndef MP_NO_FILE
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifdef MP_8BIT
|
||||
# ifdef _MSC_VER
|
||||
# pragma message("8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version.")
|
||||
# else
|
||||
# warning "8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version."
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
|
||||
#if (defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT)
|
||||
# define MP_32BIT
|
||||
#endif
|
||||
|
||||
/* detect 64-bit mode if possible */
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
|
||||
defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
|
||||
defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
|
||||
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
|
||||
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
|
||||
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
|
||||
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
|
||||
# if defined(__GNUC__) && !defined(__hppa)
|
||||
/* we support 128bit integers only via: __attribute__((mode(TI))) */
|
||||
# define MP_64BIT
|
||||
# else
|
||||
/* otherwise we fall back to MP_32BIT even on 64bit platforms */
|
||||
# define MP_32BIT
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef MP_DIGIT_BIT
|
||||
# error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT
|
||||
#endif
|
||||
|
||||
/* some default configurations.
|
||||
*
|
||||
* A "mp_digit" must be able to hold MP_DIGIT_BIT + 1 bits
|
||||
* A "mp_word" must be able to hold 2*MP_DIGIT_BIT + 1 bits
|
||||
*
|
||||
* At the very least a mp_digit must be able to hold 7 bits
|
||||
* [any size beyond that is ok provided it doesn't overflow the data type]
|
||||
*/
|
||||
|
||||
#ifdef MP_8BIT
|
||||
typedef uint8_t mp_digit;
|
||||
typedef uint16_t private_mp_word;
|
||||
# define MP_DIGIT_BIT 7
|
||||
#elif defined(MP_16BIT)
|
||||
typedef uint16_t mp_digit;
|
||||
typedef uint32_t private_mp_word;
|
||||
# define MP_DIGIT_BIT 15
|
||||
#elif defined(MP_64BIT)
|
||||
/* for GCC only on supported platforms */
|
||||
typedef uint64_t mp_digit;
|
||||
#if defined(__GNUC__)
|
||||
typedef unsigned long private_mp_word __attribute__((mode(TI)));
|
||||
#endif
|
||||
# define MP_DIGIT_BIT 60
|
||||
#else
|
||||
typedef uint32_t mp_digit;
|
||||
typedef uint64_t private_mp_word;
|
||||
# ifdef MP_31BIT
|
||||
/*
|
||||
* This is an extension that uses 31-bit digits.
|
||||
* Please be aware that not all functions support this size, especially s_mp_mul_digs_fast
|
||||
* will be reduced to work on small numbers only:
|
||||
* Up to 8 limbs, 248 bits instead of up to 512 limbs, 15872 bits with MP_28BIT.
|
||||
*/
|
||||
# define MP_DIGIT_BIT 31
|
||||
# else
|
||||
/* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
|
||||
# define MP_DIGIT_BIT 28
|
||||
# define MP_28BIT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* mp_word is a private type */
|
||||
#define mp_word MP_DEPRECATED_PRAGMA("mp_word has been made private") private_mp_word
|
||||
|
||||
#define MP_SIZEOF_MP_DIGIT (MP_DEPRECATED_PRAGMA("MP_SIZEOF_MP_DIGIT has been deprecated, use sizeof (mp_digit)") sizeof (mp_digit))
|
||||
|
||||
#define MP_MASK ((((mp_digit)1)<<((mp_digit)MP_DIGIT_BIT))-((mp_digit)1))
|
||||
#define MP_DIGIT_MAX MP_MASK
|
||||
|
||||
/* Primality generation flags */
|
||||
#define MP_PRIME_BBS 0x0001 /* BBS style prime */
|
||||
#define MP_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
|
||||
#define MP_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
|
||||
|
||||
#define LTM_PRIME_BBS (MP_DEPRECATED_PRAGMA("LTM_PRIME_BBS has been deprecated, use MP_PRIME_BBS") MP_PRIME_BBS)
|
||||
#define LTM_PRIME_SAFE (MP_DEPRECATED_PRAGMA("LTM_PRIME_SAFE has been deprecated, use MP_PRIME_SAFE") MP_PRIME_SAFE)
|
||||
#define LTM_PRIME_2MSB_ON (MP_DEPRECATED_PRAGMA("LTM_PRIME_2MSB_ON has been deprecated, use MP_PRIME_2MSB_ON") MP_PRIME_2MSB_ON)
|
||||
|
||||
#ifdef MP_USE_ENUMS
|
||||
typedef enum {
|
||||
MP_ZPOS = 0, /* positive */
|
||||
MP_NEG = 1 /* negative */
|
||||
} mp_sign;
|
||||
typedef enum {
|
||||
MP_LT = -1, /* less than */
|
||||
MP_EQ = 0, /* equal */
|
||||
MP_GT = 1 /* greater than */
|
||||
} mp_ord;
|
||||
typedef enum {
|
||||
MP_NO = 0,
|
||||
MP_YES = 1
|
||||
} mp_bool;
|
||||
typedef enum {
|
||||
MP_OKAY = 0, /* no error */
|
||||
MP_ERR = -1, /* unknown error */
|
||||
MP_MEM = -2, /* out of mem */
|
||||
MP_VAL = -3, /* invalid input */
|
||||
MP_ITER = -4, /* maximum iterations reached */
|
||||
MP_BUF = -5 /* buffer overflow, supplied buffer too small */
|
||||
} mp_err;
|
||||
typedef enum {
|
||||
MP_LSB_FIRST = -1,
|
||||
MP_MSB_FIRST = 1
|
||||
} mp_order;
|
||||
typedef enum {
|
||||
MP_LITTLE_ENDIAN = -1,
|
||||
MP_NATIVE_ENDIAN = 0,
|
||||
MP_BIG_ENDIAN = 1
|
||||
} mp_endian;
|
||||
#else
|
||||
typedef int mp_sign;
|
||||
#define MP_ZPOS 0 /* positive integer */
|
||||
#define MP_NEG 1 /* negative */
|
||||
typedef int mp_ord;
|
||||
#define MP_LT -1 /* less than */
|
||||
#define MP_EQ 0 /* equal to */
|
||||
#define MP_GT 1 /* greater than */
|
||||
typedef int mp_bool;
|
||||
#define MP_YES 1
|
||||
#define MP_NO 0
|
||||
typedef int mp_err;
|
||||
#define MP_OKAY 0 /* no error */
|
||||
#define MP_ERR -1 /* unknown error */
|
||||
#define MP_MEM -2 /* out of mem */
|
||||
#define MP_VAL -3 /* invalid input */
|
||||
#define MP_RANGE (MP_DEPRECATED_PRAGMA("MP_RANGE has been deprecated in favor of MP_VAL") MP_VAL)
|
||||
#define MP_ITER -4 /* maximum iterations reached */
|
||||
#define MP_BUF -5 /* buffer overflow, supplied buffer too small */
|
||||
typedef int mp_order;
|
||||
#define MP_LSB_FIRST -1
|
||||
#define MP_MSB_FIRST 1
|
||||
typedef int mp_endian;
|
||||
#define MP_LITTLE_ENDIAN -1
|
||||
#define MP_NATIVE_ENDIAN 0
|
||||
#define MP_BIG_ENDIAN 1
|
||||
#endif
|
||||
|
||||
/* tunable cutoffs */
|
||||
|
||||
#ifndef MP_FIXED_CUTOFFS
|
||||
extern int
|
||||
KARATSUBA_MUL_CUTOFF,
|
||||
KARATSUBA_SQR_CUTOFF,
|
||||
TOOM_MUL_CUTOFF,
|
||||
TOOM_SQR_CUTOFF;
|
||||
#endif
|
||||
|
||||
/* define this to use lower memory usage routines (exptmods mostly) */
|
||||
/* #define MP_LOW_MEM */
|
||||
|
||||
/* default precision */
|
||||
#ifndef MP_PREC
|
||||
# ifndef MP_LOW_MEM
|
||||
# define PRIVATE_MP_PREC 32 /* default digits of precision */
|
||||
# elif defined(MP_8BIT)
|
||||
# define PRIVATE_MP_PREC 16 /* default digits of precision */
|
||||
# else
|
||||
# define PRIVATE_MP_PREC 8 /* default digits of precision */
|
||||
# endif
|
||||
# define MP_PREC (MP_DEPRECATED_PRAGMA("MP_PREC is an internal macro") PRIVATE_MP_PREC)
|
||||
#endif
|
||||
|
||||
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
|
||||
#define PRIVATE_MP_WARRAY (int)(1uLL << (((CHAR_BIT * sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1))
|
||||
#define MP_WARRAY (MP_DEPRECATED_PRAGMA("MP_WARRAY is an internal macro") PRIVATE_MP_WARRAY)
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_NULL_TERMINATED __attribute__((sentinel))
|
||||
#else
|
||||
# define MP_NULL_TERMINATED
|
||||
#endif
|
||||
|
||||
/*
|
||||
* MP_WUR - warn unused result
|
||||
* ---------------------------
|
||||
*
|
||||
* The result of functions annotated with MP_WUR must be
|
||||
* checked and cannot be ignored.
|
||||
*
|
||||
* Most functions in libtommath return an error code.
|
||||
* This error code must be checked in order to prevent crashes or invalid
|
||||
* results.
|
||||
*
|
||||
* If you still want to avoid the error checks for quick and dirty programs
|
||||
* without robustness guarantees, you can `#define MP_WUR` before including
|
||||
* tommath.h, disabling the warnings.
|
||||
*/
|
||||
#ifndef MP_WUR
|
||||
# if defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_WUR __attribute__((warn_unused_result))
|
||||
# else
|
||||
# define MP_WUR
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
|
||||
# define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x)))
|
||||
# define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s)
|
||||
# define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s)
|
||||
#elif defined(_MSC_VER) && _MSC_VER >= 1500
|
||||
# define MP_DEPRECATED(x) __declspec(deprecated("replaced by " #x))
|
||||
# define MP_DEPRECATED_PRAGMA(s) __pragma(message(s))
|
||||
#else
|
||||
# define MP_DEPRECATED(s)
|
||||
# define MP_DEPRECATED_PRAGMA(s)
|
||||
#endif
|
||||
|
||||
#define DIGIT_BIT (MP_DEPRECATED_PRAGMA("DIGIT_BIT macro is deprecated, MP_DIGIT_BIT instead") MP_DIGIT_BIT)
|
||||
#define USED(m) (MP_DEPRECATED_PRAGMA("USED macro is deprecated, use z->used instead") (m)->used)
|
||||
#define DIGIT(m, k) (MP_DEPRECATED_PRAGMA("DIGIT macro is deprecated, use z->dp instead") (m)->dp[(k)])
|
||||
#define SIGN(m) (MP_DEPRECATED_PRAGMA("SIGN macro is deprecated, use z->sign instead") (m)->sign)
|
||||
|
||||
/* the infamous mp_int structure */
|
||||
typedef struct {
|
||||
int used, alloc;
|
||||
mp_sign sign;
|
||||
mp_digit *dp;
|
||||
} mp_int;
|
||||
|
||||
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
|
||||
typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat);
|
||||
typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source) ltm_prime_callback;
|
||||
|
||||
/* error code to char* string */
|
||||
const char *mp_error_to_string(mp_err code) MP_WUR;
|
||||
|
||||
/* ---> init and deinit bignum functions <--- */
|
||||
/* init a bignum */
|
||||
mp_err mp_init(mp_int *a) MP_WUR;
|
||||
|
||||
/* free a bignum */
|
||||
void mp_clear(mp_int *a);
|
||||
|
||||
/* init a null terminated series of arguments */
|
||||
mp_err mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED MP_WUR;
|
||||
|
||||
/* clear a null terminated series of arguments */
|
||||
void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
|
||||
|
||||
/* exchange two ints */
|
||||
void mp_exch(mp_int *a, mp_int *b);
|
||||
|
||||
/* shrink ram required for a bignum */
|
||||
mp_err mp_shrink(mp_int *a) MP_WUR;
|
||||
|
||||
/* grow an int to a given size */
|
||||
mp_err mp_grow(mp_int *a, int size) MP_WUR;
|
||||
|
||||
/* init to a given number of digits */
|
||||
mp_err mp_init_size(mp_int *a, int size) MP_WUR;
|
||||
|
||||
/* ---> Basic Manipulations <--- */
|
||||
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
|
||||
mp_bool mp_iseven(const mp_int *a) MP_WUR;
|
||||
mp_bool mp_isodd(const mp_int *a) MP_WUR;
|
||||
#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
|
||||
|
||||
/* set to zero */
|
||||
void mp_zero(mp_int *a);
|
||||
|
||||
/* get and set doubles */
|
||||
double mp_get_double(const mp_int *a) MP_WUR;
|
||||
mp_err mp_set_double(mp_int *a, double b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (int32_t) */
|
||||
int32_t mp_get_i32(const mp_int *a) MP_WUR;
|
||||
void mp_set_i32(mp_int *a, int32_t b);
|
||||
mp_err mp_init_i32(mp_int *a, int32_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */
|
||||
#define mp_get_u32(a) ((uint32_t)mp_get_i32(a))
|
||||
void mp_set_u32(mp_int *a, uint32_t b);
|
||||
mp_err mp_init_u32(mp_int *a, uint32_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (int64_t) */
|
||||
int64_t mp_get_i64(const mp_int *a) MP_WUR;
|
||||
void mp_set_i64(mp_int *a, int64_t b);
|
||||
mp_err mp_init_i64(mp_int *a, int64_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */
|
||||
#define mp_get_u64(a) ((uint64_t)mp_get_i64(a))
|
||||
void mp_set_u64(mp_int *a, uint64_t b);
|
||||
mp_err mp_init_u64(mp_int *a, uint64_t b) MP_WUR;
|
||||
|
||||
/* get magnitude */
|
||||
uint32_t mp_get_mag_u32(const mp_int *a) MP_WUR;
|
||||
uint64_t mp_get_mag_u64(const mp_int *a) MP_WUR;
|
||||
unsigned long mp_get_mag_ul(const mp_int *a) MP_WUR;
|
||||
unsigned long long mp_get_mag_ull(const mp_int *a) MP_WUR;
|
||||
|
||||
/* get integer, set integer (long) */
|
||||
long mp_get_l(const mp_int *a) MP_WUR;
|
||||
void mp_set_l(mp_int *a, long b);
|
||||
mp_err mp_init_l(mp_int *a, long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (unsigned long) */
|
||||
#define mp_get_ul(a) ((unsigned long)mp_get_l(a))
|
||||
void mp_set_ul(mp_int *a, unsigned long b);
|
||||
mp_err mp_init_ul(mp_int *a, unsigned long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (long long) */
|
||||
long long mp_get_ll(const mp_int *a) MP_WUR;
|
||||
void mp_set_ll(mp_int *a, long long b);
|
||||
mp_err mp_init_ll(mp_int *a, long long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (unsigned long long) */
|
||||
#define mp_get_ull(a) ((unsigned long long)mp_get_ll(a))
|
||||
void mp_set_ull(mp_int *a, unsigned long long b);
|
||||
mp_err mp_init_ull(mp_int *a, unsigned long long b) MP_WUR;
|
||||
|
||||
/* set to single unsigned digit, up to MP_DIGIT_MAX */
|
||||
void mp_set(mp_int *a, mp_digit b);
|
||||
mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (deprecated) */
|
||||
MP_DEPRECATED(mp_get_mag_u32/mp_get_u32) unsigned long mp_get_int(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_get_mag_ul/mp_get_ul) unsigned long mp_get_long(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_get_mag_ull/mp_get_ull) unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int *a, unsigned long b);
|
||||
MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int *a, unsigned long b);
|
||||
MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int *a, unsigned long long b);
|
||||
MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
|
||||
|
||||
/* copy, b = a */
|
||||
mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* inits and copies, a = b */
|
||||
mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* trim unused digits */
|
||||
void mp_clamp(mp_int *a);
|
||||
|
||||
|
||||
/* export binary data */
|
||||
MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
|
||||
int endian, size_t nails, const mp_int *op) MP_WUR;
|
||||
|
||||
/* import binary data */
|
||||
MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order,
|
||||
size_t size, int endian, size_t nails,
|
||||
const void *op) MP_WUR;
|
||||
|
||||
/* unpack binary data */
|
||||
mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian,
|
||||
size_t nails, const void *op) MP_WUR;
|
||||
|
||||
/* pack binary data */
|
||||
size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) MP_WUR;
|
||||
mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size,
|
||||
mp_endian endian, size_t nails, const mp_int *op) MP_WUR;
|
||||
|
||||
/* ---> digit manipulation <--- */
|
||||
|
||||
/* right shift by "b" digits */
|
||||
void mp_rshd(mp_int *a, int b);
|
||||
|
||||
/* left shift by "b" digits */
|
||||
mp_err mp_lshd(mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* c = a / 2**b, implemented as c = a >> b */
|
||||
mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* b = a/2 */
|
||||
mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* a/3 => 3c + d == a */
|
||||
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
|
||||
|
||||
/* c = a * 2**b, implemented as c = a << b */
|
||||
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = a*2 */
|
||||
mp_err mp_mul_2(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* c = a mod 2**b */
|
||||
mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* computes a = 2**b */
|
||||
mp_err mp_2expt(mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* Counts the number of lsbs which are zero before the first zero bit */
|
||||
int mp_cnt_lsb(const mp_int *a) MP_WUR;
|
||||
|
||||
/* I Love Earth! */
|
||||
|
||||
/* makes a pseudo-random mp_int of a given size */
|
||||
mp_err mp_rand(mp_int *a, int digits) MP_WUR;
|
||||
/* makes a pseudo-random small int of a given size */
|
||||
MP_DEPRECATED(mp_rand) mp_err mp_rand_digit(mp_digit *r) MP_WUR;
|
||||
/* use custom random data source instead of source provided the platform */
|
||||
void mp_rand_source(mp_err(*source)(void *out, size_t size));
|
||||
|
||||
#ifdef MP_PRNG_ENABLE_LTM_RNG
|
||||
# warning MP_PRNG_ENABLE_LTM_RNG has been deprecated, use mp_rand_source instead.
|
||||
/* A last resort to provide random data on systems without any of the other
|
||||
* implemented ways to gather entropy.
|
||||
* It is compatible with `rng_get_bytes()` from libtomcrypt so you could
|
||||
* provide that one and then set `ltm_rng = rng_get_bytes;` */
|
||||
extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
|
||||
extern void (*ltm_rng_callback)(void);
|
||||
#endif
|
||||
|
||||
/* ---> binary operations <--- */
|
||||
|
||||
/* Checks the bit at position b and returns MP_YES
|
||||
* if the bit is 1, MP_NO if it is 0 and MP_VAL
|
||||
* in case of error
|
||||
*/
|
||||
MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* c = a XOR b (two complement) */
|
||||
MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a OR b (two complement) */
|
||||
MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a AND b (two complement) */
|
||||
MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = ~a (bitwise not, two complement) */
|
||||
mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* right shift with sign extension */
|
||||
MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* ---> Basic arithmetic <--- */
|
||||
|
||||
/* b = -a */
|
||||
mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* b = |a| */
|
||||
mp_err mp_abs(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* compare a to b */
|
||||
mp_ord mp_cmp(const mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* compare |a| to |b| */
|
||||
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* c = a + b */
|
||||
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a - b */
|
||||
mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a * b */
|
||||
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = a*a */
|
||||
mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* a/b => cb + d == a */
|
||||
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* Increment "a" by one like "a++". Changes input! */
|
||||
mp_err mp_incr(mp_int *a) MP_WUR;
|
||||
|
||||
/* Decrement "a" by one like "a--". Changes input! */
|
||||
mp_err mp_decr(mp_int *a) MP_WUR;
|
||||
|
||||
/* ---> single digit functions <--- */
|
||||
|
||||
/* compare against a single digit */
|
||||
mp_ord mp_cmp_d(const mp_int *a, mp_digit b) MP_WUR;
|
||||
|
||||
/* c = a + b */
|
||||
mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a - b */
|
||||
mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a * b */
|
||||
mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* a/b => cb + d == a */
|
||||
mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR;
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
|
||||
|
||||
/* ---> number theory <--- */
|
||||
|
||||
/* d = a + b (mod c) */
|
||||
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* d = a - b (mod c) */
|
||||
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* d = a * b (mod c) */
|
||||
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* c = a * a (mod b) */
|
||||
mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = 1/a (mod b) */
|
||||
mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = (a, b) */
|
||||
mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* produces value such that U1*a + U2*b = U3 */
|
||||
mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR;
|
||||
|
||||
/* c = [a, b] or (a*b)/(a, b) */
|
||||
mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* finds one of the b'th root of a, such that |c|**b <= |a|
|
||||
*
|
||||
* returns error if a < 0 and b is even
|
||||
*/
|
||||
mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
|
||||
|
||||
/* special sqrt algo */
|
||||
mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
|
||||
|
||||
/* special sqrt (mod prime) */
|
||||
mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR;
|
||||
|
||||
/* is number a square? */
|
||||
mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR;
|
||||
|
||||
/* computes the jacobi c = (a | n) (or Legendre if b is prime) */
|
||||
MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR;
|
||||
|
||||
/* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
|
||||
mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR;
|
||||
|
||||
/* used to setup the Barrett reduction for a given modulus b */
|
||||
mp_err mp_reduce_setup(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* Barrett Reduction, computes a (mod b) with a precomputed value c
|
||||
*
|
||||
* Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
|
||||
* compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
|
||||
*/
|
||||
mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) MP_WUR;
|
||||
|
||||
/* setups the montgomery reduction */
|
||||
mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR;
|
||||
|
||||
/* computes a = B**n mod b without division or multiplication useful for
|
||||
* normalizing numbers in a Montgomery system.
|
||||
*/
|
||||
mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* computes x/R == x (mod N) via Montgomery Reduction */
|
||||
mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
|
||||
|
||||
/* returns 1 if a is a valid DR modulus */
|
||||
mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
|
||||
|
||||
/* sets the value of "d" required for mp_dr_reduce */
|
||||
void mp_dr_setup(const mp_int *a, mp_digit *d);
|
||||
|
||||
/* reduces a modulo n using the Diminished Radix method */
|
||||
mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR;
|
||||
|
||||
/* returns true if a can be reduced with mp_reduce_2k */
|
||||
mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
|
||||
|
||||
/* determines k value for 2k reduction */
|
||||
mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
|
||||
|
||||
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
||||
mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR;
|
||||
|
||||
/* returns true if a can be reduced with mp_reduce_2k_l */
|
||||
mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
|
||||
|
||||
/* determines k value for 2k reduction */
|
||||
mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
|
||||
|
||||
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
||||
mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) MP_WUR;
|
||||
|
||||
/* Y = G**X (mod P) */
|
||||
mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR;
|
||||
|
||||
/* ---> Primes <--- */
|
||||
|
||||
/* number of primes */
|
||||
#ifdef MP_8BIT
|
||||
# define PRIVATE_MP_PRIME_TAB_SIZE 31
|
||||
#else
|
||||
# define PRIVATE_MP_PRIME_TAB_SIZE 256
|
||||
#endif
|
||||
#define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE)
|
||||
|
||||
/* table of first PRIME_SIZE primes */
|
||||
MP_DEPRECATED(internal) extern const mp_digit ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE];
|
||||
|
||||
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
|
||||
MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Fermat test of "a" using base "b".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Miller-Rabin test of "a" using base "b".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
|
||||
|
||||
/* This gives [for a given bit size] the number of trials required
|
||||
* such that Miller-Rabin gives a prob of failure lower than 2^-96
|
||||
*/
|
||||
int mp_prime_rabin_miller_trials(int size) MP_WUR;
|
||||
|
||||
/* performs one strong Lucas-Selfridge test of "a".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Frobenius test of "a" as described by Paul Underwood.
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs t random rounds of Miller-Rabin on "a" additional to
|
||||
* bases 2 and 3. Also performs an initial sieve of trial
|
||||
* division. Determines if "a" is prime with probability
|
||||
* of error no more than (1/4)**t.
|
||||
* Both a strong Lucas-Selfridge to complete the BPSW test
|
||||
* and a separate Frobenius test are available at compile time.
|
||||
* With t<0 a deterministic test is run for primes up to
|
||||
* 318665857834031151167461. With t<13 (abs(t)-13) additional
|
||||
* tests with sequential small primes are run starting at 43.
|
||||
* Is Fips 186.4 compliant if called with t as computed by
|
||||
* mp_prime_rabin_miller_trials();
|
||||
*
|
||||
* Sets result to 1 if probably prime, 0 otherwise
|
||||
*/
|
||||
mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
|
||||
|
||||
/* finds the next prime after the number "a" using "t" trials
|
||||
* of Miller-Rabin.
|
||||
*
|
||||
* bbs_style = 1 means the prime must be congruent to 3 mod 4
|
||||
*/
|
||||
mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
|
||||
|
||||
/* makes a truly random prime of a given size (bytes),
|
||||
* call with bbs = 1 if you want it to be congruent to 3 mod 4
|
||||
*
|
||||
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
||||
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
||||
* so it can be NULL
|
||||
*
|
||||
* The prime generated will be larger than 2^(8*size).
|
||||
*/
|
||||
#define mp_prime_random(a, t, size, bbs, cb, dat) (MP_DEPRECATED_PRAGMA("mp_prime_random has been deprecated, use mp_prime_rand instead") mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?MP_PRIME_BBS:0, cb, dat))
|
||||
|
||||
/* makes a truly random prime of a given size (bits),
|
||||
*
|
||||
* Flags are as follows:
|
||||
*
|
||||
* MP_PRIME_BBS - make prime congruent to 3 mod 4
|
||||
* MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS)
|
||||
* MP_PRIME_2MSB_ON - make the 2nd highest bit one
|
||||
*
|
||||
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
||||
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
||||
* so it can be NULL
|
||||
*
|
||||
*/
|
||||
MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags,
|
||||
private_mp_prime_callback cb, void *dat) MP_WUR;
|
||||
mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR;
|
||||
|
||||
/* Integer logarithm to integer base */
|
||||
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
|
||||
|
||||
/* c = a**b */
|
||||
mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
|
||||
|
||||
/* ---> radix conversion <--- */
|
||||
int mp_count_bits(const mp_int *a) MP_WUR;
|
||||
|
||||
|
||||
MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
|
||||
|
||||
MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
|
||||
|
||||
size_t mp_ubin_size(const mp_int *a) MP_WUR;
|
||||
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
|
||||
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
|
||||
|
||||
size_t mp_sbin_size(const mp_int *a) MP_WUR;
|
||||
mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
|
||||
mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
|
||||
|
||||
mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
|
||||
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
|
||||
mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
|
||||
|
||||
#ifndef MP_NO_FILE
|
||||
mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR;
|
||||
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
|
||||
#endif
|
||||
|
||||
#define mp_read_raw(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_signed_bin") mp_read_signed_bin((mp), (str), (len)))
|
||||
#define mp_raw_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_signed_bin_size") mp_signed_bin_size(mp))
|
||||
#define mp_toraw(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_signed_bin") mp_to_signed_bin((mp), (str)))
|
||||
#define mp_read_mag(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_unsigned_bin") mp_read_unsigned_bin((mp), (str), (len))
|
||||
#define mp_mag_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp))
|
||||
#define mp_tomag(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str)))
|
||||
|
||||
#define mp_tobinary(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary") mp_toradix((M), (S), 2))
|
||||
#define mp_tooctal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal") mp_toradix((M), (S), 8))
|
||||
#define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10))
|
||||
#define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16))
|
||||
|
||||
#define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), NULL, 2)
|
||||
#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8)
|
||||
#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10)
|
||||
#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1319
libtom/libtommath/include/tommath_class.h
Normal file
1319
libtom/libtommath/include/tommath_class.h
Normal file
File diff suppressed because it is too large
Load Diff
13
libtom/libtommath/include/tommath_cutoffs.h
Normal file
13
libtom/libtommath/include/tommath_cutoffs.h
Normal file
@ -0,0 +1,13 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
/*
|
||||
Current values evaluated on an AMD A8-6600K (64-bit).
|
||||
Type "make tune" to optimize them for your machine but
|
||||
be aware that it may take a long time. It took 2:30 minutes
|
||||
on the aforementioned machine for example.
|
||||
*/
|
||||
|
||||
#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF 80
|
||||
#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF 120
|
||||
#define MP_DEFAULT_TOOM_MUL_CUTOFF 350
|
||||
#define MP_DEFAULT_TOOM_SQR_CUTOFF 400
|
303
libtom/libtommath/include/tommath_private.h
Normal file
303
libtom/libtommath/include/tommath_private.h
Normal file
@ -0,0 +1,303 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#ifndef TOMMATH_PRIV_H_
|
||||
#define TOMMATH_PRIV_H_
|
||||
|
||||
#include "tommath.h"
|
||||
#include "tommath_class.h"
|
||||
|
||||
/*
|
||||
* Private symbols
|
||||
* ---------------
|
||||
*
|
||||
* On Unix symbols can be marked as hidden if libtommath is compiled
|
||||
* as a shared object. By default, symbols are visible.
|
||||
* As of now, this feature is opt-in via the MP_PRIVATE_SYMBOLS define.
|
||||
*
|
||||
* On Win32 a .def file must be used to specify the exported symbols.
|
||||
*/
|
||||
#if defined (MP_PRIVATE_SYMBOLS) && defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_PRIVATE __attribute__ ((visibility ("hidden")))
|
||||
#else
|
||||
# define MP_PRIVATE
|
||||
#endif
|
||||
|
||||
/* Hardening libtommath
|
||||
* --------------------
|
||||
*
|
||||
* By default memory is zeroed before calling
|
||||
* MP_FREE to avoid leaking data. This is good
|
||||
* practice in cryptographical applications.
|
||||
*
|
||||
* Note however that memory allocators used
|
||||
* in cryptographical applications can often
|
||||
* be configured by itself to clear memory,
|
||||
* rendering the clearing in tommath unnecessary.
|
||||
* See for example https://github.com/GrapheneOS/hardened_malloc
|
||||
* and the option CONFIG_ZERO_ON_FREE.
|
||||
*
|
||||
* Furthermore there are applications which
|
||||
* value performance more and want this
|
||||
* feature to be disabled. For such applications
|
||||
* define MP_NO_ZERO_ON_FREE during compilation.
|
||||
*/
|
||||
#ifdef MP_NO_ZERO_ON_FREE
|
||||
# define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
|
||||
# define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
|
||||
#else
|
||||
# define MP_FREE_BUFFER(mem, size) \
|
||||
do { \
|
||||
size_t fs_ = (size); \
|
||||
void* fm_ = (mem); \
|
||||
if (fm_ != NULL) { \
|
||||
MP_ZERO_BUFFER(fm_, fs_); \
|
||||
MP_FREE(fm_, fs_); \
|
||||
} \
|
||||
} while (0)
|
||||
# define MP_FREE_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int fd_ = (digits); \
|
||||
void* fm_ = (mem); \
|
||||
if (fm_ != NULL) { \
|
||||
size_t fs_ = sizeof (mp_digit) * (size_t)fd_; \
|
||||
MP_ZERO_BUFFER(fm_, fs_); \
|
||||
MP_FREE(fm_, fs_); \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifdef MP_USE_MEMSET
|
||||
# include <string.h>
|
||||
# define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
|
||||
# define MP_ZERO_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int zd_ = (digits); \
|
||||
if (zd_ > 0) { \
|
||||
memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
|
||||
} \
|
||||
} while (0)
|
||||
#else
|
||||
# define MP_ZERO_BUFFER(mem, size) \
|
||||
do { \
|
||||
size_t zs_ = (size); \
|
||||
char* zm_ = (char*)(mem); \
|
||||
while (zs_-- > 0u) { \
|
||||
*zm_++ = '\0'; \
|
||||
} \
|
||||
} while (0)
|
||||
# define MP_ZERO_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int zd_ = (digits); \
|
||||
mp_digit* zm_ = (mem); \
|
||||
while (zd_-- > 0) { \
|
||||
*zm_++ = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
/* Tunable cutoffs
|
||||
* ---------------
|
||||
*
|
||||
* - In the default settings, a cutoff X can be modified at runtime
|
||||
* by adjusting the corresponding X_CUTOFF variable.
|
||||
*
|
||||
* - Tunability of the library can be disabled at compile time
|
||||
* by defining the MP_FIXED_CUTOFFS macro.
|
||||
*
|
||||
* - There is an additional file tommath_cutoffs.h, which defines
|
||||
* the default cutoffs. These can be adjusted manually or by the
|
||||
* autotuner.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef MP_FIXED_CUTOFFS
|
||||
# include "tommath_cutoffs.h"
|
||||
# define MP_KARATSUBA_MUL_CUTOFF MP_DEFAULT_KARATSUBA_MUL_CUTOFF
|
||||
# define MP_KARATSUBA_SQR_CUTOFF MP_DEFAULT_KARATSUBA_SQR_CUTOFF
|
||||
# define MP_TOOM_MUL_CUTOFF MP_DEFAULT_TOOM_MUL_CUTOFF
|
||||
# define MP_TOOM_SQR_CUTOFF MP_DEFAULT_TOOM_SQR_CUTOFF
|
||||
#else
|
||||
# define MP_KARATSUBA_MUL_CUTOFF KARATSUBA_MUL_CUTOFF
|
||||
# define MP_KARATSUBA_SQR_CUTOFF KARATSUBA_SQR_CUTOFF
|
||||
# define MP_TOOM_MUL_CUTOFF TOOM_MUL_CUTOFF
|
||||
# define MP_TOOM_SQR_CUTOFF TOOM_SQR_CUTOFF
|
||||
#endif
|
||||
|
||||
/* define heap macros */
|
||||
#ifndef MP_MALLOC
|
||||
/* default to libc stuff */
|
||||
# include <stdlib.h>
|
||||
# define MP_MALLOC(size) malloc(size)
|
||||
# define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize))
|
||||
# define MP_CALLOC(nmemb, size) calloc((nmemb), (size))
|
||||
# define MP_FREE(mem, size) free(mem)
|
||||
#else
|
||||
/* prototypes for our heap functions */
|
||||
extern void *MP_MALLOC(size_t size);
|
||||
extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
|
||||
extern void *MP_CALLOC(size_t nmemb, size_t size);
|
||||
extern void MP_FREE(void *mem, size_t size);
|
||||
#endif
|
||||
|
||||
/* feature detection macro */
|
||||
#ifdef _MSC_VER
|
||||
/* Prevent false positive: not enough arguments for function-like macro invocation */
|
||||
#pragma warning(disable: 4003)
|
||||
#endif
|
||||
#define MP_STRINGIZE(x) MP__STRINGIZE(x)
|
||||
#define MP__STRINGIZE(x) ""#x""
|
||||
#define MP_HAS(x) (sizeof(MP_STRINGIZE(BN_##x##_C)) == 1u)
|
||||
|
||||
/* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */
|
||||
#undef mp_word
|
||||
typedef private_mp_word mp_word;
|
||||
|
||||
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
|
||||
/* Static assertion */
|
||||
#define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];
|
||||
|
||||
/* ---> Basic Manipulations <--- */
|
||||
#define MP_IS_ZERO(a) ((a)->used == 0)
|
||||
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
|
||||
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
|
||||
|
||||
#define MP_SIZEOF_BITS(type) ((size_t)CHAR_BIT * sizeof(type))
|
||||
#define MP_MAXFAST (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
|
||||
|
||||
/* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
|
||||
#undef MP_WARRAY
|
||||
#define MP_WARRAY PRIVATE_MP_WARRAY
|
||||
|
||||
/* TODO: Remove PRIVATE_MP_PREC as soon as deprecated MP_PREC is removed from tommath.h */
|
||||
#ifdef PRIVATE_MP_PREC
|
||||
# undef MP_PREC
|
||||
# define MP_PREC PRIVATE_MP_PREC
|
||||
#endif
|
||||
|
||||
/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
|
||||
#define MP_MIN_PREC ((((int)MP_SIZEOF_BITS(long long) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
|
||||
|
||||
MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
|
||||
|
||||
/* random number source */
|
||||
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
|
||||
|
||||
/* lowlevel functions, do not call! */
|
||||
MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
|
||||
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat);
|
||||
MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
|
||||
MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
|
||||
|
||||
/* TODO: jenkins prng is not thread safe as of now */
|
||||
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
|
||||
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
|
||||
|
||||
extern MP_PRIVATE const char *const mp_s_rmap;
|
||||
extern MP_PRIVATE const uint8_t mp_s_rmap_reverse[];
|
||||
extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
|
||||
extern MP_PRIVATE const mp_digit *s_mp_prime_tab;
|
||||
|
||||
/* deprecated functions */
|
||||
MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n,
|
||||
mp_digit rho);
|
||||
MP_DEPRECATED(s_mp_mul_digs_fast) mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c,
|
||||
int digs);
|
||||
MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b,
|
||||
mp_int *c,
|
||||
int digs);
|
||||
MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P,
|
||||
mp_int *Y,
|
||||
int redmode);
|
||||
MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
|
||||
|
||||
#define MP_GET_ENDIANNESS(x) \
|
||||
do{\
|
||||
int16_t n = 0x1; \
|
||||
char *p = (char *)&n; \
|
||||
x = (p[0] == '\x01') ? MP_LITTLE_ENDIAN : MP_BIG_ENDIAN; \
|
||||
} while (0)
|
||||
|
||||
/* code-generating macros */
|
||||
#define MP_SET_UNSIGNED(name, type) \
|
||||
void name(mp_int * a, type b) \
|
||||
{ \
|
||||
int i = 0; \
|
||||
while (b != 0u) { \
|
||||
a->dp[i++] = ((mp_digit)b & MP_MASK); \
|
||||
if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
|
||||
b >>= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
|
||||
} \
|
||||
a->used = i; \
|
||||
a->sign = MP_ZPOS; \
|
||||
MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
|
||||
}
|
||||
|
||||
#define MP_SET_SIGNED(name, uname, type, utype) \
|
||||
void name(mp_int * a, type b) \
|
||||
{ \
|
||||
uname(a, (b < 0) ? -(utype)b : (utype)b); \
|
||||
if (b < 0) { a->sign = MP_NEG; } \
|
||||
}
|
||||
|
||||
#define MP_INIT_INT(name , set, type) \
|
||||
mp_err name(mp_int * a, type b) \
|
||||
{ \
|
||||
mp_err err; \
|
||||
if ((err = mp_init(a)) != MP_OKAY) { \
|
||||
return err; \
|
||||
} \
|
||||
set(a, b); \
|
||||
return MP_OKAY; \
|
||||
}
|
||||
|
||||
#define MP_GET_MAG(name, type) \
|
||||
type name(const mp_int* a) \
|
||||
{ \
|
||||
unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
|
||||
type res = 0u; \
|
||||
while (i --> 0u) { \
|
||||
res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
|
||||
res |= (type)a->dp[i]; \
|
||||
if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
|
||||
} \
|
||||
return res; \
|
||||
}
|
||||
|
||||
#define MP_GET_SIGNED(name, mag, type, utype) \
|
||||
type name(const mp_int* a) \
|
||||
{ \
|
||||
utype res = mag(a); \
|
||||
return (a->sign == MP_NEG) ? (type)-res : (type)res; \
|
||||
}
|
||||
|
||||
#endif
|
110
libtom/libtommath/include/tommath_superclass.h
Normal file
110
libtom/libtommath/include/tommath_superclass.h
Normal file
@ -0,0 +1,110 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
/* super class file for PK algos */
|
||||
|
||||
/* default ... include all MPI */
|
||||
#ifndef LTM_NOTHING
|
||||
#define LTM_ALL
|
||||
#endif
|
||||
|
||||
/* RSA only (does not support DH/DSA/ECC) */
|
||||
/* #define SC_RSA_1 */
|
||||
/* #define SC_RSA_1_WITH_TESTS */
|
||||
|
||||
/* For reference.... On an Athlon64 optimizing for speed...
|
||||
|
||||
LTM's mpi.o with all functions [striped] is 142KiB in size.
|
||||
|
||||
*/
|
||||
|
||||
#ifdef SC_RSA_1_WITH_TESTS
|
||||
# define BN_MP_ERROR_TO_STRING_C
|
||||
# define BN_MP_FREAD_C
|
||||
# define BN_MP_FWRITE_C
|
||||
# define BN_MP_INCR_C
|
||||
# define BN_MP_ISEVEN_C
|
||||
# define BN_MP_ISODD_C
|
||||
# define BN_MP_NEG_C
|
||||
# define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C
|
||||
# define BN_MP_RADIX_SIZE_C
|
||||
# define BN_MP_RAND_C
|
||||
# define BN_MP_REDUCE_C
|
||||
# define BN_MP_REDUCE_2K_L_C
|
||||
# define BN_MP_FROM_SBIN_C
|
||||
# define BN_MP_ROOT_U32_C
|
||||
# define BN_MP_SET_L_C
|
||||
# define BN_MP_SET_UL_C
|
||||
# define BN_MP_SBIN_SIZE_C
|
||||
# define BN_MP_TO_RADIX_C
|
||||
# define BN_MP_TO_SBIN_C
|
||||
# define BN_S_MP_RAND_JENKINS_C
|
||||
# define BN_S_MP_RAND_PLATFORM_C
|
||||
#endif
|
||||
|
||||
/* Works for RSA only, mpi.o is 68KiB */
|
||||
#if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS)
|
||||
# define BN_CUTOFFS_C
|
||||
# define BN_MP_ADDMOD_C
|
||||
# define BN_MP_CLEAR_MULTI_C
|
||||
# define BN_MP_EXPTMOD_C
|
||||
# define BN_MP_GCD_C
|
||||
# define BN_MP_INIT_MULTI_C
|
||||
# define BN_MP_INVMOD_C
|
||||
# define BN_MP_LCM_C
|
||||
# define BN_MP_MOD_C
|
||||
# define BN_MP_MOD_D_C
|
||||
# define BN_MP_MULMOD_C
|
||||
# define BN_MP_PRIME_IS_PRIME_C
|
||||
# define BN_MP_PRIME_RABIN_MILLER_TRIALS_C
|
||||
# define BN_MP_PRIME_RAND_C
|
||||
# define BN_MP_RADIX_SMAP_C
|
||||
# define BN_MP_SET_INT_C
|
||||
# define BN_MP_SHRINK_C
|
||||
# define BN_MP_TO_UNSIGNED_BIN_C
|
||||
# define BN_MP_UNSIGNED_BIN_SIZE_C
|
||||
# define BN_PRIME_TAB_C
|
||||
# define BN_S_MP_REVERSE_C
|
||||
|
||||
/* other modifiers */
|
||||
# define BN_MP_DIV_SMALL /* Slower division, not critical */
|
||||
|
||||
|
||||
/* here we are on the last pass so we turn things off. The functions classes are still there
|
||||
* but we remove them specifically from the build. This also invokes tweaks in functions
|
||||
* like removing support for even moduli, etc...
|
||||
*/
|
||||
# ifdef LTM_LAST
|
||||
# undef BN_MP_DR_IS_MODULUS_C
|
||||
# undef BN_MP_DR_SETUP_C
|
||||
# undef BN_MP_DR_REDUCE_C
|
||||
# undef BN_MP_DIV_3_C
|
||||
# undef BN_MP_REDUCE_2K_SETUP_C
|
||||
# undef BN_MP_REDUCE_2K_C
|
||||
# undef BN_MP_REDUCE_IS_2K_C
|
||||
# undef BN_MP_REDUCE_SETUP_C
|
||||
# undef BN_S_MP_BALANCE_MUL_C
|
||||
# undef BN_S_MP_EXPTMOD_C
|
||||
# undef BN_S_MP_INVMOD_FAST_C
|
||||
# undef BN_S_MP_KARATSUBA_MUL_C
|
||||
# undef BN_S_MP_KARATSUBA_SQR_C
|
||||
# undef BN_S_MP_MUL_HIGH_DIGS_C
|
||||
# undef BN_S_MP_MUL_HIGH_DIGS_FAST_C
|
||||
# undef BN_S_MP_TOOM_MUL_C
|
||||
# undef BN_S_MP_TOOM_SQR_C
|
||||
|
||||
# ifndef SC_RSA_1_WITH_TESTS
|
||||
# undef BN_MP_REDUCE_C
|
||||
# endif
|
||||
|
||||
/* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without
|
||||
* trouble.
|
||||
*/
|
||||
# undef BN_MP_MONTGOMERY_REDUCE_C
|
||||
# undef BN_S_MP_MUL_DIGS_C
|
||||
# undef BN_S_MP_SQR_C
|
||||
# endif
|
||||
|
||||
#endif
|
BIN
libtom/libtommath/lib/tommath.lib
Normal file
BIN
libtom/libtommath/lib/tommath.lib
Normal file
Binary file not shown.
781
libtom/libtommath/tommath.h
Normal file
781
libtom/libtommath/tommath.h
Normal file
@ -0,0 +1,781 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#ifndef BN_H_
|
||||
#define BN_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef LTM_NO_FILE
|
||||
# warning LTM_NO_FILE has been deprecated, use MP_NO_FILE.
|
||||
# define MP_NO_FILE
|
||||
#endif
|
||||
|
||||
#ifndef MP_NO_FILE
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifdef MP_8BIT
|
||||
# ifdef _MSC_VER
|
||||
# pragma message("8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version.")
|
||||
# else
|
||||
# warning "8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version."
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
|
||||
#if (defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT)
|
||||
# define MP_32BIT
|
||||
#endif
|
||||
|
||||
/* detect 64-bit mode if possible */
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
|
||||
defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
|
||||
defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
|
||||
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
|
||||
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
|
||||
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
|
||||
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
|
||||
# if defined(__GNUC__) && !defined(__hppa)
|
||||
/* we support 128bit integers only via: __attribute__((mode(TI))) */
|
||||
# define MP_64BIT
|
||||
# else
|
||||
/* otherwise we fall back to MP_32BIT even on 64bit platforms */
|
||||
# define MP_32BIT
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef MP_DIGIT_BIT
|
||||
# error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT
|
||||
#endif
|
||||
|
||||
/* some default configurations.
|
||||
*
|
||||
* A "mp_digit" must be able to hold MP_DIGIT_BIT + 1 bits
|
||||
* A "mp_word" must be able to hold 2*MP_DIGIT_BIT + 1 bits
|
||||
*
|
||||
* At the very least a mp_digit must be able to hold 7 bits
|
||||
* [any size beyond that is ok provided it doesn't overflow the data type]
|
||||
*/
|
||||
|
||||
#ifdef MP_8BIT
|
||||
typedef uint8_t mp_digit;
|
||||
typedef uint16_t private_mp_word;
|
||||
# define MP_DIGIT_BIT 7
|
||||
#elif defined(MP_16BIT)
|
||||
typedef uint16_t mp_digit;
|
||||
typedef uint32_t private_mp_word;
|
||||
# define MP_DIGIT_BIT 15
|
||||
#elif defined(MP_64BIT)
|
||||
/* for GCC only on supported platforms */
|
||||
typedef uint64_t mp_digit;
|
||||
#if defined(__GNUC__)
|
||||
typedef unsigned long private_mp_word __attribute__((mode(TI)));
|
||||
#endif
|
||||
# define MP_DIGIT_BIT 60
|
||||
#else
|
||||
typedef uint32_t mp_digit;
|
||||
typedef uint64_t private_mp_word;
|
||||
# ifdef MP_31BIT
|
||||
/*
|
||||
* This is an extension that uses 31-bit digits.
|
||||
* Please be aware that not all functions support this size, especially s_mp_mul_digs_fast
|
||||
* will be reduced to work on small numbers only:
|
||||
* Up to 8 limbs, 248 bits instead of up to 512 limbs, 15872 bits with MP_28BIT.
|
||||
*/
|
||||
# define MP_DIGIT_BIT 31
|
||||
# else
|
||||
/* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
|
||||
# define MP_DIGIT_BIT 28
|
||||
# define MP_28BIT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* mp_word is a private type */
|
||||
#define mp_word MP_DEPRECATED_PRAGMA("mp_word has been made private") private_mp_word
|
||||
|
||||
#define MP_SIZEOF_MP_DIGIT (MP_DEPRECATED_PRAGMA("MP_SIZEOF_MP_DIGIT has been deprecated, use sizeof (mp_digit)") sizeof (mp_digit))
|
||||
|
||||
#define MP_MASK ((((mp_digit)1)<<((mp_digit)MP_DIGIT_BIT))-((mp_digit)1))
|
||||
#define MP_DIGIT_MAX MP_MASK
|
||||
|
||||
/* Primality generation flags */
|
||||
#define MP_PRIME_BBS 0x0001 /* BBS style prime */
|
||||
#define MP_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
|
||||
#define MP_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
|
||||
|
||||
#define LTM_PRIME_BBS (MP_DEPRECATED_PRAGMA("LTM_PRIME_BBS has been deprecated, use MP_PRIME_BBS") MP_PRIME_BBS)
|
||||
#define LTM_PRIME_SAFE (MP_DEPRECATED_PRAGMA("LTM_PRIME_SAFE has been deprecated, use MP_PRIME_SAFE") MP_PRIME_SAFE)
|
||||
#define LTM_PRIME_2MSB_ON (MP_DEPRECATED_PRAGMA("LTM_PRIME_2MSB_ON has been deprecated, use MP_PRIME_2MSB_ON") MP_PRIME_2MSB_ON)
|
||||
|
||||
#ifdef MP_USE_ENUMS
|
||||
typedef enum {
|
||||
MP_ZPOS = 0, /* positive */
|
||||
MP_NEG = 1 /* negative */
|
||||
} mp_sign;
|
||||
typedef enum {
|
||||
MP_LT = -1, /* less than */
|
||||
MP_EQ = 0, /* equal */
|
||||
MP_GT = 1 /* greater than */
|
||||
} mp_ord;
|
||||
typedef enum {
|
||||
MP_NO = 0,
|
||||
MP_YES = 1
|
||||
} mp_bool;
|
||||
typedef enum {
|
||||
MP_OKAY = 0, /* no error */
|
||||
MP_ERR = -1, /* unknown error */
|
||||
MP_MEM = -2, /* out of mem */
|
||||
MP_VAL = -3, /* invalid input */
|
||||
MP_ITER = -4, /* maximum iterations reached */
|
||||
MP_BUF = -5 /* buffer overflow, supplied buffer too small */
|
||||
} mp_err;
|
||||
typedef enum {
|
||||
MP_LSB_FIRST = -1,
|
||||
MP_MSB_FIRST = 1
|
||||
} mp_order;
|
||||
typedef enum {
|
||||
MP_LITTLE_ENDIAN = -1,
|
||||
MP_NATIVE_ENDIAN = 0,
|
||||
MP_BIG_ENDIAN = 1
|
||||
} mp_endian;
|
||||
#else
|
||||
typedef int mp_sign;
|
||||
#define MP_ZPOS 0 /* positive integer */
|
||||
#define MP_NEG 1 /* negative */
|
||||
typedef int mp_ord;
|
||||
#define MP_LT -1 /* less than */
|
||||
#define MP_EQ 0 /* equal to */
|
||||
#define MP_GT 1 /* greater than */
|
||||
typedef int mp_bool;
|
||||
#define MP_YES 1
|
||||
#define MP_NO 0
|
||||
typedef int mp_err;
|
||||
#define MP_OKAY 0 /* no error */
|
||||
#define MP_ERR -1 /* unknown error */
|
||||
#define MP_MEM -2 /* out of mem */
|
||||
#define MP_VAL -3 /* invalid input */
|
||||
#define MP_RANGE (MP_DEPRECATED_PRAGMA("MP_RANGE has been deprecated in favor of MP_VAL") MP_VAL)
|
||||
#define MP_ITER -4 /* maximum iterations reached */
|
||||
#define MP_BUF -5 /* buffer overflow, supplied buffer too small */
|
||||
typedef int mp_order;
|
||||
#define MP_LSB_FIRST -1
|
||||
#define MP_MSB_FIRST 1
|
||||
typedef int mp_endian;
|
||||
#define MP_LITTLE_ENDIAN -1
|
||||
#define MP_NATIVE_ENDIAN 0
|
||||
#define MP_BIG_ENDIAN 1
|
||||
#endif
|
||||
|
||||
/* tunable cutoffs */
|
||||
|
||||
#ifndef MP_FIXED_CUTOFFS
|
||||
extern int
|
||||
KARATSUBA_MUL_CUTOFF,
|
||||
KARATSUBA_SQR_CUTOFF,
|
||||
TOOM_MUL_CUTOFF,
|
||||
TOOM_SQR_CUTOFF;
|
||||
#endif
|
||||
|
||||
/* define this to use lower memory usage routines (exptmods mostly) */
|
||||
/* #define MP_LOW_MEM */
|
||||
|
||||
/* default precision */
|
||||
#ifndef MP_PREC
|
||||
# ifndef MP_LOW_MEM
|
||||
# define PRIVATE_MP_PREC 32 /* default digits of precision */
|
||||
# elif defined(MP_8BIT)
|
||||
# define PRIVATE_MP_PREC 16 /* default digits of precision */
|
||||
# else
|
||||
# define PRIVATE_MP_PREC 8 /* default digits of precision */
|
||||
# endif
|
||||
# define MP_PREC (MP_DEPRECATED_PRAGMA("MP_PREC is an internal macro") PRIVATE_MP_PREC)
|
||||
#endif
|
||||
|
||||
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
|
||||
#define PRIVATE_MP_WARRAY (int)(1uLL << (((CHAR_BIT * sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1))
|
||||
#define MP_WARRAY (MP_DEPRECATED_PRAGMA("MP_WARRAY is an internal macro") PRIVATE_MP_WARRAY)
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_NULL_TERMINATED __attribute__((sentinel))
|
||||
#else
|
||||
# define MP_NULL_TERMINATED
|
||||
#endif
|
||||
|
||||
/*
|
||||
* MP_WUR - warn unused result
|
||||
* ---------------------------
|
||||
*
|
||||
* The result of functions annotated with MP_WUR must be
|
||||
* checked and cannot be ignored.
|
||||
*
|
||||
* Most functions in libtommath return an error code.
|
||||
* This error code must be checked in order to prevent crashes or invalid
|
||||
* results.
|
||||
*
|
||||
* If you still want to avoid the error checks for quick and dirty programs
|
||||
* without robustness guarantees, you can `#define MP_WUR` before including
|
||||
* tommath.h, disabling the warnings.
|
||||
*/
|
||||
#ifndef MP_WUR
|
||||
# if defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_WUR __attribute__((warn_unused_result))
|
||||
# else
|
||||
# define MP_WUR
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
|
||||
# define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x)))
|
||||
# define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s)
|
||||
# define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s)
|
||||
#elif defined(_MSC_VER) && _MSC_VER >= 1500
|
||||
# define MP_DEPRECATED(x) __declspec(deprecated("replaced by " #x))
|
||||
# define MP_DEPRECATED_PRAGMA(s) __pragma(message(s))
|
||||
#else
|
||||
# define MP_DEPRECATED(s)
|
||||
# define MP_DEPRECATED_PRAGMA(s)
|
||||
#endif
|
||||
|
||||
#define DIGIT_BIT (MP_DEPRECATED_PRAGMA("DIGIT_BIT macro is deprecated, MP_DIGIT_BIT instead") MP_DIGIT_BIT)
|
||||
#define USED(m) (MP_DEPRECATED_PRAGMA("USED macro is deprecated, use z->used instead") (m)->used)
|
||||
#define DIGIT(m, k) (MP_DEPRECATED_PRAGMA("DIGIT macro is deprecated, use z->dp instead") (m)->dp[(k)])
|
||||
#define SIGN(m) (MP_DEPRECATED_PRAGMA("SIGN macro is deprecated, use z->sign instead") (m)->sign)
|
||||
|
||||
/* the infamous mp_int structure */
|
||||
typedef struct {
|
||||
int used, alloc;
|
||||
mp_sign sign;
|
||||
mp_digit *dp;
|
||||
} mp_int;
|
||||
|
||||
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
|
||||
typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat);
|
||||
typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source) ltm_prime_callback;
|
||||
|
||||
/* error code to char* string */
|
||||
const char *mp_error_to_string(mp_err code) MP_WUR;
|
||||
|
||||
/* ---> init and deinit bignum functions <--- */
|
||||
/* init a bignum */
|
||||
mp_err mp_init(mp_int *a) MP_WUR;
|
||||
|
||||
/* free a bignum */
|
||||
void mp_clear(mp_int *a);
|
||||
|
||||
/* init a null terminated series of arguments */
|
||||
mp_err mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED MP_WUR;
|
||||
|
||||
/* clear a null terminated series of arguments */
|
||||
void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
|
||||
|
||||
/* exchange two ints */
|
||||
void mp_exch(mp_int *a, mp_int *b);
|
||||
|
||||
/* shrink ram required for a bignum */
|
||||
mp_err mp_shrink(mp_int *a) MP_WUR;
|
||||
|
||||
/* grow an int to a given size */
|
||||
mp_err mp_grow(mp_int *a, int size) MP_WUR;
|
||||
|
||||
/* init to a given number of digits */
|
||||
mp_err mp_init_size(mp_int *a, int size) MP_WUR;
|
||||
|
||||
/* ---> Basic Manipulations <--- */
|
||||
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
|
||||
mp_bool mp_iseven(const mp_int *a) MP_WUR;
|
||||
mp_bool mp_isodd(const mp_int *a) MP_WUR;
|
||||
#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
|
||||
|
||||
/* set to zero */
|
||||
void mp_zero(mp_int *a);
|
||||
|
||||
/* get and set doubles */
|
||||
double mp_get_double(const mp_int *a) MP_WUR;
|
||||
mp_err mp_set_double(mp_int *a, double b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (int32_t) */
|
||||
int32_t mp_get_i32(const mp_int *a) MP_WUR;
|
||||
void mp_set_i32(mp_int *a, int32_t b);
|
||||
mp_err mp_init_i32(mp_int *a, int32_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */
|
||||
#define mp_get_u32(a) ((uint32_t)mp_get_i32(a))
|
||||
void mp_set_u32(mp_int *a, uint32_t b);
|
||||
mp_err mp_init_u32(mp_int *a, uint32_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (int64_t) */
|
||||
int64_t mp_get_i64(const mp_int *a) MP_WUR;
|
||||
void mp_set_i64(mp_int *a, int64_t b);
|
||||
mp_err mp_init_i64(mp_int *a, int64_t b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */
|
||||
#define mp_get_u64(a) ((uint64_t)mp_get_i64(a))
|
||||
void mp_set_u64(mp_int *a, uint64_t b);
|
||||
mp_err mp_init_u64(mp_int *a, uint64_t b) MP_WUR;
|
||||
|
||||
/* get magnitude */
|
||||
uint32_t mp_get_mag_u32(const mp_int *a) MP_WUR;
|
||||
uint64_t mp_get_mag_u64(const mp_int *a) MP_WUR;
|
||||
unsigned long mp_get_mag_ul(const mp_int *a) MP_WUR;
|
||||
unsigned long long mp_get_mag_ull(const mp_int *a) MP_WUR;
|
||||
|
||||
/* get integer, set integer (long) */
|
||||
long mp_get_l(const mp_int *a) MP_WUR;
|
||||
void mp_set_l(mp_int *a, long b);
|
||||
mp_err mp_init_l(mp_int *a, long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (unsigned long) */
|
||||
#define mp_get_ul(a) ((unsigned long)mp_get_l(a))
|
||||
void mp_set_ul(mp_int *a, unsigned long b);
|
||||
mp_err mp_init_ul(mp_int *a, unsigned long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (long long) */
|
||||
long long mp_get_ll(const mp_int *a) MP_WUR;
|
||||
void mp_set_ll(mp_int *a, long long b);
|
||||
mp_err mp_init_ll(mp_int *a, long long b) MP_WUR;
|
||||
|
||||
/* get integer, set integer (unsigned long long) */
|
||||
#define mp_get_ull(a) ((unsigned long long)mp_get_ll(a))
|
||||
void mp_set_ull(mp_int *a, unsigned long long b);
|
||||
mp_err mp_init_ull(mp_int *a, unsigned long long b) MP_WUR;
|
||||
|
||||
/* set to single unsigned digit, up to MP_DIGIT_MAX */
|
||||
void mp_set(mp_int *a, mp_digit b);
|
||||
mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR;
|
||||
|
||||
/* get integer, set integer and init with integer (deprecated) */
|
||||
MP_DEPRECATED(mp_get_mag_u32/mp_get_u32) unsigned long mp_get_int(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_get_mag_ul/mp_get_ul) unsigned long mp_get_long(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_get_mag_ull/mp_get_ull) unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int *a, unsigned long b);
|
||||
MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int *a, unsigned long b);
|
||||
MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int *a, unsigned long long b);
|
||||
MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
|
||||
|
||||
/* copy, b = a */
|
||||
mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* inits and copies, a = b */
|
||||
mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* trim unused digits */
|
||||
void mp_clamp(mp_int *a);
|
||||
|
||||
|
||||
/* export binary data */
|
||||
MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
|
||||
int endian, size_t nails, const mp_int *op) MP_WUR;
|
||||
|
||||
/* import binary data */
|
||||
MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order,
|
||||
size_t size, int endian, size_t nails,
|
||||
const void *op) MP_WUR;
|
||||
|
||||
/* unpack binary data */
|
||||
mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian,
|
||||
size_t nails, const void *op) MP_WUR;
|
||||
|
||||
/* pack binary data */
|
||||
size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) MP_WUR;
|
||||
mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size,
|
||||
mp_endian endian, size_t nails, const mp_int *op) MP_WUR;
|
||||
|
||||
/* ---> digit manipulation <--- */
|
||||
|
||||
/* right shift by "b" digits */
|
||||
void mp_rshd(mp_int *a, int b);
|
||||
|
||||
/* left shift by "b" digits */
|
||||
mp_err mp_lshd(mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* c = a / 2**b, implemented as c = a >> b */
|
||||
mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* b = a/2 */
|
||||
mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* a/3 => 3c + d == a */
|
||||
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
|
||||
|
||||
/* c = a * 2**b, implemented as c = a << b */
|
||||
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = a*2 */
|
||||
mp_err mp_mul_2(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* c = a mod 2**b */
|
||||
mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* computes a = 2**b */
|
||||
mp_err mp_2expt(mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* Counts the number of lsbs which are zero before the first zero bit */
|
||||
int mp_cnt_lsb(const mp_int *a) MP_WUR;
|
||||
|
||||
/* I Love Earth! */
|
||||
|
||||
/* makes a pseudo-random mp_int of a given size */
|
||||
mp_err mp_rand(mp_int *a, int digits) MP_WUR;
|
||||
/* makes a pseudo-random small int of a given size */
|
||||
MP_DEPRECATED(mp_rand) mp_err mp_rand_digit(mp_digit *r) MP_WUR;
|
||||
/* use custom random data source instead of source provided the platform */
|
||||
void mp_rand_source(mp_err(*source)(void *out, size_t size));
|
||||
|
||||
#ifdef MP_PRNG_ENABLE_LTM_RNG
|
||||
# warning MP_PRNG_ENABLE_LTM_RNG has been deprecated, use mp_rand_source instead.
|
||||
/* A last resort to provide random data on systems without any of the other
|
||||
* implemented ways to gather entropy.
|
||||
* It is compatible with `rng_get_bytes()` from libtomcrypt so you could
|
||||
* provide that one and then set `ltm_rng = rng_get_bytes;` */
|
||||
extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
|
||||
extern void (*ltm_rng_callback)(void);
|
||||
#endif
|
||||
|
||||
/* ---> binary operations <--- */
|
||||
|
||||
/* Checks the bit at position b and returns MP_YES
|
||||
* if the bit is 1, MP_NO if it is 0 and MP_VAL
|
||||
* in case of error
|
||||
*/
|
||||
MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR;
|
||||
|
||||
/* c = a XOR b (two complement) */
|
||||
MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a OR b (two complement) */
|
||||
MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a AND b (two complement) */
|
||||
MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = ~a (bitwise not, two complement) */
|
||||
mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* right shift with sign extension */
|
||||
MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR;
|
||||
|
||||
/* ---> Basic arithmetic <--- */
|
||||
|
||||
/* b = -a */
|
||||
mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* b = |a| */
|
||||
mp_err mp_abs(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* compare a to b */
|
||||
mp_ord mp_cmp(const mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* compare |a| to |b| */
|
||||
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* c = a + b */
|
||||
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a - b */
|
||||
mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a * b */
|
||||
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* b = a*a */
|
||||
mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
|
||||
/* a/b => cb + d == a */
|
||||
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* Increment "a" by one like "a++". Changes input! */
|
||||
mp_err mp_incr(mp_int *a) MP_WUR;
|
||||
|
||||
/* Decrement "a" by one like "a--". Changes input! */
|
||||
mp_err mp_decr(mp_int *a) MP_WUR;
|
||||
|
||||
/* ---> single digit functions <--- */
|
||||
|
||||
/* compare against a single digit */
|
||||
mp_ord mp_cmp_d(const mp_int *a, mp_digit b) MP_WUR;
|
||||
|
||||
/* c = a + b */
|
||||
mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a - b */
|
||||
mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = a * b */
|
||||
mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
|
||||
/* a/b => cb + d == a */
|
||||
mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR;
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
|
||||
|
||||
/* ---> number theory <--- */
|
||||
|
||||
/* d = a + b (mod c) */
|
||||
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* d = a - b (mod c) */
|
||||
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* d = a * b (mod c) */
|
||||
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
|
||||
|
||||
/* c = a * a (mod b) */
|
||||
mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = 1/a (mod b) */
|
||||
mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* c = (a, b) */
|
||||
mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* produces value such that U1*a + U2*b = U3 */
|
||||
mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR;
|
||||
|
||||
/* c = [a, b] or (a*b)/(a, b) */
|
||||
mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
|
||||
/* finds one of the b'th root of a, such that |c|**b <= |a|
|
||||
*
|
||||
* returns error if a < 0 and b is even
|
||||
*/
|
||||
mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
|
||||
|
||||
/* special sqrt algo */
|
||||
mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
|
||||
|
||||
/* special sqrt (mod prime) */
|
||||
mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR;
|
||||
|
||||
/* is number a square? */
|
||||
mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR;
|
||||
|
||||
/* computes the jacobi c = (a | n) (or Legendre if b is prime) */
|
||||
MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR;
|
||||
|
||||
/* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
|
||||
mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR;
|
||||
|
||||
/* used to setup the Barrett reduction for a given modulus b */
|
||||
mp_err mp_reduce_setup(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* Barrett Reduction, computes a (mod b) with a precomputed value c
|
||||
*
|
||||
* Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
|
||||
* compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
|
||||
*/
|
||||
mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) MP_WUR;
|
||||
|
||||
/* setups the montgomery reduction */
|
||||
mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR;
|
||||
|
||||
/* computes a = B**n mod b without division or multiplication useful for
|
||||
* normalizing numbers in a Montgomery system.
|
||||
*/
|
||||
mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR;
|
||||
|
||||
/* computes x/R == x (mod N) via Montgomery Reduction */
|
||||
mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
|
||||
|
||||
/* returns 1 if a is a valid DR modulus */
|
||||
mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
|
||||
|
||||
/* sets the value of "d" required for mp_dr_reduce */
|
||||
void mp_dr_setup(const mp_int *a, mp_digit *d);
|
||||
|
||||
/* reduces a modulo n using the Diminished Radix method */
|
||||
mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR;
|
||||
|
||||
/* returns true if a can be reduced with mp_reduce_2k */
|
||||
mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
|
||||
|
||||
/* determines k value for 2k reduction */
|
||||
mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
|
||||
|
||||
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
||||
mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR;
|
||||
|
||||
/* returns true if a can be reduced with mp_reduce_2k_l */
|
||||
mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
|
||||
|
||||
/* determines k value for 2k reduction */
|
||||
mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
|
||||
|
||||
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
|
||||
mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) MP_WUR;
|
||||
|
||||
/* Y = G**X (mod P) */
|
||||
mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR;
|
||||
|
||||
/* ---> Primes <--- */
|
||||
|
||||
/* number of primes */
|
||||
#ifdef MP_8BIT
|
||||
# define PRIVATE_MP_PRIME_TAB_SIZE 31
|
||||
#else
|
||||
# define PRIVATE_MP_PRIME_TAB_SIZE 256
|
||||
#endif
|
||||
#define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE)
|
||||
|
||||
/* table of first PRIME_SIZE primes */
|
||||
MP_DEPRECATED(internal) extern const mp_digit ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE];
|
||||
|
||||
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
|
||||
MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Fermat test of "a" using base "b".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Miller-Rabin test of "a" using base "b".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
|
||||
|
||||
/* This gives [for a given bit size] the number of trials required
|
||||
* such that Miller-Rabin gives a prob of failure lower than 2^-96
|
||||
*/
|
||||
int mp_prime_rabin_miller_trials(int size) MP_WUR;
|
||||
|
||||
/* performs one strong Lucas-Selfridge test of "a".
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs one Frobenius test of "a" as described by Paul Underwood.
|
||||
* Sets result to 0 if composite or 1 if probable prime
|
||||
*/
|
||||
mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
|
||||
|
||||
/* performs t random rounds of Miller-Rabin on "a" additional to
|
||||
* bases 2 and 3. Also performs an initial sieve of trial
|
||||
* division. Determines if "a" is prime with probability
|
||||
* of error no more than (1/4)**t.
|
||||
* Both a strong Lucas-Selfridge to complete the BPSW test
|
||||
* and a separate Frobenius test are available at compile time.
|
||||
* With t<0 a deterministic test is run for primes up to
|
||||
* 318665857834031151167461. With t<13 (abs(t)-13) additional
|
||||
* tests with sequential small primes are run starting at 43.
|
||||
* Is Fips 186.4 compliant if called with t as computed by
|
||||
* mp_prime_rabin_miller_trials();
|
||||
*
|
||||
* Sets result to 1 if probably prime, 0 otherwise
|
||||
*/
|
||||
mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
|
||||
|
||||
/* finds the next prime after the number "a" using "t" trials
|
||||
* of Miller-Rabin.
|
||||
*
|
||||
* bbs_style = 1 means the prime must be congruent to 3 mod 4
|
||||
*/
|
||||
mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
|
||||
|
||||
/* makes a truly random prime of a given size (bytes),
|
||||
* call with bbs = 1 if you want it to be congruent to 3 mod 4
|
||||
*
|
||||
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
||||
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
||||
* so it can be NULL
|
||||
*
|
||||
* The prime generated will be larger than 2^(8*size).
|
||||
*/
|
||||
#define mp_prime_random(a, t, size, bbs, cb, dat) (MP_DEPRECATED_PRAGMA("mp_prime_random has been deprecated, use mp_prime_rand instead") mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?MP_PRIME_BBS:0, cb, dat))
|
||||
|
||||
/* makes a truly random prime of a given size (bits),
|
||||
*
|
||||
* Flags are as follows:
|
||||
*
|
||||
* MP_PRIME_BBS - make prime congruent to 3 mod 4
|
||||
* MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS)
|
||||
* MP_PRIME_2MSB_ON - make the 2nd highest bit one
|
||||
*
|
||||
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
||||
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
||||
* so it can be NULL
|
||||
*
|
||||
*/
|
||||
MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags,
|
||||
private_mp_prime_callback cb, void *dat) MP_WUR;
|
||||
mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR;
|
||||
|
||||
/* Integer logarithm to integer base */
|
||||
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
|
||||
|
||||
/* c = a**b */
|
||||
mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
|
||||
MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
|
||||
|
||||
/* ---> radix conversion <--- */
|
||||
int mp_count_bits(const mp_int *a) MP_WUR;
|
||||
|
||||
|
||||
MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
|
||||
|
||||
MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR;
|
||||
MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
|
||||
|
||||
size_t mp_ubin_size(const mp_int *a) MP_WUR;
|
||||
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
|
||||
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
|
||||
|
||||
size_t mp_sbin_size(const mp_int *a) MP_WUR;
|
||||
mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
|
||||
mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
|
||||
|
||||
mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
|
||||
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
|
||||
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
|
||||
mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
|
||||
|
||||
#ifndef MP_NO_FILE
|
||||
mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR;
|
||||
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
|
||||
#endif
|
||||
|
||||
#define mp_read_raw(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_signed_bin") mp_read_signed_bin((mp), (str), (len)))
|
||||
#define mp_raw_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_signed_bin_size") mp_signed_bin_size(mp))
|
||||
#define mp_toraw(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_signed_bin") mp_to_signed_bin((mp), (str)))
|
||||
#define mp_read_mag(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_unsigned_bin") mp_read_unsigned_bin((mp), (str), (len))
|
||||
#define mp_mag_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp))
|
||||
#define mp_tomag(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str)))
|
||||
|
||||
#define mp_tobinary(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary") mp_toradix((M), (S), 2))
|
||||
#define mp_tooctal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal") mp_toradix((M), (S), 8))
|
||||
#define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10))
|
||||
#define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16))
|
||||
|
||||
#define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), NULL, 2)
|
||||
#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8)
|
||||
#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10)
|
||||
#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1319
libtom/libtommath/tommath_class.h
Normal file
1319
libtom/libtommath/tommath_class.h
Normal file
File diff suppressed because it is too large
Load Diff
13
libtom/libtommath/tommath_cutoffs.h
Normal file
13
libtom/libtommath/tommath_cutoffs.h
Normal file
@ -0,0 +1,13 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
/*
|
||||
Current values evaluated on an AMD A8-6600K (64-bit).
|
||||
Type "make tune" to optimize them for your machine but
|
||||
be aware that it may take a long time. It took 2:30 minutes
|
||||
on the aforementioned machine for example.
|
||||
*/
|
||||
|
||||
#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF 80
|
||||
#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF 120
|
||||
#define MP_DEFAULT_TOOM_MUL_CUTOFF 350
|
||||
#define MP_DEFAULT_TOOM_SQR_CUTOFF 400
|
303
libtom/libtommath/tommath_private.h
Normal file
303
libtom/libtommath/tommath_private.h
Normal file
@ -0,0 +1,303 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#ifndef TOMMATH_PRIV_H_
|
||||
#define TOMMATH_PRIV_H_
|
||||
|
||||
#include "tommath.h"
|
||||
#include "tommath_class.h"
|
||||
|
||||
/*
|
||||
* Private symbols
|
||||
* ---------------
|
||||
*
|
||||
* On Unix symbols can be marked as hidden if libtommath is compiled
|
||||
* as a shared object. By default, symbols are visible.
|
||||
* As of now, this feature is opt-in via the MP_PRIVATE_SYMBOLS define.
|
||||
*
|
||||
* On Win32 a .def file must be used to specify the exported symbols.
|
||||
*/
|
||||
#if defined (MP_PRIVATE_SYMBOLS) && defined(__GNUC__) && __GNUC__ >= 4
|
||||
# define MP_PRIVATE __attribute__ ((visibility ("hidden")))
|
||||
#else
|
||||
# define MP_PRIVATE
|
||||
#endif
|
||||
|
||||
/* Hardening libtommath
|
||||
* --------------------
|
||||
*
|
||||
* By default memory is zeroed before calling
|
||||
* MP_FREE to avoid leaking data. This is good
|
||||
* practice in cryptographical applications.
|
||||
*
|
||||
* Note however that memory allocators used
|
||||
* in cryptographical applications can often
|
||||
* be configured by itself to clear memory,
|
||||
* rendering the clearing in tommath unnecessary.
|
||||
* See for example https://github.com/GrapheneOS/hardened_malloc
|
||||
* and the option CONFIG_ZERO_ON_FREE.
|
||||
*
|
||||
* Furthermore there are applications which
|
||||
* value performance more and want this
|
||||
* feature to be disabled. For such applications
|
||||
* define MP_NO_ZERO_ON_FREE during compilation.
|
||||
*/
|
||||
#ifdef MP_NO_ZERO_ON_FREE
|
||||
# define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
|
||||
# define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
|
||||
#else
|
||||
# define MP_FREE_BUFFER(mem, size) \
|
||||
do { \
|
||||
size_t fs_ = (size); \
|
||||
void* fm_ = (mem); \
|
||||
if (fm_ != NULL) { \
|
||||
MP_ZERO_BUFFER(fm_, fs_); \
|
||||
MP_FREE(fm_, fs_); \
|
||||
} \
|
||||
} while (0)
|
||||
# define MP_FREE_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int fd_ = (digits); \
|
||||
void* fm_ = (mem); \
|
||||
if (fm_ != NULL) { \
|
||||
size_t fs_ = sizeof (mp_digit) * (size_t)fd_; \
|
||||
MP_ZERO_BUFFER(fm_, fs_); \
|
||||
MP_FREE(fm_, fs_); \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifdef MP_USE_MEMSET
|
||||
# include <string.h>
|
||||
# define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
|
||||
# define MP_ZERO_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int zd_ = (digits); \
|
||||
if (zd_ > 0) { \
|
||||
memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
|
||||
} \
|
||||
} while (0)
|
||||
#else
|
||||
# define MP_ZERO_BUFFER(mem, size) \
|
||||
do { \
|
||||
size_t zs_ = (size); \
|
||||
char* zm_ = (char*)(mem); \
|
||||
while (zs_-- > 0u) { \
|
||||
*zm_++ = '\0'; \
|
||||
} \
|
||||
} while (0)
|
||||
# define MP_ZERO_DIGITS(mem, digits) \
|
||||
do { \
|
||||
int zd_ = (digits); \
|
||||
mp_digit* zm_ = (mem); \
|
||||
while (zd_-- > 0) { \
|
||||
*zm_++ = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
/* Tunable cutoffs
|
||||
* ---------------
|
||||
*
|
||||
* - In the default settings, a cutoff X can be modified at runtime
|
||||
* by adjusting the corresponding X_CUTOFF variable.
|
||||
*
|
||||
* - Tunability of the library can be disabled at compile time
|
||||
* by defining the MP_FIXED_CUTOFFS macro.
|
||||
*
|
||||
* - There is an additional file tommath_cutoffs.h, which defines
|
||||
* the default cutoffs. These can be adjusted manually or by the
|
||||
* autotuner.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef MP_FIXED_CUTOFFS
|
||||
# include "tommath_cutoffs.h"
|
||||
# define MP_KARATSUBA_MUL_CUTOFF MP_DEFAULT_KARATSUBA_MUL_CUTOFF
|
||||
# define MP_KARATSUBA_SQR_CUTOFF MP_DEFAULT_KARATSUBA_SQR_CUTOFF
|
||||
# define MP_TOOM_MUL_CUTOFF MP_DEFAULT_TOOM_MUL_CUTOFF
|
||||
# define MP_TOOM_SQR_CUTOFF MP_DEFAULT_TOOM_SQR_CUTOFF
|
||||
#else
|
||||
# define MP_KARATSUBA_MUL_CUTOFF KARATSUBA_MUL_CUTOFF
|
||||
# define MP_KARATSUBA_SQR_CUTOFF KARATSUBA_SQR_CUTOFF
|
||||
# define MP_TOOM_MUL_CUTOFF TOOM_MUL_CUTOFF
|
||||
# define MP_TOOM_SQR_CUTOFF TOOM_SQR_CUTOFF
|
||||
#endif
|
||||
|
||||
/* define heap macros */
|
||||
#ifndef MP_MALLOC
|
||||
/* default to libc stuff */
|
||||
# include <stdlib.h>
|
||||
# define MP_MALLOC(size) malloc(size)
|
||||
# define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize))
|
||||
# define MP_CALLOC(nmemb, size) calloc((nmemb), (size))
|
||||
# define MP_FREE(mem, size) free(mem)
|
||||
#else
|
||||
/* prototypes for our heap functions */
|
||||
extern void *MP_MALLOC(size_t size);
|
||||
extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
|
||||
extern void *MP_CALLOC(size_t nmemb, size_t size);
|
||||
extern void MP_FREE(void *mem, size_t size);
|
||||
#endif
|
||||
|
||||
/* feature detection macro */
|
||||
#ifdef _MSC_VER
|
||||
/* Prevent false positive: not enough arguments for function-like macro invocation */
|
||||
#pragma warning(disable: 4003)
|
||||
#endif
|
||||
#define MP_STRINGIZE(x) MP__STRINGIZE(x)
|
||||
#define MP__STRINGIZE(x) ""#x""
|
||||
#define MP_HAS(x) (sizeof(MP_STRINGIZE(BN_##x##_C)) == 1u)
|
||||
|
||||
/* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */
|
||||
#undef mp_word
|
||||
typedef private_mp_word mp_word;
|
||||
|
||||
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
|
||||
/* Static assertion */
|
||||
#define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];
|
||||
|
||||
/* ---> Basic Manipulations <--- */
|
||||
#define MP_IS_ZERO(a) ((a)->used == 0)
|
||||
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
|
||||
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
|
||||
|
||||
#define MP_SIZEOF_BITS(type) ((size_t)CHAR_BIT * sizeof(type))
|
||||
#define MP_MAXFAST (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
|
||||
|
||||
/* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
|
||||
#undef MP_WARRAY
|
||||
#define MP_WARRAY PRIVATE_MP_WARRAY
|
||||
|
||||
/* TODO: Remove PRIVATE_MP_PREC as soon as deprecated MP_PREC is removed from tommath.h */
|
||||
#ifdef PRIVATE_MP_PREC
|
||||
# undef MP_PREC
|
||||
# define MP_PREC PRIVATE_MP_PREC
|
||||
#endif
|
||||
|
||||
/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
|
||||
#define MP_MIN_PREC ((((int)MP_SIZEOF_BITS(long long) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
|
||||
|
||||
MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
|
||||
|
||||
/* random number source */
|
||||
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
|
||||
|
||||
/* lowlevel functions, do not call! */
|
||||
MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
|
||||
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
|
||||
MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat);
|
||||
MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
|
||||
MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
|
||||
|
||||
/* TODO: jenkins prng is not thread safe as of now */
|
||||
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
|
||||
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
|
||||
|
||||
extern MP_PRIVATE const char *const mp_s_rmap;
|
||||
extern MP_PRIVATE const uint8_t mp_s_rmap_reverse[];
|
||||
extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
|
||||
extern MP_PRIVATE const mp_digit *s_mp_prime_tab;
|
||||
|
||||
/* deprecated functions */
|
||||
MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n,
|
||||
mp_digit rho);
|
||||
MP_DEPRECATED(s_mp_mul_digs_fast) mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c,
|
||||
int digs);
|
||||
MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b,
|
||||
mp_int *c,
|
||||
int digs);
|
||||
MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P,
|
||||
mp_int *Y,
|
||||
int redmode);
|
||||
MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
||||
MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
|
||||
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
|
||||
|
||||
#define MP_GET_ENDIANNESS(x) \
|
||||
do{\
|
||||
int16_t n = 0x1; \
|
||||
char *p = (char *)&n; \
|
||||
x = (p[0] == '\x01') ? MP_LITTLE_ENDIAN : MP_BIG_ENDIAN; \
|
||||
} while (0)
|
||||
|
||||
/* code-generating macros */
|
||||
#define MP_SET_UNSIGNED(name, type) \
|
||||
void name(mp_int * a, type b) \
|
||||
{ \
|
||||
int i = 0; \
|
||||
while (b != 0u) { \
|
||||
a->dp[i++] = ((mp_digit)b & MP_MASK); \
|
||||
if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
|
||||
b >>= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
|
||||
} \
|
||||
a->used = i; \
|
||||
a->sign = MP_ZPOS; \
|
||||
MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
|
||||
}
|
||||
|
||||
#define MP_SET_SIGNED(name, uname, type, utype) \
|
||||
void name(mp_int * a, type b) \
|
||||
{ \
|
||||
uname(a, (b < 0) ? -(utype)b : (utype)b); \
|
||||
if (b < 0) { a->sign = MP_NEG; } \
|
||||
}
|
||||
|
||||
#define MP_INIT_INT(name , set, type) \
|
||||
mp_err name(mp_int * a, type b) \
|
||||
{ \
|
||||
mp_err err; \
|
||||
if ((err = mp_init(a)) != MP_OKAY) { \
|
||||
return err; \
|
||||
} \
|
||||
set(a, b); \
|
||||
return MP_OKAY; \
|
||||
}
|
||||
|
||||
#define MP_GET_MAG(name, type) \
|
||||
type name(const mp_int* a) \
|
||||
{ \
|
||||
unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
|
||||
type res = 0u; \
|
||||
while (i --> 0u) { \
|
||||
res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
|
||||
res |= (type)a->dp[i]; \
|
||||
if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
|
||||
} \
|
||||
return res; \
|
||||
}
|
||||
|
||||
#define MP_GET_SIGNED(name, mag, type, utype) \
|
||||
type name(const mp_int* a) \
|
||||
{ \
|
||||
utype res = mag(a); \
|
||||
return (a->sign == MP_NEG) ? (type)-res : (type)res; \
|
||||
}
|
||||
|
||||
#endif
|
110
libtom/libtommath/tommath_superclass.h
Normal file
110
libtom/libtommath/tommath_superclass.h
Normal file
@ -0,0 +1,110 @@
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
/* super class file for PK algos */
|
||||
|
||||
/* default ... include all MPI */
|
||||
#ifndef LTM_NOTHING
|
||||
#define LTM_ALL
|
||||
#endif
|
||||
|
||||
/* RSA only (does not support DH/DSA/ECC) */
|
||||
/* #define SC_RSA_1 */
|
||||
/* #define SC_RSA_1_WITH_TESTS */
|
||||
|
||||
/* For reference.... On an Athlon64 optimizing for speed...
|
||||
|
||||
LTM's mpi.o with all functions [striped] is 142KiB in size.
|
||||
|
||||
*/
|
||||
|
||||
#ifdef SC_RSA_1_WITH_TESTS
|
||||
# define BN_MP_ERROR_TO_STRING_C
|
||||
# define BN_MP_FREAD_C
|
||||
# define BN_MP_FWRITE_C
|
||||
# define BN_MP_INCR_C
|
||||
# define BN_MP_ISEVEN_C
|
||||
# define BN_MP_ISODD_C
|
||||
# define BN_MP_NEG_C
|
||||
# define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C
|
||||
# define BN_MP_RADIX_SIZE_C
|
||||
# define BN_MP_RAND_C
|
||||
# define BN_MP_REDUCE_C
|
||||
# define BN_MP_REDUCE_2K_L_C
|
||||
# define BN_MP_FROM_SBIN_C
|
||||
# define BN_MP_ROOT_U32_C
|
||||
# define BN_MP_SET_L_C
|
||||
# define BN_MP_SET_UL_C
|
||||
# define BN_MP_SBIN_SIZE_C
|
||||
# define BN_MP_TO_RADIX_C
|
||||
# define BN_MP_TO_SBIN_C
|
||||
# define BN_S_MP_RAND_JENKINS_C
|
||||
# define BN_S_MP_RAND_PLATFORM_C
|
||||
#endif
|
||||
|
||||
/* Works for RSA only, mpi.o is 68KiB */
|
||||
#if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS)
|
||||
# define BN_CUTOFFS_C
|
||||
# define BN_MP_ADDMOD_C
|
||||
# define BN_MP_CLEAR_MULTI_C
|
||||
# define BN_MP_EXPTMOD_C
|
||||
# define BN_MP_GCD_C
|
||||
# define BN_MP_INIT_MULTI_C
|
||||
# define BN_MP_INVMOD_C
|
||||
# define BN_MP_LCM_C
|
||||
# define BN_MP_MOD_C
|
||||
# define BN_MP_MOD_D_C
|
||||
# define BN_MP_MULMOD_C
|
||||
# define BN_MP_PRIME_IS_PRIME_C
|
||||
# define BN_MP_PRIME_RABIN_MILLER_TRIALS_C
|
||||
# define BN_MP_PRIME_RAND_C
|
||||
# define BN_MP_RADIX_SMAP_C
|
||||
# define BN_MP_SET_INT_C
|
||||
# define BN_MP_SHRINK_C
|
||||
# define BN_MP_TO_UNSIGNED_BIN_C
|
||||
# define BN_MP_UNSIGNED_BIN_SIZE_C
|
||||
# define BN_PRIME_TAB_C
|
||||
# define BN_S_MP_REVERSE_C
|
||||
|
||||
/* other modifiers */
|
||||
# define BN_MP_DIV_SMALL /* Slower division, not critical */
|
||||
|
||||
|
||||
/* here we are on the last pass so we turn things off. The functions classes are still there
|
||||
* but we remove them specifically from the build. This also invokes tweaks in functions
|
||||
* like removing support for even moduli, etc...
|
||||
*/
|
||||
# ifdef LTM_LAST
|
||||
# undef BN_MP_DR_IS_MODULUS_C
|
||||
# undef BN_MP_DR_SETUP_C
|
||||
# undef BN_MP_DR_REDUCE_C
|
||||
# undef BN_MP_DIV_3_C
|
||||
# undef BN_MP_REDUCE_2K_SETUP_C
|
||||
# undef BN_MP_REDUCE_2K_C
|
||||
# undef BN_MP_REDUCE_IS_2K_C
|
||||
# undef BN_MP_REDUCE_SETUP_C
|
||||
# undef BN_S_MP_BALANCE_MUL_C
|
||||
# undef BN_S_MP_EXPTMOD_C
|
||||
# undef BN_S_MP_INVMOD_FAST_C
|
||||
# undef BN_S_MP_KARATSUBA_MUL_C
|
||||
# undef BN_S_MP_KARATSUBA_SQR_C
|
||||
# undef BN_S_MP_MUL_HIGH_DIGS_C
|
||||
# undef BN_S_MP_MUL_HIGH_DIGS_FAST_C
|
||||
# undef BN_S_MP_TOOM_MUL_C
|
||||
# undef BN_S_MP_TOOM_SQR_C
|
||||
|
||||
# ifndef SC_RSA_1_WITH_TESTS
|
||||
# undef BN_MP_REDUCE_C
|
||||
# endif
|
||||
|
||||
/* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without
|
||||
* trouble.
|
||||
*/
|
||||
# undef BN_MP_MONTGOMERY_REDUCE_C
|
||||
# undef BN_S_MP_MUL_DIGS_C
|
||||
# undef BN_S_MP_SQR_C
|
||||
# endif
|
||||
|
||||
#endif
|
31
ultimateteknotool.v2.sln
Normal file
31
ultimateteknotool.v2.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.29806.167
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ultimateteknotool.v2", "ultimateteknotool.v2\ultimateteknotool.v2.vcxproj", "{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Debug|x64.Build.0 = Debug|x64
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Debug|x86.Build.0 = Debug|Win32
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Release|x64.ActiveCfg = Release|x64
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Release|x64.Build.0 = Release|x64
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Release|x86.ActiveCfg = Release|Win32
|
||||
{95BE6499-8951-48EA-BF37-4EA0EF2FA58B}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {2F846983-2EAE-4CE2-9FC3-21A34CD4E479}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
88
ultimateteknotool.v2/CWinAPIFileW.cpp
Normal file
88
ultimateteknotool.v2/CWinAPIFileW.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include "pch.h"
|
||||
#include "CWinAPIFileW.h"
|
||||
|
||||
constexpr LARGE_INTEGER zero = { 0 };
|
||||
|
||||
CWinAPIFileW::CWinAPIFileW()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
CWinAPIFileW::~CWinAPIFileW()
|
||||
{
|
||||
if (this->m_hFile != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
::CloseHandle(this->m_hFile);
|
||||
}
|
||||
}
|
||||
|
||||
bool CWinAPIFileW::OpenForWriting(wchar_t* name)
|
||||
{
|
||||
this->Close();
|
||||
this->m_hFile = CreateFileW(name, GENERIC_WRITE, 0, NULL,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (this->m_hFile != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CWinAPIFileW::WriteInAppend(const char* src, size_t len)
|
||||
{
|
||||
if (::SetFilePointerEx(this->m_hFile, zero, NULL, FILE_END))
|
||||
/* If the function succeeds, the return value is nonzero. */
|
||||
{
|
||||
::WriteFile(this->m_hFile, src, len, &m_byteswriten, NULL);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool CWinAPIFileW::OpenForReading(wchar_t* name)
|
||||
{
|
||||
this->Close();
|
||||
this->m_hFile = CreateFileW(name, GENERIC_READ, 0, NULL,
|
||||
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (this->m_hFile != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CWinAPIFileW::ReadFrom(char* dst, size_t len)
|
||||
{
|
||||
::ReadFile(this->m_hFile, dst, len, &m_bytesread, NULL);
|
||||
|
||||
}
|
||||
|
||||
void CWinAPIFileW::Truncate(wchar_t* name)
|
||||
{
|
||||
this->Close();
|
||||
this->m_hFile = CreateFileW(name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
||||
TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
this->Close();
|
||||
}
|
||||
|
||||
bool CWinAPIFileW::IsFileExist(wchar_t* name) const
|
||||
{
|
||||
HANDLE DummyBoi = INVALID_HANDLE_VALUE;
|
||||
DummyBoi = CreateFileW(name, 0, 0, NULL,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (DummyBoi != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
::CloseHandle(DummyBoi);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CWinAPIFileW::Close()
|
||||
{
|
||||
if (this->m_hFile != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
::CloseHandle(this->m_hFile);
|
||||
}
|
||||
}
|
24
ultimateteknotool.v2/CWinAPIFileW.h
Normal file
24
ultimateteknotool.v2/CWinAPIFileW.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "debugconsole.h"
|
||||
|
||||
class CWinAPIFileW
|
||||
{
|
||||
private:
|
||||
//static constexpr unsigned short MAX_LENOFNAME = 64;
|
||||
//wchar_t m_wsName[MAX_LENOFNAME] = { 0 };
|
||||
HANDLE m_hFile = INVALID_HANDLE_VALUE;
|
||||
DWORD m_byteswriten = 0;
|
||||
DWORD m_bytesread = 0;
|
||||
public:
|
||||
CWinAPIFileW();
|
||||
~CWinAPIFileW();
|
||||
bool OpenForWriting(wchar_t* name);
|
||||
void WriteInAppend(const char* src, size_t len);
|
||||
bool OpenForReading(wchar_t* name);
|
||||
void ReadFrom(char* dst, size_t len);
|
||||
void Truncate(wchar_t* name);
|
||||
bool IsFileExist(wchar_t* name) const;
|
||||
void Close();
|
||||
};
|
||||
|
166
ultimateteknotool.v2/CWindowCfgReader.cpp
Normal file
166
ultimateteknotool.v2/CWindowCfgReader.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowCfgReader.h"
|
||||
|
||||
//static constexpr unsigned short chuck_sz = 32;
|
||||
|
||||
inline void CWindowCfgReader::IncrTotal_DeserialSuccess()
|
||||
{
|
||||
this->m_wcharsreadtotal += this->m_deserialwcharsread;
|
||||
if (this->m_buffersize > this->m_deserialwcharsread) { this->m_buffersize -= this->m_deserialwcharsread; }
|
||||
else
|
||||
{
|
||||
this->m_buffersize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline void CWindowCfgReader::IncrTotal_DeserialFailed()
|
||||
{
|
||||
this->m_wcharsreadtotal++;
|
||||
if (this->m_buffersize > 0) { this->m_buffersize--; }
|
||||
else
|
||||
{
|
||||
this->m_buffersize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowCfgReader::LoadTextSettingW(CWindow_edit_cfg* win, unsigned short textlimit)
|
||||
{
|
||||
wchar_t* pointer;
|
||||
pointer = lazysectionbuffer_3x512.GetNulledMem(textlimit + 4, 0);
|
||||
this->m_deserialwcharsread = serializer::DeserialTextW(&this->m_buffer[this->m_wcharsreadtotal], this->m_buffersize, pointer, textlimit);
|
||||
if (this->m_deserialwcharsread)
|
||||
{
|
||||
//if (chuck_sz > textlimit && textlimit >= 1)
|
||||
//{
|
||||
// pointer[textlimit - 1] = 0x00;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// pointer[chuck_sz - 1] = 0x00;
|
||||
//}
|
||||
CWindowTextWriter_WriteText(win, pointer);
|
||||
this->IncrTotal_DeserialSuccess();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->IncrTotal_DeserialFailed();
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowCfgReader::LoadButtonStateW(CWindow_button_cfg* win)
|
||||
{
|
||||
this->m_deserialwcharsread = serializer::DeserialButtonStateW(&this->m_buffer[this->m_wcharsreadtotal], this->m_buffersize, win->isPressed);
|
||||
if (this->m_deserialwcharsread)
|
||||
{
|
||||
if (win->isPressed)
|
||||
{
|
||||
::SendMessageW(win->hwnd, BM_SETCHECK, BST_CHECKED, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
::SendMessageW(win->hwnd, BM_SETCHECK, BST_UNCHECKED, NULL);
|
||||
}
|
||||
this->IncrTotal_DeserialSuccess();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->IncrTotal_DeserialFailed();
|
||||
}
|
||||
}
|
||||
|
||||
bool CWindowCfgReader::LoadButtonStateW()
|
||||
{
|
||||
bool ret = false;
|
||||
this->m_deserialwcharsread = serializer::DeserialButtonStateW(&this->m_buffer[this->m_wcharsreadtotal], this->m_buffersize, ret);
|
||||
if (this->m_deserialwcharsread)
|
||||
{
|
||||
this->IncrTotal_DeserialSuccess();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->IncrTotal_DeserialFailed();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const wchar_t* CWindowCfgReader::LoadTextSettingW(unsigned short textlimit)
|
||||
{
|
||||
wchar_t* pointer;
|
||||
pointer = lazysectionbuffer_3x512.GetNulledMem(textlimit + 4, 0);
|
||||
this->m_deserialwcharsread = serializer::DeserialTextW(&this->m_buffer[this->m_wcharsreadtotal], this->m_buffersize, pointer, textlimit);
|
||||
if (this->m_deserialwcharsread)
|
||||
{
|
||||
//if (chuck_sz > textlimit && textlimit >= 1)
|
||||
//{
|
||||
// pointer[textlimit - 1] = 0x00;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// pointer[chuck_sz - 1] = 0x00;
|
||||
//}
|
||||
this->IncrTotal_DeserialSuccess();
|
||||
}
|
||||
else
|
||||
{
|
||||
pointer[0] = 0x0000;
|
||||
this->IncrTotal_DeserialFailed();
|
||||
}
|
||||
return pointer;
|
||||
}
|
||||
|
||||
constexpr unsigned char EXTRA_SPACE_BUFF = 1;
|
||||
|
||||
wchar_t* CWindowPostLoadStrings::GetMemoryForString(unsigned short textlimit)
|
||||
{
|
||||
if ((textlimit + this->currentUsedSize) >= wchart_buffer.size())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->currentUsedSize += textlimit;
|
||||
return wchart_buffer.data() + (this->currentUsedSize - textlimit);
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowPostLoadStrings::Register(size_t buffersize)
|
||||
{
|
||||
this->Unregister();
|
||||
wchart_buffer.resize(buffersize, 0x0000);
|
||||
}
|
||||
|
||||
void CWindowPostLoadStrings::Unregister()
|
||||
{
|
||||
if (this->wchart_buffer.empty() == false)
|
||||
{
|
||||
this->wchart_buffer.clear();
|
||||
}
|
||||
if (this->pairs.empty() == false)
|
||||
{
|
||||
this->pairs.clear();
|
||||
}
|
||||
this->currentUsedSize = 0;
|
||||
}
|
||||
|
||||
void CWindowPostLoadStrings::LoadTextSettingW(CWindow_edit_cfg* win, const wchar_t* ptxt, unsigned short textlimit)
|
||||
{
|
||||
wchar_t* pp = nullptr;
|
||||
pp = this->GetMemoryForString(textlimit + EXTRA_SPACE_BUFF);
|
||||
if (pp != nullptr)
|
||||
{
|
||||
for (unsigned short i = 0; i < textlimit && ptxt[i]; i++)
|
||||
{
|
||||
pp[i] = ptxt[i];
|
||||
}
|
||||
this->pairs.push_back(std::pair<CWindow_edit_cfg*, wchar_t*>(win, pp));
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowPostLoadStrings::LoadTextInEdits()
|
||||
{
|
||||
for (auto& x : this->pairs)
|
||||
{
|
||||
CWindowTextWriter_WriteText(x.first, x.second);
|
||||
}
|
||||
}
|
||||
|
77
ultimateteknotool.v2/CWindowCfgReader.h
Normal file
77
ultimateteknotool.v2/CWindowCfgReader.h
Normal file
@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
#include "CWinAPIFileW.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindowTextWriter.h"
|
||||
#include "Serializers.h"
|
||||
#include "LazyBuffer_512wchart.h"
|
||||
|
||||
class CWindowCfgReader
|
||||
{
|
||||
private:
|
||||
wchar_t* m_buffer = nullptr;
|
||||
size_t m_buffersize = 0;
|
||||
size_t m_deserialwcharsread = 0;
|
||||
size_t m_wcharsreadtotal = 0;
|
||||
inline void IncrTotal_DeserialSuccess();
|
||||
inline void IncrTotal_DeserialFailed();
|
||||
public:
|
||||
CWindowCfgReader() = delete;
|
||||
CWindowCfgReader(wchar_t* p, size_t sz) :m_buffer(p), m_buffersize(sz) {
|
||||
this->m_deserialwcharsread = 0; this->m_wcharsreadtotal = 0;
|
||||
}
|
||||
~CWindowCfgReader() = default;
|
||||
void LoadTextSettingW(CWindow_edit_cfg* win, unsigned short textlimit);
|
||||
void LoadButtonStateW(CWindow_button_cfg* win);
|
||||
bool LoadButtonStateW();
|
||||
const wchar_t* LoadTextSettingW(unsigned short textlimit);
|
||||
};
|
||||
|
||||
|
||||
|
||||
class CWindowPostLoadStrings
|
||||
{
|
||||
private:
|
||||
std::vector<wchar_t> wchart_buffer;
|
||||
std::vector<std::pair<CWindow_edit_cfg*, wchar_t*>> pairs;
|
||||
size_t currentUsedSize = 0;
|
||||
wchar_t* GetMemoryForString(unsigned short textlimit);
|
||||
public:
|
||||
CWindowPostLoadStrings() = default;
|
||||
~CWindowPostLoadStrings() = default;
|
||||
void Register(size_t buffersize);
|
||||
void Unregister();
|
||||
void LoadTextSettingW(CWindow_edit_cfg* win, const wchar_t* ptxt, unsigned short textlimit);
|
||||
void LoadTextInEdits();
|
||||
};
|
||||
|
||||
class CWindowButtonStatePostLoader : public WindowsVisitor
|
||||
{
|
||||
virtual void Visit(CWindow_edit* win) override
|
||||
{
|
||||
return;
|
||||
}
|
||||
virtual void Visit(CWindow_edit_cfg* win) override
|
||||
{
|
||||
return;
|
||||
}
|
||||
virtual void Visit(CWindow_button* win) override
|
||||
{
|
||||
return;
|
||||
}
|
||||
virtual void Visit(CWindow_button_cfg* win) override
|
||||
{
|
||||
if (win->isPressed)
|
||||
{
|
||||
::SendMessageW(win->hwnd, BM_SETCHECK, BST_CHECKED, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
::SendMessageW(win->hwnd, BM_SETCHECK, BST_UNCHECKED, NULL);
|
||||
}
|
||||
}
|
||||
virtual void Visit(CWindow_static* win) override
|
||||
{
|
||||
return;
|
||||
}
|
||||
};
|
50
ultimateteknotool.v2/CWindowCfgWriter.cpp
Normal file
50
ultimateteknotool.v2/CWindowCfgWriter.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowCfgWriter.h"
|
||||
|
||||
static constexpr wchar_t dummytext[] = L"\0\0\0\0";
|
||||
|
||||
//void CWindowCfgWriter_SaveTextSetting(CWindow_edit_cfg* win, CWinAPIFileW* apifile)
|
||||
//{
|
||||
// // read window
|
||||
// // serialize
|
||||
// // write file
|
||||
// //
|
||||
//}
|
||||
//
|
||||
//
|
||||
//void CWindowCfgWriter_SaveButtonSetting(CWindow_button_cfg* win, CWinAPIFileW* apifile)
|
||||
//{
|
||||
// serializer::ButtonStateWDescriptor desc = { 0 };
|
||||
// bytes_serialized = serializer::SerialButtonStateW(win->isPressed, desc);
|
||||
// apifile->WriteInAppend((char*)desc.text, bytes_serialized * sizeof(wchar_t));
|
||||
//}
|
||||
|
||||
void CWindowCfgWriter::DecreaseBufferSize(size_t value)
|
||||
{
|
||||
if (value >= this->m_buffersize)
|
||||
{
|
||||
this->m_buffersize = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_buffersize = this->m_buffersize - value;
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowCfgWriter::SaveTextSettingW(CWindow_edit_cfg* win, unsigned short chunk)
|
||||
{
|
||||
const wchar_t* dummyboi = dummytext;
|
||||
dummyboi = CWindowTextReader_GetTextW(win, chunk);
|
||||
if (this->m_wcharswrotetotal >= this->m_constBufferSizeVerifyValue) { throw 1; }
|
||||
this->m_serialwcharswrote = serializer::SerialTextW(dummyboi, chunk, &(this->m_buffer[this->m_wcharswrotetotal]), this->m_buffersize);
|
||||
this->m_wcharswrotetotal += this->m_serialwcharswrote;
|
||||
this->DecreaseBufferSize(this->m_serialwcharswrote);
|
||||
}
|
||||
|
||||
void CWindowCfgWriter::SaveButtonStateW(CWindow_button_cfg* win)
|
||||
{
|
||||
if (this->m_wcharswrotetotal >= this->m_constBufferSizeVerifyValue) { throw 1; }
|
||||
this->m_serialwcharswrote = serializer::SerialButtonStateW(win->isPressed, &(this->m_buffer[this->m_wcharswrotetotal]), this->m_buffersize);
|
||||
this->m_wcharswrotetotal += this->m_serialwcharswrote;
|
||||
this->DecreaseBufferSize(this->m_serialwcharswrote);
|
||||
}
|
32
ultimateteknotool.v2/CWindowCfgWriter.h
Normal file
32
ultimateteknotool.v2/CWindowCfgWriter.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "CWinAPIFileW.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindowTextReader.h"
|
||||
#include "Serializers.h"
|
||||
|
||||
|
||||
class CWindowCfgWriter
|
||||
{
|
||||
private:
|
||||
wchar_t* m_buffer = nullptr;
|
||||
const size_t m_constBufferSizeVerifyValue = 0;
|
||||
size_t m_buffersize = 0;
|
||||
size_t m_serialwcharswrote = 0;
|
||||
size_t m_wcharswrotetotal = 0;
|
||||
void DecreaseBufferSize(size_t value);
|
||||
|
||||
public:
|
||||
CWindowCfgWriter() = delete;
|
||||
CWindowCfgWriter(wchar_t* p, size_t sz) :m_buffer(p), m_buffersize(sz), m_constBufferSizeVerifyValue(sz) {
|
||||
this->m_serialwcharswrote = 0; this->m_wcharswrotetotal = 0;
|
||||
}
|
||||
~CWindowCfgWriter() = default;
|
||||
void SaveTextSettingW(CWindow_edit_cfg* win, unsigned short chunk = 32);
|
||||
void SaveButtonStateW(CWindow_button_cfg* win);
|
||||
size_t GetTotalBytesWriten() const { return this->m_wcharswrotetotal; }
|
||||
};
|
||||
|
||||
//void CWindowCfgWriter_SaveTextSetting(CWindow_edit_cfg* win, CWinAPIFileW* apifile);
|
||||
//void CWindowCfgWriter_SaveButtonSetting(CWindow_button_cfg* win, CWinAPIFileW* apifile);
|
||||
|
2
ultimateteknotool.v2/CWindowFileW.cpp
Normal file
2
ultimateteknotool.v2/CWindowFileW.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWinAPIFileW.h"
|
13
ultimateteknotool.v2/CWindowTextLimiter.cpp
Normal file
13
ultimateteknotool.v2/CWindowTextLimiter.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowTextLimiter.h"
|
||||
|
||||
static_assert(sizeof(WPARAM) >= sizeof(size_t), "SendMessage requires WPARAM type arg#3");
|
||||
void CWindowTextLimiter_LimitText(CWindow_edit* win, size_t limit)
|
||||
{
|
||||
if (limit) { ::SendMessageW(win->hwnd, EM_SETLIMITTEXT, limit, 0); }
|
||||
}
|
||||
|
||||
void CWindowTextLimiter_LimitText(CWindow_edit_cfg* win, size_t limit)
|
||||
{
|
||||
if (limit) { ::SendMessageW(win->hwnd, EM_SETLIMITTEXT, limit, 0); }
|
||||
}
|
14
ultimateteknotool.v2/CWindowTextLimiter.h
Normal file
14
ultimateteknotool.v2/CWindowTextLimiter.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "WindowsVisitor.h"
|
||||
#include "CWindow_hwnd.h"
|
||||
#include "CWindow_edit.h"
|
||||
#include "CWindow_button.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindow_static.h"
|
||||
|
||||
|
||||
void CWindowTextLimiter_LimitText(CWindow_edit* win, size_t limit);
|
||||
void CWindowTextLimiter_LimitText(CWindow_edit_cfg* win, size_t limit);
|
||||
|
50
ultimateteknotool.v2/CWindowTextReader.cpp
Normal file
50
ultimateteknotool.v2/CWindowTextReader.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowTextReader.h"
|
||||
#include "ParserRandMask.h"
|
||||
#include "LazyBuffer.h"
|
||||
|
||||
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW(CWindow_edit* win, unsigned short maxchunksz)
|
||||
{
|
||||
wchar_t* future_text = lazysectionbuffer_3x512.GetNulledMem(maxchunksz + 4, 0);
|
||||
::GetWindowTextW(win->hwnd, future_text, maxchunksz);
|
||||
return future_text;
|
||||
}
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW(CWindow_edit_cfg* win, unsigned short maxchunksz)
|
||||
{
|
||||
wchar_t* future_text = lazysectionbuffer_3x512.GetNulledMem(maxchunksz + 4, 0);
|
||||
::GetWindowTextW(win->hwnd, future_text, maxchunksz);
|
||||
return future_text;
|
||||
}
|
||||
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW_ParseRandMask(CWindow_edit_cfg* win, unsigned short maxchunksz)
|
||||
{
|
||||
wchar_t* future_text = lazysectionbuffer_3x512.GetNulledMem(maxchunksz + maxchunksz + 4, 1);
|
||||
/* fix for overwrite func -> prevent double call*/
|
||||
::GetWindowTextW(win->hwnd, future_text + maxchunksz + 1, maxchunksz);
|
||||
parsers::ParseRandMaskTextW(future_text + maxchunksz + 1, maxchunksz, future_text, maxchunksz);
|
||||
return future_text;
|
||||
}
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW_ParseRandMask_Overwrite(CWindow_edit_cfg* normal, CWindow_edit_cfg* rand, unsigned short maxchunksz)
|
||||
{
|
||||
wchar_t* normal_text = CWindowTextReader_GetTextW(normal, maxchunksz);
|
||||
wchar_t* rand_text = CWindowTextReader_GetTextW_ParseRandMask(rand, maxchunksz);
|
||||
wchar_t* future_text = lazysectionbuffer_3x512.GetNulledMem(maxchunksz + 4, 2);
|
||||
|
||||
for (unsigned short i = 0; i < maxchunksz && (normal_text[i] || rand_text[i]); i++)
|
||||
{
|
||||
if (rand_text[i] != L' ')
|
||||
{
|
||||
future_text[i] = rand_text[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
future_text[i] = normal_text[i];
|
||||
}
|
||||
}
|
||||
return future_text;
|
||||
}
|
45
ultimateteknotool.v2/CWindowTextReader.h
Normal file
45
ultimateteknotool.v2/CWindowTextReader.h
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "WindowsVisitor.h"
|
||||
#include "CWindow_hwnd.h"
|
||||
#include "CWindow_edit.h"
|
||||
#include "CWindow_button.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindow_static.h"
|
||||
#include "LazyBuffer_512wchart.h"
|
||||
|
||||
// bad idea
|
||||
//class CWindowTextReader : public WindowsVisitor
|
||||
//{
|
||||
//private:
|
||||
// unsigned short maxchunksize = 32;
|
||||
//public:
|
||||
// void ChangeMaxChunkSize(unsigned short value) { if (value) { this->maxchunksize = value; } }
|
||||
// virtual void Visit(CWindow_edit* win) override
|
||||
// {
|
||||
// ::GetWindowTextW(win->hwnd, lazybuffer512wchart.GetNulledMem(maxchunksize), maxchunksize);
|
||||
// }
|
||||
// virtual void Visit(CWindow_edit_cfg* win) override
|
||||
// {
|
||||
// ::GetWindowTextW(win->hwnd, lazybuffer512wchart.GetNulledMem(maxchunksize), maxchunksize);
|
||||
// }
|
||||
// virtual void Visit(CWindow_button* win) override
|
||||
// {
|
||||
// /* lmao, no */
|
||||
// }
|
||||
// virtual void Visit(CWindow_button_cfg* win) override
|
||||
// {
|
||||
// /* lmao, no */
|
||||
// }
|
||||
// virtual void Visit(CWindow_static* win) override
|
||||
// {
|
||||
// /* lmao, no */
|
||||
// }
|
||||
//};
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW(CWindow_edit* win, unsigned short maxchunksz);
|
||||
wchar_t* CWindowTextReader_GetTextW(CWindow_edit_cfg* win, unsigned short maxchunksz);
|
||||
|
||||
wchar_t* CWindowTextReader_GetTextW_ParseRandMask(CWindow_edit_cfg* win, unsigned short maxchunksz);
|
||||
wchar_t* CWindowTextReader_GetTextW_ParseRandMask_Overwrite(CWindow_edit_cfg* normal, CWindow_edit_cfg* rand, unsigned short maxchunksz);
|
18
ultimateteknotool.v2/CWindowTextWriter.cpp
Normal file
18
ultimateteknotool.v2/CWindowTextWriter.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowTextWriter.h"
|
||||
|
||||
void CWindowTextWriter_WriteText(CWindow_edit* win, const wchar_t* text)
|
||||
{
|
||||
if (text != nullptr)
|
||||
{
|
||||
::SetWindowTextW(win->hwnd, text);
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowTextWriter_WriteText(CWindow_edit_cfg* win, const wchar_t* text)
|
||||
{
|
||||
if (text != nullptr)
|
||||
{
|
||||
::SetWindowTextW(win->hwnd, text);
|
||||
}
|
||||
}
|
20
ultimateteknotool.v2/CWindowTextWriter.h
Normal file
20
ultimateteknotool.v2/CWindowTextWriter.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "WindowsVisitor.h"
|
||||
#include "CWindow_hwnd.h"
|
||||
#include "CWindow_edit.h"
|
||||
#include "CWindow_button.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindow_static.h"
|
||||
|
||||
static_assert(nullptr == 0, "nullptr is not 0");
|
||||
|
||||
namespace cwindowtextwriter_internal {
|
||||
constexpr wchar_t ws_emptytext[] = L"";
|
||||
}
|
||||
|
||||
void CWindowTextWriter_WriteText(CWindow_edit* win, const wchar_t* text);
|
||||
void CWindowTextWriter_WriteText(CWindow_edit_cfg* win, const wchar_t* text);
|
||||
|
||||
|
2
ultimateteknotool.v2/CWindow_button.cpp
Normal file
2
ultimateteknotool.v2/CWindow_button.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindow_button.h"
|
12
ultimateteknotool.v2/CWindow_button.h
Normal file
12
ultimateteknotool.v2/CWindow_button.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "CWindow_hwnd.h"
|
||||
|
||||
class CWindow_button : public CWindow_hwnd, public CWindow_abs_temp<CWindow_button>
|
||||
{
|
||||
public:
|
||||
DWORD id = 0;
|
||||
CWindow_button() :CWindow_hwnd() { id = 0; }
|
||||
~CWindow_button() = default;
|
||||
//DWORD GetID()const { return this->id; }
|
||||
};
|
||||
|
2
ultimateteknotool.v2/CWindow_button_cfg.cpp
Normal file
2
ultimateteknotool.v2/CWindow_button_cfg.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindow_button_cfg.h"
|
15
ultimateteknotool.v2/CWindow_button_cfg.h
Normal file
15
ultimateteknotool.v2/CWindow_button_cfg.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "CWindow_hwnd.h"
|
||||
|
||||
class CWindow_button_cfg : public CWindow_hwnd, public CWindow_abs_temp<CWindow_button_cfg>
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
DWORD id = 0; /*LOWORD of WPARAM = UINT_PTR*/
|
||||
bool isPressed = false;
|
||||
CWindow_button_cfg() :CWindow_hwnd() {}
|
||||
~CWindow_button_cfg() = default;
|
||||
void SwitchState() { isPressed = !isPressed; }
|
||||
};
|
||||
|
2
ultimateteknotool.v2/CWindow_edit.cpp
Normal file
2
ultimateteknotool.v2/CWindow_edit.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindow_edit.h"
|
12
ultimateteknotool.v2/CWindow_edit.h
Normal file
12
ultimateteknotool.v2/CWindow_edit.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "CWindow_hwnd.h"
|
||||
|
||||
class CWindow_edit : public CWindow_hwnd, public CWindow_abs_temp<CWindow_edit>
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
CWindow_edit():CWindow_hwnd(){}
|
||||
~CWindow_edit() = default;
|
||||
};
|
||||
|
2
ultimateteknotool.v2/CWindow_edit_cfg.cpp
Normal file
2
ultimateteknotool.v2/CWindow_edit_cfg.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindow_edit_cfg.h"
|
12
ultimateteknotool.v2/CWindow_edit_cfg.h
Normal file
12
ultimateteknotool.v2/CWindow_edit_cfg.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "CWindow_hwnd.h"
|
||||
|
||||
class CWindow_edit_cfg : public CWindow_hwnd, public CWindow_abs_temp<CWindow_edit_cfg>
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
CWindow_edit_cfg() :CWindow_hwnd() {}
|
||||
~CWindow_edit_cfg() = default;
|
||||
};
|
||||
|
36
ultimateteknotool.v2/CWindow_hwnd.h
Normal file
36
ultimateteknotool.v2/CWindow_hwnd.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "WindowsVisitor.h"
|
||||
//struct WindowsVisitor;
|
||||
|
||||
class CWindow_abs
|
||||
{
|
||||
public:
|
||||
virtual void Visit(WindowsVisitor* v) = 0;
|
||||
virtual ~CWindow_abs() = default;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class CWindow_abs_temp : public CWindow_abs
|
||||
{
|
||||
public:
|
||||
CWindow_abs_temp() {}
|
||||
void Visit(WindowsVisitor* v) override
|
||||
{
|
||||
v->Visit(static_cast<T*>(this));
|
||||
}
|
||||
};
|
||||
|
||||
class CWindow_hwnd
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
HWND hwnd = NULL;
|
||||
CWindow_hwnd():hwnd(0){}
|
||||
virtual ~CWindow_hwnd() = default;
|
||||
|
||||
void Show() const { ::ShowWindow(this->hwnd, SW_SHOW); }
|
||||
void Hide() const { ::ShowWindow(this->hwnd, SW_HIDE); }
|
||||
};
|
||||
|
2
ultimateteknotool.v2/CWindow_static.cpp
Normal file
2
ultimateteknotool.v2/CWindow_static.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindow_static.h"
|
10
ultimateteknotool.v2/CWindow_static.h
Normal file
10
ultimateteknotool.v2/CWindow_static.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "CWindow_hwnd.h"
|
||||
|
||||
class CWindow_static : public CWindow_hwnd , public CWindow_abs_temp<CWindow_static>
|
||||
{
|
||||
public:
|
||||
CWindow_static():CWindow_hwnd(){}
|
||||
~CWindow_static() = default;
|
||||
};
|
||||
|
75
ultimateteknotool.v2/CWindowsBuilder.h
Normal file
75
ultimateteknotool.v2/CWindowsBuilder.h
Normal file
@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
#include "WindowsVisitor.h"
|
||||
#include "CWindow_hwnd.h"
|
||||
#include "CWindow_edit.h"
|
||||
#include "CWindow_button.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindow_static.h"
|
||||
|
||||
class CWindowsBuilder : public WindowsVisitor
|
||||
{
|
||||
private:
|
||||
const wchar_t* build_text = (wchar_t*)L" ";
|
||||
DWORD build_style = ~WS_VISIBLE;
|
||||
HWND build_parenthwnd = NULL;
|
||||
DWORD build_uniqueid = 1;
|
||||
public:
|
||||
int build_left_pos = 0;
|
||||
int build_top_pos = 0;
|
||||
int build_width = 0;
|
||||
int build_height = 0;
|
||||
void ChangeTextP(const wchar_t* only_static_text) { this->build_text = only_static_text; }
|
||||
void ChangeStyle(DWORD style) { this->build_style = style; }
|
||||
void ChangeLeftPos(int pos) { this->build_left_pos = pos; }
|
||||
void ChangeTopPos(int pos) { this->build_top_pos = pos; }
|
||||
void ChangeWidth(int sz) { this->build_width = sz; }
|
||||
void ChangeHeight(int sz) { this->build_height = sz; }
|
||||
void ChangeParentHWND(HWND newhwnd) { this->build_parenthwnd = newhwnd; }
|
||||
/*void ChangeUniqueID(DWORD id) { this->build_uniqueid = id; }*/
|
||||
|
||||
virtual void Visit(CWindow_edit* win) override
|
||||
{
|
||||
win->hwnd = ::CreateWindowExW(
|
||||
0, L"edit",
|
||||
build_text, build_style,
|
||||
build_left_pos, build_top_pos, build_width, build_height,
|
||||
build_parenthwnd, 0, 0, 0);
|
||||
}
|
||||
virtual void Visit(CWindow_edit_cfg* win) override
|
||||
{
|
||||
win->hwnd = ::CreateWindowExW(
|
||||
0, L"edit",
|
||||
build_text, build_style,
|
||||
build_left_pos, build_top_pos, build_width, build_height,
|
||||
build_parenthwnd, 0, 0, 0);
|
||||
}
|
||||
virtual void Visit(CWindow_button* win) override
|
||||
{
|
||||
win->hwnd = ::CreateWindowExW(
|
||||
0, L"button",
|
||||
build_text, build_style,
|
||||
build_left_pos, build_top_pos, build_width, build_height,
|
||||
build_parenthwnd, (HMENU)build_uniqueid, 0, 0);
|
||||
win->id = build_uniqueid;
|
||||
build_uniqueid++;
|
||||
}
|
||||
virtual void Visit(CWindow_button_cfg* win) override
|
||||
{
|
||||
win->hwnd = ::CreateWindowExW(
|
||||
0, L"button",
|
||||
build_text, build_style,
|
||||
build_left_pos, build_top_pos, build_width, build_height,
|
||||
build_parenthwnd, (HMENU)build_uniqueid, 0, 0);
|
||||
win->id = build_uniqueid;
|
||||
build_uniqueid++;
|
||||
}
|
||||
virtual void Visit(CWindow_static* win) override
|
||||
{
|
||||
win->hwnd = ::CreateWindowExW(
|
||||
0, L"static",
|
||||
build_text, build_style,
|
||||
build_left_pos, build_top_pos, build_width, build_height,
|
||||
build_parenthwnd, (HMENU)build_uniqueid, 0, 0);
|
||||
}
|
||||
};
|
2
ultimateteknotool.v2/CWindowsFontApplier.cpp
Normal file
2
ultimateteknotool.v2/CWindowsFontApplier.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "CWindowsFontApplier.h"
|
38
ultimateteknotool.v2/CWindowsFontApplier.h
Normal file
38
ultimateteknotool.v2/CWindowsFontApplier.h
Normal file
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include "HFont_Wrapper.h"
|
||||
#include "WindowsVisitor.h"
|
||||
#include "CWindow_hwnd.h"
|
||||
#include "CWindow_edit.h"
|
||||
#include "CWindow_button.h"
|
||||
#include "CWindow_edit_cfg.h"
|
||||
#include "CWindow_button_cfg.h"
|
||||
#include "CWindow_static.h"
|
||||
|
||||
class CWindowsFontApplier : public WindowsVisitor
|
||||
{
|
||||
public:
|
||||
HFONT hFont;
|
||||
CWindowsFontApplier(HFont_Wrapper& wrapper) { hFont = wrapper.GetFont(); }
|
||||
virtual void Visit(CWindow_edit* win) override
|
||||
{
|
||||
if (hFont != 0) { ::SendMessageW(win->hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); }
|
||||
}
|
||||
virtual void Visit(CWindow_edit_cfg* win) override
|
||||
{
|
||||
if (hFont != 0) { ::SendMessageW(win->hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); }
|
||||
}
|
||||
virtual void Visit(CWindow_button* win) override
|
||||
{
|
||||
if (hFont != 0) { ::SendMessageW(win->hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); }
|
||||
}
|
||||
virtual void Visit(CWindow_button_cfg* win) override
|
||||
{
|
||||
if (hFont != 0) { ::SendMessageW(win->hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); }
|
||||
}
|
||||
virtual void Visit(CWindow_static* win) override
|
||||
{
|
||||
if (hFont != 0) { ::SendMessageW(win->hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); }
|
||||
}
|
||||
};
|
||||
|
2
ultimateteknotool.v2/HFont_Wrapper.cpp
Normal file
2
ultimateteknotool.v2/HFont_Wrapper.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "HFont_Wrapper.h"
|
16
ultimateteknotool.v2/HFont_Wrapper.h
Normal file
16
ultimateteknotool.v2/HFont_Wrapper.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
class HFont_Wrapper
|
||||
{
|
||||
private:
|
||||
HFONT font;
|
||||
public:
|
||||
HFont_Wrapper() { font = 0; }
|
||||
void HFONT_Wrap(HFONT vfont) { font = vfont; }
|
||||
const HFONT& GetFont() { return this->font; }
|
||||
~HFont_Wrapper()
|
||||
{
|
||||
::DeleteObject(this->font);
|
||||
}
|
||||
};
|
||||
|
82
ultimateteknotool.v2/Hooker_JMP.cpp
Normal file
82
ultimateteknotool.v2/Hooker_JMP.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include "pch.h"
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include "Hooker_JMP.h"
|
||||
|
||||
|
||||
struct MallocNoob
|
||||
{
|
||||
std::vector<void*> holders;
|
||||
MallocNoob()
|
||||
{
|
||||
|
||||
}
|
||||
unsigned char* doalloc(const size_t sz)
|
||||
{
|
||||
void* retard = 0;
|
||||
retard = ::malloc(sz);
|
||||
holders.push_back(retard);
|
||||
return static_cast<unsigned char*>(retard);
|
||||
}
|
||||
~MallocNoob()
|
||||
{
|
||||
for (auto& x : this->holders)
|
||||
{
|
||||
if (x != 0)
|
||||
{
|
||||
::free(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MallocNoob allocer;
|
||||
|
||||
|
||||
typedef BOOL(WINAPI* sys_VirtualProtect_t)(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
|
||||
|
||||
#pragma optimize("" ,off)
|
||||
void* Hooker_JMP::__Hook32_internal__NativeHook(uint8_t* addr, uint8_t* hook, uint8_t len)
|
||||
{
|
||||
DWORD oldProtectID;
|
||||
if (::VirtualProtect(addr, len, PAGE_READWRITE, &oldProtectID) == TRUE)
|
||||
{
|
||||
uint32_t jumpto, newjump;
|
||||
unsigned char* jmp = 0;
|
||||
if (addr[0] == 0xE9)
|
||||
{
|
||||
|
||||
jmp = allocer.doalloc(10);
|
||||
if (jmp == 0) { return nullptr; }
|
||||
jumpto = (*reinterpret_cast<uint32_t*>((addr + 1))) + (reinterpret_cast<uint32_t>(addr)) + 5;
|
||||
newjump = (jumpto - reinterpret_cast<uint32_t>(jmp + 5));
|
||||
jmp[0] = 0xE9;
|
||||
*reinterpret_cast<uint32_t*>((jmp + 1)) = newjump;
|
||||
jmp += 5;
|
||||
jmp[0] = 0xE9;
|
||||
*(reinterpret_cast<uint32_t*>((jmp + 1))) = reinterpret_cast<uint32_t>(reinterpret_cast<uint32_t*>((addr - jmp)));
|
||||
}
|
||||
else
|
||||
{
|
||||
jmp = allocer.doalloc(5+len);
|
||||
if (jmp == 0) { return nullptr; }
|
||||
::memcpy(jmp, addr, len);
|
||||
jmp += len;
|
||||
jmp[0] = 0xE9;
|
||||
*reinterpret_cast<uint32_t*>((jmp + 1)) = reinterpret_cast<uint32_t>(reinterpret_cast<uint32_t*>(addr + len - jmp)) - 5;
|
||||
}
|
||||
addr[0] = 0xE9;
|
||||
*reinterpret_cast<uint32_t*>((addr + 1)) = reinterpret_cast<uint32_t>(reinterpret_cast<uint32_t*>(hook - addr)) - 5;
|
||||
|
||||
for (uint32_t i = 5; i < len; i++)
|
||||
addr[i] = 0x90;
|
||||
|
||||
|
||||
::VirtualProtect(addr, len, oldProtectID, &oldProtectID);
|
||||
return (jmp - len);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
#pragma optimize("" ,on)
|
||||
|
25
ultimateteknotool.v2/Hooker_JMP.h
Normal file
25
ultimateteknotool.v2/Hooker_JMP.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "stdint.h"
|
||||
#include "debugconsole.h"
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include "SystemFunctionXRefRemover.h"
|
||||
|
||||
namespace Hooker_JMP
|
||||
{
|
||||
|
||||
void* __Hook32_internal__NativeHook(uint8_t* addr, uint8_t* hook, uint8_t len);
|
||||
|
||||
template<typename FunctionType, typename HookType>
|
||||
bool Hook32(uint32_t addr, HookType& hook, uint8_t asm_length_of_original_code, FunctionType& original_call)
|
||||
{
|
||||
original_call = nullptr;
|
||||
original_call = reinterpret_cast<FunctionType>(
|
||||
__Hook32_internal__NativeHook(reinterpret_cast<uint8_t*>(addr), reinterpret_cast<uint8_t*>(hook), asm_length_of_original_code)
|
||||
);
|
||||
|
||||
return original_call != nullptr;
|
||||
}
|
||||
|
||||
|
||||
};
|
11
ultimateteknotool.v2/HooksAfterResumingThreads.cpp
Normal file
11
ultimateteknotool.v2/HooksAfterResumingThreads.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "pch.h"
|
||||
#include "HooksAfterResumingThreads.h"
|
||||
|
||||
bool hooks_after_resuming_threads::Hook()
|
||||
{
|
||||
bool ret = false;
|
||||
ret = hooks::JMPHook_SerializeNetData(iw5mp_functions_addresses::SerializeNetData, 5) &&
|
||||
hooks::bdDTLSAssociation_functions::JMPHook_sendInit(iw5mp_functions_addresses::bdDTLSAssociation_functions::sendInit, 5) &&
|
||||
hooks::iw5mp_functions::JMPHook_GetChallengeS2(iw5mp_functions_addresses::GetChallengeS2, 5);
|
||||
return ret;
|
||||
}
|
10
ultimateteknotool.v2/HooksAfterResumingThreads.h
Normal file
10
ultimateteknotool.v2/HooksAfterResumingThreads.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "JMPHook_SerializeNetdata.h"
|
||||
#include "JMPHook_bdDTLSAssociation_functions.h"
|
||||
#include "iw5mp_functions_declaration.h"
|
||||
#include "JMP_iw5mp_functions.h"
|
||||
|
||||
namespace hooks_after_resuming_threads
|
||||
{
|
||||
bool Hook();
|
||||
};
|
15
ultimateteknotool.v2/HooksOnThreadsStart.cpp
Normal file
15
ultimateteknotool.v2/HooksOnThreadsStart.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "pch.h"
|
||||
#include "HooksOnThreadsStart.h"
|
||||
|
||||
|
||||
bool hooks_on_threads_start::Hook()
|
||||
{
|
||||
bool ret = false;
|
||||
ret = hooks::sysinfoapi_functions::JMPHook_GetSystemFirmwareTable((uint32_t)((void*)GetSystemFirmwareTable), 5);
|
||||
if (ret)
|
||||
{
|
||||
ret = hooks::bdUPnP_functions::JMPHook_extractDeviceInfo(iw5mp_functions_addresses::bdUPnP_functions::extractDeviceInfo, 6);
|
||||
//hooks::iphlpapi_functions::JMPHook_GetInterfaceInfo((uint32_t)((void*)::GetInterfaceInfo), 5);
|
||||
}
|
||||
return ret;
|
||||
}
|
10
ultimateteknotool.v2/HooksOnThreadsStart.h
Normal file
10
ultimateteknotool.v2/HooksOnThreadsStart.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "iw5mp_functions_declaration.h"
|
||||
#include "JMPHook_sysinfoapi_functions.h"
|
||||
#include "JMPHook_bdUPnP_functions.h"
|
||||
#include "JMPHook_iphlpapi_functions.h"
|
||||
|
||||
namespace hooks_on_threads_start
|
||||
{
|
||||
bool Hook();
|
||||
};
|
126
ultimateteknotool.v2/Injector.cpp
Normal file
126
ultimateteknotool.v2/Injector.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
#include "pch.h"
|
||||
#include "Injector.h"
|
||||
|
||||
std::unique_ptr<Injector> Injector::m_pInst;
|
||||
std::once_flag Injector::m_Initflag;
|
||||
|
||||
//void Injector::OverwriteTargetLibName(const char* target_lib_ASCII_nullterminated_string, const char* injecting_this)
|
||||
// {
|
||||
// // src: http://dronesec.pw/blog/2017/09/19/abusing-delay-load-dll/
|
||||
// /*
|
||||
// defined in winnt.h
|
||||
// typedef struct _IMAGE_DELAYLOAD_DESCRIPTOR {
|
||||
// union {
|
||||
// DWORD AllAttributes;
|
||||
// struct {
|
||||
// DWORD RvaBased : 1; // Delay load version 2
|
||||
// DWORD ReservedAttributes : 31;
|
||||
// } DUMMYSTRUCTNAME;
|
||||
// } Attributes;
|
||||
//
|
||||
// DWORD DllNameRVA; // RVA to the name of the target library (NULL-terminate ASCII string)
|
||||
// DWORD ModuleHandleRVA; // RVA to the HMODULE caching location (PHMODULE)
|
||||
// DWORD ImportAddressTableRVA; // RVA to the start of the IAT (PIMAGE_THUNK_DATA)
|
||||
// DWORD ImportNameTableRVA; // RVA to the start of the name table (PIMAGE_THUNK_DATA::AddressOfData)
|
||||
// DWORD BoundImportAddressTableRVA; // RVA to an optional bound IAT
|
||||
// DWORD UnloadInformationTableRVA; // RVA to an optional unload info table
|
||||
// DWORD TimeDateStamp; // 0 if not bound,
|
||||
// // Otherwise, date/time of the target DLL
|
||||
//
|
||||
// } IMAGE_DELAYLOAD_DESCRIPTOR, *PIMAGE_DELAYLOAD_DESCRIPTOR;
|
||||
// */
|
||||
// PIMAGE_DOS_HEADER pImgDos = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL);
|
||||
// PIMAGE_NT_HEADERS pImgNt = (PIMAGE_NT_HEADERS)((LPBYTE)pImgDos + pImgDos->e_lfanew);
|
||||
// PIMAGE_DELAYLOAD_DESCRIPTOR pImgDelay =
|
||||
// (PIMAGE_DELAYLOAD_DESCRIPTOR)((LPBYTE)pImgDos + pImgNt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress);
|
||||
// DWORD dwBaseAddr = (DWORD)GetModuleHandle(NULL);
|
||||
// DWORD dwOldProtect = 0;
|
||||
// SIZE_T wBytes = 0;
|
||||
// for (IMAGE_DELAYLOAD_DESCRIPTOR* entry = pImgDelay; entry->ImportAddressTableRVA != NULL; entry++) {
|
||||
// char* _cDllName = (char*)(dwBaseAddr + entry->DllNameRVA);
|
||||
// if (::strcmp(_cDllName, target_lib_ASCII_nullterminated_string) == 0) {
|
||||
// DWORD dwEntryAddr = (DWORD)((DWORD)GetModuleHandle(NULL) + entry->DllNameRVA);
|
||||
// VirtualProtect((LPVOID)dwEntryAddr, sizeof(DWORD), PAGE_READWRITE, &dwOldProtect);
|
||||
// WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwEntryAddr, (LPVOID)injecting_this, strlen(injecting_this), &wBytes);
|
||||
// VirtualProtect((LPVOID)dwEntryAddr, sizeof(DWORD), dwOldProtect, &dwOldProtect);
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
|
||||
Injector& Injector::Inject0r()
|
||||
{
|
||||
std::call_once(m_Initflag, []()
|
||||
{
|
||||
m_pInst.reset(new Injector); // straight calling default constructor
|
||||
});
|
||||
return *m_pInst.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Injector::APC_Inject(const std::wstring& dll_path)
|
||||
{
|
||||
HANDLE CurrentProcess = 0;
|
||||
CurrentProcess = ::GetCurrentProcess();
|
||||
SIZE_T StringSizeInBytes = (dll_path.size() + 4) * sizeof(decltype(dll_path[0]));
|
||||
if (CurrentProcess == 0) return false;
|
||||
else
|
||||
{
|
||||
LPVOID pThreadData = 0;
|
||||
pThreadData = VirtualAllocEx(CurrentProcess, NULL, StringSizeInBytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if (pThreadData == 0)
|
||||
{
|
||||
::CloseHandle(CurrentProcess);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!WriteProcessMemory(CurrentProcess, pThreadData, (LPCVOID)dll_path.c_str(), StringSizeInBytes, NULL))
|
||||
{
|
||||
::CloseHandle(CurrentProcess);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
LPTHREAD_START_ROUTINE LoadLibraryAddress = 0;
|
||||
HMODULE Kernel32Module = ::GetModuleHandleW(L"Kernel32");
|
||||
if (Kernel32Module == 0)
|
||||
{
|
||||
::CloseHandle(CurrentProcess);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
LoadLibraryAddress = (LPTHREAD_START_ROUTINE)::GetProcAddress(Kernel32Module, "LoadLibraryW");
|
||||
if (LoadLibraryAddress == 0)
|
||||
{
|
||||
::CloseHandle(CurrentProcess);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
HANDLE hThread = 0;
|
||||
hThread = ::CreateRemoteThread(CurrentProcess, NULL, 0, LoadLibraryAddress, pThreadData, 0, NULL);
|
||||
if (hThread == 0) {
|
||||
::CloseHandle(CurrentProcess);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
WaitForSingleObject(hThread, INFINITE);
|
||||
::CloseHandle(hThread);
|
||||
::CloseHandle(CurrentProcess);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Injector::APC_FreeDll(const std::wstring& dll_path)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
28
ultimateteknotool.v2/Injector.h
Normal file
28
ultimateteknotool.v2/Injector.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
//#include <thread>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <libloaderapi.h>
|
||||
|
||||
// must be singleton
|
||||
class Injector
|
||||
{
|
||||
private:
|
||||
static std::unique_ptr<Injector> m_pInst;
|
||||
static std::once_flag m_Initflag;
|
||||
Injector() = default;
|
||||
|
||||
Injector(const Injector&) = delete;
|
||||
Injector(Injector&&) = delete;
|
||||
Injector& operator=(const Injector&) = delete;
|
||||
Injector& operator=(Injector&&) = delete;
|
||||
|
||||
bool VerifyInject(const std::wstring& dll_path);
|
||||
//[[deprecated]] void OverwriteTargetLibName(const char*, const char*);
|
||||
public:
|
||||
~Injector() = default;
|
||||
static Injector& Inject0r();
|
||||
bool APC_Inject(const std::wstring& dll_path);
|
||||
bool APC_FreeDll(const std::wstring& dll_path);
|
||||
};
|
||||
|
190
ultimateteknotool.v2/JMPHook_SerializeNetdata.cpp
Normal file
190
ultimateteknotool.v2/JMPHook_SerializeNetdata.cpp
Normal file
@ -0,0 +1,190 @@
|
||||
#include "pch.h"
|
||||
#include "JMPHook_SerializeNetdata.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
typedef void* (__fastcall* SerializeNETData_t)(void* _this, void* edx, void* pDst); // game is using this function for serialize net data in cookieecho
|
||||
void* __fastcall hooked_SerializeNetData(void* _this, void* edx, void* pDst);
|
||||
};
|
||||
|
||||
|
||||
static hooks::SerializeNETData_t OriginalFunction;
|
||||
|
||||
struct _sub_6EC2A0_data
|
||||
{
|
||||
uint8_t LocalIP[4]; // 0-3
|
||||
uint16_t LPort; // 4-5
|
||||
uint8_t RouteIP1[4];
|
||||
uint16_t RPort1;
|
||||
uint8_t RouteIP2[4];
|
||||
uint16_t RPort2;
|
||||
uint8_t ExternalIP[4]; // 18-21
|
||||
uint16_t EPort; // 22-23 LPort==EPort
|
||||
};
|
||||
|
||||
static _sub_6EC2A0_data NewData = { 0 };
|
||||
static _sub_6EC2A0_data CurrentData = { 0 };
|
||||
|
||||
namespace MustBeChanged
|
||||
{
|
||||
bool ExternalIP = false;
|
||||
bool LocalIP = false;
|
||||
bool OtherIP1 = false;
|
||||
bool OtherIP2 = false;
|
||||
bool ExternalPort = false;
|
||||
bool LocalPort = false;
|
||||
bool OtherPort1 = false;
|
||||
bool OtherPort2 = false;
|
||||
};
|
||||
|
||||
|
||||
inline void __copy_4bytes__in__4bytes__(uint8_t dstIP[4], const uint8_t srcIP[4])
|
||||
{
|
||||
dstIP[0] = srcIP[0];
|
||||
dstIP[1] = srcIP[1];
|
||||
dstIP[2] = srcIP[2];
|
||||
dstIP[3] = srcIP[3];
|
||||
}
|
||||
|
||||
inline void __copy_sub_6EC2A0_data__in___sub_6EC2A0_data__(_sub_6EC2A0_data* dst, _sub_6EC2A0_data* src)
|
||||
{
|
||||
__copy_4bytes__in__4bytes__(dst->ExternalIP, src->ExternalIP);
|
||||
__copy_4bytes__in__4bytes__(dst->LocalIP, src->LocalIP);
|
||||
__copy_4bytes__in__4bytes__(dst->RouteIP1, src->RouteIP1);
|
||||
__copy_4bytes__in__4bytes__(dst->RouteIP2, src->RouteIP2);
|
||||
dst->EPort = src->EPort;
|
||||
dst->LPort = src->LPort;
|
||||
dst->RPort1 = src->RPort1;
|
||||
dst->RPort2 = src->RPort2;
|
||||
}
|
||||
|
||||
#define MACRO_CHANGEIP(boolv, dst, src) if(boolv){ __copy_4bytes__in__4bytes__(dst, src); }
|
||||
#define MACRO_CHANGEPORT(boolv, dst, src) if(boolv){ dst = src ; }
|
||||
|
||||
void* __fastcall hooks::hooked_SerializeNetData(void* _this, void* edx, void* pDst)
|
||||
{
|
||||
void* ret;
|
||||
ret = OriginalFunction(_this, edx, pDst);
|
||||
|
||||
_sub_6EC2A0_data* pdata = static_cast<_sub_6EC2A0_data*>(pDst);
|
||||
MACRO_CHANGEIP(MustBeChanged::ExternalIP, pdata->ExternalIP, NewData.ExternalIP);
|
||||
MACRO_CHANGEIP(MustBeChanged::LocalIP, pdata->LocalIP, NewData.LocalIP);
|
||||
MACRO_CHANGEIP(MustBeChanged::OtherIP1, pdata->RouteIP1, NewData.RouteIP1);
|
||||
MACRO_CHANGEIP(MustBeChanged::OtherIP2, pdata->RouteIP2, NewData.RouteIP2);
|
||||
|
||||
MACRO_CHANGEPORT(MustBeChanged::ExternalPort, pdata->EPort, NewData.EPort);
|
||||
MACRO_CHANGEPORT(MustBeChanged::LocalPort, pdata->LPort, NewData.LPort);
|
||||
MACRO_CHANGEPORT(MustBeChanged::OtherPort1, pdata->RPort1, NewData.RPort1);
|
||||
MACRO_CHANGEPORT(MustBeChanged::OtherPort2, pdata->RPort2, NewData.RPort2);
|
||||
|
||||
__copy_sub_6EC2A0_data__in___sub_6EC2A0_data__(&CurrentData, pdata);
|
||||
|
||||
windows_accountpage::UpdateCurrentNETData();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#undef MACRO_CHANGEIP
|
||||
#undef MACRO_CHANGEPORT
|
||||
|
||||
bool hooks::JMPHook_SerializeNetData(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<SerializeNETData_t, decltype(hooked_SerializeNetData)>(
|
||||
addr,
|
||||
hooked_SerializeNetData,
|
||||
asmlen,
|
||||
OriginalFunction
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void hooks::serializeNetData_change::ExternalIP(const uint8_t newIP[4])
|
||||
{
|
||||
MustBeChanged::ExternalIP = true;
|
||||
__copy_4bytes__in__4bytes__(NewData.ExternalIP, newIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::LocalIP(const uint8_t newIP[4])
|
||||
{
|
||||
MustBeChanged::LocalIP = true;
|
||||
__copy_4bytes__in__4bytes__(NewData.LocalIP, newIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::OtherlIP1(const uint8_t newIP[4])
|
||||
{
|
||||
MustBeChanged::OtherIP1 = true;
|
||||
__copy_4bytes__in__4bytes__(NewData.RouteIP1, newIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::OtherlIP2(const uint8_t newIP[4])
|
||||
{
|
||||
MustBeChanged::OtherIP2 = true;
|
||||
__copy_4bytes__in__4bytes__(NewData.RouteIP2, newIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::ExternalPort(const uint16_t newPort)
|
||||
{
|
||||
MustBeChanged::ExternalPort = true;
|
||||
NewData.EPort = newPort;
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::LocalPort(const uint16_t newPort)
|
||||
{
|
||||
MustBeChanged::LocalPort = true;
|
||||
NewData.LPort = newPort;
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::OtherPort1(const uint16_t newPort)
|
||||
{
|
||||
MustBeChanged::OtherPort1 = true;
|
||||
NewData.RPort1 = newPort;
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_change::OtherPort2(const uint16_t newPort)
|
||||
{
|
||||
MustBeChanged::OtherPort2 = true;
|
||||
NewData.RPort2 = newPort;
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_get::ExternalIP(uint8_t newIP[4])
|
||||
{
|
||||
__copy_4bytes__in__4bytes__(newIP, CurrentData.ExternalIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_get::LocalIP(uint8_t newIP[4])
|
||||
{
|
||||
__copy_4bytes__in__4bytes__(newIP, CurrentData.LocalIP);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_get::OtherlIP1(uint8_t newIP[4])
|
||||
{
|
||||
__copy_4bytes__in__4bytes__(newIP, CurrentData.RouteIP1);
|
||||
}
|
||||
|
||||
void hooks::serializeNetData_get::OtherlIP2(uint8_t newIP[4])
|
||||
{
|
||||
__copy_4bytes__in__4bytes__(newIP, CurrentData.RouteIP2);
|
||||
}
|
||||
|
||||
uint16_t hooks::serializeNetData_get::ExternalPort()
|
||||
{
|
||||
return CurrentData.EPort;
|
||||
}
|
||||
|
||||
uint16_t hooks::serializeNetData_get::LocalPort()
|
||||
{
|
||||
return CurrentData.LPort;
|
||||
}
|
||||
|
||||
uint16_t hooks::serializeNetData_get::OtherPort1()
|
||||
{
|
||||
return CurrentData.RPort1;
|
||||
}
|
||||
|
||||
uint16_t hooks::serializeNetData_get::OtherPort2()
|
||||
{
|
||||
return CurrentData.RPort2;
|
||||
}
|
||||
|
30
ultimateteknotool.v2/JMPHook_SerializeNetdata.h
Normal file
30
ultimateteknotool.v2/JMPHook_SerializeNetdata.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
#include "windows_accountpage.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
bool JMPHook_SerializeNetData(uint32_t addr, uint8_t asmlen);
|
||||
|
||||
namespace serializeNetData_change {
|
||||
void ExternalIP(const uint8_t newIP[4]);
|
||||
void LocalIP(const uint8_t newIP[4]);
|
||||
void OtherlIP1(const uint8_t newIP[4]);
|
||||
void OtherlIP2(const uint8_t newIP[4]);
|
||||
void ExternalPort(const uint16_t newPort);
|
||||
void LocalPort(const uint16_t newPort);
|
||||
void OtherPort1(const uint16_t newPort);
|
||||
void OtherPort2(const uint16_t newPort);
|
||||
};
|
||||
namespace serializeNetData_get {
|
||||
void ExternalIP(uint8_t newIP[4]);
|
||||
void LocalIP(uint8_t newIP[4]);
|
||||
void OtherlIP1(uint8_t newIP[4]);
|
||||
void OtherlIP2(uint8_t newIP[4]);
|
||||
uint16_t ExternalPort();
|
||||
uint16_t LocalPort();
|
||||
uint16_t OtherPort1();
|
||||
uint16_t OtherPort2();
|
||||
};
|
||||
};
|
34
ultimateteknotool.v2/JMPHook_bdDTLSAssociation_functions.cpp
Normal file
34
ultimateteknotool.v2/JMPHook_bdDTLSAssociation_functions.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "pch.h"
|
||||
#include "JMPHook_bdDTLSAssociation_functions.h"
|
||||
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace bdDTLSAssociation_functions
|
||||
{
|
||||
namespace sendInit
|
||||
{
|
||||
typedef void(__thiscall* sendInit_t)(int thisinstance);
|
||||
static sendInit_t originalFunc = nullptr;
|
||||
void __fastcall Hooked_sendInit(int thisinstance, int _edx);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
bool hooks::bdDTLSAssociation_functions::JMPHook_sendInit(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<sendInit::sendInit_t, decltype(sendInit::Hooked_sendInit)>(
|
||||
addr,
|
||||
sendInit::Hooked_sendInit,
|
||||
asmlen,
|
||||
sendInit::originalFunc
|
||||
);
|
||||
}
|
||||
|
||||
void __fastcall hooks::bdDTLSAssociation_functions::sendInit::Hooked_sendInit(int thisinstance, int _edx)
|
||||
{
|
||||
windows_accountpage::DoJobOnConnect();
|
||||
originalFunc(thisinstance);
|
||||
return;
|
||||
}
|
12
ultimateteknotool.v2/JMPHook_bdDTLSAssociation_functions.h
Normal file
12
ultimateteknotool.v2/JMPHook_bdDTLSAssociation_functions.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
#include "windows_accountpage.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace bdDTLSAssociation_functions
|
||||
{
|
||||
bool JMPHook_sendInit(uint32_t addr, uint8_t asmlen);
|
||||
};
|
||||
};
|
213
ultimateteknotool.v2/JMPHook_bdUPnP_functions.cpp
Normal file
213
ultimateteknotool.v2/JMPHook_bdUPnP_functions.cpp
Normal file
@ -0,0 +1,213 @@
|
||||
#include "pch.h"
|
||||
#include "JMPHook_bdUPnP_functions.h"
|
||||
#include "LazyBuffer.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace bdUPnP_functions
|
||||
{
|
||||
namespace ns_extractDeviceInfo {
|
||||
typedef char(__thiscall* bdUPnP_extractDeviceInfo_t)(void* th1s, int a2, char** a3, DWORD* a4);
|
||||
bdUPnP_extractDeviceInfo_t original_func;
|
||||
char __fastcall hooked_extractDeviceInfo(uint8_t* th1s, void* deadboi, int a2, char** a3, DWORD* a4);
|
||||
static constexpr size_t bufferSize = 256;
|
||||
/*
|
||||
SAMPLE ->
|
||||
LOCATION: http://192.168.0.0:1900/igd.xml
|
||||
SERVER: ipos/7.0 UPnP/1.0 TL-wa850re/5.0
|
||||
ST: urn:schemas-upnp-org:service:WANIPConnection:1
|
||||
USN: uuid:923865f5-f5da-4ad5-15b7-7404637fdf37::urn:schemas-upnp-org:service:WANIPConnection:1
|
||||
*/
|
||||
|
||||
|
||||
template<unsigned short SIZE>
|
||||
class Field
|
||||
{
|
||||
private:
|
||||
uint8_t newtext[SIZE + 1] = { 0 };
|
||||
bool mustchange_field = false;
|
||||
void CopyStringIn(uint8_t* dst) const
|
||||
{
|
||||
for (unsigned short i = 0; i < SIZE && newtext[i]; i++)
|
||||
{
|
||||
dst[i] = newtext[i];
|
||||
}
|
||||
}
|
||||
void Flush()
|
||||
{
|
||||
for (unsigned short i = 0; i < SIZE; i++)
|
||||
{
|
||||
newtext[i] = 0x00;
|
||||
}
|
||||
}
|
||||
public:
|
||||
Field() = default;
|
||||
~Field()= default;
|
||||
void UpdateCurrentString(const uint8_t* src, unsigned short sz_src)
|
||||
{
|
||||
this->Flush();
|
||||
for (unsigned short i = 0; i < SIZE && src[i] && i < sz_src; i++)
|
||||
{
|
||||
newtext[i] = src[i];
|
||||
}
|
||||
this->mustchange_field = true;
|
||||
}
|
||||
void ChangeFieldIn(uint8_t* dst)
|
||||
{
|
||||
this->CopyStringIn(dst);
|
||||
mustchange_field = false;
|
||||
}
|
||||
};
|
||||
|
||||
static Field<maxtext_field_location>location;
|
||||
static Field<maxtext_field_server>server;
|
||||
static Field<maxtext_field_USN>USN;
|
||||
|
||||
static uint8_t* stat_relative = nullptr;
|
||||
uint8_t* GetFirstRightEntry()
|
||||
{
|
||||
return stat_relative;
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
static CONSTEXPRENCRYPTSTRW(Error_UPnPSpoofFailed, L"<Error>UPnP spoof failed", 0x31F1);
|
||||
#define MACRO_Error_UPnPSpoofFailed DECRYPTCONSTENCSTRW(Error_UPnPSpoofFailed, sizeof(L"<Error>UPnP spoof failed") / sizeof(wchar_t), 0x31F1)
|
||||
|
||||
bool hooks::bdUPnP_functions::JMPHook_extractDeviceInfo(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<ns_extractDeviceInfo::bdUPnP_extractDeviceInfo_t, decltype(ns_extractDeviceInfo::hooked_extractDeviceInfo)>(
|
||||
addr,
|
||||
ns_extractDeviceInfo::hooked_extractDeviceInfo,
|
||||
asmlen,
|
||||
ns_extractDeviceInfo::original_func
|
||||
);
|
||||
}
|
||||
|
||||
void hooks::bdUPnP_functions::SetDeviceInfo_location(const wchar_t* str)
|
||||
{
|
||||
bdUPnP_functions::ns_extractDeviceInfo::location.UpdateCurrentString(
|
||||
textconvertors::ConvertWTextToU8Text(str, bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_location),
|
||||
bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_location
|
||||
);
|
||||
}
|
||||
|
||||
void hooks::bdUPnP_functions::SetDeviceInfo_server(const wchar_t* str)
|
||||
{
|
||||
bdUPnP_functions::ns_extractDeviceInfo::server.UpdateCurrentString(
|
||||
textconvertors::ConvertWTextToU8Text(str, bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_server),
|
||||
bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_server
|
||||
);
|
||||
}
|
||||
|
||||
void hooks::bdUPnP_functions::SetDeviceInfo_USN(const wchar_t* str)
|
||||
{
|
||||
bdUPnP_functions::ns_extractDeviceInfo::USN.UpdateCurrentString(
|
||||
textconvertors::ConvertWTextToU8Text(str, bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_USN),
|
||||
bdUPnP_functions::ns_extractDeviceInfo::maxtext_field_USN
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
template<uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4>
|
||||
uint8_t* searchEntry(uint8_t* th1s, const size_t len)
|
||||
{
|
||||
uint8_t* addr = th1s;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
if (*addr == b1)
|
||||
{
|
||||
addr++;
|
||||
if (*addr == b2)
|
||||
{
|
||||
addr++;
|
||||
if (*addr == b3)
|
||||
{
|
||||
addr++;
|
||||
if (*addr == b4)
|
||||
{
|
||||
addr++;
|
||||
return addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addr++;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
static_assert(nullptr == 0, "nullptr is not equal to null?!");
|
||||
|
||||
char __fastcall hooks::bdUPnP_functions::ns_extractDeviceInfo::hooked_extractDeviceInfo(uint8_t* th1s, void* deadboi, int a2, char** a3, DWORD* a4)
|
||||
{
|
||||
uint8_t* relative = th1s;
|
||||
size_t validsize = bufferSize;
|
||||
|
||||
relative = searchEntry<'T', 'I', 'O', 'N'>(th1s, validsize);
|
||||
if (relative != 0 )
|
||||
{
|
||||
relative += 2;
|
||||
stat_relative = relative;
|
||||
location.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::Location(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, maxtext_field_location));
|
||||
relative = searchEntry<'R', 'V', 'E', 'R'>(relative, validsize);
|
||||
if (relative != 0)
|
||||
{
|
||||
relative += 2;
|
||||
relative += 18;
|
||||
server.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::Server(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, maxtext_field_server));
|
||||
relative = searchEntry<'U', 'S', 'N', ':'>(relative, validsize);
|
||||
if (relative != 0)
|
||||
{
|
||||
relative += 1;
|
||||
USN.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::USN(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, maxtext_field_USN));
|
||||
}
|
||||
else {
|
||||
windows_accountpage::UPnP::update_device::USN(MACRO_Error_UPnPSpoofFailed);
|
||||
}
|
||||
}
|
||||
else { windows_accountpage::UPnP::update_device::Server(MACRO_Error_UPnPSpoofFailed); }
|
||||
}
|
||||
else { windows_accountpage::UPnP::update_device::Location(MACRO_Error_UPnPSpoofFailed); }
|
||||
return original_func(th1s, a2, a3, a4);
|
||||
}
|
||||
|
||||
void hooks::bdUPnP_functions::SetDeviceInfo()
|
||||
{
|
||||
uint8_t* relative = nullptr;
|
||||
relative = ns_extractDeviceInfo::GetFirstRightEntry();
|
||||
if (relative == nullptr) { return; }
|
||||
size_t validsize = ns_extractDeviceInfo::bufferSize;
|
||||
ns_extractDeviceInfo::location.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::Location(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, ns_extractDeviceInfo::maxtext_field_location));
|
||||
relative = searchEntry<'R', 'V', 'E', 'R'>(relative, validsize);
|
||||
if (relative != 0)
|
||||
{
|
||||
relative += 2;
|
||||
relative += 18;
|
||||
ns_extractDeviceInfo::server.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::Server(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, ns_extractDeviceInfo::maxtext_field_server));
|
||||
relative = searchEntry<'U', 'S', 'N', ':'>(relative, validsize);
|
||||
if (relative != 0)
|
||||
{
|
||||
relative += 1;
|
||||
ns_extractDeviceInfo::USN.ChangeFieldIn(relative);
|
||||
windows_accountpage::UPnP::update_device::USN(textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(relative, ns_extractDeviceInfo::maxtext_field_USN));
|
||||
}
|
||||
else {
|
||||
windows_accountpage::UPnP::update_device::USN(MACRO_Error_UPnPSpoofFailed);
|
||||
}
|
||||
}
|
||||
else { windows_accountpage::UPnP::update_device::Server(MACRO_Error_UPnPSpoofFailed); }
|
||||
}
|
||||
|
||||
|
||||
#undef MACRO_Error_UPnPSpoofFailed
|
27
ultimateteknotool.v2/JMPHook_bdUPnP_functions.h
Normal file
27
ultimateteknotool.v2/JMPHook_bdUPnP_functions.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
#include "windows_accountpage.h"
|
||||
#include "TextConvertors.h"
|
||||
#include "compiletime_XORstring.h"
|
||||
#include "debugconsole.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace bdUPnP_functions
|
||||
{
|
||||
bool JMPHook_extractDeviceInfo(uint32_t addr, uint8_t asmlen);
|
||||
void SetDeviceInfo_location(const wchar_t* str);
|
||||
void SetDeviceInfo_server(const wchar_t* str);
|
||||
void SetDeviceInfo_USN(const wchar_t* str);
|
||||
void SetDeviceInfo();
|
||||
namespace ns_extractDeviceInfo
|
||||
{
|
||||
constexpr unsigned short maxtext_field_location = 36;
|
||||
constexpr unsigned short maxtext_field_server = 36;
|
||||
constexpr unsigned short maxtext_field_USN = 72;
|
||||
uint8_t* GetFirstRightEntry();
|
||||
};
|
||||
|
||||
};
|
||||
};
|
34
ultimateteknotool.v2/JMPHook_iphlpapi_functions.cpp
Normal file
34
ultimateteknotool.v2/JMPHook_iphlpapi_functions.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "pch.h"
|
||||
#include "JMPHook_iphlpapi_functions.h"
|
||||
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace iphlpapi_functions
|
||||
{
|
||||
typedef DWORD (*GetInterfaceInfo_t)(
|
||||
PIP_INTERFACE_INFO pIfTable,
|
||||
PULONG dwOutBufLen
|
||||
);
|
||||
GetInterfaceInfo_t OriginalFunc = nullptr;
|
||||
DWORD Hook_GetInterfaceInfo(
|
||||
PIP_INTERFACE_INFO pIfTable,
|
||||
PULONG dwOutBufLen
|
||||
)
|
||||
{
|
||||
WCWOUT("Hook_GetInterfaceInfo");
|
||||
return OriginalFunc(pIfTable, dwOutBufLen);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
bool hooks::iphlpapi_functions::JMPHook_GetInterfaceInfo(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<GetInterfaceInfo_t, decltype(Hook_GetInterfaceInfo)>(
|
||||
addr,
|
||||
Hook_GetInterfaceInfo,
|
||||
asmlen,
|
||||
OriginalFunc
|
||||
);
|
||||
}
|
14
ultimateteknotool.v2/JMPHook_iphlpapi_functions.h
Normal file
14
ultimateteknotool.v2/JMPHook_iphlpapi_functions.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
#include <iphlpapi.h>
|
||||
#pragma comment(lib, "iphlpapi.lib")
|
||||
#include "debugconsole.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace iphlpapi_functions
|
||||
{
|
||||
bool JMPHook_GetInterfaceInfo(uint32_t addr, uint8_t asmlen);
|
||||
};
|
||||
};
|
333
ultimateteknotool.v2/JMPHook_sysinfoapi_functions.cpp
Normal file
333
ultimateteknotool.v2/JMPHook_sysinfoapi_functions.cpp
Normal file
@ -0,0 +1,333 @@
|
||||
#include "pch.h"
|
||||
#include "JMPHook_sysinfoapi_functions.h"
|
||||
#include "windows_accountpage.h"
|
||||
#include "Randomizer.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace sysinfoapi_functions
|
||||
{
|
||||
namespace ns_getSystemFirmwareTable {
|
||||
typedef UINT(WINAPI* GetSystemFirmwareTable_t) (DWORD FirmwareTableProviderSignature, DWORD FirmwareTableID, PVOID pFirmwareTableBuffer, DWORD BufferSize);
|
||||
static GetSystemFirmwareTable_t OriginalFunction = nullptr;
|
||||
UINT WINAPI hooked_GetSystemFirmwareTable(DWORD FirmwareTableProviderSignature, DWORD FirmwareTableID, PVOID pFirmwareTableBuffer, DWORD BufferSize);
|
||||
static bool request_generate_new_data = true;
|
||||
static bool always_generate_new_data = false;
|
||||
static bool faking_firmware_data_is_enabled = false;
|
||||
static bool stupid_randomise_whole_data = false;
|
||||
typedef struct _RawSMBIOSData
|
||||
{
|
||||
BYTE Used20CallingMethod;
|
||||
BYTE SMBIOSMajorVersion;
|
||||
BYTE SMBIOSMinorVersion;
|
||||
BYTE DmiRevision;
|
||||
DWORD Length;
|
||||
BYTE* SMBIOSTableData;
|
||||
} RawSMBIOSData, * PRawSMBIOSData;
|
||||
typedef struct _SMBIOSHEADER_
|
||||
{
|
||||
BYTE Type;
|
||||
BYTE Length;
|
||||
WORD Handle;
|
||||
} SMBIOSHEADER, * PSMBIOSHEADER;
|
||||
/*
|
||||
real one header struct
|
||||
struct HEADER
|
||||
{
|
||||
BYTE type;
|
||||
BYTE length;
|
||||
BYTE data[length - 2];
|
||||
BYTE Text[depend on];
|
||||
BYTE _nulledbyte; // so struct has doublenull in the end
|
||||
}
|
||||
*/
|
||||
enum StructureType
|
||||
{
|
||||
BIOS_INFO_TYPE = 0x00,
|
||||
SYSTEM_INFO_TYPE = 0x01,
|
||||
BASEBOARD_INFO_TYPE = 0x02,
|
||||
CHASSIS_INFO_TYPE = 0x03,
|
||||
PROCESSOR_INFO_TYPE = 0x04,
|
||||
CACHE_INFO_TYPE = 0x07,
|
||||
PORTS_INFO_TYPE = 0x08,
|
||||
SYSTEMSLOTS_INFO_TYPE = 0x09,
|
||||
ONBOARDDEVS_INFO_TYPE = 0x0A,
|
||||
OEMSTRING_INFO_TYPE = 0x0B,
|
||||
SYSTEMCONFIG_INFO_TYPE = 0x0C,
|
||||
BIOSLANG_INFO_TYPE = 0x0D,
|
||||
GROUPASSOCS_INFO_TYPE = 0x0E,
|
||||
SYSLOG_INFO_TYPE = 0x0F,
|
||||
PHYSMEM_INFO_TYPE = 0x10,
|
||||
MEMDEV_INFO_TYPE = 0x11,
|
||||
MEMERROR32_INFO_TYPE = 0x12,
|
||||
MEMARRAYMAPPED_INFO_TYPE = 0x13,
|
||||
MEMDEVMAPPED_INFO_TYPE = 0x14,
|
||||
BUILTINPTRDEV_INFO_TYPE = 0x15,
|
||||
BATTERY_INFO_TYPE = 0x16,
|
||||
SYSRESET_INFO_TYPE = 0x17,
|
||||
HARDSEC_INFO_TYPE = 0x18,
|
||||
SYSPOWER_INFO_TYPE = 0x19,
|
||||
VOLTPROBE_INFO_TYPE = 0x1A,
|
||||
COOLINGDEV_INFO_TYPE = 0x1B,
|
||||
TEMPPROBE_INFO_TYPE = 0x1C,
|
||||
ELECPROBE_INFO_TYPE = 0x1D,
|
||||
OOBRA_INFO_TYPE = 0x1E,
|
||||
SYSBOOT_INFO_TYPE = 0x20,
|
||||
MEMERROR64_INFO_TYPE = 0x21,
|
||||
MNGDEV_INFO_TYPE = 0x22,
|
||||
MNGDEVCOMP_INFO_TYPE = 0x23,
|
||||
MNGDEVTHRES_INFO_TYPE = 0x24,
|
||||
MEMCHAN_INFO_TYPE = 0x25,
|
||||
IPMIDEV_INFO_TYPE = 0x26,
|
||||
POWERSUPPLY_INFO_TYPE = 0x27,
|
||||
ADDITIONAL_INFO_TYPE = 0x28,
|
||||
ONBOARDDEVSEX_INFO_TYPE = 0x29,
|
||||
MNGCTRLHOSTIF_INFO_TYPE = 0x2A,
|
||||
INACTIVE_INFO_TYPE = 0x7E,
|
||||
EOF_INFO_TYPE = 0x7F,
|
||||
};
|
||||
/*
|
||||
h000 b22fa79e 4f86f8a0 9e8ea282 539f98a0 normal hash
|
||||
h001 43677e04 ceccbe47 3f2be224 2d700c7b changing data -> new hash
|
||||
h002 7b738ab1 23373d8d 5f308163 6975e1d3 changing data -> new hash
|
||||
h003 b22fa79e 4f86f8a0 9e8ea282 539f98a0 normal hash
|
||||
...
|
||||
h010 b22fa79e 4f86f8a0 9e8ea282 539f98a0 normal hash
|
||||
h011 e7059b40 85f66d3a 892101a4 f0e65b93 changing data -> new hash
|
||||
h012 b22fa79e 4f86f8a0 9e8ea282 539f98a0 normal hash
|
||||
...
|
||||
h255 b22fa79e 4f86f8a0 9e8ea282 539f98a0 normal hash
|
||||
|
||||
GSFT: blocksize->12
|
||||
data->1.2.?240.3.255.128.152.139.55.1.??
|
||||
GSFT: blocksize->124
|
||||
text->xxxxxxxxxxxxxxxxxx?xxxxGigabyte Technology Co., Ltd.?Z390 UD?Default string?Default string?Default string?Default string?
|
||||
GSFT: blocksize->101
|
||||
text->xxxxxxxxxxxxxx??Gigabyte Technology Co., Ltd.?Z390 UD?x.x?Default string?Default string?Default string?
|
||||
GSFT: blocksize->9
|
||||
data->9.17.28.?1.165.13.4.4.?
|
||||
GSFT: blocksize->19
|
||||
text->xxx?xDefault string?
|
||||
GSFT: blocksize->8
|
||||
data->34.11.37.?1.4.???
|
||||
GSFT: blocksize->12
|
||||
data->17.40.59.?58.?254.255.?????
|
||||
GSFT: blocksize->12
|
||||
data->17.40.61.?58.?254.255.?????
|
||||
GSFT: blocksize->17
|
||||
data->221.26.65.?3.1.?7.?49.52.?2.?????
|
||||
GSFT: blocksize->5
|
||||
data->11.?7.???
|
||||
GSFT: blocksize->4
|
||||
data->1.????
|
||||
GSFT: blocksize->12
|
||||
data->10.?255.255.255.255.255.11.?255.255.??
|
||||
GSFT: blocksize->1
|
||||
data->16.?
|
||||
|
||||
blocksize (not intended) = 4,5,8,9,17,19
|
||||
|
||||
GSFT: blocksize->12
|
||||
data->1.2.?240.3.255.128.152.139.55.1.??
|
||||
GSFT: blocksize->124
|
||||
text->xxxxxxxxxxxxxxxxxx?xxxxGigabyte Technology Co., Ltd.?Z390 UD?Default string?Default string?Default string?Default string?
|
||||
GSFT: blocksize->101
|
||||
text->xxxxxxxxxxxxxx??Gigabyte Technology Co., Ltd.?Z390 UD?x.x?Default string?Default string?Default string?
|
||||
GSFT: blocksize->12
|
||||
data->17.40.59.?58.?254.255.?????
|
||||
GSFT: blocksize->12
|
||||
data->17.40.59.?58.?254.255.?????
|
||||
GSFT: blocksize->12
|
||||
data->10.?255.255.255.255.255.11.?255.255.??
|
||||
*/
|
||||
template<size_t BUFFSIZE>
|
||||
class GenFakeData
|
||||
{
|
||||
private:
|
||||
char _data[BUFFSIZE] = { 0 };
|
||||
bool _is_generated = false;
|
||||
public:
|
||||
GenFakeData() = default;
|
||||
~GenFakeData() = default;
|
||||
void RegenerateData() { this->_is_generated = false; }
|
||||
char* GetData()
|
||||
{
|
||||
if (this->_is_generated == false)
|
||||
{
|
||||
for (size_t i = 0; i < BUFFSIZE; i++)
|
||||
{
|
||||
this->_data[i] = RANDALNUM;
|
||||
}
|
||||
this->_is_generated = true;
|
||||
}
|
||||
return this->_data;
|
||||
}
|
||||
size_t GetSZ() const { return BUFFSIZE; }
|
||||
};
|
||||
|
||||
GenFakeData<128>x128_data;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
bool hooks::sysinfoapi_functions::JMPHook_GetSystemFirmwareTable(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<ns_getSystemFirmwareTable::GetSystemFirmwareTable_t, decltype(ns_getSystemFirmwareTable::hooked_GetSystemFirmwareTable)>(
|
||||
addr,
|
||||
ns_getSystemFirmwareTable::hooked_GetSystemFirmwareTable,
|
||||
asmlen,
|
||||
ns_getSystemFirmwareTable::OriginalFunction
|
||||
);
|
||||
}
|
||||
|
||||
void hooks::sysinfoapi_functions::GetSystemFirmwareTable_SetState_GenerateNewData()
|
||||
{
|
||||
ns_getSystemFirmwareTable::request_generate_new_data = true;
|
||||
}
|
||||
|
||||
void hooks::sysinfoapi_functions::GetSystemFirmwareTable_SetState_FakingDataEnabled(const bool& state)
|
||||
{
|
||||
|
||||
ns_getSystemFirmwareTable::faking_firmware_data_is_enabled = state;
|
||||
WCWOUT("ns_getSystemFirmwareTable::faking_firmware_data_is_enabled->", ns_getSystemFirmwareTable::faking_firmware_data_is_enabled);
|
||||
}
|
||||
|
||||
void hooks::sysinfoapi_functions::GetSystemFirmwareTable_SetState_AlwaysGenerateNewDataEnabled(const bool& state)
|
||||
{
|
||||
ns_getSystemFirmwareTable::always_generate_new_data = state;
|
||||
}
|
||||
|
||||
void hooks::sysinfoapi_functions::GetSystemFirmwareTable_SetState_RandomiseAllDataEnabled(const bool& state)
|
||||
{
|
||||
ns_getSystemFirmwareTable::stupid_randomise_whole_data = state;
|
||||
}
|
||||
|
||||
std::wstringstream wstream;
|
||||
std::wstringstream wstream2;
|
||||
|
||||
|
||||
UINT WINAPI hooks::sysinfoapi_functions::ns_getSystemFirmwareTable::hooked_GetSystemFirmwareTable
|
||||
(DWORD FirmwareTableProviderSignature, DWORD FirmwareTableID, PVOID pFirmwareTableBuffer, DWORD BufferSize)
|
||||
{
|
||||
if (pFirmwareTableBuffer == NULL) { return OriginalFunction(FirmwareTableProviderSignature, FirmwareTableID, pFirmwareTableBuffer, BufferSize); }
|
||||
UINT ret = 0;
|
||||
ret = OriginalFunction(FirmwareTableProviderSignature, FirmwareTableID, pFirmwareTableBuffer, BufferSize);
|
||||
if (ret != 0) {
|
||||
const PRawSMBIOSData pDMIData = static_cast<const PRawSMBIOSData>(pFirmwareTableBuffer);
|
||||
const LPBYTE pEOFData = static_cast<const LPBYTE>(pFirmwareTableBuffer) + pDMIData->Length;
|
||||
PSMBIOSHEADER pHeader = reinterpret_cast<PSMBIOSHEADER>(&(pDMIData->SMBIOSTableData));
|
||||
LPBYTE pCurrentData = reinterpret_cast<LPBYTE>(&(pDMIData->SMBIOSTableData));
|
||||
LPBYTE prevDataP = pCurrentData;
|
||||
SIZE_T blockSize = 0;
|
||||
SIZE_T exBlockSize = 0;
|
||||
size_t counterchecker = 0;
|
||||
char* FakeData = x128_data.GetData();
|
||||
|
||||
if (request_generate_new_data || always_generate_new_data) {
|
||||
x128_data.RegenerateData();
|
||||
request_generate_new_data = false;
|
||||
}
|
||||
if (faking_firmware_data_is_enabled)
|
||||
{
|
||||
if (stupid_randomise_whole_data)
|
||||
{
|
||||
while (pCurrentData <= pEOFData)
|
||||
{
|
||||
if (pCurrentData[0] == 0 && pCurrentData[1] == 0 && pCurrentData[2] != 0)
|
||||
{
|
||||
prevDataP = pCurrentData;
|
||||
while (blockSize != 0)
|
||||
{
|
||||
prevDataP--;
|
||||
if (*prevDataP) { *prevDataP = RANDBYTE; }
|
||||
blockSize--;
|
||||
}
|
||||
pCurrentData += 2;
|
||||
blockSize = 0;
|
||||
}
|
||||
pCurrentData++;
|
||||
blockSize++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (pCurrentData <= pEOFData)
|
||||
{
|
||||
if (pCurrentData[0] == 0 && pCurrentData[1] == 0 && pCurrentData[2] != 0 /* EOData p[2] is next header, header is non-unique*/)
|
||||
{
|
||||
|
||||
if (pCurrentData[2] == 1 + 1 || pCurrentData[2] == 2 + 1 || pCurrentData[2] == 11 + 1)
|
||||
{
|
||||
exBlockSize = blockSize;
|
||||
prevDataP = pCurrentData;
|
||||
WCWOUT(L"GSFT: blocksize->", std::dec, blockSize);
|
||||
while (blockSize != 0)
|
||||
{
|
||||
prevDataP--;
|
||||
blockSize--;
|
||||
}
|
||||
if (exBlockSize > 50) {
|
||||
while (prevDataP < pCurrentData)
|
||||
{
|
||||
if (::isalnum(prevDataP[0]) && ::isalnum(prevDataP[1]) && ::isalnum(prevDataP[2]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
prevDataP++;
|
||||
}
|
||||
if (prevDataP < pCurrentData) {
|
||||
FakeData = x128_data.GetData();
|
||||
for (counterchecker = 0; counterchecker < x128_data.GetSZ() && *prevDataP; counterchecker++)
|
||||
{
|
||||
*prevDataP = FakeData[counterchecker];
|
||||
prevDataP++;
|
||||
}
|
||||
}
|
||||
while (prevDataP < pCurrentData)
|
||||
{
|
||||
prevDataP++;
|
||||
}
|
||||
while (exBlockSize != 0)
|
||||
{
|
||||
prevDataP--;
|
||||
exBlockSize--;
|
||||
}
|
||||
wstream.clear();
|
||||
wstream.str(L"");
|
||||
while (prevDataP < pCurrentData)
|
||||
{
|
||||
wstream << (wchar_t)*prevDataP;
|
||||
prevDataP++;
|
||||
}
|
||||
WCWOUT(L"Firmware: ", wstream.str());
|
||||
}
|
||||
else
|
||||
{
|
||||
while (prevDataP < pCurrentData)
|
||||
{
|
||||
/* modifying header's data here */
|
||||
|
||||
/* end */
|
||||
prevDataP++;
|
||||
}
|
||||
}
|
||||
}
|
||||
pCurrentData += 2;
|
||||
blockSize = 0;
|
||||
}
|
||||
pCurrentData++;
|
||||
blockSize++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
16
ultimateteknotool.v2/JMPHook_sysinfoapi_functions.h
Normal file
16
ultimateteknotool.v2/JMPHook_sysinfoapi_functions.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
#include "debugconsole.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace sysinfoapi_functions
|
||||
{
|
||||
bool JMPHook_GetSystemFirmwareTable(uint32_t addr, uint8_t asmlen);
|
||||
void GetSystemFirmwareTable_SetState_GenerateNewData();
|
||||
void GetSystemFirmwareTable_SetState_FakingDataEnabled(const bool& state);
|
||||
void GetSystemFirmwareTable_SetState_AlwaysGenerateNewDataEnabled(const bool& state);
|
||||
void GetSystemFirmwareTable_SetState_RandomiseAllDataEnabled(const bool& state);
|
||||
};
|
||||
};
|
94
ultimateteknotool.v2/JMP_iw5mp_functions.cpp
Normal file
94
ultimateteknotool.v2/JMP_iw5mp_functions.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include "pch.h"
|
||||
#include "JMP_iw5mp_functions.h"
|
||||
#include "windows_accountpage.h"
|
||||
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace iw5mp_functions
|
||||
{
|
||||
namespace ns_GetChallengeS2
|
||||
{
|
||||
typedef uint32_t* (*getChallengeS2_t)(void);
|
||||
getChallengeS2_t OriginalFunction = nullptr;
|
||||
uint32_t* hooked_GetChallengeS2();
|
||||
struct SChallengeS2
|
||||
{
|
||||
uint32_t p1;
|
||||
uint32_t p2;
|
||||
uint32_t p3;
|
||||
uint32_t p4;
|
||||
void Set(uint32_t* addr)
|
||||
{
|
||||
this->p1 = addr[0];
|
||||
this->p2 = addr[1];
|
||||
this->p3 = addr[2];
|
||||
this->p4 = addr[3];
|
||||
}
|
||||
};
|
||||
SChallengeS2 currentData = { 0 };
|
||||
SChallengeS2 newData = { 0 };
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
bool hooks::iw5mp_functions::JMPHook_GetChallengeS2(uint32_t addr, uint8_t asmlen)
|
||||
{
|
||||
return Hooker_JMP::Hook32<ns_GetChallengeS2::getChallengeS2_t, decltype(ns_GetChallengeS2::hooked_GetChallengeS2)>(
|
||||
addr,
|
||||
ns_GetChallengeS2::hooked_GetChallengeS2,
|
||||
asmlen,
|
||||
ns_GetChallengeS2::OriginalFunction
|
||||
);
|
||||
}
|
||||
|
||||
uint32_t* hooks::iw5mp_functions::ns_GetChallengeS2::hooked_GetChallengeS2()
|
||||
{
|
||||
uint32_t* ret = OriginalFunction();
|
||||
if (newData.p1) { ret[0] = newData.p1; }
|
||||
if (newData.p2) { ret[1] = newData.p2; }
|
||||
if (newData.p3) { ret[2] = newData.p3; }
|
||||
if (newData.p4) { ret[3] = newData.p4; }
|
||||
currentData.Set(ret);
|
||||
windows_accountpage::UpdateCurrentChallengeS2();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t hooks::iw5mp_functions::ns_GetChallengeS2::Get_P1() noexcept
|
||||
{
|
||||
return currentData.p1;
|
||||
}
|
||||
uint32_t hooks::iw5mp_functions::ns_GetChallengeS2::Get_P2() noexcept
|
||||
{
|
||||
return currentData.p2;
|
||||
}
|
||||
uint32_t hooks::iw5mp_functions::ns_GetChallengeS2::Get_P3() noexcept
|
||||
{
|
||||
return currentData.p3;
|
||||
}
|
||||
uint32_t hooks::iw5mp_functions::ns_GetChallengeS2::Get_P4() noexcept
|
||||
{
|
||||
return currentData.p4;
|
||||
}
|
||||
|
||||
void hooks::iw5mp_functions::ns_GetChallengeS2::Set_P1(uint32_t nv) noexcept
|
||||
{
|
||||
newData.p1 = nv;
|
||||
}
|
||||
|
||||
void hooks::iw5mp_functions::ns_GetChallengeS2::Set_P2(uint32_t nv) noexcept
|
||||
{
|
||||
newData.p2 = nv;
|
||||
}
|
||||
|
||||
void hooks::iw5mp_functions::ns_GetChallengeS2::Set_P3(uint32_t nv) noexcept
|
||||
{
|
||||
newData.p3 = nv;
|
||||
}
|
||||
|
||||
void hooks::iw5mp_functions::ns_GetChallengeS2::Set_P4(uint32_t nv) noexcept
|
||||
{
|
||||
newData.p4 = nv;
|
||||
}
|
||||
|
||||
|
23
ultimateteknotool.v2/JMP_iw5mp_functions.h
Normal file
23
ultimateteknotool.v2/JMP_iw5mp_functions.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "Hooker_JMP.h"
|
||||
|
||||
namespace hooks
|
||||
{
|
||||
namespace iw5mp_functions
|
||||
{
|
||||
bool JMPHook_GetChallengeS2(uint32_t addr, uint8_t asmlen);
|
||||
namespace ns_GetChallengeS2
|
||||
{
|
||||
uint32_t Get_P1() noexcept;
|
||||
uint32_t Get_P2() noexcept;
|
||||
uint32_t Get_P3() noexcept;
|
||||
uint32_t Get_P4() noexcept;
|
||||
|
||||
void Set_P1(uint32_t nv) noexcept;
|
||||
void Set_P2(uint32_t nv) noexcept;
|
||||
void Set_P3(uint32_t nv) noexcept;
|
||||
void Set_P4(uint32_t nv) noexcept;
|
||||
};
|
||||
};
|
||||
};
|
2
ultimateteknotool.v2/LazyBuffer.cpp
Normal file
2
ultimateteknotool.v2/LazyBuffer.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "pch.h"
|
||||
#include "LazyBuffer.h"
|
279
ultimateteknotool.v2/LazyBuffer.h
Normal file
279
ultimateteknotool.v2/LazyBuffer.h
Normal file
@ -0,0 +1,279 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "debugconsole.h"
|
||||
#include "DRM/DRM_DllUnloadThread.h"
|
||||
|
||||
template<typename T1, unsigned short SIZE>
|
||||
class LazyBuffer
|
||||
{
|
||||
private:
|
||||
std::vector<T1> m_vec;
|
||||
unsigned short m_currentNullPos = 0;
|
||||
public:
|
||||
LazyBuffer():m_vec(SIZE+4, 0)
|
||||
{
|
||||
|
||||
}
|
||||
~LazyBuffer() = default;
|
||||
T1* GetNulledMem(unsigned short sz)
|
||||
{
|
||||
if (sz > SIZE)
|
||||
{
|
||||
throw 1;
|
||||
}
|
||||
|
||||
if (sz + m_currentNullPos > SIZE)
|
||||
{
|
||||
for (auto& x : this->m_vec) { x = 0; }
|
||||
this->m_currentNullPos = 0;
|
||||
this->m_currentNullPos += sz;
|
||||
return &(this->m_vec[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_currentNullPos += sz;
|
||||
return this->m_vec.data() + (this->m_currentNullPos - sz);
|
||||
}
|
||||
}
|
||||
#ifdef UTT2_DEBUG
|
||||
void fill(T1* p, unsigned short sz, T1 value = 1)
|
||||
{
|
||||
for (unsigned short i = 0; i < sz; i++)
|
||||
{
|
||||
p[i] = value;
|
||||
}
|
||||
}
|
||||
bool checvalid()
|
||||
{
|
||||
// asked sz -> next byte/wchar_t must be null
|
||||
unsigned short verifyer = this->m_currentNullPos;
|
||||
while (verifyer < SIZE+1)
|
||||
{
|
||||
if (*(this->m_vec.data() + verifyer) != 0)
|
||||
{
|
||||
WCWOUT(std::dec,L"Check failed at this->m_vec[",verifyer,L"]");
|
||||
return false;
|
||||
}
|
||||
verifyer++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Test()
|
||||
{
|
||||
T1* p = nullptr;
|
||||
unsigned short sz = SIZE / 2;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; };
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz+1);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
sz = 100;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
sz = SIZE - 1;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
sz = 1;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
sz = SIZE - 1;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
sz = 2;
|
||||
p = this->GetNulledMem(sz);
|
||||
fill(p, sz);
|
||||
if (!checvalid()) { return false; }
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template<unsigned short SIZE>
|
||||
using LazyBuffer_wchart = LazyBuffer<wchar_t, SIZE>;
|
||||
|
||||
template<unsigned short SIZE>
|
||||
using LazyBuffer_char = LazyBuffer<char, SIZE>;
|
||||
|
||||
template<typename T1, unsigned short SECTIONSIZE, unsigned char NUMBEROFSECTIONS>
|
||||
class LazySectionBuffer
|
||||
{
|
||||
private:
|
||||
std::vector<T1> m_vec;
|
||||
T1* retAddr[NUMBEROFSECTIONS] = { nullptr };
|
||||
public:
|
||||
LazySectionBuffer():m_vec(SECTIONSIZE * NUMBEROFSECTIONS + 4, 0)
|
||||
{
|
||||
for(unsigned char i = 0; i < NUMBEROFSECTIONS; i++)
|
||||
{
|
||||
this->retAddr[i] = this->m_vec.data() + SECTIONSIZE * i;
|
||||
}
|
||||
}
|
||||
~LazySectionBuffer() = default;
|
||||
T1* GetNulledMem(unsigned short sz, unsigned char section_id)
|
||||
{
|
||||
if (sz > SECTIONSIZE || section_id >= NUMBEROFSECTIONS)
|
||||
{
|
||||
throw 1;
|
||||
}
|
||||
|
||||
//WCWOUT(L"ret[", std::dec, section_id, L"] = &(this->m_vec[", retAddr[section_id] - this->m_vec.data(), L"]) when size = ", sz);
|
||||
if (retAddr[section_id] + sz > this->m_vec.data() + SECTIONSIZE * section_id + SECTIONSIZE)
|
||||
{
|
||||
retAddr[section_id] = this->m_vec.data() + SECTIONSIZE * section_id;
|
||||
for (size_t i = 0; i < SECTIONSIZE; i++)
|
||||
{
|
||||
retAddr[section_id][i] = 0x00;
|
||||
}
|
||||
retAddr[section_id] += sz;
|
||||
//WCWOUT(L"return = ", std::dec, retAddr[section_id] - this->m_vec.data() - sz);
|
||||
return retAddr[section_id] - sz;
|
||||
}
|
||||
else
|
||||
{
|
||||
retAddr[section_id] += sz;
|
||||
//WCWOUT(L"return = ", std::dec, retAddr[section_id] - this->m_vec.data() - sz );
|
||||
return retAddr[section_id] - sz;
|
||||
}
|
||||
}
|
||||
#ifdef UTT2_DEBUG
|
||||
bool VerifyMem_OnlyOneSection(unsigned char secID)
|
||||
{
|
||||
T1* vecData = this->m_vec.data() + SECTIONSIZE * secID;
|
||||
size_t SZ = retAddr[secID] - vecData;
|
||||
if (SZ > SECTIONSIZE)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->VerifyMem(): false->SZ > SECTIONSIZE");
|
||||
return false;
|
||||
}
|
||||
for (size_t i = SZ; i < SECTIONSIZE; i++)
|
||||
{
|
||||
if (retAddr[secID][i] != 0)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->VerifyMem(): false->retAddr[", std::dec, secID, L"][", i , L"] != 0" );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ((this->m_vec.data() + SECTIONSIZE * secID)[SECTIONSIZE] != 0)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->VerifyMem(): false->(this->m_vec.data() + SECTIONSIZE * secID)[SECTIONSIZE] != 0");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool VerifyMem_allfilled(unsigned char secID, unsigned short size)
|
||||
{
|
||||
T1* vecData = this->m_vec.data() + secID * SECTIONSIZE;
|
||||
size_t SZ = retAddr[secID] - vecData;
|
||||
if (SZ != size)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->VerifyMem_allfilled(): false->wrong pointer, size is ", SZ, L" when expect ", size);
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (vecData[i] == 0)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->VerifyMem(): false->not filled");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void fill(T1* p, unsigned short sz, T1 value = 1)
|
||||
{
|
||||
if (p == nullptr)
|
||||
{
|
||||
WCWOUT(L"LazySectionBuffer->fill(): nullptr?");
|
||||
}
|
||||
else {
|
||||
for (unsigned short i = 0; i < sz; i++)
|
||||
{
|
||||
p[i] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
bool Test1()
|
||||
{
|
||||
unsigned short memSize = SECTIONSIZE / 2;
|
||||
T1* pData = this->GetNulledMem(memSize, 0); this->fill(pData, memSize);
|
||||
if (VerifyMem_OnlyOneSection(0))
|
||||
{
|
||||
pData = this->GetNulledMem(memSize, 0); this->fill(pData, memSize);
|
||||
if (VerifyMem_OnlyOneSection(0))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool Test2()
|
||||
{
|
||||
unsigned short memSize = SECTIONSIZE / 2;
|
||||
T1* pData = nullptr;
|
||||
for (auto& x : this->m_vec) { x = 0; }
|
||||
for (unsigned char i = 0; i < NUMBEROFSECTIONS; i++)
|
||||
{
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
if (VerifyMem_allfilled(i, memSize * 2) == false || VerifyMem_OnlyOneSection(i) == false) { return false; }
|
||||
}
|
||||
memSize = 1;
|
||||
if (SECTIONSIZE - memSize * 2)
|
||||
{
|
||||
memSize = 2;
|
||||
}
|
||||
for (unsigned char i = 0; i < NUMBEROFSECTIONS; i++)
|
||||
{
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
if (VerifyMem_allfilled(i, memSize * 2) == false) { return false; }
|
||||
}
|
||||
memSize = SECTIONSIZE / 2 + 1;
|
||||
for (unsigned char i = 0; i < NUMBEROFSECTIONS; i++)
|
||||
{
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
pData = this->GetNulledMem(memSize, i);
|
||||
this->fill(pData, memSize);
|
||||
if (VerifyMem_allfilled(i, memSize) == false) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template<unsigned short SECTIONSIZE, unsigned char NUMBEROFSECTIONS>
|
||||
using LazySectionBuffer_wchart = LazySectionBuffer<wchar_t, SECTIONSIZE, NUMBEROFSECTIONS>;
|
||||
|
||||
template<unsigned short SECTIONSIZE, unsigned char NUMBEROFSECTIONS>
|
||||
using LazySectionBuffer_char = LazySectionBuffer<char, SECTIONSIZE, NUMBEROFSECTIONS>;
|
5
ultimateteknotool.v2/LazyBuffer_512wchart.cpp
Normal file
5
ultimateteknotool.v2/LazyBuffer_512wchart.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include "pch.h"
|
||||
#include "LazyBuffer_512wchart.h"
|
||||
|
||||
//LazyBuffer_wchart<512>lazybuffer512wchart;
|
||||
LazySectionBuffer_wchart<512, 3> lazysectionbuffer_3x512;
|
5
ultimateteknotool.v2/LazyBuffer_512wchart.h
Normal file
5
ultimateteknotool.v2/LazyBuffer_512wchart.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include "LazyBuffer.h"
|
||||
|
||||
//extern LazyBuffer_wchart<512>lazybuffer512wchart;
|
||||
extern LazySectionBuffer_wchart<512, 3> lazysectionbuffer_3x512;
|
7
ultimateteknotool.v2/MainWindowSizes.cpp
Normal file
7
ultimateteknotool.v2/MainWindowSizes.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "pch.h"
|
||||
|
||||
namespace MainWindowSizes
|
||||
{
|
||||
constexpr int width = 620;
|
||||
constexpr int height = 450 + 6;
|
||||
};
|
34
ultimateteknotool.v2/ParserRandMask.cpp
Normal file
34
ultimateteknotool.v2/ParserRandMask.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "pch.h"
|
||||
#include "ParserRandMask.h"
|
||||
#include "Randomizer.h"
|
||||
|
||||
void parsers::ParseRandMaskTextW(const wchar_t* randmask, const size_t randmask_sz, wchar_t* output, const size_t output_sz)
|
||||
{
|
||||
for (size_t i = 0; i < randmask_sz && i < output_sz; i++)
|
||||
{
|
||||
if (randmask[i] == 'd')
|
||||
{
|
||||
output[i] = static_cast<wchar_t>(RANDANSIDIGIT);
|
||||
}
|
||||
else if (randmask[i] == 'u')
|
||||
{
|
||||
output[i] = static_cast<wchar_t>(RANDANSIUPPERCASE);
|
||||
}
|
||||
else if (randmask[i] == 'l')
|
||||
{
|
||||
output[i] = static_cast<wchar_t>(RANDANSILOWCASE);
|
||||
}
|
||||
else if (randmask[i] == 'c')
|
||||
{
|
||||
output[i] = static_cast<wchar_t>(RANDANSICHAR);
|
||||
}
|
||||
else if (randmask[i] == 'h')
|
||||
{
|
||||
output[i] = static_cast<wchar_t>(RANDHEXSYMBOL);
|
||||
}
|
||||
else
|
||||
{
|
||||
output[i] = randmask[i];
|
||||
}
|
||||
}
|
||||
}
|
7
ultimateteknotool.v2/ParserRandMask.h
Normal file
7
ultimateteknotool.v2/ParserRandMask.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace parsers
|
||||
{
|
||||
typedef void (parserfunction_t)(const wchar_t* randmask, const size_t randmask_sz, wchar_t* output, const size_t output_sz);
|
||||
void ParseRandMaskTextW(const wchar_t* randmask, const size_t randmask_sz, wchar_t* output, const size_t output_sz);
|
||||
};
|
176
ultimateteknotool.v2/PatternSearcher.cpp
Normal file
176
ultimateteknotool.v2/PatternSearcher.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
#include "pch.h"
|
||||
#include "PatternSearcher.h"
|
||||
|
||||
static_assert(0xFF == (0x0F | 0xF0), "");
|
||||
static_assert(0xFF == (0xF0 | 0x0F), "");
|
||||
|
||||
static constexpr uint8_t PATTERN_MAXSIZE_BYTES = 16;
|
||||
struct SmartByte
|
||||
{
|
||||
uint8_t value = 0;
|
||||
uint8_t mustbeskipped = 0;
|
||||
};
|
||||
|
||||
uint8_t ConvertCharToByte(char left, char right)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
switch (left)
|
||||
{
|
||||
case '0':
|
||||
ret = 0x00;
|
||||
break;
|
||||
case '1':
|
||||
ret = 0x10;
|
||||
break;
|
||||
case '2':
|
||||
ret = 0x20;
|
||||
break;
|
||||
case '3':
|
||||
ret = 0x30;
|
||||
break;
|
||||
case '4':
|
||||
ret = 0x40;
|
||||
break;
|
||||
case '5':
|
||||
ret = 0x50;
|
||||
break;
|
||||
case '6':
|
||||
ret = 0x60;
|
||||
break;
|
||||
case '7':
|
||||
ret = 0x70;
|
||||
break;
|
||||
case '8':
|
||||
ret = 0x80;
|
||||
break;
|
||||
case '9':
|
||||
ret = 0x90;
|
||||
break;
|
||||
case 'A':
|
||||
ret = 0xA0;
|
||||
break;
|
||||
case 'B':
|
||||
ret = 0xB0;
|
||||
break;
|
||||
case 'C':
|
||||
ret = 0xC0;
|
||||
break;
|
||||
case 'D':
|
||||
ret = 0xD0;
|
||||
break;
|
||||
case 'E':
|
||||
ret = 0xE0;
|
||||
break;
|
||||
case 'F':
|
||||
ret = 0xF0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (right)
|
||||
{
|
||||
case '0':
|
||||
ret = ret | 0x00;
|
||||
break;
|
||||
case '1':
|
||||
ret = ret | 0x01;
|
||||
break;
|
||||
case '2':
|
||||
ret = ret | 0x02;
|
||||
break;
|
||||
case '3':
|
||||
ret = ret | 0x03;
|
||||
break;
|
||||
case '4':
|
||||
ret = ret | 0x04;
|
||||
break;
|
||||
case '5':
|
||||
ret = ret | 0x05;
|
||||
break;
|
||||
case '6':
|
||||
ret = ret | 0x06;
|
||||
break;
|
||||
case '7':
|
||||
ret = ret | 0x07;
|
||||
break;
|
||||
case '8':
|
||||
ret = ret | 0x08;
|
||||
break;
|
||||
case '9':
|
||||
ret = ret | 0x09;
|
||||
break;
|
||||
case 'A':
|
||||
ret = ret | 0x0A;
|
||||
break;
|
||||
case 'B':
|
||||
ret = ret | 0x0B;
|
||||
break;
|
||||
case 'C':
|
||||
ret = ret | 0x0C;
|
||||
break;
|
||||
case 'D':
|
||||
ret = ret | 0x0D;
|
||||
break;
|
||||
case 'E':
|
||||
ret = ret | 0x0E;
|
||||
break;
|
||||
case 'F':
|
||||
ret = ret | 0x0F;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
uint32_t patternsearcher::PatternSearch(char* pattern, uint32_t start_address, uint32_t maxdeep)
|
||||
{
|
||||
SmartByte BYTES[PATTERN_MAXSIZE_BYTES] = { 0 };
|
||||
uint8_t counter = 0;
|
||||
uint8_t bytepos = 0;
|
||||
for (; pattern[counter] && pattern[counter + 1] && counter/2 < PATTERN_MAXSIZE_BYTES;)
|
||||
{
|
||||
if (pattern[counter] == '?' && pattern[counter + 1] == '?')
|
||||
{
|
||||
BYTES[counter/2].mustbeskipped = 0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
BYTES[counter/2].value = ConvertCharToByte(pattern[counter], pattern[counter + 1]);
|
||||
}
|
||||
counter += 2;
|
||||
}
|
||||
|
||||
counter = counter / 2;
|
||||
uint32_t RET = start_address;
|
||||
uint8_t verify = 0;
|
||||
for (; RET < (start_address + maxdeep);)
|
||||
{
|
||||
if (*reinterpret_cast<uint8_t*>(RET) == BYTES[0].value)
|
||||
{
|
||||
for (verify = 1; verify < counter; verify++)
|
||||
{
|
||||
RET++;
|
||||
if (BYTES[verify].mustbeskipped == 0x00) {
|
||||
if (*reinterpret_cast<uint8_t*>(RET) != BYTES[verify].value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (verify == counter) {
|
||||
return RET - verify + 1; }
|
||||
}
|
||||
RET++;
|
||||
}
|
||||
return 0;
|
||||
}
|
6
ultimateteknotool.v2/PatternSearcher.h
Normal file
6
ultimateteknotool.v2/PatternSearcher.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "debugconsole.h"
|
||||
#include <stdint.h>
|
||||
namespace patternsearcher {
|
||||
uint32_t PatternSearch(char* pattern, uint32_t start_address, uint32_t maxdeep);
|
||||
};
|
50
ultimateteknotool.v2/ProcessController.h
Normal file
50
ultimateteknotool.v2/ProcessController.h
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include <tlhelp32.h>
|
||||
|
||||
|
||||
namespace ProcessController
|
||||
{
|
||||
/* following Raymond, 2006 microsoft devblog */
|
||||
void SuspendThreads(bool suspend)
|
||||
{
|
||||
DWORD ProcId = 0;
|
||||
DWORD ThreadId = 0;
|
||||
ProcId = ::GetCurrentProcessId();
|
||||
ThreadId = ::GetCurrentThreadId();
|
||||
HANDLE snapthreads = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
|
||||
if (snapthreads == INVALID_HANDLE_VALUE) { return; }
|
||||
else
|
||||
{
|
||||
THREADENTRY32 threadentry = { 0 };
|
||||
threadentry.dwSize = sizeof(THREADENTRY32);
|
||||
if (::Thread32First(snapthreads, &threadentry) == TRUE)
|
||||
{
|
||||
HANDLE threadhandle = NULL;
|
||||
do
|
||||
{
|
||||
if (threadentry.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
|
||||
sizeof(threadentry.th32OwnerProcessID))
|
||||
{
|
||||
if (threadentry.th32OwnerProcessID == ProcId) {
|
||||
if (threadentry.th32ThreadID == ThreadId) {}
|
||||
else {
|
||||
threadhandle = ::OpenThread(THREAD_ALL_ACCESS, FALSE, threadentry.th32ThreadID);
|
||||
if (threadhandle != NULL && threadhandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (suspend) { ::SuspendThread(threadhandle); }
|
||||
else { ::ResumeThread(threadhandle); }
|
||||
::CloseHandle(threadhandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
threadentry.dwSize = sizeof(threadentry);
|
||||
}
|
||||
while (::Thread32Next(snapthreads, &threadentry));
|
||||
}
|
||||
::CloseHandle(snapthreads);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
78
ultimateteknotool.v2/Randomizer.cpp
Normal file
78
ultimateteknotool.v2/Randomizer.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "pch.h"
|
||||
#include "Randomizer.h"
|
||||
|
||||
|
||||
std::once_flag Randomizer::m_createonceflag;
|
||||
std::unique_ptr<Randomizer> Randomizer::m_pInstance;
|
||||
|
||||
XORShiftSeeder Randomizer::m_xorshift;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_distrByte;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_distrByteHighBit_1;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_distrByteHighBit_0;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIdigit;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIuppercase;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIlowcase;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIchar;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIfromAtoF;
|
||||
std::uniform_int_distribution<unsigned int> Randomizer::m_ANSIfromAtoF_lower;
|
||||
|
||||
Randomizer & Randomizer::GetInstance()
|
||||
{
|
||||
std::call_once(Randomizer::m_createonceflag, [] {
|
||||
Randomizer::m_pInstance.reset(new Randomizer);
|
||||
});
|
||||
return *Randomizer::m_pInstance.get();
|
||||
}
|
||||
|
||||
uint8_t Randomizer::GetSingleByte(Randomizer::FillType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Randomizer::FillType::_0_to_127:
|
||||
return static_cast<uint8_t>(Randomizer::m_distrByteHighBit_0(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::_128_to_255:
|
||||
return static_cast<uint8_t>(Randomizer::m_distrByteHighBit_1(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::_0_to_255:
|
||||
return static_cast<uint8_t>(Randomizer::m_distrByte(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIchar:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIchar(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIdigit:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIdigit(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIlowcase:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIlowcase(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIuppercase:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIuppercase(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIfromAtoF:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIfromAtoF(Randomizer::m_xorshift));
|
||||
case Randomizer::FillType::ANSIfromAtoF_lower:
|
||||
return static_cast<uint8_t>(Randomizer::m_ANSIfromAtoF_lower(Randomizer::m_xorshift));
|
||||
default:
|
||||
return static_cast<uint8_t>(Randomizer::m_distrByte(Randomizer::m_xorshift));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Randomizer::Randomizer()
|
||||
{
|
||||
std::random_device randomdevice;
|
||||
Randomizer::m_xorshift = XORShiftSeeder(randomdevice);
|
||||
Randomizer::m_distrByte = std::uniform_int_distribution<unsigned int>(0, 255);
|
||||
Randomizer::m_distrByteHighBit_1 = std::uniform_int_distribution<unsigned int>(128, 255);
|
||||
Randomizer::m_distrByteHighBit_0 = std::uniform_int_distribution<unsigned int>(0, 127);
|
||||
Randomizer::m_ANSIdigit = std::uniform_int_distribution<unsigned int>(48, 57);
|
||||
Randomizer::m_ANSIuppercase = std::uniform_int_distribution<unsigned int>(65, 90);
|
||||
Randomizer::m_ANSIlowcase = std::uniform_int_distribution<unsigned int>(97, 122);
|
||||
Randomizer::m_ANSIchar = std::uniform_int_distribution<unsigned int>(33, 126);
|
||||
Randomizer::m_ANSIfromAtoF = std::uniform_int_distribution<unsigned int>(65, 70);
|
||||
Randomizer::m_ANSIfromAtoF_lower = std::uniform_int_distribution<unsigned int>('a', 'f');
|
||||
}
|
||||
|
||||
bool operator==(XORShiftSeeder const& lhs, XORShiftSeeder const& rhs)
|
||||
{
|
||||
return lhs.m_seed == rhs.m_seed;
|
||||
}
|
||||
bool operator!=(XORShiftSeeder const& lhs, XORShiftSeeder const& rhs)
|
||||
{
|
||||
return lhs.m_seed != rhs.m_seed;
|
||||
}
|
89
ultimateteknotool.v2/Randomizer.h
Normal file
89
ultimateteknotool.v2/Randomizer.h
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
|
||||
class XORShiftSeeder; // stolen from https://arvid.io/2018/07/02/better-cxx-prng/
|
||||
|
||||
class Randomizer
|
||||
{
|
||||
public:
|
||||
~Randomizer() = default;
|
||||
static Randomizer& GetInstance();
|
||||
enum FillType { _0_to_255, _0_to_127, _128_to_255, ANSIdigit, ANSIuppercase, ANSIlowcase, ANSIchar, ANSIfromAtoF, ANSIfromAtoF_lower
|
||||
};
|
||||
uint8_t GetSingleByte(FillType type);
|
||||
private:
|
||||
Randomizer();
|
||||
static std::once_flag m_createonceflag;
|
||||
static std::unique_ptr<Randomizer> m_pInstance;
|
||||
Randomizer(const Randomizer&) = delete;
|
||||
Randomizer(Randomizer&&) = delete;
|
||||
Randomizer& operator=(const Randomizer&) = delete;
|
||||
Randomizer& operator=(Randomizer&&) = delete;
|
||||
|
||||
static XORShiftSeeder m_xorshift;
|
||||
static std::uniform_int_distribution<unsigned int> m_distrByte;
|
||||
static std::uniform_int_distribution<unsigned int> m_distrByteHighBit_1;
|
||||
static std::uniform_int_distribution<unsigned int> m_distrByteHighBit_0;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIdigit;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIuppercase;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIlowcase;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIchar;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIfromAtoF;
|
||||
static std::uniform_int_distribution<unsigned int> m_ANSIfromAtoF_lower;
|
||||
};
|
||||
|
||||
|
||||
#define RANDBYTE Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::_0_to_255)
|
||||
#define RANDBYTEH0 Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::_0_to_127)
|
||||
#define RANDBYTEH1 Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::_128_to_255)
|
||||
#define RANDANSIDIGIT Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::ANSIdigit)
|
||||
#define RANDANSIUPPERCASE Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::ANSIuppercase)
|
||||
#define RANDANSILOWCASE Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::ANSIlowcase)
|
||||
#define RANDANSICHAR Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::ANSIchar)
|
||||
#define RANDHEXSYMBOL (Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::_0_to_127) < 64) ? \
|
||||
Randomizer::GetInstance().GetSingleByte(Randomizer::FillType::ANSIfromAtoF_lower) : RANDANSIDIGIT
|
||||
#define RANDALNUM RANDBYTEH0 < 64 ? RANDANSIUPPERCASE : (RANDBYTEH0 < 64 ? RANDANSILOWCASE : RANDANSIDIGIT)
|
||||
|
||||
|
||||
|
||||
class XORShiftSeeder
|
||||
{
|
||||
public:
|
||||
using result_type = uint32_t;
|
||||
static constexpr result_type(min)() { return 0; }
|
||||
static constexpr result_type(max)() { return UINT32_MAX; }
|
||||
friend bool operator==(XORShiftSeeder const &, XORShiftSeeder const &);
|
||||
friend bool operator!=(XORShiftSeeder const &, XORShiftSeeder const &);
|
||||
|
||||
XORShiftSeeder() : m_seed(0xc1f651c67c62c6e0ull) {}
|
||||
explicit XORShiftSeeder(std::random_device &rd)
|
||||
{
|
||||
seed(rd);
|
||||
}
|
||||
|
||||
void seed(std::random_device &rd)
|
||||
{
|
||||
m_seed = uint64_t(rd()) << 31 | uint64_t(rd());
|
||||
}
|
||||
|
||||
result_type operator()()
|
||||
{
|
||||
uint64_t result = m_seed * 0xd989bcaef137dcd5ull;
|
||||
m_seed ^= m_seed >> 11;
|
||||
m_seed ^= m_seed << 31;
|
||||
m_seed ^= m_seed >> 18;
|
||||
return uint32_t(result >> 32ull);
|
||||
}
|
||||
|
||||
void discard(unsigned long long n)
|
||||
{
|
||||
for (unsigned long long i = 0; i < n; ++i)
|
||||
operator()();
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t m_seed;
|
||||
};
|
||||
|
79
ultimateteknotool.v2/Serializers.cpp
Normal file
79
ultimateteknotool.v2/Serializers.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "pch.h"
|
||||
#include "Serializers.h"
|
||||
|
||||
size_t serializer::SerialTextW(const wchar_t* input, const size_t input_sz, wchar_t* output, const size_t sz)
|
||||
{
|
||||
size_t i = 0;
|
||||
for (; i < sz && i < input_sz; i++)
|
||||
{
|
||||
if (!input[i])
|
||||
{
|
||||
output[i] = serializer::constants::WDelimeter;
|
||||
return i + 1;
|
||||
}
|
||||
output[i] = input[i];
|
||||
}
|
||||
i--;
|
||||
output[i] = serializer::constants::WDelimeter;
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
size_t serializer::SerialButtonStateW(bool state, ButtonStateWDescriptor& desc)
|
||||
{
|
||||
desc.SetState(state);
|
||||
return desc.RetSize();
|
||||
}
|
||||
|
||||
size_t serializer::SerialButtonStateW(bool state, wchar_t* output, const size_t sz)
|
||||
{
|
||||
ButtonStateWDescriptor desc = { 0 };
|
||||
desc.SetState(state);
|
||||
if (desc.RetSize() > sz) { return 0; }
|
||||
else
|
||||
{
|
||||
static_assert(ButtonStateWDescriptor::SIZE == 2, "ButtonStateWDescriptor::SIZE is not 2");
|
||||
output[0] = desc.text[0];
|
||||
output[1] = desc.text[1];
|
||||
return desc.RetSize();
|
||||
}
|
||||
}
|
||||
|
||||
size_t serializer::DeserialTextW(const wchar_t* inputdata, const size_t& inputsize, wchar_t* outputdata, const size_t& outputsize)
|
||||
{
|
||||
decltype(inputsize + outputsize)i = 0;
|
||||
for (; i < inputsize && i < outputsize; i++)
|
||||
{
|
||||
if (inputdata[i] == serializer::constants::WDelimeter)
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
outputdata[i] = inputdata[i];
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
size_t serializer::DeserialButtonStateW(const wchar_t* inputdata, const size_t& inputsize, bool& state)
|
||||
{
|
||||
if (inputsize < 2) { return 0u; }
|
||||
if (inputdata[0] == serializer::constants::WButtonPressed)
|
||||
{
|
||||
state = true;
|
||||
|
||||
}
|
||||
else if(inputdata[0] == serializer::constants::WButtonIsNotPressed)
|
||||
{
|
||||
state = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
if (inputdata[1] == serializer::constants::WDelimeter)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
}
|
30
ultimateteknotool.v2/Serializers.h
Normal file
30
ultimateteknotool.v2/Serializers.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
namespace serializer
|
||||
{
|
||||
namespace constants
|
||||
{
|
||||
constexpr wchar_t WDelimeter = 0x2049;
|
||||
constexpr wchar_t WButtonPressed = L'1';
|
||||
constexpr wchar_t WButtonIsNotPressed = L'0';
|
||||
};
|
||||
struct ButtonStateWDescriptor
|
||||
{
|
||||
static constexpr unsigned short SIZE = 2;
|
||||
wchar_t text[SIZE] = { 0 , 0 };
|
||||
void SetState(bool state)
|
||||
{
|
||||
if (state) { text[0] = constants::WButtonPressed; }
|
||||
else {
|
||||
text[0] = constants::WButtonIsNotPressed;
|
||||
}
|
||||
text[SIZE - 1] = constants::WDelimeter;
|
||||
}
|
||||
size_t RetSize() const { return SIZE; }
|
||||
// state, delimeter
|
||||
};
|
||||
size_t SerialTextW(const wchar_t* input, const size_t input_sz, wchar_t* output, const size_t sz);
|
||||
size_t SerialButtonStateW(bool state, ButtonStateWDescriptor& desc);
|
||||
size_t SerialButtonStateW(bool state, wchar_t* output, const size_t sz);
|
||||
size_t DeserialTextW(const wchar_t* inputdata, const size_t& inputsize, wchar_t* outputdata, const size_t& outputsize);
|
||||
size_t DeserialButtonStateW(const wchar_t* inputdata, const size_t& inputsize, bool& state);
|
||||
};
|
68
ultimateteknotool.v2/SystemFunctionXRefRemover.h
Normal file
68
ultimateteknotool.v2/SystemFunctionXRefRemover.h
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "debugconsole.h"
|
||||
|
||||
template<size_t BFSZ>
|
||||
struct XRefRemover_SysFunctionUI32
|
||||
{
|
||||
uint32_t _pointers[BFSZ];
|
||||
XRefRemover_SysFunctionUI32()
|
||||
{
|
||||
if (BFSZ < 4) { throw 1; }
|
||||
_pointers[0] = 0x00;
|
||||
for (size_t sz = 1; sz < BFSZ; sz++)
|
||||
{
|
||||
_pointers[sz] = _pointers[0];
|
||||
}
|
||||
}
|
||||
~XRefRemover_SysFunctionUI32() = default;
|
||||
#define XRefRemover_FAKEADR(index) index + fake_address - xfake_address
|
||||
#pragma optimize("" ,off)
|
||||
template<uint32_t fake_address>
|
||||
void AddNewFunction(void* addr, uint32_t xfake_address)
|
||||
{
|
||||
|
||||
_pointers[XRefRemover_FAKEADR(0)] = *reinterpret_cast<uint32_t*>(addr);
|
||||
_pointers[XRefRemover_FAKEADR(1)] = reinterpret_cast<uint32_t>(addr) ^ _pointers[0];
|
||||
_pointers[XRefRemover_FAKEADR(2)] = *(reinterpret_cast<uint32_t*>(addr) + 1);
|
||||
}
|
||||
template<uint32_t fake_address>
|
||||
void* GetFunction(uint32_t xfake_address) const
|
||||
{
|
||||
return reinterpret_cast<void*>(_pointers[XRefRemover_FAKEADR(1)] ^ _pointers[XRefRemover_FAKEADR(0)]);
|
||||
}
|
||||
void VerifyFunction() const
|
||||
{
|
||||
uint32_t addr = 0x00;
|
||||
if (
|
||||
*reinterpret_cast<uint32_t*>(_pointers[1] ^ _pointers[0]) != _pointers[0]
|
||||
)
|
||||
{
|
||||
addr = 0x7F340000;
|
||||
__asm {
|
||||
jmp [addr];
|
||||
}
|
||||
}
|
||||
else if (*(reinterpret_cast<uint32_t*>(_pointers[1] ^ _pointers[0]) + 1) != _pointers[2])
|
||||
{
|
||||
addr = 0x0010AB00;
|
||||
__asm {
|
||||
jmp [addr];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
template<uint32_t fake_address>
|
||||
void* GetVerifiedFunction(uint32_t xfake_address) const
|
||||
{
|
||||
this->VerifyFunction();
|
||||
return this->GetFunction<fake_address>(xfake_address);
|
||||
}
|
||||
|
||||
size_t Size() const noexcept { return BFSZ; }
|
||||
#undef XRefRemover_FAKEADR
|
||||
#pragma optimize("" ,on)
|
||||
};
|
209
ultimateteknotool.v2/TextConvertors.cpp
Normal file
209
ultimateteknotool.v2/TextConvertors.cpp
Normal file
@ -0,0 +1,209 @@
|
||||
#include "pch.h"
|
||||
#include "TextConvertors.h"
|
||||
|
||||
|
||||
static std::wistringstream inputWstringstream;
|
||||
static std::wostringstream outputWstringstream(L"");
|
||||
static std::wstring aaaaaa;
|
||||
static LazyBuffer_wchart<256> LazyW256;
|
||||
static LazyBuffer_char<256> LazyC256;
|
||||
|
||||
uint32_t textconvertors::ConvertWTextHexTypeToU32(const wchar_t* text)
|
||||
{
|
||||
inputWstringstream.clear();
|
||||
inputWstringstream.str(text);
|
||||
uint32_t ret = 0;
|
||||
if (inputWstringstream >> std::hex >> ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t textconvertors::ConvertWTextDecTypeToU32(const wchar_t* text)
|
||||
{
|
||||
inputWstringstream.clear();
|
||||
inputWstringstream.str(text);
|
||||
uint32_t ret = 0;
|
||||
if (inputWstringstream >> std::dec >> ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
const wchar_t* textconvertors::ConvertHexTypeU32ToWText(const uint32_t value)
|
||||
{
|
||||
//greatstringstream.clear();
|
||||
outputWstringstream.str(L"00000000");
|
||||
outputWstringstream << std::hex << value;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa.c_str();
|
||||
}
|
||||
|
||||
const wchar_t* textconvertors::ConvertDecTypeU32ToWText(const uint32_t value)
|
||||
{
|
||||
//greatstringstream.clear();
|
||||
outputWstringstream.str(L"0");
|
||||
outputWstringstream << std::dec << value;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa.c_str();
|
||||
}
|
||||
|
||||
uint16_t textconvertors::ConvertWTextDecTypeToU16(const wchar_t* text)
|
||||
{
|
||||
inputWstringstream.clear();
|
||||
inputWstringstream.str(text);
|
||||
uint16_t PortInput = 0;
|
||||
if (inputWstringstream >> std::dec >> PortInput) {
|
||||
return PortInput;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const wchar_t* textconvertors::ConvertDecTypeToU16ToWText(const uint16_t value)
|
||||
{
|
||||
outputWstringstream.str(L"0");
|
||||
outputWstringstream << std::dec << value;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa.c_str();
|
||||
}
|
||||
|
||||
void textconvertors::ConvertWTextToIP(const wchar_t* text, uint8_t NewIP[4])
|
||||
{
|
||||
inputWstringstream.clear();
|
||||
inputWstringstream.str(text);
|
||||
wchar_t ignore;
|
||||
uint32_t NewIPinput[4];
|
||||
if (inputWstringstream >> std::dec >> NewIPinput[0] >> ignore >> NewIPinput[1] >> ignore >> NewIPinput[2] >> ignore >> NewIPinput[3])
|
||||
{
|
||||
NewIPinput[0] <<= 24; NewIPinput[1] <<= 24; NewIPinput[2] <<= 24; NewIPinput[3] <<= 24;
|
||||
NewIP[0] = ((NewIPinput[0] >> 24) & 0xFF);
|
||||
NewIP[1] = ((NewIPinput[1] >> 24) & 0xFF);
|
||||
NewIP[2] = ((NewIPinput[2] >> 24) & 0xFF);
|
||||
NewIP[3] = ((NewIPinput[3] >> 24) & 0xFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
NewIP[0] = 0;
|
||||
NewIP[1] = 0;
|
||||
NewIP[2] = 0;
|
||||
NewIP[3] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
const wchar_t* textconvertors::ConvertIPToWText(const uint8_t NewIP[4])
|
||||
{
|
||||
outputWstringstream.str(L"");
|
||||
outputWstringstream << std::dec <<
|
||||
static_cast<uint16_t>(NewIP[0]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[1]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[2]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[3]);
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa.c_str();
|
||||
}
|
||||
|
||||
const wchar_t* textconvertors::ConvertU8TextToWText_StupidButFast(const uint8_t* data, unsigned short sz)
|
||||
{
|
||||
wchar_t* diesoon = LazyW256.GetNulledMem(sz + 2);
|
||||
for (size_t i = 0; i < sz && data[i]; i++)
|
||||
{
|
||||
diesoon[i] = static_cast<wchar_t>(data[i]);
|
||||
}
|
||||
return diesoon;
|
||||
}
|
||||
|
||||
|
||||
const wchar_t* textconvertors::ConvertU8TextToWText_StupidButFast_graphNspace(const uint8_t* data, unsigned short sz)
|
||||
{
|
||||
wchar_t* diesoon = LazyW256.GetNulledMem(sz + 2);
|
||||
for (size_t i = 0; i < sz && (::isgraph(data[i]) || data[i] == static_cast<uint8_t>(' ')); i++)
|
||||
{
|
||||
diesoon[i] = static_cast<wchar_t>(data[i]);
|
||||
}
|
||||
return diesoon;
|
||||
}
|
||||
|
||||
const wchar_t* textconvertors::ConvertCharTextToWText_StupidButFast(const char* data, unsigned short sz)
|
||||
{
|
||||
wchar_t* diesoon = LazyW256.GetNulledMem(sz + 2);
|
||||
for (size_t i = 0; i < sz && data[i]; i++)
|
||||
{
|
||||
diesoon[i] = static_cast<wchar_t>(data[i]);
|
||||
}
|
||||
return diesoon;
|
||||
}
|
||||
|
||||
static_assert(sizeof(wchar_t) == sizeof(char) * 2, "size of wchar_t is not equal to size of two char");
|
||||
static_assert(sizeof(wchar_t) == sizeof(uint8_t) * 2, "size of wchar_t is not equal to size of two uint8_t");
|
||||
static_assert(sizeof(char) == sizeof(uint8_t), "size of char is not equal to size of uint8_t");
|
||||
|
||||
const uint8_t* textconvertors::ConvertWTextToU8Text(const wchar_t* src, unsigned short sz)
|
||||
{
|
||||
uint8_t* diesoon = reinterpret_cast<uint8_t*>(LazyC256.GetNulledMem(sz + 2));
|
||||
const uint8_t* txtp = reinterpret_cast<const uint8_t*>(src);
|
||||
for (decltype(sz) i = 0; i < sz && txtp[2*i]; i++)
|
||||
{
|
||||
diesoon[i] = txtp[2 * i];
|
||||
}
|
||||
return diesoon;
|
||||
}
|
||||
|
||||
const char* textconvertors::ConvertWTextToCharText(const wchar_t* src, unsigned short sz)
|
||||
{
|
||||
char* diesoon = LazyC256.GetNulledMem(sz + 2);
|
||||
const char* txtp = reinterpret_cast<const char*>(src);
|
||||
for (decltype(sz) i = 0; i < sz && txtp[2 * i]; i++)
|
||||
{
|
||||
diesoon[i] = txtp[2 * i];
|
||||
}
|
||||
return diesoon;
|
||||
}
|
||||
|
||||
const std::wstring& textconvertors::ret_stdstring::ConvertDecTypeU32ToWText(const uint32_t v)
|
||||
{
|
||||
outputWstringstream.str(L"0");
|
||||
outputWstringstream << std::dec << v;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa;
|
||||
}
|
||||
|
||||
const std::wstring& textconvertors::ret_stdstring::ConvertHexTypeU32ToWText(const uint32_t v)
|
||||
{
|
||||
outputWstringstream.str(L"0");
|
||||
outputWstringstream << std::hex << v;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa;
|
||||
}
|
||||
|
||||
const std::wstring& textconvertors::ret_stdstring::ConvertDecTypeU16ToWText(const uint16_t v)
|
||||
{
|
||||
outputWstringstream.str(L"0");
|
||||
outputWstringstream << std::dec << v;
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa;
|
||||
}
|
||||
|
||||
const std::wstring& textconvertors::ret_stdstring::ConvertIPToWText(const uint8_t NewIP[4])
|
||||
{
|
||||
outputWstringstream.str(L"");
|
||||
outputWstringstream << std::dec <<
|
||||
static_cast<uint16_t>(NewIP[0]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[1]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[2]) << L'.' <<
|
||||
static_cast<uint16_t>(NewIP[3]);
|
||||
aaaaaa = outputWstringstream.str();
|
||||
return aaaaaa;
|
||||
}
|
37
ultimateteknotool.v2/TextConvertors.h
Normal file
37
ultimateteknotool.v2/TextConvertors.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <sstream>
|
||||
#include "LazyBuffer.h"
|
||||
|
||||
namespace textconvertors
|
||||
{
|
||||
/* never hold returned pointers if you don't know what you exactly do */
|
||||
|
||||
uint32_t ConvertWTextHexTypeToU32(const wchar_t* text);
|
||||
uint32_t ConvertWTextDecTypeToU32(const wchar_t* text);
|
||||
const wchar_t* ConvertHexTypeU32ToWText(const uint32_t v);
|
||||
const wchar_t* ConvertDecTypeU32ToWText(const uint32_t v);
|
||||
|
||||
uint16_t ConvertWTextDecTypeToU16(const wchar_t* text);
|
||||
const wchar_t* ConvertDecTypeToU16ToWText(const uint16_t v);
|
||||
|
||||
void ConvertWTextToIP(const wchar_t* text, uint8_t NewIP[4]);
|
||||
const wchar_t* ConvertIPToWText(const uint8_t NewIP[4]);
|
||||
|
||||
const wchar_t* ConvertU8TextToWText_StupidButFast(const uint8_t* data, unsigned short sz);
|
||||
|
||||
const wchar_t* ConvertU8TextToWText_StupidButFast_graphNspace(const uint8_t* data, unsigned short sz);
|
||||
const wchar_t* ConvertCharTextToWText_StupidButFast(const char* data, unsigned short sz);
|
||||
|
||||
const uint8_t* ConvertWTextToU8Text(const wchar_t* src, unsigned short sz);
|
||||
const char* ConvertWTextToCharText(const wchar_t* src, unsigned short sz);
|
||||
|
||||
namespace ret_stdstring
|
||||
{
|
||||
const std::wstring& ConvertDecTypeU32ToWText(const uint32_t v);
|
||||
const std::wstring& ConvertHexTypeU32ToWText(const uint32_t v);
|
||||
const std::wstring& ConvertDecTypeU16ToWText(const uint16_t v);
|
||||
const std::wstring& ConvertIPToWText(const uint8_t NewIP[4]);
|
||||
};
|
||||
};
|
||||
|
60
ultimateteknotool.v2/TheWinMain.cpp
Normal file
60
ultimateteknotool.v2/TheWinMain.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "pch.h"
|
||||
#include "constexprcolorcodes.h"
|
||||
#include "TheWinMain.h"
|
||||
#include "WinMainProc.h"
|
||||
#include "MainWindowSizes.cpp"
|
||||
|
||||
static HWND MainHwnd = NULL;
|
||||
|
||||
bool windowsMain::WinStart()
|
||||
{
|
||||
HBRUSH hbkgrbrush = CreateSolidBrush(constexprcolorcodes::Color_00FAFAFF);
|
||||
HICON hIconLarge = NULL;
|
||||
HICON hIconSmall = NULL;
|
||||
UINT ExtactRetL = 0;
|
||||
UINT ExtactRetS = 0;
|
||||
ExtactRetL = ::ExtractIconExA("iw5mp.exe", 0, &hIconLarge, 0, 1);
|
||||
ExtactRetS = ::ExtractIconExA("iw5mp.exe", 0, 0, &hIconSmall, 1);
|
||||
WNDCLASSEXW WClass;
|
||||
WClass.cbSize = sizeof(WNDCLASSEXW);
|
||||
WClass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
|
||||
WClass.lpfnWndProc = windowsMain::MainWinProc;
|
||||
WClass.cbClsExtra = 0;
|
||||
WClass.cbWndExtra = 0;
|
||||
WClass.hInstance = ::GetModuleHandle(NULL);
|
||||
if (ExtactRetL) WClass.hIcon = hIconLarge;
|
||||
else WClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||
WClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
WClass.hbrBackground = hbkgrbrush;
|
||||
WClass.lpszMenuName = NULL;
|
||||
WClass.lpszClassName = classname;
|
||||
if (ExtactRetS) WClass.hIconSm = hIconSmall;
|
||||
else WClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
||||
if (!::RegisterClassEx(&WClass))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int v1 = ::GetSystemMetrics(SM_CXFULLSCREEN);
|
||||
int v2 = ::GetSystemMetrics(SM_CYFULLSCREEN);
|
||||
RECT rect = { 0, 0 , MainWindowSizes::width , MainWindowSizes::height };
|
||||
MainHwnd = ::CreateWindowExW(0, classname, L"iw5mp", WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX | WS_VISIBLE,
|
||||
(v1 - MainWindowSizes::width) / 2, (v2 - MainWindowSizes::height) / 2,
|
||||
MainWindowSizes::width, MainWindowSizes::height, NULL, 0, ::GetModuleHandle(NULL), 0);
|
||||
if (!MainHwnd)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ShowWindow(MainHwnd, SW_SHOWDEFAULT);
|
||||
UpdateWindow(MainHwnd);
|
||||
MSG msg;
|
||||
ZeroMemory(&msg, sizeof(msg));
|
||||
|
||||
while (GetMessage(&msg, NULL, 0, 0))
|
||||
{
|
||||
|
||||
//WinMainLoop();
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
return true;
|
||||
}
|
9
ultimateteknotool.v2/TheWinMain.h
Normal file
9
ultimateteknotool.v2/TheWinMain.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
|
||||
namespace windowsMain
|
||||
{
|
||||
constexpr wchar_t classname[] = L"iw5mpx";
|
||||
bool WinStart();
|
||||
};
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user