Add a block_size parameter to mifare_des().

This commit is contained in:
Romain Tartiere 2010-10-29 12:34:11 +00:00
parent 356219e21b
commit a0b9df6ece
2 changed files with 22 additions and 16 deletions

View file

@ -96,6 +96,8 @@
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MAX_CRYPTO_BLOCK_SIZE 8
struct mad_sector_0x00; struct mad_sector_0x00;
struct mad_sector_0x10; struct mad_sector_0x10;
@ -183,7 +185,7 @@ struct mifare_desfire_tag {
char *last_pcd_error; char *last_pcd_error;
MifareDESFireKey session_key; MifareDESFireKey session_key;
uint8_t authenticated_key_no; uint8_t authenticated_key_no;
uint8_t ivect[16]; uint8_t ivect[MAX_CRYPTO_BLOCK_SIZE];
uint8_t *crypto_buffer; uint8_t *crypto_buffer;
size_t crypto_buffer_size; size_t crypto_buffer_size;
uint8_t block_number; uint8_t block_number;

View file

@ -28,7 +28,7 @@
#include "freefare_internal.h" #include "freefare_internal.h"
static void xor (const uint8_t *ivect, uint8_t *data, const size_t len); static void xor (const uint8_t *ivect, uint8_t *data, const size_t len);
static void mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac); static void mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac, size_t block_size);
static size_t padded_data_length (size_t nbytes); static size_t padded_data_length (size_t nbytes);
static size_t maced_data_length (size_t nbytes); static size_t maced_data_length (size_t nbytes);
@ -240,16 +240,17 @@ mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int c
} }
static void static void
mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac) mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac, size_t block_size)
{ {
uint8_t ovect[8]; uint8_t ovect[MAX_CRYPTO_BLOCK_SIZE];
if (direction == MD_SEND) { if (direction == MD_SEND) {
xor (ivect, data, 8); xor (ivect, data, block_size);
} else { } else {
memcpy (ovect, data, 8); memcpy (ovect, data, block_size);
} }
uint8_t edata[8];
uint8_t edata[MAX_CRYPTO_BLOCK_SIZE];
switch (key->type) { switch (key->type) {
case T_DES: case T_DES:
@ -258,7 +259,6 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection
} else { } else {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
} }
memcpy (data, edata, 8);
break; break;
case T_3DES: case T_3DES:
if (mac) { if (mac) {
@ -270,31 +270,35 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection
DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT); DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
} }
memcpy (data, edata, 8);
break; break;
} }
memcpy (data, edata, block_size);
if (direction == MD_SEND) { if (direction == MD_SEND) {
memcpy (ivect, data, 8); memcpy (ivect, data, block_size);
} else { } else {
xor (ivect, data, 8); xor (ivect, data, block_size);
memcpy (ivect, ovect, 8); memcpy (ivect, ovect, block_size);
} }
} }
void void
mifare_cbc_des (MifareDESFireKey key, uint8_t *ivect, uint8_t *data, size_t data_size, MifareDirection direction, int mac) mifare_cbc_des (MifareDESFireKey key, uint8_t *ivect, uint8_t *data, size_t data_size, MifareDirection direction, int mac)
{ {
size_t block_size;
switch (key->type) { switch (key->type) {
case T_DES: case T_DES:
case T_3DES: case T_3DES:
bzero (ivect, 8); bzero (ivect, MAX_CRYPTO_BLOCK_SIZE);
block_size = 8;
break;
} }
size_t offset = 0; size_t offset = 0;
while (offset < data_size) { while (offset < data_size) {
mifare_des (key, data + offset, ivect, direction, mac); mifare_des (key, data + offset, ivect, direction, mac, block_size);
offset += 8; offset += block_size;
} }
} }