Nokia Extensions for JSR-257

com.nokia.nfc.nxp.mfstd
Class MFAccessBits

java.lang.Object
  extended by com.nokia.nfc.nxp.mfstd.MFAccessBits

public class MFAccessBits
extends java.lang.Object

A class for handling Mifare Standard card's access bits.

Access to certain block - either data or trailer - is defined using access bits C1, C2 and C3. Combination of these bits define what operations are allowed and which keys should be used during these operations.

This class provides separate methods for setting trailer block and data block access conditions. For specifying data block access conditions only the indices 0 to 2 are allowed. For the last 8 Mifare Standard 4k sectors the access conditions are defined in units of 5 blocks. So the block index 0 defines access conditions for the data blocks 0-4, block index 1 for data blocks 5-9 and block index 2 for data blocks 10-14.

Detailed description of access conditions and bit combinations can be found from Mifare Standard Card Data sheets.


Field Summary
static int C1
          The constant corresponding to access bit C1.
static int C2
          The constant corresponding to access bit C2.
static int C3
          The constant corresponding to access bit C3.
static int KEY_A
          Authentication key type A.
static int KEY_B
          Authentication key type B.
 
Constructor Summary
MFAccessBits()
          Creates a MFAccessBits instance so that the access bits are in transport configuration: for data blocks all access bits (C1, C2, C3) are 0 and the trailer access bits are 0, 0 and 1.
MFAccessBits(byte[] accessBits)
          Creates a MFAccessBits instance based on the given access bit bytes.
MFAccessBits(byte[] accessBits, int offset)
          Creates a MFAccessBits instance based on the given access bit bytes.
MFAccessBits(int accessBits)
          Creates a MFAccessBits instance based on the given access bits.
 
Method Summary
 boolean canDecrement(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can decrement the value in block defined by blockIndex.
 boolean canIncrement(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can increment the value in block defined by blockIndex.
 boolean canRead(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can read data from the block defined by blockIndex.
 boolean canReadAccessBits(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can read the access bits from sector trailer.
 boolean canReadKeyA(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can read the Key A from sector trailer.
 boolean canReadKeyB(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can read the Key A from sector trailer.
 boolean canRestore(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can restore the value from block defined by blockIndex.
 boolean canTransfer(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can transfer the value to block defined by blockIndex.
 boolean canWrite(int keyType, int blockIndex)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can write data to block defined by blockIndex.
 boolean canWriteAccessBits(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can read the access bits from sector trailer.
 boolean canWriteKeyA(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can write the Key A in sector trailer.
 boolean canWriteKeyB(int keyType)
          Tests whether the access conditions are set so that with a authentication key of type keyType one can write the Key B in sector trailer.
 boolean equals(java.lang.Object obj)
          Tests if the given object equal to this object.
 int getAccessBitsForBlock(int blockIndex)
          Returns access bits for specified block.
 int hashCode()
          Returns a hashcode for this MFAccessBits object.
 void setAccessBitsForBlock(boolean c1, boolean c2, boolean c3, int blockIndex)
          Sets the access bits for the specified data block(s).
 void setAccessBitsForBlock(int accessBits, int blockIndex)
          Sets the access bits for the specified data block(s).
 void setAccessBitsForDataBlocks(boolean c1, boolean c2, boolean c3)
          Sets the access bits for all data blocks.
 void setAccessBitsForTrailer(boolean c1, boolean c2, boolean c3)
          Sets the accessbits for the sector trailer block.
 byte[] toByteArray()
          Returns the defined access bits as a byte array that can be used for writing the sector trailer.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_A

public static final int KEY_A
Authentication key type A.

See Also:
Constant Field Values

KEY_B

public static final int KEY_B
Authentication key type B.

See Also:
Constant Field Values

C1

public static final int C1
The constant corresponding to access bit C1.

See Also:
Constant Field Values

C2

public static final int C2
The constant corresponding to access bit C2.

See Also:
Constant Field Values

C3

public static final int C3
The constant corresponding to access bit C3.

See Also:
Constant Field Values
Constructor Detail

MFAccessBits

public MFAccessBits()
Creates a MFAccessBits instance so that the access bits are in transport configuration: for data blocks all access bits (C1, C2, C3) are 0 and the trailer access bits are 0, 0 and 1.


MFAccessBits

public MFAccessBits(byte[] accessBits)
Creates a MFAccessBits instance based on the given access bit bytes. The first three bytes of the accessBits array are read as the sixth, seventh and eight byte of the sector trailer.

Parameters:
accessBits - the access bit bytes.
Throws:
java.lang.NullPointerException - if the accessBits array is null.
java.lang.ArrayIndexOutOfBoundsException - if the array length is less than 3.
java.lang.IllegalArgumentException - if the access bits are not set correctly.

MFAccessBits

public MFAccessBits(byte[] accessBits,
                    int offset)
Creates a MFAccessBits instance based on the given access bit bytes. The first three bytes starting from offset of the accessBits array are read as the sixth, seventh and eight byte of the sector trailer.

Parameters:
accessBits - the access bit bytes.
offset - the index of the first access bit byte in the array.
Throws:
java.lang.NullPointerException - if the accessBits array is null.
java.lang.ArrayIndexOutOfBoundsException - if the offset is < 0 or > accessBits.length-3.
java.lang.IllegalArgumentException - if the access bits are not set correctly.

MFAccessBits

public MFAccessBits(int accessBits)
Creates a MFAccessBits instance based on the given access bits. The integer is read so that the most significant byte defines the sector trailer byte 6. The least significant byte is ignored.

Parameters:
accessBits - the access bits as a integer.
Throws:
java.lang.IllegalArgumentException - if the access bits are not set correctly.
Method Detail

toByteArray

public byte[] toByteArray()
Returns the defined access bits as a byte array that can be used for writing the sector trailer. The length of the returned array is 3. The first byte corresponds with the sector trailer byte 6.

Returns:
the access bits as a byte array.

getAccessBitsForBlock

public int getAccessBitsForBlock(int blockIndex)
Returns access bits for specified block. The return value is a bit mask consisting of C1, C2 and C3.

Parameters:
blockIndex - index of the block.
Returns:
a bitmask defining the access conditions for the specified block.
Throws:
java.lang.IllegalArgumentException - if the blockIndex is not valid.

setAccessBitsForBlock

public void setAccessBitsForBlock(int accessBits,
                                  int blockIndex)
Sets the access bits for the specified data block(s). The accessBits value must be a bitmask that is a combination of C1, C2 and C3.

Parameters:
accessBits - a bitmask defining the access bits.
blockIndex - index of the block.
Throws:
java.lang.IllegalArgumentException - if the blockIndex is not valid.

setAccessBitsForBlock

public void setAccessBitsForBlock(boolean c1,
                                  boolean c2,
                                  boolean c3,
                                  int blockIndex)
Sets the access bits for the specified data block(s).

Parameters:
c1 - the access bit C1.
c2 - the access bit C2.
c3 - the access bit C3.
blockIndex - index of the block.
Throws:
java.lang.IllegalArgumentException - if the blockIndex is not valid.

setAccessBitsForDataBlocks

public void setAccessBitsForDataBlocks(boolean c1,
                                       boolean c2,
                                       boolean c3)
Sets the access bits for all data blocks.

Parameters:
c1 - the access bit C1.
c2 - the access bit C2.
c3 - the access bit C3.

setAccessBitsForTrailer

public void setAccessBitsForTrailer(boolean c1,
                                    boolean c2,
                                    boolean c3)
Sets the accessbits for the sector trailer block.

Parameters:
c1 - the access bit C1.
c2 - the access bit C2.
c3 - the access bit C3.

canWrite

public boolean canWrite(int keyType,
                        int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can write data to block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be written and false if not.

canRead

public boolean canRead(int keyType,
                       int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can read data from the block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be read and false if not.

canIncrement

public boolean canIncrement(int keyType,
                            int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can increment the value in block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be written and false if not.

canDecrement

public boolean canDecrement(int keyType,
                            int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can decrement the value in block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be written and false if not.

canTransfer

public boolean canTransfer(int keyType,
                           int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can transfer the value to block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be written and false if not.

canRestore

public boolean canRestore(int keyType,
                          int blockIndex)
Tests whether the access conditions are set so that with a authentication key of type keyType one can restore the value from block defined by blockIndex. For the trailer this method will always return false.

Parameters:
keyType - the authentication key type.
blockIndex - index of the block.
Returns:
true if data can be written and false if not.

canReadKeyA

public boolean canReadKeyA(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can read the Key A from sector trailer. This method always returns false

Parameters:
keyType - the authentication key type.
Returns:
false as reading Key A is never allowed.

canWriteKeyA

public boolean canWriteKeyA(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can write the Key A in sector trailer.

Parameters:
keyType - the authentication key type.
Returns:
true if Key A can be written and false if not.

canReadKeyB

public boolean canReadKeyB(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can read the Key A from sector trailer.

Parameters:
keyType - the authentication key type.
Returns:
true if Key B can be read and false if not.

canWriteKeyB

public boolean canWriteKeyB(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can write the Key B in sector trailer.

Parameters:
keyType - the authentication key type.
Returns:
true if Key B can be written and false if not.

canReadAccessBits

public boolean canReadAccessBits(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can read the access bits from sector trailer. Access bits can always be read with Key A.

Parameters:
keyType - the authentication key type.
Returns:
true if access bits can be read and false if not.

canWriteAccessBits

public boolean canWriteAccessBits(int keyType)
Tests whether the access conditions are set so that with a authentication key of type keyType one can read the access bits from sector trailer. Access bits can always be read with Key A.

Parameters:
keyType - the authentication key type.
Returns:
true if access bits can be read and false if not.

equals

public boolean equals(java.lang.Object obj)
Tests if the given object equal to this object. Two MFAccessBits objects are equal if the access bits information is the same.

Overrides:
equals in class java.lang.Object
Returns:
true if the objects are equal and false if not.

hashCode

public int hashCode()
Returns a hashcode for this MFAccessBits object. The hashcode is generated by combining each block's access bit bitmask.

Overrides:
hashCode in class java.lang.Object
Returns:
a hashcode for this object.
See Also:
Object.hashCode()

Nokia Extensions for JSR-257

Copyright � 2010 Nokia Corporation. All Rights Reserved.
Java is a trademark of Sun Microsystems, Inc.