Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRC tool #275

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Mifare Classic Tool/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
android:icon="@drawable/clone_uid"
android:label="@string/title_activity_clone_uid" >
</activity>
<activity android:name=".Activities.CrcCalc"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_crc_calc" >
</activity>

</application>

Expand Down
88 changes: 88 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/AlgoParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package crccalc;

/**
* Created by anthony on 11.05.2017.
* https://github.com/meetanthony/crcjava
*/
public class AlgoParams
{

public AlgoParams(String name, int hashSize, long poly, long init, boolean refIn, boolean refOut, long xorOut, long check)
{
Name = name;
Check = check;
Init = init;
Poly = poly;
RefIn = refIn;
RefOut = refOut;
XorOut = xorOut;
HashSize = hashSize;
}

/// <summary>
/// This field is not strictly part of the definition, and, in
/// the event of an inconsistency between this field and the other
/// field, the other fields take precedence.This field is a check
/// value that can be used as a weak validator of implementations of
/// the algorithm.The field contains the checksum obtained when the
/// ASCII string "123456789" is fed through the specified algorithm
/// (i.e. 313233... (hexadecimal)).
/// </summary>
public long Check;

/// <summary>
/// This is hash size.
/// </summary>
public int HashSize;

/// <summary>
/// This parameter specifies the initial value of the register
/// when the algorithm starts.This is the value that is to be assigned
/// to the register in the direct table algorithm. In the table
/// algorithm, we may think of the register always commencing with the
/// value zero, and this value being XORed into the register after the
/// N'th bit iteration. This parameter should be specified as a
/// hexadecimal number.
/// </summary>
public long Init;

/// <summary>
/// This is a name given to the algorithm. A string value.
/// </summary>
public String Name;

/// <summary>
/// This parameter is the poly. This is a binary value that
/// should be specified as a hexadecimal number.The top bit of the
/// poly should be omitted.For example, if the poly is 10110, you
/// should specify 06. An important aspect of this parameter is that it
/// represents the unreflected poly; the bottom bit of this parameter
/// is always the LSB of the divisor during the division regardless of
/// whether the algorithm being modelled is reflected.
/// </summary>
public long Poly;

/// <summary>
/// This is a boolean parameter. If it is FALSE, input bytes are
/// processed with bit 7 being treated as the most significant bit
/// (MSB) and bit 0 being treated as the least significant bit.If this
/// parameter is FALSE, each byte is reflected before being processed.
/// </summary>
public boolean RefIn;

/// <summary>
/// This is a boolean parameter. If it is set to FALSE, the
/// final value in the register is fed into the XOROUT stage directly,
/// otherwise, if this parameter is TRUE, the final register value is
/// reflected first.
/// </summary>
public boolean RefOut;

/// <summary>
/// This is an W-bit value that should be specified as a
/// hexadecimal number.It is XORed to the final register value (after
/// the REFOUT) stage before the value is returned as the official
/// checksum.
/// </summary>
public long XorOut;
}
57 changes: 57 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/Crc16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package crccalc;

