#include <string.h>#include "xyssl/des.h"Go to the source code of this file.
Defines | |
| #define | _CRT_SECURE_NO_DEPRECATE 1 |
| #define | GET_UINT32_BE(n, b, i) |
| #define | PUT_UINT32_BE(n, b, i) |
| #define | DES_IP(X, Y) |
| #define | DES_FP(X, Y) |
| #define | DES_ROUND(X, Y) |
Functions | |
| static void | des_main_ks (unsigned long SK[32], unsigned char key[8]) |
| void | des_set_key (des_context *ctx, unsigned char key[8]) |
| DES key schedule (56-bit). | |
| static void | des_crypt (unsigned long SK[32], unsigned char input[8], unsigned char output[8]) |
| void | des_encrypt (des_context *ctx, unsigned char input[8], unsigned char output[8]) |
| DES block encryption (ECB mode). | |
| void | des_decrypt (des_context *ctx, unsigned char input[8], unsigned char output[8]) |
| DES block decryption (ECB mode). | |
| void | des_cbc_encrypt (des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len) |
| DES-CBC buffer encryption. | |
| void | des_cbc_decrypt (des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len) |
| DES-CBC buffer decryption. | |
| void | des3_set_2keys (des3_context *ctx, unsigned char key[16]) |
| Triple-DES key schedule (112-bit). | |
| void | des3_set_3keys (des3_context *ctx, unsigned char key[24]) |
| Triple-DES key schedule (168-bit). | |
| static void | des3_crypt (unsigned long SK[96], unsigned char input[8], unsigned char output[8]) |
| void | des3_encrypt (des3_context *ctx, unsigned char input[8], unsigned char output[8]) |
| Triple-DES block encryption (ECB mode). | |
| void | des3_decrypt (des3_context *ctx, unsigned char input[8], unsigned char output[8]) |
| Triple-DES block decryption (ECB mode). | |
| void | des3_cbc_encrypt (des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len) |
| 3DES-CBC buffer encryption | |
| void | des3_cbc_decrypt (des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len) |
| 3DES-CBC buffer decryption | |
| int | des_self_test (int verbose) |
Variables | |
| static const unsigned long | SB1 [64] |
| static const unsigned long | SB2 [64] |
| static const unsigned long | SB3 [64] |
| static const unsigned long | SB4 [64] |
| static const unsigned long | SB5 [64] |
| static const unsigned long | SB6 [64] |
| static const unsigned long | SB7 [64] |
| static const unsigned long | SB8 [64] |
| static const unsigned long | LHs [16] |
| static const unsigned long | RHs [16] |
| static const char | _des_src [] = "_des_src" |
| #define DES_FP | ( | X, | |||
| Y | ) |
Value:
{ \
X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
}
Definition at line 259 of file des.c.
Referenced by des3_crypt(), and des_crypt().
| #define DES_IP | ( | X, | |||
| Y | ) |
Value:
{ \
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
}
Definition at line 245 of file des.c.
Referenced by des3_crypt(), and des_crypt().
| #define DES_ROUND | ( | X, | |||
| Y | ) |
Value:
{ \
T = *SK++ ^ X; \
Y ^= SB8[ (T ) & 0x3F ] ^ \
SB6[ (T >> 8) & 0x3F ] ^ \
SB4[ (T >> 16) & 0x3F ] ^ \
SB2[ (T >> 24) & 0x3F ]; \
\
T = *SK++ ^ ((X << 28) | (X >> 4)); \
Y ^= SB7[ (T ) & 0x3F ] ^ \
SB5[ (T >> 8) & 0x3F ] ^ \
SB3[ (T >> 16) & 0x3F ] ^ \
SB1[ (T >> 24) & 0x3F ]; \
}
Definition at line 273 of file des.c.
Referenced by des3_crypt(), and des_crypt().
| #define GET_UINT32_BE | ( | n, | |||
| b, | |||||
| i | ) |
| #define PUT_UINT32_BE | ( | n, | |||
| b, | |||||
| i | ) |
| void des3_cbc_decrypt | ( | des3_context * | ctx, | |
| unsigned char | iv[8], | |||
| unsigned char * | input, | |||
| unsigned char * | output, | |||
| int | len | |||
| ) |
3DES-CBC buffer decryption
| ctx | 3DES context | |
| iv | initialization vector (modified after use) | |
| input | buffer holding the ciphertext | |
| output | buffer holding the plaintext | |
| len | length of the data to be decrypted |
Definition at line 614 of file des.c.
References des3_crypt(), and des3_context::dsk.
Referenced by ssl_decrypt_buf(), and x509_des3_decrypt().
| void des3_cbc_encrypt | ( | des3_context * | ctx, | |
| unsigned char | iv[8], | |||
| unsigned char * | input, | |||
| unsigned char * | output, | |||
| int | len | |||
| ) |
3DES-CBC buffer encryption
| ctx | 3DES context | |
| iv | initialization vector (modified after use) | |
| input | buffer holding the plaintext | |
| output | buffer holding the ciphertext | |
| len | length of the data to be encrypted |
Definition at line 589 of file des.c.
References des3_crypt(), and des3_context::esk.
Referenced by main(), and ssl_encrypt_buf().
| static void des3_crypt | ( | unsigned long | SK[96], | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) | [static] |
Definition at line 522 of file des.c.
References DES_FP, DES_IP, DES_ROUND, GET_UINT32_BE, and PUT_UINT32_BE.
Referenced by des3_cbc_decrypt(), des3_cbc_encrypt(), des3_decrypt(), and des3_encrypt().
| void des3_decrypt | ( | des3_context * | ctx, | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) |
Triple-DES block decryption (ECB mode).
| ctx | 3DES context | |
| input | ciphertext block | |
| output | plaintext block |
Definition at line 579 of file des.c.
References des3_crypt(), and des3_context::dsk.
| void des3_encrypt | ( | des3_context * | ctx, | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) |
Triple-DES block encryption (ECB mode).
| ctx | 3DES context | |
| input | plaintext block | |
| output | ciphertext block |
Definition at line 569 of file des.c.
References des3_crypt(), and des3_context::esk.
| void des3_set_2keys | ( | des3_context * | ctx, | |
| unsigned char | key[16] | |||
| ) |
Triple-DES key schedule (112-bit).
| ctx | 3DES context to be initialized | |
| key | 16-byte secret key |
Definition at line 475 of file des.c.
References des_main_ks(), des3_context::dsk, and des3_context::esk.
| void des3_set_3keys | ( | des3_context * | ctx, | |
| unsigned char | key[24] | |||
| ) |
Triple-DES key schedule (168-bit).
| ctx | 3DES context to be initialized | |
| key | 24-byte secret key |
Definition at line 501 of file des.c.
References des_main_ks(), des3_context::dsk, and des3_context::esk.
Referenced by main(), ssl_derive_keys(), and x509_des3_decrypt().
| void des_cbc_decrypt | ( | des_context * | ctx, | |
| unsigned char | iv[8], | |||
| unsigned char * | input, | |||
| unsigned char * | output, | |||
| int | len | |||
| ) |
DES-CBC buffer decryption.
| ctx | DES context | |
| iv | initialization vector (modified after use) | |
| input | buffer holding the ciphertext | |
| output | buffer holding the plaintext | |
| len | length of the data to be decrypted |
Definition at line 447 of file des.c.
References des_crypt(), and des_context::dsk.
| void des_cbc_encrypt | ( | des_context * | ctx, | |
| unsigned char | iv[8], | |||
| unsigned char * | input, | |||
| unsigned char * | output, | |||
| int | len | |||
| ) |
DES-CBC buffer encryption.
| ctx | DES context | |
| iv | initialization vector (modified after use) | |
| input | buffer holding the plaintext | |
| output | buffer holding the ciphertext | |
| len | length of the data to be encrypted |
Definition at line 422 of file des.c.
References des_crypt(), and des_context::esk.
Referenced by main().
| static void des_crypt | ( | unsigned long | SK[32], | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) | [static] |
Definition at line 373 of file des.c.
References DES_FP, DES_IP, DES_ROUND, GET_UINT32_BE, and PUT_UINT32_BE.
Referenced by des_cbc_decrypt(), des_cbc_encrypt(), des_decrypt(), and des_encrypt().
| void des_decrypt | ( | des_context * | ctx, | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) |
DES block decryption (ECB mode).
| ctx | DES context | |
| input | ciphertext block | |
| output | plaintext block |
Definition at line 412 of file des.c.
References des_crypt(), and des_context::dsk.
| void des_encrypt | ( | des_context * | ctx, | |
| unsigned char | input[8], | |||
| unsigned char | output[8] | |||
| ) |
DES block encryption (ECB mode).
| ctx | DES context | |
| input | plaintext block | |
| output | ciphertext block |
Definition at line 402 of file des.c.
References des_crypt(), and des_context::esk.
| static void des_main_ks | ( | unsigned long | SK[32], | |
| unsigned char | key[8] | |||
| ) | [static] |
Definition at line 288 of file des.c.
References GET_UINT32_BE, LHs, and RHs.
Referenced by des3_set_2keys(), des3_set_3keys(), and des_set_key().
| void des_set_key | ( | des_context * | ctx, | |
| unsigned char | key[8] | |||
| ) |
DES key schedule (56-bit).
| ctx | DES context to be initialized | |
| key | 8-byte secret key |
Definition at line 360 of file des.c.
References des_main_ks(), des_context::dsk, and des_context::esk.
Referenced by main().
const unsigned long LHs[16] [static] |
Initial value:
{
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
}
Definition at line 226 of file des.c.
Referenced by des_main_ks().
const unsigned long RHs[16] [static] |
Initial value:
{
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100,
0x00000001, 0x01000001, 0x00010001, 0x01010001,
0x00000101, 0x01000101, 0x00010101, 0x01010101,
}
Definition at line 234 of file des.c.
Referenced by des_main_ks().
const unsigned long SB1[64] [static] |
Initial value:
{
0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400,
0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000,
0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004,
0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000,
0x00010004, 0x00010400, 0x00000000, 0x01010004
}
const unsigned long SB2[64] [static] |
Initial value:
{
0x80108020, 0x80008000, 0x00008000, 0x00108020,
0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000,
0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000,
0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020,
0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020,
0x80000000, 0x80100020, 0x80108020, 0x00108000
}
const unsigned long SB3[64] [static] |
Initial value:
{
0x00000208, 0x08020200, 0x00000000, 0x08020008,
0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200,
0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208,
0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200,
0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
0x00000008, 0x00020208, 0x00020200, 0x08000008,
0x08020000, 0x08000208, 0x00000208, 0x08020000,
0x00020208, 0x00000008, 0x08020008, 0x00020200
}
const unsigned long SB4[64] [static] |
Initial value:
{
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
0x00000001, 0x00002000, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002001, 0x00002080,
0x00800081, 0x00000001, 0x00002080, 0x00800080,
0x00002000, 0x00802080, 0x00802081, 0x00000081,
0x00800080, 0x00800001, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00000000, 0x00802000,
0x00002080, 0x00800080, 0x00800081, 0x00000001,
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
0x00800001, 0x00002001, 0x00802080, 0x00800081,
0x00002001, 0x00002080, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002000, 0x00802080
}
const unsigned long SB5[64] [static] |
Initial value:
{
0x00000100, 0x02080100, 0x02080000, 0x42000100,
0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
0x02000000, 0x40080000, 0x40080000, 0x00000000,
0x40000100, 0x42080100, 0x42080100, 0x02000100,
0x42080000, 0x40000100, 0x00000000, 0x42000000,
0x02080100, 0x02000000, 0x42000000, 0x00080100,
0x00080000, 0x42000100, 0x00000100, 0x02000000,
0x40000000, 0x02080000, 0x42000100, 0x40080100,
0x02000100, 0x40000000, 0x42080000, 0x02080100,
0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
0x02080000, 0x00000000, 0x40080000, 0x42000000,
0x00080100, 0x02000100, 0x40000100, 0x00080000,
0x00000000, 0x40080000, 0x02080100, 0x40000100
}
const unsigned long SB6[64] [static] |
Initial value:
{
0x20000010, 0x20400000, 0x00004000, 0x20404010,
0x20400000, 0x00000010, 0x20404010, 0x00400000,
0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
0x00000000, 0x00400010, 0x20004010, 0x00004000,
0x00404000, 0x20004010, 0x00000010, 0x20400010,
0x20400010, 0x00000000, 0x00404010, 0x20404000,
0x00004010, 0x00404000, 0x20404000, 0x20000000,
0x20004000, 0x00000010, 0x20400010, 0x00404000,
0x20404010, 0x00400000, 0x00004010, 0x20000010,
0x00400000, 0x20004000, 0x20000000, 0x00004010,
0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
0x00000010, 0x00004000, 0x20400000, 0x00404010,
0x00004000, 0x00400010, 0x20004010, 0x00000000,
0x20404000, 0x20000000, 0x00400010, 0x20004010
}
const unsigned long SB7[64] [static] |
Initial value:
{
0x00200000, 0x04200002, 0x04000802, 0x00000000,
0x00000800, 0x04000802, 0x00200802, 0x04200800,
0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
0x04000800, 0x00200802, 0x00200002, 0x04000800,
0x04000002, 0x04200000, 0x04200800, 0x00200002,
0x04200000, 0x00000800, 0x00000802, 0x04200802,
0x00200800, 0x00000002, 0x04000000, 0x00200800,
0x04000000, 0x00200800, 0x00200000, 0x04000802,
0x04000802, 0x04200002, 0x04200002, 0x00000002,
0x00200002, 0x04000000, 0x04000800, 0x00200000,
0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
0x00200800, 0x00000000, 0x00000002, 0x04200802,
0x00000000, 0x00200802, 0x04200000, 0x00000800,
0x04000002, 0x04000800, 0x00000800, 0x00200002
}
const unsigned long SB8[64] [static] |
Initial value:
{
0x10001040, 0x00001000, 0x00040000, 0x10041040,
0x10000000, 0x10001040, 0x00000040, 0x10000000,
0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
0x10040000, 0x10000040, 0x10001000, 0x00001040,
0x00041000, 0x00040040, 0x10040040, 0x10041000,
0x00001040, 0x00000000, 0x00000000, 0x10040040,
0x10000040, 0x10001000, 0x00041040, 0x00040000,
0x00041040, 0x00040000, 0x10041000, 0x00001000,
0x00000040, 0x10040040, 0x00001000, 0x00041040,
0x10001000, 0x00000040, 0x10000040, 0x10040000,
0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
0x10040000, 0x10001000, 0x10001040, 0x00000000,
0x10041040, 0x00041000, 0x00041000, 0x00001040,
0x00001040, 0x00040040, 0x10000000, 0x10041000
}
1.5.4