31
31
#include "crc32.h"
32
32
#include "crc.h"
33
33
#include "crc16.h" // crc16 ccitt
34
- #include "printf .h"
34
+ #include "nprintf .h"
35
35
#include "iso14443a.h"
36
36
#include "dbprint.h"
37
37
#include "BigBuf.h"
@@ -65,25 +65,32 @@ void des_decrypt(void *out, const void *in, const void *key) {
65
65
mbedtls_des_crypt_ecb (& ctx , in , out );
66
66
}
67
67
68
- void tdes_nxp_receive (const void * in , void * out , size_t length , const void * key , unsigned char iv [8 ], int keymode ) {
69
- if (length % 8 ) return ;
70
- if (keymode == 2 )
68
+ void tdes_nxp_receive (const void * in , void * out , size_t length , const void * key , uint8_t * iv , int keymode ) {
69
+
70
+ // must be even blocks of 8 bytes.
71
+ if (length % 8 ) {
72
+ return ;
73
+ }
74
+
75
+ if (keymode == 2 ) {
71
76
mbedtls_des3_set2key_dec (& ctx3 , key );
72
- else
77
+ } else {
73
78
mbedtls_des3_set3key_dec (& ctx3 , key );
79
+ }
74
80
75
- uint8_t i ;
76
81
unsigned char temp [8 ];
77
82
uint8_t * tin = (uint8_t * ) in ;
78
83
uint8_t * tout = (uint8_t * ) out ;
79
84
80
85
while (length > 0 ) {
86
+
81
87
memcpy (temp , tin , 8 );
82
88
83
89
mbedtls_des3_crypt_ecb (& ctx3 , tin , tout );
84
90
85
- for (i = 0 ; i < 8 ; i ++ )
86
- tout [i ] = (unsigned char )(tout [i ] ^ iv [i ]);
91
+ for (uint8_t i = 0 ; i < 8 ; i ++ ) {
92
+ tout [i ] ^= iv [i ];
93
+ }
87
94
88
95
memcpy (iv , temp , 8 );
89
96
@@ -93,20 +100,26 @@ void tdes_nxp_receive(const void *in, void *out, size_t length, const void *key,
93
100
}
94
101
}
95
102
96
- void tdes_nxp_send (const void * in , void * out , size_t length , const void * key , unsigned char iv [8 ], int keymode ) {
97
- if (length % 8 ) return ;
98
- if (keymode == 2 )
103
+ void tdes_nxp_send (const void * in , void * out , size_t length , const void * key , uint8_t * iv , int keymode ) {
104
+
105
+ // must be even blocks of 8 bytes.
106
+ if (length % 8 ) {
107
+ return ;
108
+ }
109
+
110
+ if (keymode == 2 ) {
99
111
mbedtls_des3_set2key_enc (& ctx3 , key );
100
- else
112
+ } else {
101
113
mbedtls_des3_set3key_enc (& ctx3 , key );
114
+ }
102
115
103
- uint8_t i ;
104
116
uint8_t * tin = (uint8_t * ) in ;
105
117
uint8_t * tout = (uint8_t * ) out ;
106
118
107
119
while (length > 0 ) {
108
- for (i = 0 ; i < 8 ; i ++ ) {
109
- tin [i ] = (unsigned char )(tin [i ] ^ iv [i ]);
120
+
121
+ for (uint8_t i = 0 ; i < 8 ; i ++ ) {
122
+ tin [i ] ^= iv [i ];
110
123
}
111
124
112
125
mbedtls_des3_crypt_ecb (& ctx3 , tin , tout );
0 commit comments