/**
* Created by anthony on 15.05.2017.
*/
public class Crc16 {
public static AlgoParams Crc16CcittFalse = new AlgoParams("CRC-16/CCITT-FALSE", 16, 0x1021, 0xFFFF, false, false, 0x0, 0x29B1);
public static AlgoParams Crc16Arc = new AlgoParams("CRC-16/ARC", 16, 0x8005, 0x0, true, true, 0x0, 0xBB3D);
public static AlgoParams Crc16AugCcitt = new AlgoParams("CRC-16/AUG-CCITT", 16, 0x1021, 0x1D0F, false, false, 0x0, 0xE5CC);
public static AlgoParams Crc16Buypass = new AlgoParams("CRC-16/BUYPASS", 16, 0x8005, 0x0, false, false, 0x0, 0xFEE8);
public static AlgoParams Crc16Cdma2000 = new AlgoParams("CRC-16/CDMA2000", 16, 0xC867, 0xFFFF, false, false, 0x0, 0x4C06);
public static AlgoParams Crc16Dds110 = new AlgoParams("CRC-16/DDS-110", 16, 0x8005, 0x800D, false, false, 0x0, 0x9ECF);
public static AlgoParams Crc16DectR = new AlgoParams("CRC-16/DECT-R", 16, 0x589, 0x0, false, false, 0x1, 0x7E);
public static AlgoParams Crc16DectX = new AlgoParams("CRC-16/DECT-X", 16, 0x589, 0x0, false, false, 0x0, 0x7F);
public static AlgoParams Crc16Dnp = new AlgoParams("CRC-16/DNP", 16, 0x3D65, 0x0, true, true, 0xFFFF, 0xEA82);
public static AlgoParams Crc16En13757 = new AlgoParams("CRC-16/EN-13757", 16, 0x3D65, 0x0, false, false, 0xFFFF, 0xC2B7);
public static AlgoParams Crc16Genibus = new AlgoParams("CRC-16/GENIBUS", 16, 0x1021, 0xFFFF, false, false, 0xFFFF, 0xD64E);
public static AlgoParams Crc16Maxim = new AlgoParams("CRC-16/MAXIM", 16, 0x8005, 0x0, true, true, 0xFFFF, 0x44C2);
public static AlgoParams Crc16Mcrf4Xx = new AlgoParams("CRC-16/MCRF4XX", 16, 0x1021, 0xFFFF, true, true, 0x0, 0x6F91);
public static AlgoParams Crc16Riello = new AlgoParams("CRC-16/RIELLO", 16, 0x1021, 0xB2AA, true, true, 0x0, 0x63D0);
public static AlgoParams Crc16T10Dif = new AlgoParams("CRC-16/T10-DIF", 16, 0x8BB7, 0x0, false, false, 0x0, 0xD0DB);
public static AlgoParams Crc16Teledisk = new AlgoParams("CRC-16/TELEDISK", 16, 0xA097, 0x0, false, false, 0x0, 0xFB3);
public static AlgoParams Crc16Tms37157 = new AlgoParams("CRC-16/TMS37157", 16, 0x1021, 0x89EC, true, true, 0x0, 0x26B1);
public static AlgoParams Crc16Usb = new AlgoParams("CRC-16/USB", 16, 0x8005, 0xFFFF, true, true, 0xFFFF, 0xB4C8);
public static AlgoParams CrcA = new AlgoParams("CRC-A", 16, 0x1021, 0xc6c6, true, true, 0x0, 0xBF05);
public static AlgoParams Crc16Kermit = new AlgoParams("CRC-16/KERMIT", 16, 0x1021, 0x0, true, true, 0x0, 0x2189);
public static AlgoParams Crc16Modbus = new AlgoParams("CRC-16/MODBUS", 16, 0x8005, 0xFFFF, true, true, 0x0, 0x4B37);
public static AlgoParams Crc16X25 = new AlgoParams("CRC-16/X-25", 16, 0x1021, 0xFFFF, true, true, 0xFFFF, 0x906E);
public static AlgoParams Crc16Xmodem = new AlgoParams("CRC-16/XMODEM", 16, 0x1021, 0x0, false, false, 0x0, 0x31C3);

public static final AlgoParams[] Params = new AlgoParams[]
{
Crc16CcittFalse,
Crc16Arc,
Crc16AugCcitt,
Crc16Buypass,
Crc16Cdma2000,
Crc16Dds110,
Crc16DectR,
Crc16DectX,
Crc16Dnp,
Crc16En13757,
Crc16Genibus,
Crc16Maxim,
Crc16Mcrf4Xx,
Crc16Riello,
Crc16T10Dif,
Crc16Teledisk,
Crc16Tms37157,
Crc16Usb,
CrcA,
Crc16Kermit,
Crc16Modbus,
Crc16X25,
Crc16Xmodem,
};
}
20 changes: 20 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/Crc32.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package crccalc;

