ID Chip Reader
des.h
Go to the documentation of this file.
1 
17 #pragma once
18 #include "typedef.h"
19 
20 #ifndef CRYPTOGRAPHY_DES3_H_
21 #define CRYPTOGRAPHY_DES3_H_
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define MBEDTLS_DES_ENCRYPT 1
28 #define MBEDTLS_DES_DECRYPT 0
29 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH 0
30 
31 #define USE_DES_EN 1 // Use DES
32 #define USE_DES_ECB_EN 1 // ECB mode using DES
33 #define USE_DES_CBC_EN 0 // CBC mode using DES
34 
35 #define USE_3DES_EN 1 // Use 3DES
36 #define USE_3DES_ECB_EN 0 // ECB mode using 3DES
37 #define USE_3DES_CBC_EN 1 // CBC mode using 3DES
38 
39 #define DES_INPUT_LENGTH -0x0002 // The data input has an invalid length
40 
41 #define MBEDTLS_DES_KEY_SIZE 8
42 #define DES_KEY_SIZE (8)
43 #define DES3_KEY2_SIZE (16)
44 #define DES3_KEY3_SIZE (24)
45 
46 #if USE_DES_EN
47 #if USE_DES_ECB_EN
48 // DES-EBC buffer encryption API
49 unsigned int des_ecb_encrypt(unsigned char* pout,
50  unsigned char* pdata,
51  unsigned int nlen,
52  unsigned char* pkey);
53 
54 // DES-EBC buffer decryption API
55 unsigned int des_ecb_decrypt(unsigned char* pout,
56  unsigned char* pdata,
57  unsigned int nlen,
58  unsigned char* pkey);
59 #endif // #if USE_DES_ECB_EN
60 
61 #if USE_DES_EN
62 typedef struct {
63  uint32_t sk[32]; // DES subkeys
64 } des_context;
65 #endif
66 
67 #if USE_DES_CBC_EN
68 int des_crypt_cbc(des_context* ctx,
69  int mode,
70  size_t length,
71  unsigned char iv[8],
72  const unsigned char* input,
73  unsigned char* output);
74 unsigned int des_cbc_encrypt(unsigned char* pout,
75  unsigned char* pdata,
76  unsigned int nlen,
77  unsigned char* pkey,
78  unsigned char* piv);
79 #endif // #if USE_DES_CBC_EN
80 #endif // #if USE_DES_EN
81 
82 #if USE_3DES_EN
83 #if USE_3DES_ECB_EN
84 // 3DES-ECB buffer encryption API
85 unsigned int des3_ecb_encrypt(unsigned char* pout,
86  unsigned char* pdata,
87  unsigned int nlen,
88  unsigned char* pkey,
89  unsigned int klen);
90 
91 // 3DES-ECB buffer decryption API
92 unsigned int des3_ecb_decrypt(unsigned char* pout,
93  unsigned char* pdata,
94  unsigned int nlen,
95  unsigned char* pkey,
96  unsigned int klen);
97 #endif // #if USE_3DES_ECB_EN
98 
99 #if USE_3DES_CBC_EN
100 // 3DES-CBC buffer encryption API
101 unsigned int des3_cbc_encrypt(unsigned char* pout,
102  unsigned char* pdata,
103  unsigned int nlen,
104  unsigned char* pkey,
105  unsigned int klen,
106  unsigned char* piv);
107 
108 // 3DES-CBC buffer decryption API
109 unsigned int des3_cbc_decrypt(unsigned char* pout,
110  unsigned char* pdata,
111  unsigned int nlen,
112  unsigned char* pkey,
113  unsigned int klen,
114  unsigned char* piv);
115 #endif // #if USE_3DES_CBC_EN
116 #endif // #if USE_3DES_EN
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif // #ifndef CRYPTOGRAPHY_DES3_H_