Namespace loclass library (#1379)
* Namespace loclass library * Lib: const for immutable variables and bss cleanup Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -39,18 +39,18 @@ From "Dismantling iclass":
|
||||
algorithm intends to circumvent weaknesses in the cipher by preventing the
|
||||
usage of certain ‘weak’ keys. In order to compute a diversified key, the iClass
|
||||
reader first encrypts the card identity id with the master key K, using single
|
||||
DES. The resulting ciphertext is then input to a function called hash0 which
|
||||
DES. The resulting ciphertext is then input to a function called loclass_hash0 which
|
||||
outputs the diversified key k.
|
||||
|
||||
k = hash0(DES enc (id, K))
|
||||
k = loclass_hash0(DES enc (id, K))
|
||||
|
||||
Here the DES encryption of id with master key K outputs a cryptogram c
|
||||
of 64 bits. These 64 bits are divided as c = x, y, z [0] , . . . , z [7] ∈ F 82 × F 82 × (F 62 ) 8
|
||||
which is used as input to the hash0 function. This function introduces some
|
||||
which is used as input to the loclass_hash0 function. This function introduces some
|
||||
obfuscation by performing a number of permutations, complement and modulo
|
||||
operations, see Figure 2.5. Besides that, it checks for and removes patterns like
|
||||
similar key bytes, which could produce a strong bias in the cipher. Finally, the
|
||||
output of hash0 is the diversified card key k = k [0] , . . . , k [7] ∈ (F 82 ) 8 .
|
||||
output of loclass_hash0 is the diversified card key k = k [0] , . . . , k [7] ∈ (F 82 ) 8 .
|
||||
|
||||
**/
|
||||
#include "optimized_ikeys.h"
|
||||
@@ -61,7 +61,7 @@ From "Dismantling iclass":
|
||||
#include <mbedtls/des.h>
|
||||
#include "optimized_cipherutils.h"
|
||||
|
||||
static uint8_t pi[35] = {
|
||||
static const uint8_t loclass_pi[35] = {
|
||||
0x0F, 0x17, 0x1B, 0x1D, 0x1E, 0x27, 0x2B, 0x2D,
|
||||
0x2E, 0x33, 0x35, 0x39, 0x36, 0x3A, 0x3C, 0x47,
|
||||
0x4B, 0x4D, 0x4E, 0x53, 0x55, 0x56, 0x59, 0x5A,
|
||||
@@ -69,8 +69,6 @@ static uint8_t pi[35] = {
|
||||
0x72, 0x74, 0x78
|
||||
};
|
||||
|
||||
static mbedtls_des_context ctx_enc;
|
||||
|
||||
/**
|
||||
* @brief The key diversification algorithm uses 6-bit bytes.
|
||||
* This implementation uses 64 bit uint to pack seven of them into one
|
||||
@@ -82,7 +80,7 @@ static mbedtls_des_context ctx_enc;
|
||||
* @param n bitnumber
|
||||
* @return
|
||||
*/
|
||||
static uint8_t getSixBitByte(uint64_t c, int n) {
|
||||
static uint8_t loclass_getSixBitByte(uint64_t c, int n) {
|
||||
return (c >> (42 - 6 * n)) & 0x3F;
|
||||
}
|
||||
|
||||
@@ -92,7 +90,7 @@ static uint8_t getSixBitByte(uint64_t c, int n) {
|
||||
* @param z the value to place there
|
||||
* @param n bitnumber.
|
||||
*/
|
||||
static void pushbackSixBitByte(uint64_t *c, uint8_t z, int n) {
|
||||
static void loclass_pushbackSixBitByte(uint64_t *c, uint8_t z, int n) {
|
||||
//0x XXXX YYYY ZZZZ ZZZZ ZZZZ
|
||||
// ^z0 ^z7
|
||||
//z0: 1111 1100 0000 0000
|
||||
@@ -117,16 +115,16 @@ static void pushbackSixBitByte(uint64_t *c, uint8_t z, int n) {
|
||||
* @param c
|
||||
* @return
|
||||
*/
|
||||
static uint64_t swapZvalues(uint64_t c) {
|
||||
static uint64_t loclass_swapZvalues(uint64_t c) {
|
||||
uint64_t newz = 0;
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 0), 7);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 1), 6);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 2), 5);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 3), 4);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 4), 3);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 5), 2);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 6), 1);
|
||||
pushbackSixBitByte(&newz, getSixBitByte(c, 7), 0);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 0), 7);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 1), 6);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 2), 5);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 3), 4);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 4), 3);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 5), 2);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 6), 1);
|
||||
loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 7), 0);
|
||||
newz |= (c & 0xFFFF000000000000);
|
||||
return newz;
|
||||
}
|
||||
@@ -134,58 +132,58 @@ static uint64_t swapZvalues(uint64_t c) {
|
||||
/**
|
||||
* @return 4 six-bit bytes chunked into a uint64_t,as 00..00a0a1a2a3
|
||||
*/
|
||||
static uint64_t ck(int i, int j, uint64_t z) {
|
||||
static uint64_t loclass_ck(int i, int j, uint64_t z) {
|
||||
if (i == 1 && j == -1) {
|
||||
// ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
|
||||
// loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
|
||||
return z;
|
||||
} else if (j == -1) {
|
||||
// ck(i, −1, z [0] . . . z [3] ) = ck(i − 1, i − 2, z [0] . . . z [3] )
|
||||
return ck(i - 1, i - 2, z);
|
||||
// loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
|
||||
return loclass_ck(i - 1, i - 2, z);
|
||||
}
|
||||
|
||||
if (getSixBitByte(z, i) == getSixBitByte(z, j)) {
|
||||
//ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] )
|
||||
if (loclass_getSixBitByte(z, i) == loclass_getSixBitByte(z, j)) {
|
||||
//loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] )
|
||||
uint64_t newz = 0;
|
||||
int c;
|
||||
for (c = 0; c < 4; c++) {
|
||||
uint8_t val = getSixBitByte(z, c);
|
||||
uint8_t val = loclass_getSixBitByte(z, c);
|
||||
if (c == i)
|
||||
pushbackSixBitByte(&newz, j, c);
|
||||
loclass_pushbackSixBitByte(&newz, j, c);
|
||||
else
|
||||
pushbackSixBitByte(&newz, val, c);
|
||||
loclass_pushbackSixBitByte(&newz, val, c);
|
||||
}
|
||||
return ck(i, j - 1, newz);
|
||||
return loclass_ck(i, j - 1, newz);
|
||||
} else {
|
||||
return ck(i, j - 1, z);
|
||||
return loclass_ck(i, j - 1, z);
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
Definition 8.
|
||||
Let the function check : (F 62 ) 8 → (F 62 ) 8 be defined as
|
||||
check(z [0] . . . z [7] ) = ck(3, 2, z [0] . . . z [3] ) · ck(3, 2, z [4] . . . z [7] )
|
||||
check(z [0] . . . z [7] ) = loclass_ck(3, 2, z [0] . . . z [3] ) · loclass_ck(3, 2, z [4] . . . z [7] )
|
||||
|
||||
where ck : N × N × (F 62 ) 4 → (F 62 ) 4 is defined as
|
||||
where loclass_ck : N × N × (F 62 ) 4 → (F 62 ) 4 is defined as
|
||||
|
||||
ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
|
||||
ck(i, −1, z [0] . . . z [3] ) = ck(i − 1, i − 2, z [0] . . . z [3] )
|
||||
ck(i, j, z [0] . . . z [3] ) =
|
||||
ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] ), if z [i] = z [j] ;
|
||||
ck(i, j − 1, z [0] . . . z [3] ), otherwise
|
||||
loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
|
||||
loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
|
||||
loclass_ck(i, j, z [0] . . . z [3] ) =
|
||||
loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] ), if z [i] = z [j] ;
|
||||
loclass_ck(i, j − 1, z [0] . . . z [3] ), otherwise
|
||||
|
||||
otherwise.
|
||||
**/
|
||||
|
||||
static uint64_t check(uint64_t z) {
|
||||
static uint64_t loclass_check(uint64_t z) {
|
||||
//These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
|
||||
|
||||
// ck(3, 2, z [0] . . . z [3] )
|
||||
uint64_t ck1 = ck(3, 2, z);
|
||||
// loclass_ck(3, 2, z [0] . . . z [3] )
|
||||
uint64_t ck1 = loclass_ck(3, 2, z);
|
||||
|
||||
// ck(3, 2, z [4] . . . z [7] )
|
||||
uint64_t ck2 = ck(3, 2, z << 24);
|
||||
// loclass_ck(3, 2, z [4] . . . z [7] )
|
||||
uint64_t ck2 = loclass_ck(3, 2, z << 24);
|
||||
|
||||
//The ck function will place the values
|
||||
//The loclass_ck function will place the values
|
||||
// in the middle of z.
|
||||
ck1 &= 0x00000000FFFFFF000000;
|
||||
ck2 &= 0x00000000FFFFFF000000;
|
||||
@@ -193,28 +191,28 @@ static uint64_t check(uint64_t z) {
|
||||
return ck1 | ck2 >> 24;
|
||||
}
|
||||
|
||||
static void permute(BitstreamIn_t *p_in, uint64_t z, int l, int r, BitstreamOut_t *out) {
|
||||
if (bitsLeft(p_in) == 0)
|
||||
static void loclass_permute(LoclassBitstreamIn_t *p_in, uint64_t z, int l, int r, LoclassBitstreamOut_t *out) {
|
||||
if (loclass_bitsLeft(p_in) == 0)
|
||||
return;
|
||||
|
||||
bool pn = tailBit(p_in);
|
||||
bool pn = loclass_tailBit(p_in);
|
||||
if (pn) { // pn = 1
|
||||
uint8_t zl = getSixBitByte(z, l);
|
||||
uint8_t zl = loclass_getSixBitByte(z, l);
|
||||
|
||||
push6bits(out, zl + 1);
|
||||
permute(p_in, z, l + 1, r, out);
|
||||
loclass_push6bits(out, zl + 1);
|
||||
loclass_permute(p_in, z, l + 1, r, out);
|
||||
} else { // otherwise
|
||||
uint8_t zr = getSixBitByte(z, r);
|
||||
uint8_t zr = loclass_getSixBitByte(z, r);
|
||||
|
||||
push6bits(out, zr);
|
||||
permute(p_in, z, l, r + 1, out);
|
||||
loclass_push6bits(out, zr);
|
||||
loclass_permute(p_in, z, l, r + 1, out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*Definition 11. Let the function hash0 : F 82 × F 82 × (F 62 ) 8 → (F 82 ) 8 be defined as
|
||||
* hash0(x, y, z [0] . . . z [7] ) = k [0] . . . k [7] where
|
||||
*Definition 11. Let the function loclass_hash0 : F 82 × F 82 × (F 62 ) 8 → (F 82 ) 8 be defined as
|
||||
* loclass_hash0(x, y, z [0] . . . z [7] ) = k [0] . . . k [7] where
|
||||
* z'[i] = (z[i] mod (63-i)) + i i = 0...3
|
||||
* z'[i+4] = (z[i+4] mod (64-i)) + i i = 0...3
|
||||
* ẑ = check(z');
|
||||
@@ -222,8 +220,8 @@ static void permute(BitstreamIn_t *p_in, uint64_t z, int l, int r, BitstreamOut_
|
||||
* @param k this is where the diversified key is put (should be 8 bytes)
|
||||
* @return
|
||||
*/
|
||||
void hash0(uint64_t c, uint8_t k[8]) {
|
||||
c = swapZvalues(c);
|
||||
void loclass_hash0(uint64_t c, uint8_t k[8]) {
|
||||
c = loclass_swapZvalues(c);
|
||||
|
||||
//These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
|
||||
// x = 8 bits
|
||||
@@ -234,30 +232,30 @@ void hash0(uint64_t c, uint8_t k[8]) {
|
||||
uint64_t zP = 0;
|
||||
|
||||
for (int n = 0; n < 4 ; n++) {
|
||||
uint8_t zn = getSixBitByte(c, n);
|
||||
uint8_t zn4 = getSixBitByte(c, n + 4);
|
||||
uint8_t zn = loclass_getSixBitByte(c, n);
|
||||
uint8_t zn4 = loclass_getSixBitByte(c, n + 4);
|
||||
uint8_t _zn = (zn % (63 - n)) + n;
|
||||
uint8_t _zn4 = (zn4 % (64 - n)) + n;
|
||||
pushbackSixBitByte(&zP, _zn, n);
|
||||
pushbackSixBitByte(&zP, _zn4, n + 4);
|
||||
loclass_pushbackSixBitByte(&zP, _zn, n);
|
||||
loclass_pushbackSixBitByte(&zP, _zn4, n + 4);
|
||||
}
|
||||
|
||||
uint64_t zCaret = check(zP);
|
||||
uint8_t p = pi[x % 35];
|
||||
uint64_t zCaret = loclass_check(zP);
|
||||
uint8_t p = loclass_pi[x % 35];
|
||||
|
||||
if (x & 1) //Check if x7 is 1
|
||||
p = ~p;
|
||||
|
||||
BitstreamIn_t p_in = { &p, 8, 0 };
|
||||
LoclassBitstreamIn_t p_in = { &p, 8, 0 };
|
||||
uint8_t outbuffer[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
BitstreamOut_t out = {outbuffer, 0, 0};
|
||||
permute(&p_in, zCaret, 0, 4, &out); //returns 48 bits? or 6 8-bytes
|
||||
LoclassBitstreamOut_t out = {outbuffer, 0, 0};
|
||||
loclass_permute(&p_in, zCaret, 0, 4, &out); //returns 48 bits? or 6 8-bytes
|
||||
|
||||
//Out is now a buffer containing six-bit bytes, should be 48 bits
|
||||
// if all went well
|
||||
//Shift z-values down onto the lower segment
|
||||
|
||||
uint64_t zTilde = x_bytes_to_num(outbuffer, sizeof(outbuffer));
|
||||
uint64_t zTilde = loclass_x_bytes_to_num(outbuffer, sizeof(outbuffer));
|
||||
|
||||
zTilde >>= 16;
|
||||
|
||||
@@ -274,7 +272,7 @@ void hash0(uint64_t c, uint8_t k[8]) {
|
||||
// First, place y(7-i) leftmost in k
|
||||
k[i] |= (y << (7 - i)) & 0x80 ;
|
||||
|
||||
uint8_t zTilde_i = getSixBitByte(zTilde, i);
|
||||
uint8_t zTilde_i = loclass_getSixBitByte(zTilde, i);
|
||||
// zTildeI is now on the form 00XXXXXX
|
||||
// with one leftshift, it'll be
|
||||
// 0XXXXXX0
|
||||
@@ -304,18 +302,20 @@ void hash0(uint64_t c, uint8_t k[8]) {
|
||||
* @param key
|
||||
* @param div_key
|
||||
*/
|
||||
void diversifyKey(uint8_t *csn, const uint8_t *key, uint8_t *div_key) {
|
||||
void loclass_diversifyKey(uint8_t *csn, const uint8_t *key, uint8_t *div_key) {
|
||||
mbedtls_des_context loclass_ctx_enc;
|
||||
|
||||
// Prepare the DES key
|
||||
mbedtls_des_setkey_enc(&ctx_enc, key);
|
||||
mbedtls_des_setkey_enc(&loclass_ctx_enc, key);
|
||||
|
||||
uint8_t crypted_csn[8] = {0};
|
||||
|
||||
// Calculate DES(CSN, KEY)
|
||||
mbedtls_des_crypt_ecb(&ctx_enc, csn, crypted_csn);
|
||||
mbedtls_des_crypt_ecb(&loclass_ctx_enc, csn, crypted_csn);
|
||||
|
||||
//Calculate HASH0(DES))
|
||||
uint64_t c_csn = x_bytes_to_num(crypted_csn, sizeof(crypted_csn));
|
||||
uint64_t c_csn = loclass_x_bytes_to_num(crypted_csn, sizeof(crypted_csn));
|
||||
|
||||
hash0(c_csn, div_key);
|
||||
loclass_hash0(c_csn, div_key);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user