/**
* Created by anthony on 15.05.2017.
*/
public class Crc32 {
public static AlgoParams Crc32 = new AlgoParams("CRC-32", 32, 0x04C11DB7L, 0xFFFFFFFFL, true, true, 0xFFFFFFFFL, 0xCBF43926L);
public static AlgoParams Crc32Bzip2 = new AlgoParams("CRC-32/BZIP2", 32, 0x04C11DB7L, 0xFFFFFFFFL, false, false, 0xFFFFFFFFL, 0xFC891918L);
public static AlgoParams Crc32C = new AlgoParams("CRC-32C", 32, 0x1EDC6F41L, 0xFFFFFFFFL, true, true, 0xFFFFFFFFL, 0xE3069283L);
public static AlgoParams Crc32D = new AlgoParams("CRC-32D", 32, 0xA833982BL, 0xFFFFFFFFL, true, true, 0xFFFFFFFFL, 0x87315576L);
public static AlgoParams Crc32Jamcrc = new AlgoParams("CRC-32/JAMCRC", 32, 0x04C11DB7L, 0xFFFFFFFFL, true, true, 0x00000000L, 0x340BC6D9L);
public static AlgoParams Crc32Mpeg2 = new AlgoParams("CRC-32/MPEG-2", 32, 0x04C11DB7L, 0xFFFFFFFFL, false, false, 0x00000000L, 0x0376E6E7L);
public static AlgoParams Crc32Posix = new AlgoParams("CRC-32/POSIX", 32, 0x04C11DB7L, 0x00000000L, false, false, 0xFFFFFFFFL, 0x765E7680L);
public static AlgoParams Crc32Q = new AlgoParams("CRC-32Q", 32, 0x814141ABL, 0x00000000L, false, false, 0x00000000L, 0x3010BF7FL);
public static AlgoParams Crc32Xfer = new AlgoParams("CRC-32/XFER", 32, 0x000000AFL, 0x00000000L, false, false, 0x00000000L, 0xBD0BE338L);

public static final AlgoParams[] Params = new AlgoParams[]{
Crc32, Crc32Bzip2, Crc32C, Crc32D, Crc32Jamcrc, Crc32Mpeg2, Crc32Posix, Crc32Q, Crc32Xfer
};
}
12 changes: 12 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/Crc64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package crccalc;

public class Crc64 {
public static AlgoParams Crc64 = new AlgoParams("CRC-64",64, 0x42F0E1EBA9EA3693L, 0x00000000L, false, false, 0x00000000L, 0x6C40DF5F0B497347L);
public static AlgoParams Crc64We = new AlgoParams("CRC-64/WE", 64, 0x42F0E1EBA9EA3693L, 0xFFFFFFFFFFFFFFFFL, false, false, 0xFFFFFFFFFFFFFFFFL,0x62EC59E3F1A4F00AL);
public static AlgoParams Crc64Xz = new AlgoParams("CRC-64/XZ", 64, 0x42F0E1EBA9EA3693L, 0xFFFFFFFFFFFFFFFFL, true, true, 0xFFFFFFFFFFFFFFFFL,0x995DC9BBDF1939FAL);


public static final AlgoParams[] Params = new AlgoParams[]{
Crc64, Crc64We, Crc64Xz
};
}
21 changes: 21 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/Crc8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package crccalc;

/**
* Created by anthony on 15.05.2017.
*/
public class Crc8 {
public static AlgoParams Crc8 = new AlgoParams("CRC-8", 8, 0x7, 0x0, false, false, 0x0, 0xF4);
public static AlgoParams Crc8Cdma2000 = new AlgoParams("CRC-8/CDMA2000", 8, 0x9B, 0xFF, false, false, 0x0, 0xDA);
public static AlgoParams Crc8Darc = new AlgoParams("CRC-8/DARC", 8, 0x39, 0x0, true, true, 0x0, 0x15);
public static AlgoParams Crc8DvbS2 = new AlgoParams("CRC-8/DVB-S2", 8, 0xD5, 0x0, false, false, 0x0, 0xBC);
public static AlgoParams Crc8Ebu = new AlgoParams("CRC-8/EBU", 8, 0x1D, 0xFF, true, true, 0x0, 0x97);
public static AlgoParams Crc8ICode = new AlgoParams("CRC-8/I-CODE", 8, 0x1D, 0xFD, false, false, 0x0, 0x7E);
public static AlgoParams Crc8Itu = new AlgoParams("CRC-8/ITU", 8, 0x7, 0x0, false, false, 0x55, 0xA1);
public static AlgoParams Crc8Maxim = new AlgoParams("CRC-8/MAXIM", 8, 0x31, 0x0, true, true, 0x0, 0xA1);
public static AlgoParams Crc8Rohc = new AlgoParams("CRC-8/ROHC", 8, 0x7, 0xFF, true, true, 0x0, 0xD0);
public static AlgoParams Crc8Wcdma = new AlgoParams("CRC-8/WCDMA", 8, 0x9B, 0x0, true, true, 0x0, 0x25);

public static final AlgoParams[] Params = new AlgoParams[]{
Crc8, Crc8Cdma2000, Crc8Darc, Crc8DvbS2, Crc8Ebu, Crc8ICode, Crc8Itu, Crc8Maxim, Crc8Rohc, Crc8Wcdma
};
}
91 changes: 91 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/CrcCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package crccalc;

