Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
X
xxtea-c
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
xxtea-c
Commits
1180839f
Commit
1180839f
authored
Mar 09, 2015
by
andot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimized code.
parent
1eebd0dc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
65 deletions
+75
-65
xxtea.c
xxtea.c
+75
-65
No files found.
xxtea.c
View file @
1180839f
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
| |
| |
| Code Authors: Chen fei <cf850118@163.com> |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar
3
, 2015 |
| LastModified: Mar
9
, 2015 |
| |
| |
\**********************************************************/
\**********************************************************/
...
@@ -22,8 +22,64 @@
...
@@ -22,8 +22,64 @@
typedef
unsigned
__int8
uint8_t
;
typedef
unsigned
__int8
uint8_t
;
typedef
unsigned
__int32
uint32_t
;
typedef
unsigned
__int32
uint32_t
;
#else
#else
#if defined(__FreeBSD__) && __FreeBSD__ < 5
/* FreeBSD 4 doesn't have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#include <stdint.h>
#endif
#endif
#endif
#include <sys/types.h>
/* This will likely define BYTE_ORDER */
#ifndef BYTE_ORDER
#if (BSD >= 199103)
# include <machine/endian.h>
#else
#if defined(linux) || defined(__linux__)
# include <endian.h>
#else
#define LITTLE_ENDIAN 1234
/* least-significant byte first (vax, pc) */
#define BIG_ENDIAN 4321
/* most-significant byte first (IBM, net) */
#define PDP_ENDIAN 3412
/* LSB first in word, MSW first in long (pdp)*/
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
defined
(
vax
)
||
defined
(
ns32000
)
||
defined
(
sun386
)
||
\
defined
(
MIPSEL
)
||
defined
(
_MIPSEL
)
||
defined
(
BIT_ZERO_ON_RIGHT
)
||
\
defined
(
__alpha__
)
||
defined
(
__alpha
)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
defined
(
is68k
)
||
defined
(
tahoe
)
||
defined
(
ibm032
)
||
defined
(
ibm370
)
||
\
defined
(
MIPSEB
)
||
defined
(
_MIPSEB
)
||
defined
(
_IBMR2
)
||
defined
(
DGUX
)
||
\
defined
(
apollo
)
||
defined
(
__convex__
)
||
defined
(
_CRAY
)
||
\
defined
(
__hppa
)
||
defined
(
__hp9000
)
||
\
defined
(
__hp9000s300
)
||
defined
(
__hp9000s700
)
||
\
defined
(
BIT_ZERO_ON_LEFT
)
||
defined
(
m68k
)
||
defined
(
__sparc
)
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
/* linux */
#endif
/* BSD */
#endif
/* BYTE_ORDER */
#ifndef BYTE_ORDER
#ifdef __BYTE_ORDER
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
#endif
#endif
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9
#define DELTA 0x9e3779b9
...
@@ -36,17 +92,7 @@ typedef unsigned __int32 uint32_t;
...
@@ -36,17 +92,7 @@ typedef unsigned __int32 uint32_t;
for
(
++
i
;
i
<
16
;
++
i
)
fixed_key
[
i
]
=
0
;
\
for
(
++
i
;
i
<
16
;
++
i
)
fixed_key
[
i
]
=
0
;
\
/**
static
uint32_t
*
xxtea_to_uint_array
(
const
uint8_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
* Function: xxtea_to_uint_array
* @data: Data to be converted
* @len: Length of the data to be converted
* @inc_len: Including the length of the information?
* @out_len: Pointer to output length variable
* Returns: UInt array or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
uint32_t
*
xxtea_to_uint_array
(
const
uint8_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint32_t
*
out
;
uint32_t
*
out
;
size_t
i
,
n
;
size_t
i
,
n
;
...
@@ -63,25 +109,18 @@ uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, si
...
@@ -63,25 +109,18 @@ uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, si
if
(
!
out
)
return
NULL
;
if
(
!
out
)
return
NULL
;
*
out_len
=
n
;
*
out_len
=
n
;
}
}
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
memcpy
(
out
,
data
,
len
);
#else
for
(
i
=
0
;
i
<
len
;
++
i
)
{
for
(
i
=
0
;
i
<
len
;
++
i
)
{
out
[
i
>>
2
]
|=
(
uint32_t
)
data
[
i
]
<<
((
i
&
3
)
<<
3
);
out
[
i
>>
2
]
|=
(
uint32_t
)
data
[
i
]
<<
((
i
&
3
)
<<
3
);
}
}
#endif
return
out
;
return
out
;
}
}
/**
static
uint8_t
*
xxtea_to_ubyte_array
(
const
uint32_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
* Function: xxtea_to_ubyte_array
* @data: Data to be converted
* @len: Length of the data to be converted
* @inc_len: Included the length of the information?
* @out_len: Pointer to output length variable
* Returns: UByte array or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_to_ubyte_array
(
const
uint32_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint8_t
*
out
;
uint8_t
*
out
;
size_t
i
,
m
,
n
;
size_t
i
,
m
,
n
;
...
@@ -89,15 +128,20 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
...
@@ -89,15 +128,20 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
if
(
inc_len
)
{
if
(
inc_len
)
{
m
=
data
[
len
-
1
];
m
=
data
[
len
-
1
];
if
(
m
>
n
)
return
NULL
;
n
-=
4
;
if
((
m
<
n
-
3
)
||
(
m
>
n
))
return
NULL
;
n
=
m
;
n
=
m
;
}
}
out
=
(
uint8_t
*
)
malloc
(
n
+
1
);
out
=
(
uint8_t
*
)
malloc
(
n
+
1
);
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
memcpy
(
out
,
data
,
n
);
#else
for
(
i
=
0
;
i
<
n
;
++
i
)
{
for
(
i
=
0
;
i
<
n
;
++
i
)
{
out
[
i
]
=
(
uint8_t
)(
data
[
i
>>
2
]
>>
((
i
&
3
)
<<
3
));
out
[
i
]
=
(
uint8_t
)(
data
[
i
>>
2
]
>>
((
i
&
3
)
<<
3
));
}
}
#endif
out
[
n
]
=
'\0'
;
out
[
n
]
=
'\0'
;
*
out_len
=
n
;
*
out_len
=
n
;
...
@@ -105,14 +149,7 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
...
@@ -105,14 +149,7 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
return
out
;
return
out
;
}
}
/**
static
uint32_t
*
xxtea_uint_encrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
* Function: xxtea_uint_encrypt
* @data: Data to be encrypted
* @len: Length of the data to be encrypted
* @key: Symmetric key
* Returns: Encrypted data
*/
uint32_t
*
xxtea_uint_encrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
0
,
e
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
0
,
e
;
...
@@ -134,14 +171,7 @@ uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key) {
...
@@ -134,14 +171,7 @@ uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key) {
return
data
;
return
data
;
}
}
/**
static
uint32_t
*
xxtea_uint_decrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
* Function: xxtea_uint_decrypt
* @data: Data to be decrypted
* @len: Length of the data to be decrypted
* @key: Symmetric key
* Returns: Decrypted data
*/
uint32_t
*
xxtea_uint_decrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
q
*
DELTA
,
e
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
q
*
DELTA
,
e
;
...
@@ -163,17 +193,7 @@ uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key) {
...
@@ -163,17 +193,7 @@ uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key) {
return
data
;
return
data
;
}
}
/**
static
uint8_t
*
xxtea_ubyte_encrypt
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
* Function: xxtea_encrypt_ubyte
* @data: Data to be encrypted
* @len: Length of the data to be encrypted
* @key: Symmetric key
* @out_len: Pointer to output length variable
* Returns: Encrypted data or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_encrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
out
;
uint8_t
*
out
;
uint32_t
*
data_array
,
*
key_array
;
uint32_t
*
data_array
,
*
key_array
;
size_t
data_len
,
key_len
;
size_t
data_len
,
key_len
;
...
@@ -197,17 +217,7 @@ uint8_t * xxtea_encrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
...
@@ -197,17 +217,7 @@ uint8_t * xxtea_encrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
return
out
;
return
out
;
}
}
/**
static
uint8_t
*
xxtea_ubyte_decrypt
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
* Function: xxtea_decrypt_ubyte
* @data: Data to be decrypted
* @len: Length of the data to be decrypted
* @key: Symmetric key
* @out_len: Pointer to output length variable
* Returns: Decrypted data or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_decrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
out
;
uint8_t
*
out
;
uint32_t
*
data_array
,
*
key_array
;
uint32_t
*
data_array
,
*
key_array
;
size_t
data_len
,
key_len
;
size_t
data_len
,
key_len
;
...
@@ -235,10 +245,10 @@ uint8_t * xxtea_decrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
...
@@ -235,10 +245,10 @@ uint8_t * xxtea_decrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
void
*
xxtea_encrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
void
*
xxtea_encrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
FIXED_KEY
FIXED_KEY
return
xxtea_
encrypt_ubyte
(
data
,
len
,
fixed_key
,
out_len
);
return
xxtea_
ubyte_encrypt
(
data
,
len
,
fixed_key
,
out_len
);
}
}
void
*
xxtea_decrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
void
*
xxtea_decrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
FIXED_KEY
FIXED_KEY
return
xxtea_
decrypt_ubyte
(
data
,
len
,
fixed_key
,
out_len
);
return
xxtea_
ubyte_decrypt
(
data
,
len
,
fixed_key
,
out_len
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment