Bring stuff up2date and added own stuff
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_BASE32
|
||||
|
||||
/**
|
||||
Base32 decode a buffer
|
||||
@param in The Base32 data to decode
|
||||
@param inlen The length of the Base32 data
|
||||
@param out [out] The destination of the binary decoded data
|
||||
@param outlen [in/out] The max size and resulting size of the decoded data
|
||||
@param id Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int base32_decode(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
base32_alphabet id)
|
||||
{
|
||||
unsigned long x;
|
||||
int y = 0;
|
||||
ulong64 t = 0;
|
||||
unsigned char c;
|
||||
const unsigned char *map;
|
||||
const unsigned char tables[4][43] = {
|
||||
{ /* id = BASE32_RFC4648 : ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
|
||||
99/*0*/,99/*1*/,26/*2*/,27/*3*/,28/*4*/,29/*5*/,30/*6*/,31/*7*/,99/*8*/,99/*9*/,
|
||||
99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
|
||||
0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, 7/*H*/, 8/*I*/, 9/*J*/,10/*K*/,11/*L*/,12/*M*/,
|
||||
13/*N*/,14/*O*/,15/*P*/,16/*Q*/,17/*R*/,18/*S*/,19/*T*/,20/*U*/,21/*V*/,22/*W*/,23/*X*/,24/*Y*/,25/*Z*/
|
||||
},
|
||||
{ /* id = BASE32_BASE32HEX : 0123456789ABCDEFGHIJKLMNOPQRSTUV */
|
||||
0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
|
||||
99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
|
||||
10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/,18/*I*/,19/*J*/,20/*K*/,21/*L*/,22/*M*/,
|
||||
23/*N*/,24/*O*/,25/*P*/,26/*Q*/,27/*R*/,28/*S*/,29/*T*/,30/*U*/,31/*V*/,99/*W*/,99/*X*/,99/*Y*/,99/*Z*/
|
||||
},
|
||||
{ /* id = BASE32_ZBASE32 : YBNDRFG8EJKMCPQXOT1UWISZA345H769 */
|
||||
99/*0*/,18/*1*/,99/*2*/,25/*3*/,26/*4*/,27/*5*/,30/*6*/,29/*7*/, 7/*8*/,31/*9*/,
|
||||
99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
|
||||
24/*A*/, 1/*B*/,12/*C*/, 3/*D*/, 8/*E*/, 5/*F*/, 6/*G*/,28/*H*/,21/*I*/, 9/*J*/,10/*K*/,99/*L*/,11/*M*/,
|
||||
2/*N*/,16/*O*/,13/*P*/,14/*Q*/, 4/*R*/,22/*S*/,17/*T*/,19/*U*/,99/*V*/,20/*W*/,15/*X*/, 0/*Y*/,23/*Z*/
|
||||
},
|
||||
{ /* id = BASE32_CROCKFORD : 0123456789ABCDEFGHJKMNPQRSTVWXYZ + O=>0 + IL=>1 */
|
||||
0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
|
||||
99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
|
||||
10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/, 1/*I*/,18/*J*/,19/*K*/, 1/*L*/,20/*M*/,
|
||||
21/*N*/, 0/*O*/,22/*P*/,23/*Q*/,24/*R*/,25/*S*/,26/*T*/,99/*U*/,27/*V*/,28/*W*/,29/*X*/,30/*Y*/,31/*Z*/
|
||||
}
|
||||
};
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(id >= BASE32_RFC4648);
|
||||
LTC_ARGCHK(id <= BASE32_CROCKFORD);
|
||||
|
||||
/* ignore all trailing = */
|
||||
while (inlen > 0 && in[inlen-1] == '=') inlen--;
|
||||
|
||||
/* no input, nothing to do */
|
||||
if (inlen == 0) {
|
||||
*outlen = 0;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* check the size of output buffer */
|
||||
x = (inlen * 5) / 8;
|
||||
if (*outlen < x) {
|
||||
*outlen = x;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
*outlen = x;
|
||||
|
||||
/* check input data length */
|
||||
x = inlen % 8;
|
||||
if (x == 1 || x == 3 || x == 6) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
map = tables[id];
|
||||
for (x = 0; x < inlen; x++) {
|
||||
c = in[x];
|
||||
/* convert to upper case */
|
||||
if ((c >= 'a') && (c <= 'z')) c -= 32;
|
||||
/* '0' = 48 .. 'Z' = 90 */
|
||||
if (c < 48 || c > 90 || map[c-48] > 31) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
t = (t<<5)|map[c-48];
|
||||
if (++y == 8) {
|
||||
*out++ = (unsigned char)((t>>32) & 255);
|
||||
*out++ = (unsigned char)((t>>24) & 255);
|
||||
*out++ = (unsigned char)((t>>16) & 255);
|
||||
*out++ = (unsigned char)((t>> 8) & 255);
|
||||
*out++ = (unsigned char)( t & 255);
|
||||
y = 0;
|
||||
t = 0;
|
||||
}
|
||||
}
|
||||
if (y > 0) {
|
||||
t = t << (5 * (8 - y));
|
||||
if (y >= 2) *out++ = (unsigned char)((t>>32) & 255);
|
||||
if (y >= 4) *out++ = (unsigned char)((t>>24) & 255);
|
||||
if (y >= 5) *out++ = (unsigned char)((t>>16) & 255);
|
||||
if (y >= 7) *out++ = (unsigned char)((t>> 8) & 255);
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: $Format:%D$ */
|
||||
/* git commit: $Format:%H$ */
|
||||
/* commit time: $Format:%ai$ */
|
||||
@@ -0,0 +1,95 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_BASE32
|
||||
|
||||
/**
|
||||
Base32 encode a buffer
|
||||
@param in The input buffer to encode
|
||||
@param inlen The length of the input buffer
|
||||
@param out [out] The destination of the Base32 encoded data
|
||||
@param outlen [in/out] The max size and resulting size of the encoded data
|
||||
@param id Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int base32_encode(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
base32_alphabet id)
|
||||
{
|
||||
unsigned long i, x;
|
||||
unsigned char *codes;
|
||||
const char *alphabet[4] = {
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", /* id = BASE32_RFC4648 */
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUV", /* id = BASE32_BASE32HEX */
|
||||
"ybndrfg8ejkmcpqxot1uwisza345h769", /* id = BASE32_ZBASE32 */
|
||||
"0123456789ABCDEFGHJKMNPQRSTVWXYZ" /* id = BASE32_CROCKFORD */
|
||||
};
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(id >= BASE32_RFC4648);
|
||||
LTC_ARGCHK(id <= BASE32_CROCKFORD);
|
||||
|
||||
/* no input, nothing to do */
|
||||
if (inlen == 0) {
|
||||
*outlen = 0;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* check the size of output buffer */
|
||||
x = (8 * inlen + 4) / 5;
|
||||
if (*outlen < x) {
|
||||
*outlen = x;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
*outlen = x;
|
||||
|
||||
codes = (unsigned char*)alphabet[id];
|
||||
x = 5 * (inlen / 5);
|
||||
for (i = 0; i < x; i += 5) {
|
||||
*out++ = codes[(in[0] >> 3) & 0x1F];
|
||||
*out++ = codes[(((in[0] & 0x7) << 2) + (in[1] >> 6)) & 0x1F];
|
||||
*out++ = codes[(in[1] >> 1) & 0x1F];
|
||||
*out++ = codes[(((in[1] & 0x1) << 4) + (in[2] >> 4)) & 0x1F];
|
||||
*out++ = codes[(((in[2] & 0xF) << 1) + (in[3] >> 7)) & 0x1F];
|
||||
*out++ = codes[(in[3] >> 2) & 0x1F];
|
||||
*out++ = codes[(((in[3] & 0x3) << 3) + (in[4] >> 5)) & 0x1F];
|
||||
*out++ = codes[in[4] & 0x1F];
|
||||
in += 5;
|
||||
}
|
||||
if (i < inlen) {
|
||||
unsigned a = in[0];
|
||||
unsigned b = (i+1 < inlen) ? in[1] : 0;
|
||||
unsigned c = (i+2 < inlen) ? in[2] : 0;
|
||||
unsigned d = (i+3 < inlen) ? in[3] : 0;
|
||||
*out++ = codes[(a >> 3) & 0x1F];
|
||||
*out++ = codes[(((a & 0x7) << 2) + (b >> 6)) & 0x1F];
|
||||
if (i+1 < inlen) {
|
||||
*out++ = codes[(b >> 1) & 0x1F];
|
||||
*out++ = codes[(((b & 0x1) << 4) + (c >> 4)) & 0x1F];
|
||||
}
|
||||
if (i+2 < inlen) {
|
||||
*out++ = codes[(((c & 0xF) << 1) + (d >> 7)) & 0x1F];
|
||||
*out++ = codes[(d >> 2) & 0x1F];
|
||||
}
|
||||
if (i+3 < inlen) {
|
||||
*out++ = codes[((d & 0x3) << 3) & 0x1F];
|
||||
}
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: $Format:%D$ */
|
||||
/* git commit: $Format:%H$ */
|
||||
/* commit time: $Format:%ai$ */
|
||||
@@ -121,11 +121,23 @@ const char *crypt_build_settings =
|
||||
#endif
|
||||
#if defined(LTC_CAMELLIA)
|
||||
" Camellia\n"
|
||||
#endif
|
||||
#if defined(LTC_IDEA)
|
||||
" IDEA\n"
|
||||
#endif
|
||||
#if defined(LTC_SERPENT)
|
||||
" Serpent\n"
|
||||
#endif
|
||||
"Stream ciphers built-in:\n"
|
||||
#if defined(LTC_CHACHA)
|
||||
" ChaCha\n"
|
||||
#endif
|
||||
#if defined(LTC_SALSA20)
|
||||
" Salsa20\n"
|
||||
#endif
|
||||
#if defined(LTC_SOSEMANUK)
|
||||
" Sosemanuk\n"
|
||||
#endif
|
||||
#if defined(LTC_RC4_STREAM)
|
||||
" RC4\n"
|
||||
#endif
|
||||
@@ -393,6 +405,9 @@ const char *crypt_build_settings =
|
||||
#if defined(LTC_BASE64_URL)
|
||||
" BASE64-URL-SAFE "
|
||||
#endif
|
||||
#if defined(LTC_BASE32)
|
||||
" BASE32 "
|
||||
#endif
|
||||
#if defined(LTC_CRC32)
|
||||
" CRC32 "
|
||||
#endif
|
||||
|
||||
@@ -37,6 +37,54 @@ void init_GMP(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
int crypt_mp_init(const char* mpi)
|
||||
{
|
||||
if (mpi == NULL) return CRYPT_ERROR;
|
||||
switch (mpi[0]) {
|
||||
#ifdef LTM_DESC
|
||||
case 'l':
|
||||
case 'L':
|
||||
ltc_mp = ltm_desc;
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
#ifdef TFM_DESC
|
||||
case 't':
|
||||
case 'T':
|
||||
ltc_mp = tfm_desc;
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
#ifdef GMP_DESC
|
||||
case 'g':
|
||||
case 'G':
|
||||
ltc_mp = gmp_desc;
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
#ifdef EXT_MATH_LIB
|
||||
case 'e':
|
||||
case 'E':
|
||||
{
|
||||
extern ltc_math_descriptor EXT_MATH_LIB;
|
||||
ltc_mp = EXT_MATH_LIB;
|
||||
}
|
||||
|
||||
#if defined(LTC_TEST_DBG)
|
||||
#define NAME_VALUE(s) #s"="NAME(s)
|
||||
#define NAME(s) #s
|
||||
printf("EXT_MATH_LIB = %s\n", NAME_VALUE(EXT_MATH_LIB));
|
||||
#undef NAME_VALUE
|
||||
#undef NAME
|
||||
#endif
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
default:
|
||||
#if defined(LTC_TEST_DBG)
|
||||
printf("Unknown/Invalid MPI provider: %s\n", mpi);
|
||||
#endif
|
||||
return CRYPT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ref: $Format:%D$ */
|
||||
/* git commit: $Format:%H$ */
|
||||
|
||||
@@ -98,6 +98,9 @@ static const crypt_size _crypt_sizes[] = {
|
||||
_SZ_STRINGIFY_S(des_key),
|
||||
_SZ_STRINGIFY_S(des3_key),
|
||||
#endif
|
||||
#ifdef LTC_IDEA
|
||||
_SZ_STRINGIFY_S(idea_key),
|
||||
#endif
|
||||
#ifdef LTC_KASUMI
|
||||
_SZ_STRINGIFY_S(kasumi_key),
|
||||
#endif
|
||||
@@ -122,6 +125,9 @@ static const crypt_size _crypt_sizes[] = {
|
||||
#ifdef LTC_RC6
|
||||
_SZ_STRINGIFY_S(rc6_key),
|
||||
#endif
|
||||
#ifdef LTC_SERPENT
|
||||
_SZ_STRINGIFY_S(serpent_key),
|
||||
#endif
|
||||
#ifdef LTC_SKIPJACK
|
||||
_SZ_STRINGIFY_S(skipjack_key),
|
||||
#endif
|
||||
@@ -171,6 +177,12 @@ static const crypt_size _crypt_sizes[] = {
|
||||
#ifdef LTC_CHACHA
|
||||
_SZ_STRINGIFY_T(chacha_state),
|
||||
#endif
|
||||
#ifdef LTC_SALSA20
|
||||
_SZ_STRINGIFY_T(salsa20_state),
|
||||
#endif
|
||||
#ifdef LTC_SOSEMANUK
|
||||
_SZ_STRINGIFY_T(sosemanuk_state),
|
||||
#endif
|
||||
#ifdef LTC_RC4_STREAM
|
||||
_SZ_STRINGIFY_T(rc4_state),
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user