/**
* Created by anthony on 11.05.2017.
*/
public class CrcCalculator {

public AlgoParams Parameters;
public byte HashSize = 8;
private long _mask = 0xFFFFFFFFFFFFFFFFL;
private long[] _table = new long[256];

public static final byte[] TestBytes = new byte[]{49,50,51,52,53,54,55,56,57};

CrcCalculator(AlgoParams params)
{
Parameters = params;

HashSize = (byte) params.HashSize;
if (HashSize < 64)
{
_mask = (1L << HashSize) - 1;
}

CreateTable();
}

public long Calc(byte[] data, int offset, int length)
{
long init = Parameters.RefOut ? CrcHelper.ReverseBits(Parameters.Init, HashSize) : Parameters.Init;
long hash = ComputeCrc(init, data, offset, length);
return (hash ^ Parameters.XorOut) & _mask;
}

private long ComputeCrc(long init, byte[] data, int offset, int length)
{
long crc = init;

if (Parameters.RefOut)
{
for (int i = offset; i < offset + length; i++)
{
crc = (_table[(int)((crc ^ data[i]) & 0xFF)] ^ (crc >>> 8));
crc &= _mask;
}
}
else
{
int toRight = (HashSize - 8);
toRight = toRight < 0 ? 0 : toRight;
for (int i = offset; i < offset + length; i++)
{
crc = (_table[(int)(((crc >> toRight) ^ data[i]) & 0xFF)] ^ (crc << 8));
crc &= _mask;
}
}

return crc;
}

private void CreateTable()
{
for (int i = 0; i < _table.length; i++)
_table[i] = CreateTableEntry(i);
}

private long CreateTableEntry(int index)
{
long r = (long)index;

if (Parameters.RefIn)
r = CrcHelper.ReverseBits(r, HashSize);
else if (HashSize > 8)
r <<= (HashSize - 8);

long lastBit = (1L << (HashSize - 1));

for (int i = 0; i < 8; i++)
{
if ((r & lastBit) != 0)
r = ((r << 1) ^ Parameters.Poly);
else
r <<= 1;
}

if (Parameters.RefOut)
r = CrcHelper.ReverseBits(r, HashSize);

return r & _mask;
}
}
20 changes: 20 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/CrcHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package crccalc;

/**
* Created by anthony on 13.05.2017.
*/
public class CrcHelper {

static long ReverseBits(long ul, int valueLength)
{
long newValue = 0;

for (int i = valueLength - 1; i >= 0; i--)
{
newValue |= (ul & 1) << i;
ul >>= 1;
}

return newValue;
}
}
39 changes: 39 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package crccalc;

import android.util.Log;

public class Main {

public static void main(String[] args) {
Check(Crc8.Params);

Check(Crc16.Params);

Check(Crc32.Params);

Check(Crc64.Params);
}

private static void Check(AlgoParams[] params) {
for (AlgoParams param : params) {
CrcCalculator calculator = new CrcCalculator(param);
long result = calculator.Calc(CrcCalculator.TestBytes, 0, CrcCalculator.TestBytes.length);
Log.i(calculator.Parameters.Name, Long.toHexString(result).toUpperCase());
if (result != calculator.Parameters.Check)
Log.d("LOG:", calculator.Parameters.Name + " - BAD ALGO!!! " + Long.toHexString(result).toUpperCase());
}
}

public static StringBuilder Calculate(AlgoParams[] params) {
StringBuilder res = new StringBuilder();
for (AlgoParams param : params) {
CrcCalculator calculator = new CrcCalculator(param);
long result = calculator.Calc(CrcCalculator.TestBytes, 0, CrcCalculator.TestBytes.length);
if (result != calculator.Parameters.Check) {
Log.d("LOG:", calculator.Parameters.Name + " - BAD ALGO!!! " + Long.toHexString(result).toUpperCase());
}
res.append(String.format("%s : %s", calculator.Parameters.Name, Long.toHexString(result).toUpperCase())).append("\n");
}
return res;
}
}
21 changes: 21 additions & 0 deletions Mifare Classic Tool/app/src/main/java/crccalc/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Anton Isakov http://crccalc.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading