mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-20 21:33:19 -07:00
Merge branch 'master' into topaz
Conflicts: client/Makefile client/cmdhf.c client/cmdhf14a.c
This commit is contained in:
commit
db2b81ba11
119 changed files with 20403 additions and 4640 deletions
|
@ -25,6 +25,7 @@ CC = $(CROSS)gcc
|
|||
AS = $(CROSS)as
|
||||
LD = $(CROSS)ld
|
||||
OBJCOPY = $(CROSS)objcopy
|
||||
GZIP=gzip
|
||||
|
||||
OBJDIR = obj
|
||||
|
||||
|
@ -61,8 +62,8 @@ DETECTED_OS=Windows
|
|||
endif
|
||||
|
||||
|
||||
# Also search prerequisites in the common directory (for usb.c), and the fpga directory (for fpga.bit)
|
||||
VPATH = . ../common/ ../fpga/
|
||||
# Also search prerequisites in the common directory (for usb.c), the fpga directory (for fpga.bit), and the zlib directory
|
||||
VPATH = . ../common ../fpga ../zlib
|
||||
|
||||
INCLUDES = ../include/proxmark3.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/usb_cmd.h $(APP_INCLUDES)
|
||||
|
||||
|
@ -71,9 +72,9 @@ LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-gc-sections -n
|
|||
|
||||
LIBS = -lgcc
|
||||
|
||||
THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(THUMBSRC))
|
||||
ARMOBJ = $(ARMSRC:%.c=$(OBJDIR)/%.o)
|
||||
ASMOBJ = $(patsubst %.s,$(OBJDIR)/%.o,$(ASMSRC))
|
||||
THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(THUMBSRC)))
|
||||
ARMOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(ARMSRC)))
|
||||
ASMOBJ = $(patsubst %.s,$(OBJDIR)/%.o,$(notdir $(ASMSRC)))
|
||||
VERSIONOBJ = $(OBJDIR)/version.o
|
||||
|
||||
$(THUMBOBJ): $(OBJDIR)/%.o: %.c $(INCLUDES)
|
||||
|
@ -109,6 +110,7 @@ DEPENDENCY_FILES = $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(THUMBSRC))) \
|
|||
$(patsubst %.s,$(OBJDIR)/%.d,$(notdir $(ASMSRC)))
|
||||
|
||||
$(DEPENDENCY_FILES): Makefile ../common/Makefile.common
|
||||
|
||||
$(patsubst %.o,%.d,$(THUMBOBJ) $(ARMOBJ)): $(OBJDIR)/%.d: %.c
|
||||
@$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
|
||||
$(patsubst %.o,%.d,$(ASMOBJ)):$(OBJDIR)/%.d: %.s
|
||||
|
|
|
@ -8,38 +8,54 @@
|
|||
|
||||
#include "crc16.h"
|
||||
|
||||
|
||||
unsigned short update_crc16( unsigned short crc, unsigned char c )
|
||||
{
|
||||
unsigned short i, v, tcrc = 0;
|
||||
unsigned short i, v, tcrc = 0;
|
||||
|
||||
v = (crc ^ c) & 0xff;
|
||||
for (i = 0; i < 8; i++) {
|
||||
tcrc = ( (tcrc ^ v) & 1 ) ? ( tcrc >> 1 ) ^ 0x8408 : tcrc >> 1;
|
||||
v >>= 1;
|
||||
}
|
||||
v = (crc ^ c) & 0xff;
|
||||
for (i = 0; i < 8; i++) {
|
||||
tcrc = ( (tcrc ^ v) & 1 ) ? ( tcrc >> 1 ) ^ 0x8408 : tcrc >> 1;
|
||||
v >>= 1;
|
||||
}
|
||||
|
||||
return ((crc >> 8) ^ tcrc)&0xffff;
|
||||
return ((crc >> 8) ^ tcrc)&0xffff;
|
||||
}
|
||||
|
||||
uint16_t crc16(uint8_t const *message, int length, uint16_t remainder, uint16_t polynomial) {
|
||||
|
||||
if (length == 0)
|
||||
return (~remainder);
|
||||
|
||||
for (int byte = 0; byte < length; ++byte) {
|
||||
remainder ^= (message[byte] << 8);
|
||||
for (uint8_t bit = 8; bit > 0; --bit) {
|
||||
if (remainder & 0x8000) {
|
||||
remainder = (remainder << 1) ^ polynomial;
|
||||
} else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return remainder;
|
||||
|
||||
if (length == 0) return (~remainder);
|
||||
|
||||
for (int byte = 0; byte < length; ++byte) {
|
||||
remainder ^= (message[byte] << 8);
|
||||
for (uint8_t bit = 8; bit > 0; --bit) {
|
||||
if (remainder & 0x8000) {
|
||||
remainder = (remainder << 1) ^ polynomial;
|
||||
} else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return remainder;
|
||||
}
|
||||
|
||||
uint16_t crc16_ccitt(uint8_t const *message, int length) {
|
||||
return crc16(message, length, 0xffff, 0x1021);
|
||||
return crc16(message, length, 0xffff, 0x1021);
|
||||
}
|
||||
|
||||
uint16_t crc16_ccitt_kermit(uint8_t const *message, int length) {
|
||||
return bit_reverse_uint16(crc16(message, length, 0x0000, 0x1021));
|
||||
}
|
||||
|
||||
uint16_t bit_reverse_uint16 (uint16_t value) {
|
||||
const uint16_t mask0 = 0x5555;
|
||||
const uint16_t mask1 = 0x3333;
|
||||
const uint16_t mask2 = 0x0F0F;
|
||||
const uint16_t mask3 = 0x00FF;
|
||||
|
||||
value = (((~mask0) & value) >> 1) | ((mask0 & value) << 1);
|
||||
value = (((~mask1) & value) >> 2) | ((mask1 & value) << 2);
|
||||
value = (((~mask2) & value) >> 4) | ((mask2 & value) << 4);
|
||||
value = (((~mask3) & value) >> 8) | ((mask3 & value) << 8);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
|
|
@ -12,4 +12,6 @@
|
|||
unsigned short update_crc16(unsigned short crc, unsigned char c);
|
||||
uint16_t crc16(uint8_t const *message, int length, uint16_t remainder, uint16_t polynomial);
|
||||
uint16_t crc16_ccitt(uint8_t const *message, int length);
|
||||
uint16_t crc16_ccitt_kermit(uint8_t const *message, int length);
|
||||
uint16_t bit_reverse_uint16 (uint16_t value);
|
||||
#endif
|
||||
|
|
85
common/crc64.c
Normal file
85
common/crc64.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "crc64.h"
|
||||
|
||||
#define CRC64_ISO_PRESET 0xFFFFFFFFFFFFFFFF
|
||||
#define CRC64_ECMA_PRESET 0x0000000000000000
|
||||
|
||||
const uint64_t crc64_table[] = {
|
||||
0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5,
|
||||
0x493366450E42ECDF, 0x0BC387AEA7A8DA4C, 0xCCD2A5925D9681F9, 0x8E224479F47CB76A,
|
||||
0x9266CC8A1C85D9BE, 0xD0962D61B56FEF2D, 0x17870F5D4F51B498, 0x5577EEB6E6BB820B,
|
||||
0xDB55AACF12C73561, 0x99A54B24BB2D03F2, 0x5EB4691841135847, 0x1C4488F3E8F96ED4,
|
||||
0x663D78FF90E185EF, 0x24CD9914390BB37C, 0xE3DCBB28C335E8C9, 0xA12C5AC36ADFDE5A,
|
||||
0x2F0E1EBA9EA36930, 0x6DFEFF5137495FA3, 0xAAEFDD6DCD770416, 0xE81F3C86649D3285,
|
||||
0xF45BB4758C645C51, 0xB6AB559E258E6AC2, 0x71BA77A2DFB03177, 0x334A9649765A07E4,
|
||||
0xBD68D2308226B08E, 0xFF9833DB2BCC861D, 0x388911E7D1F2DDA8, 0x7A79F00C7818EB3B,
|
||||
0xCC7AF1FF21C30BDE, 0x8E8A101488293D4D, 0x499B3228721766F8, 0x0B6BD3C3DBFD506B,
|
||||
0x854997BA2F81E701, 0xC7B97651866BD192, 0x00A8546D7C558A27, 0x4258B586D5BFBCB4,
|
||||
0x5E1C3D753D46D260, 0x1CECDC9E94ACE4F3, 0xDBFDFEA26E92BF46, 0x990D1F49C77889D5,
|
||||
0x172F5B3033043EBF, 0x55DFBADB9AEE082C, 0x92CE98E760D05399, 0xD03E790CC93A650A,
|
||||
0xAA478900B1228E31, 0xE8B768EB18C8B8A2, 0x2FA64AD7E2F6E317, 0x6D56AB3C4B1CD584,
|
||||
0xE374EF45BF6062EE, 0xA1840EAE168A547D, 0x66952C92ECB40FC8, 0x2465CD79455E395B,
|
||||
0x3821458AADA7578F, 0x7AD1A461044D611C, 0xBDC0865DFE733AA9, 0xFF3067B657990C3A,
|
||||
0x711223CFA3E5BB50, 0x33E2C2240A0F8DC3, 0xF4F3E018F031D676, 0xB60301F359DBE0E5,
|
||||
0xDA050215EA6C212F, 0x98F5E3FE438617BC, 0x5FE4C1C2B9B84C09, 0x1D14202910527A9A,
|
||||
0x93366450E42ECDF0, 0xD1C685BB4DC4FB63, 0x16D7A787B7FAA0D6, 0x5427466C1E109645,
|
||||
0x4863CE9FF6E9F891, 0x0A932F745F03CE02, 0xCD820D48A53D95B7, 0x8F72ECA30CD7A324,
|
||||
0x0150A8DAF8AB144E, 0x43A04931514122DD, 0x84B16B0DAB7F7968, 0xC6418AE602954FFB,
|
||||
0xBC387AEA7A8DA4C0, 0xFEC89B01D3679253, 0x39D9B93D2959C9E6, 0x7B2958D680B3FF75,
|
||||
0xF50B1CAF74CF481F, 0xB7FBFD44DD257E8C, 0x70EADF78271B2539, 0x321A3E938EF113AA,
|
||||
0x2E5EB66066087D7E, 0x6CAE578BCFE24BED, 0xABBF75B735DC1058, 0xE94F945C9C3626CB,
|
||||
0x676DD025684A91A1, 0x259D31CEC1A0A732, 0xE28C13F23B9EFC87, 0xA07CF2199274CA14,
|
||||
0x167FF3EACBAF2AF1, 0x548F120162451C62, 0x939E303D987B47D7, 0xD16ED1D631917144,
|
||||
0x5F4C95AFC5EDC62E, 0x1DBC74446C07F0BD, 0xDAAD56789639AB08, 0x985DB7933FD39D9B,
|
||||
0x84193F60D72AF34F, 0xC6E9DE8B7EC0C5DC, 0x01F8FCB784FE9E69, 0x43081D5C2D14A8FA,
|
||||
0xCD2A5925D9681F90, 0x8FDAB8CE70822903, 0x48CB9AF28ABC72B6, 0x0A3B7B1923564425,
|
||||
0x70428B155B4EAF1E, 0x32B26AFEF2A4998D, 0xF5A348C2089AC238, 0xB753A929A170F4AB,
|
||||
0x3971ED50550C43C1, 0x7B810CBBFCE67552, 0xBC902E8706D82EE7, 0xFE60CF6CAF321874,
|
||||
0xE224479F47CB76A0, 0xA0D4A674EE214033, 0x67C58448141F1B86, 0x253565A3BDF52D15,
|
||||
0xAB1721DA49899A7F, 0xE9E7C031E063ACEC, 0x2EF6E20D1A5DF759, 0x6C0603E6B3B7C1CA,
|
||||
0xF6FAE5C07D3274CD, 0xB40A042BD4D8425E, 0x731B26172EE619EB, 0x31EBC7FC870C2F78,
|
||||
0xBFC9838573709812, 0xFD39626EDA9AAE81, 0x3A28405220A4F534, 0x78D8A1B9894EC3A7,
|
||||
0x649C294A61B7AD73, 0x266CC8A1C85D9BE0, 0xE17DEA9D3263C055, 0xA38D0B769B89F6C6,
|
||||
0x2DAF4F0F6FF541AC, 0x6F5FAEE4C61F773F, 0xA84E8CD83C212C8A, 0xEABE6D3395CB1A19,
|
||||
0x90C79D3FEDD3F122, 0xD2377CD44439C7B1, 0x15265EE8BE079C04, 0x57D6BF0317EDAA97,
|
||||
0xD9F4FB7AE3911DFD, 0x9B041A914A7B2B6E, 0x5C1538ADB04570DB, 0x1EE5D94619AF4648,
|
||||
0x02A151B5F156289C, 0x4051B05E58BC1E0F, 0x87409262A28245BA, 0xC5B073890B687329,
|
||||
0x4B9237F0FF14C443, 0x0962D61B56FEF2D0, 0xCE73F427ACC0A965, 0x8C8315CC052A9FF6,
|
||||
0x3A80143F5CF17F13, 0x7870F5D4F51B4980, 0xBF61D7E80F251235, 0xFD913603A6CF24A6,
|
||||
0x73B3727A52B393CC, 0x31439391FB59A55F, 0xF652B1AD0167FEEA, 0xB4A25046A88DC879,
|
||||
0xA8E6D8B54074A6AD, 0xEA16395EE99E903E, 0x2D071B6213A0CB8B, 0x6FF7FA89BA4AFD18,
|
||||
0xE1D5BEF04E364A72, 0xA3255F1BE7DC7CE1, 0x64347D271DE22754, 0x26C49CCCB40811C7,
|
||||
0x5CBD6CC0CC10FAFC, 0x1E4D8D2B65FACC6F, 0xD95CAF179FC497DA, 0x9BAC4EFC362EA149,
|
||||
0x158E0A85C2521623, 0x577EEB6E6BB820B0, 0x906FC95291867B05, 0xD29F28B9386C4D96,
|
||||
0xCEDBA04AD0952342, 0x8C2B41A1797F15D1, 0x4B3A639D83414E64, 0x09CA82762AAB78F7,
|
||||
0x87E8C60FDED7CF9D, 0xC51827E4773DF90E, 0x020905D88D03A2BB, 0x40F9E43324E99428,
|
||||
0x2CFFE7D5975E55E2, 0x6E0F063E3EB46371, 0xA91E2402C48A38C4, 0xEBEEC5E96D600E57,
|
||||
0x65CC8190991CB93D, 0x273C607B30F68FAE, 0xE02D4247CAC8D41B, 0xA2DDA3AC6322E288,
|
||||
0xBE992B5F8BDB8C5C, 0xFC69CAB42231BACF, 0x3B78E888D80FE17A, 0x7988096371E5D7E9,
|
||||
0xF7AA4D1A85996083, 0xB55AACF12C735610, 0x724B8ECDD64D0DA5, 0x30BB6F267FA73B36,
|
||||
0x4AC29F2A07BFD00D, 0x08327EC1AE55E69E, 0xCF235CFD546BBD2B, 0x8DD3BD16FD818BB8,
|
||||
0x03F1F96F09FD3CD2, 0x41011884A0170A41, 0x86103AB85A2951F4, 0xC4E0DB53F3C36767,
|
||||
0xD8A453A01B3A09B3, 0x9A54B24BB2D03F20, 0x5D45907748EE6495, 0x1FB5719CE1045206,
|
||||
0x919735E51578E56C, 0xD367D40EBC92D3FF, 0x1476F63246AC884A, 0x568617D9EF46BED9,
|
||||
0xE085162AB69D5E3C, 0xA275F7C11F7768AF, 0x6564D5FDE549331A, 0x279434164CA30589,
|
||||
0xA9B6706FB8DFB2E3, 0xEB46918411358470, 0x2C57B3B8EB0BDFC5, 0x6EA7525342E1E956,
|
||||
0x72E3DAA0AA188782, 0x30133B4B03F2B111, 0xF7021977F9CCEAA4, 0xB5F2F89C5026DC37,
|
||||
0x3BD0BCE5A45A6B5D, 0x79205D0E0DB05DCE, 0xBE317F32F78E067B, 0xFCC19ED95E6430E8,
|
||||
0x86B86ED5267CDBD3, 0xC4488F3E8F96ED40, 0x0359AD0275A8B6F5, 0x41A94CE9DC428066,
|
||||
0xCF8B0890283E370C, 0x8D7BE97B81D4019F, 0x4A6ACB477BEA5A2A, 0x089A2AACD2006CB9,
|
||||
0x14DEA25F3AF9026D, 0x562E43B4931334FE, 0x913F6188692D6F4B, 0xD3CF8063C0C759D8,
|
||||
0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507
|
||||
};
|
||||
|
||||
void crc64 (const uint8_t *data, const size_t len, uint64_t *crc) {
|
||||
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
//uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
*crc = crc64_table[tableIndex] ^ (*crc << 8);
|
||||
}
|
||||
}
|
||||
|
||||
//suint8_t x = (c & 0xFF00000000000000 ) >> 56;
|
14
common/crc64.h
Normal file
14
common/crc64.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// CRC64 ECMA
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef __CRC64_H
|
||||
#define __CRC64_H
|
||||
|
||||
void crc64 (const uint8_t *data, const size_t len, uint64_t *crc) ;
|
||||
|
||||
#endif
|
604
common/lfdemod.c
604
common/lfdemod.c
|
@ -81,10 +81,8 @@ uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_
|
|||
// otherwise could be a void with no arguments
|
||||
//set defaults
|
||||
uint32_t i = 0;
|
||||
if (BitStream[1]>1){ //allow only 1s and 0s
|
||||
// PrintAndLog("no data found");
|
||||
return 0;
|
||||
}
|
||||
if (BitStream[1]>1) return 0; //allow only 1s and 0s
|
||||
|
||||
// 111111111 bit pattern represent start of frame
|
||||
// include 0 in front to help get start pos
|
||||
uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1};
|
||||
|
@ -115,216 +113,11 @@ uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_
|
|||
}
|
||||
|
||||
//by marshmellow
|
||||
//takes 3 arguments - clock, invert, maxErr as integers
|
||||
//attempts to demodulate ask while decoding manchester
|
||||
//prints binary found and saves in graphbuffer for further commands
|
||||
int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr)
|
||||
{
|
||||
size_t i;
|
||||
int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
|
||||
if (*clk==0 || start < 0) return -3;
|
||||
if (*invert != 1) *invert=0;
|
||||
uint8_t initLoopMax = 255;
|
||||
if (initLoopMax > *size) initLoopMax = *size;
|
||||
// Detect high and lows
|
||||
// 25% fuzz in case highs and lows aren't clipped [marshmellow]
|
||||
int high, low;
|
||||
if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1) return -2; //just noise
|
||||
|
||||
// PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
|
||||
int lastBit = 0; //set first clock check
|
||||
uint16_t bitnum = 0; //output counter
|
||||
uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
|
||||
if (*clk <= 32) tol=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
|
||||
size_t iii = 0;
|
||||
//if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
|
||||
if (!maxErr) initLoopMax = *clk * 2;
|
||||
uint16_t errCnt = 0, MaxBits = 512;
|
||||
uint16_t bestStart = start;
|
||||
uint16_t bestErrCnt = 0;
|
||||
// PrintAndLog("DEBUG - lastbit - %d",lastBit);
|
||||
// if best start position not already found by detect clock then
|
||||
if (start <= 0 || start > initLoopMax){
|
||||
bestErrCnt = maxErr+1;
|
||||
// loop to find first wave that works
|
||||
for (iii=0; iii < initLoopMax; ++iii){
|
||||
// if no peak skip
|
||||
if (BinStream[iii] < high && BinStream[iii] > low) continue;
|
||||
|
||||
lastBit = iii - *clk;
|
||||
// loop through to see if this start location works
|
||||
for (i = iii; i < *size; ++i) {
|
||||
if ((i-lastBit) > (*clk-tol) && (BinStream[i] >= high || BinStream[i] <= low)) {
|
||||
lastBit += *clk;
|
||||
} else if ((i-lastBit) > (*clk+tol)) {
|
||||
errCnt++;
|
||||
lastBit += *clk;
|
||||
}
|
||||
if ((i-iii) > (MaxBits * *clk) || errCnt > maxErr) break; //got plenty of bits or too many errors
|
||||
}
|
||||
//we got more than 64 good bits and not all errors
|
||||
if ((((i-iii)/ *clk) > (64)) && (errCnt<=maxErr)) {
|
||||
//possible good read
|
||||
if (!errCnt || errCnt < bestErrCnt){
|
||||
bestStart = iii; //set this as new best run
|
||||
bestErrCnt = errCnt;
|
||||
if (!errCnt) break; //great read - finish
|
||||
}
|
||||
}
|
||||
errCnt = 0;
|
||||
}
|
||||
}
|
||||
if (bestErrCnt > maxErr){
|
||||
*invert = bestStart;
|
||||
*clk = iii;
|
||||
return -1;
|
||||
}
|
||||
//best run is good enough set to best run and set overwrite BinStream
|
||||
lastBit = bestStart - *clk;
|
||||
errCnt = 0;
|
||||
for (i = bestStart; i < *size; ++i) {
|
||||
if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
|
||||
//high found and we are expecting a bar
|
||||
lastBit += *clk;
|
||||
BinStream[bitnum++] = *invert;
|
||||
} else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
|
||||
//low found and we are expecting a bar
|
||||
lastBit += *clk;
|
||||
BinStream[bitnum++] = *invert ^ 1;
|
||||
} else if ((i-lastBit)>(*clk+tol)){
|
||||
//should have hit a high or low based on clock!!
|
||||
//PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
|
||||
if (bitnum > 0) {
|
||||
BinStream[bitnum++] = 77;
|
||||
errCnt++;
|
||||
}
|
||||
lastBit += *clk;//skip over error
|
||||
}
|
||||
if (bitnum >= MaxBits) break;
|
||||
}
|
||||
*size = bitnum;
|
||||
return bestErrCnt;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//encode binary data into binary manchester
|
||||
int ManchesterEncode(uint8_t *BitStream, size_t size)
|
||||
{
|
||||
size_t modIdx=20000, i=0;
|
||||
if (size>modIdx) return -1;
|
||||
for (size_t idx=0; idx < size; idx++){
|
||||
BitStream[idx+modIdx++] = BitStream[idx];
|
||||
BitStream[idx+modIdx++] = BitStream[idx]^1;
|
||||
}
|
||||
for (; i<(size*2); i++){
|
||||
BitStream[i] = BitStream[i+20000];
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//take 10 and 01 and manchester decode
|
||||
//run through 2 times and take least errCnt
|
||||
int manrawdecode(uint8_t * BitStream, size_t *size)
|
||||
{
|
||||
uint16_t bitnum=0, MaxBits = 512, errCnt = 0;
|
||||
size_t i, ii;
|
||||
uint16_t bestErr = 1000, bestRun = 0;
|
||||
if (size == 0) return -1;
|
||||
for (ii=0;ii<2;++ii){
|
||||
for (i=ii; i<*size-2; i+=2)
|
||||
if (BitStream[i]==BitStream[i+1])
|
||||
errCnt++;
|
||||
|
||||
if (bestErr>errCnt){
|
||||
bestErr=errCnt;
|
||||
bestRun=ii;
|
||||
}
|
||||
errCnt=0;
|
||||
}
|
||||
if (bestErr<20){
|
||||
for (i=bestRun; i < *size-2; i+=2){
|
||||
if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
|
||||
BitStream[bitnum++]=0;
|
||||
} else if((BitStream[i] == 0) && BitStream[i+1] == 1){
|
||||
BitStream[bitnum++]=1;
|
||||
} else {
|
||||
BitStream[bitnum++]=77;
|
||||
}
|
||||
if(bitnum>MaxBits) break;
|
||||
}
|
||||
*size=bitnum;
|
||||
}
|
||||
return bestErr;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//take 01 or 10 = 1 and 11 or 00 = 0
|
||||
//check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
|
||||
//decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
|
||||
int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert)
|
||||
{
|
||||
uint16_t bitnum = 0;
|
||||
uint16_t errCnt = 0;
|
||||
size_t i = offset;
|
||||
uint16_t MaxBits=512;
|
||||
//if not enough samples - error
|
||||
if (*size < 51) return -1;
|
||||
//check for phase change faults - skip one sample if faulty
|
||||
uint8_t offsetA = 1, offsetB = 1;
|
||||
for (; i<48; i+=2){
|
||||
if (BitStream[i+1]==BitStream[i+2]) offsetA=0;
|
||||
if (BitStream[i+2]==BitStream[i+3]) offsetB=0;
|
||||
}
|
||||
if (!offsetA && offsetB) offset++;
|
||||
for (i=offset; i<*size-3; i+=2){
|
||||
//check for phase error
|
||||
if (BitStream[i+1]==BitStream[i+2]) {
|
||||
BitStream[bitnum++]=77;
|
||||
errCnt++;
|
||||
}
|
||||
if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
|
||||
BitStream[bitnum++]=1^invert;
|
||||
} else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
|
||||
BitStream[bitnum++]=invert;
|
||||
} else {
|
||||
BitStream[bitnum++]=77;
|
||||
errCnt++;
|
||||
}
|
||||
if(bitnum>MaxBits) break;
|
||||
}
|
||||
*size=bitnum;
|
||||
return errCnt;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
void askAmp(uint8_t *BitStream, size_t size)
|
||||
{
|
||||
int shift = 127;
|
||||
int shiftedVal=0;
|
||||
for(size_t i = 1; i<size; i++){
|
||||
if (BitStream[i]-BitStream[i-1]>=30) //large jump up
|
||||
shift=127;
|
||||
else if(BitStream[i]-BitStream[i-1]<=-20) //large jump down
|
||||
shift=-127;
|
||||
|
||||
shiftedVal=BitStream[i]+shift;
|
||||
|
||||
if (shiftedVal>255)
|
||||
shiftedVal=255;
|
||||
else if (shiftedVal<0)
|
||||
shiftedVal=0;
|
||||
BitStream[i-1] = shiftedVal;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// demodulates strong heavily clipped samples
|
||||
//demodulates strong heavily clipped samples
|
||||
int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int high, int low)
|
||||
{
|
||||
size_t bitCnt=0, smplCnt=0, errCnt=0;
|
||||
uint8_t waveHigh = 0;
|
||||
//PrintAndLog("clk: %d", clk);
|
||||
for (size_t i=0; i < *size; i++){
|
||||
if (BinStream[i] >= high && waveHigh){
|
||||
smplCnt++;
|
||||
|
@ -335,7 +128,7 @@ int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int
|
|||
if (smplCnt > clk-(clk/4)-1) { //full clock
|
||||
if (smplCnt > clk + (clk/4)+1) { //too many samples
|
||||
errCnt++;
|
||||
BinStream[bitCnt++]=77;
|
||||
BinStream[bitCnt++]=7;
|
||||
} else if (waveHigh) {
|
||||
BinStream[bitCnt++] = invert;
|
||||
BinStream[bitCnt++] = invert;
|
||||
|
@ -371,111 +164,79 @@ int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int
|
|||
}
|
||||
|
||||
//by marshmellow
|
||||
//takes 3 arguments - clock, invert and maxErr as integers
|
||||
//attempts to demodulate ask only
|
||||
int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp)
|
||||
void askAmp(uint8_t *BitStream, size_t size)
|
||||
{
|
||||
for(size_t i = 1; i<size; i++){
|
||||
if (BitStream[i]-BitStream[i-1]>=30) //large jump up
|
||||
BitStream[i]=127;
|
||||
else if(BitStream[i]-BitStream[i-1]<=-20) //large jump down
|
||||
BitStream[i]=-127;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
|
||||
int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType)
|
||||
{
|
||||
if (*size==0) return -1;
|
||||
int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
|
||||
if (*clk==0 || start < 0) return -1;
|
||||
int start = DetectASKClock(BinStream, *size, clk, maxErr); //clock default
|
||||
if (*clk==0 || start < 0) return -3;
|
||||
if (*invert != 1) *invert = 0;
|
||||
if (amp==1) askAmp(BinStream, *size);
|
||||
|
||||
uint8_t initLoopMax = 255;
|
||||
if (initLoopMax > *size) initLoopMax=*size;
|
||||
if (initLoopMax > *size) initLoopMax = *size;
|
||||
// Detect high and lows
|
||||
//25% clip in case highs and lows aren't clipped [marshmellow]
|
||||
int high, low;
|
||||
if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1)
|
||||
return -1; //just noise
|
||||
return -2; //just noise
|
||||
|
||||
size_t errCnt = 0;
|
||||
// if clean clipped waves detected run alternate demod
|
||||
if (DetectCleanAskWave(BinStream, *size, high, low))
|
||||
return cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
|
||||
if (DetectCleanAskWave(BinStream, *size, high, low)) {
|
||||
errCnt = cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
|
||||
if (askType) //askman
|
||||
return manrawdecode(BinStream, size, 0);
|
||||
else //askraw
|
||||
return errCnt;
|
||||
}
|
||||
|
||||
int lastBit = 0; //set first clock check - can go negative
|
||||
size_t i, iii = 0;
|
||||
size_t errCnt = 0, bitnum = 0; //output counter
|
||||
int lastBit; //set first clock check - can go negative
|
||||
size_t i, bitnum = 0; //output counter
|
||||
uint8_t midBit = 0;
|
||||
size_t bestStart = start, bestErrCnt = 0; //(*size/1000);
|
||||
uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
|
||||
if (*clk <= 32) tol = 1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
|
||||
size_t MaxBits = 1024;
|
||||
lastBit = start - *clk;
|
||||
|
||||
//if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
|
||||
if (!maxErr) initLoopMax = *clk * 2;
|
||||
//if best start not already found by detectclock
|
||||
if (start <= 0 || start > initLoopMax){
|
||||
bestErrCnt = maxErr+1;
|
||||
//PrintAndLog("DEBUG - lastbit - %d",lastBit);
|
||||
//loop to find first wave that works
|
||||
for (iii=0; iii < initLoopMax; ++iii){
|
||||
if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){
|
||||
lastBit = iii - *clk;
|
||||
//loop through to see if this start location works
|
||||
for (i = iii; i < *size; ++i) {
|
||||
if (i-lastBit > *clk && (BinStream[i] >= high || BinStream[i] <= low)){
|
||||
lastBit += *clk;
|
||||
midBit = 0;
|
||||
} else if (i-lastBit > (*clk/2) && midBit == 0) {
|
||||
midBit = 1;
|
||||
} else if ((i-lastBit) > *clk) {
|
||||
//should have hit a high or low based on clock!!
|
||||
//PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
|
||||
errCnt++;
|
||||
lastBit += *clk;//skip over until hit too many errors
|
||||
if (errCnt > maxErr)
|
||||
break;
|
||||
}
|
||||
if ((i-iii)>(MaxBits * *clk)) break; //got enough bits
|
||||
}
|
||||
//we got more than 64 good bits and not all errors
|
||||
if ((((i-iii)/ *clk) > 64) && (errCnt<=maxErr)) {
|
||||
//possible good read
|
||||
if (errCnt==0){
|
||||
bestStart=iii;
|
||||
bestErrCnt=errCnt;
|
||||
break; //great read - finish
|
||||
}
|
||||
if (errCnt<bestErrCnt){ //set this as new best run
|
||||
bestErrCnt=errCnt;
|
||||
bestStart = iii;
|
||||
}
|
||||
}
|
||||
errCnt=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bestErrCnt > maxErr){
|
||||
*invert = bestStart;
|
||||
*clk = iii;
|
||||
return -1;
|
||||
}
|
||||
//best run is good enough - set to best run and overwrite BinStream
|
||||
lastBit = bestStart - *clk - 1;
|
||||
errCnt = 0;
|
||||
|
||||
for (i = bestStart; i < *size; ++i) {
|
||||
if (i - lastBit > *clk){
|
||||
for (i = start; i < *size; ++i) {
|
||||
if (i-lastBit >= *clk-tol){
|
||||
if (BinStream[i] >= high) {
|
||||
BinStream[bitnum++] = *invert;
|
||||
} else if (BinStream[i] <= low) {
|
||||
BinStream[bitnum++] = *invert ^ 1;
|
||||
} else {
|
||||
} else if (i-lastBit >= *clk+tol) {
|
||||
if (bitnum > 0) {
|
||||
BinStream[bitnum++]=77;
|
||||
BinStream[bitnum++]=7;
|
||||
errCnt++;
|
||||
}
|
||||
} else { //in tolerance - looking for peak
|
||||
continue;
|
||||
}
|
||||
midBit = 0;
|
||||
lastBit += *clk;
|
||||
} else if (i-lastBit > (*clk/2) && midBit == 0){
|
||||
} else if (i-lastBit >= (*clk/2-tol) && !midBit && !askType){
|
||||
if (BinStream[i] >= high) {
|
||||
BinStream[bitnum++] = *invert;
|
||||
} else if (BinStream[i] <= low) {
|
||||
BinStream[bitnum++] = *invert ^ 1;
|
||||
} else {
|
||||
|
||||
} else if (i-lastBit >= *clk/2+tol) {
|
||||
BinStream[bitnum] = BinStream[bitnum-1];
|
||||
bitnum++;
|
||||
} else { //in tolerance - looking for peak
|
||||
continue;
|
||||
}
|
||||
midBit = 1;
|
||||
}
|
||||
|
@ -485,6 +246,98 @@ int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int max
|
|||
return errCnt;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//take 10 and 01 and manchester decode
|
||||
//run through 2 times and take least errCnt
|
||||
int manrawdecode(uint8_t * BitStream, size_t *size, uint8_t invert)
|
||||
{
|
||||
uint16_t bitnum=0, MaxBits = 512, errCnt = 0;
|
||||
size_t i, ii;
|
||||
uint16_t bestErr = 1000, bestRun = 0;
|
||||
if (*size < 16) return -1;
|
||||
//find correct start position [alignment]
|
||||
for (ii=0;ii<2;++ii){
|
||||
for (i=ii; i<*size-3; i+=2)
|
||||
if (BitStream[i]==BitStream[i+1])
|
||||
errCnt++;
|
||||
|
||||
if (bestErr>errCnt){
|
||||
bestErr=errCnt;
|
||||
bestRun=ii;
|
||||
}
|
||||
errCnt=0;
|
||||
}
|
||||
//decode
|
||||
for (i=bestRun; i < *size-3; i+=2){
|
||||
if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
|
||||
BitStream[bitnum++]=invert;
|
||||
} else if((BitStream[i] == 0) && BitStream[i+1] == 1){
|
||||
BitStream[bitnum++]=invert^1;
|
||||
} else {
|
||||
BitStream[bitnum++]=7;
|
||||
}
|
||||
if(bitnum>MaxBits) break;
|
||||
}
|
||||
*size=bitnum;
|
||||
return bestErr;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//encode binary data into binary manchester
|
||||
int ManchesterEncode(uint8_t *BitStream, size_t size)
|
||||
{
|
||||
size_t modIdx=20000, i=0;
|
||||
if (size>modIdx) return -1;
|
||||
for (size_t idx=0; idx < size; idx++){
|
||||
BitStream[idx+modIdx++] = BitStream[idx];
|
||||
BitStream[idx+modIdx++] = BitStream[idx]^1;
|
||||
}
|
||||
for (; i<(size*2); i++){
|
||||
BitStream[i] = BitStream[i+20000];
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
//by marshmellow
|
||||
//take 01 or 10 = 1 and 11 or 00 = 0
|
||||
//check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
|
||||
//decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
|
||||
int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert)
|
||||
{
|
||||
uint16_t bitnum = 0;
|
||||
uint16_t errCnt = 0;
|
||||
size_t i = offset;
|
||||
uint16_t MaxBits=512;
|
||||
//if not enough samples - error
|
||||
if (*size < 51) return -1;
|
||||
//check for phase change faults - skip one sample if faulty
|
||||
uint8_t offsetA = 1, offsetB = 1;
|
||||
for (; i<48; i+=2){
|
||||
if (BitStream[i+1]==BitStream[i+2]) offsetA=0;
|
||||
if (BitStream[i+2]==BitStream[i+3]) offsetB=0;
|
||||
}
|
||||
if (!offsetA && offsetB) offset++;
|
||||
for (i=offset; i<*size-3; i+=2){
|
||||
//check for phase error
|
||||
if (BitStream[i+1]==BitStream[i+2]) {
|
||||
BitStream[bitnum++]=7;
|
||||
errCnt++;
|
||||
}
|
||||
if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
|
||||
BitStream[bitnum++]=1^invert;
|
||||
} else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
|
||||
BitStream[bitnum++]=invert;
|
||||
} else {
|
||||
BitStream[bitnum++]=7;
|
||||
errCnt++;
|
||||
}
|
||||
if(bitnum>MaxBits) break;
|
||||
}
|
||||
*size=bitnum;
|
||||
return errCnt;
|
||||
}
|
||||
|
||||
// by marshmellow
|
||||
// demod gProxIIDemod
|
||||
// error returns as -x
|
||||
// success returns start position in BitStream
|
||||
|
@ -673,7 +526,7 @@ int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, ui
|
|||
return (int)startIdx;
|
||||
}
|
||||
|
||||
uint32_t bytebits_to_byte(uint8_t* src, size_t numbits)
|
||||
uint32_t bytebits_to_byte(uint8_t *src, size_t numbits)
|
||||
{
|
||||
uint32_t num = 0;
|
||||
for(int i = 0 ; i < numbits ; i++)
|
||||
|
@ -684,6 +537,17 @@ uint32_t bytebits_to_byte(uint8_t* src, size_t numbits)
|
|||
return num;
|
||||
}
|
||||
|
||||
//least significant bit first
|
||||
uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits)
|
||||
{
|
||||
uint32_t num = 0;
|
||||
for(int i = 0 ; i < numbits ; i++)
|
||||
{
|
||||
num = (num << 1) | *(src + (numbits-(i+1)));
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int IOdemodFSK(uint8_t *dest, size_t size)
|
||||
{
|
||||
if (justNoise(dest, size)) return -1;
|
||||
|
@ -716,7 +580,7 @@ int IOdemodFSK(uint8_t *dest, size_t size)
|
|||
|
||||
// by marshmellow
|
||||
// takes a array of binary values, start position, length of bits per parity (includes parity bit),
|
||||
// Parity Type (1 for odd 0 for even), and binary Length (length to run)
|
||||
// Parity Type (1 for odd; 0 for even; 2 for just drop it), and binary Length (length to run)
|
||||
size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen)
|
||||
{
|
||||
uint32_t parityWd = 0;
|
||||
|
@ -728,7 +592,9 @@ size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t p
|
|||
}
|
||||
j--;
|
||||
// if parity fails then return 0
|
||||
if (parityTest(parityWd, pLen, pType) == 0) return -1;
|
||||
if (pType != 2) {
|
||||
if (parityTest(parityWd, pLen, pType) == 0) return -1;
|
||||
}
|
||||
bitCnt+=(pLen-1);
|
||||
parityWd = 0;
|
||||
}
|
||||
|
@ -737,6 +603,21 @@ size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t p
|
|||
return bitCnt;
|
||||
}
|
||||
|
||||
// Ask/Biphase Demod then try to locate an ISO 11784/85 ID
|
||||
// BitStream must contain previously askrawdemod and biphasedemoded data
|
||||
int FDXBdemodBI(uint8_t *dest, size_t *size)
|
||||
{
|
||||
//make sure buffer has enough data
|
||||
if (*size < 128) return -1;
|
||||
|
||||
size_t startIdx = 0;
|
||||
uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,1};
|
||||
|
||||
uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
|
||||
if (errChk == 0) return -2; //preamble not found
|
||||
return (int)startIdx;
|
||||
}
|
||||
|
||||
// by marshmellow
|
||||
// FSK Demod then try to locate an AWID ID
|
||||
int AWIDdemodFSK(uint8_t *dest, size_t *size)
|
||||
|
@ -780,12 +661,13 @@ int PyramiddemodFSK(uint8_t *dest, size_t *size)
|
|||
return (int)startIdx;
|
||||
}
|
||||
|
||||
|
||||
uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
|
||||
// by marshmellow
|
||||
// to detect a wave that has heavily clipped (clean) samples
|
||||
uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low)
|
||||
{
|
||||
uint16_t allPeaks=1;
|
||||
uint16_t cntPeaks=0;
|
||||
size_t loopEnd = 572;
|
||||
size_t loopEnd = 512+60;
|
||||
if (loopEnd > size) loopEnd = size;
|
||||
for (size_t i=60; i<loopEnd; i++){
|
||||
if (dest[i]>low && dest[i]<high)
|
||||
|
@ -801,53 +683,39 @@ uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
|
|||
|
||||
// by marshmellow
|
||||
// to help detect clocks on heavily clipped samples
|
||||
// based on counts between zero crossings
|
||||
int DetectStrongAskClock(uint8_t dest[], size_t size)
|
||||
// based on count of low to low
|
||||
int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low)
|
||||
{
|
||||
int clk[]={0,8,16,32,40,50,64,100,128};
|
||||
size_t idx = 40;
|
||||
uint8_t high=0;
|
||||
size_t cnt = 0;
|
||||
size_t highCnt = 0;
|
||||
size_t highCnt2 = 0;
|
||||
for (;idx < size; idx++){
|
||||
if (dest[idx]>128) {
|
||||
if (!high){
|
||||
high=1;
|
||||
if (cnt > highCnt){
|
||||
if (highCnt != 0) highCnt2 = highCnt;
|
||||
highCnt = cnt;
|
||||
} else if (cnt > highCnt2) {
|
||||
highCnt2 = cnt;
|
||||
}
|
||||
cnt=1;
|
||||
} else {
|
||||
cnt++;
|
||||
}
|
||||
} else if (dest[idx] <= 128){
|
||||
if (high) {
|
||||
high=0;
|
||||
if (cnt > highCnt) {
|
||||
if (highCnt != 0) highCnt2 = highCnt;
|
||||
highCnt = cnt;
|
||||
} else if (cnt > highCnt2) {
|
||||
highCnt2 = cnt;
|
||||
}
|
||||
cnt=1;
|
||||
} else {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
uint8_t fndClk[] = {8,16,32,40,50,64,128};
|
||||
size_t startwave;
|
||||
size_t i = 0;
|
||||
size_t minClk = 255;
|
||||
// get to first full low to prime loop and skip incomplete first pulse
|
||||
while ((dest[i] < high) && (i < size))
|
||||
++i;
|
||||
while ((dest[i] > low) && (i < size))
|
||||
++i;
|
||||
|
||||
// loop through all samples
|
||||
while (i < size) {
|
||||
// measure from low to low
|
||||
while ((dest[i] > low) && (i < size))
|
||||
++i;
|
||||
startwave= i;
|
||||
while ((dest[i] < high) && (i < size))
|
||||
++i;
|
||||
while ((dest[i] > low) && (i < size))
|
||||
++i;
|
||||
//get minimum measured distance
|
||||
if (i-startwave < minClk && i < size)
|
||||
minClk = i - startwave;
|
||||
}
|
||||
uint8_t tol;
|
||||
for (idx=8; idx>0; idx--){
|
||||
tol = clk[idx]/8;
|
||||
if (clk[idx] >= highCnt - tol && clk[idx] <= highCnt + tol)
|
||||
return clk[idx];
|
||||
if (clk[idx] >= highCnt2 - tol && clk[idx] <= highCnt2 + tol)
|
||||
return clk[idx];
|
||||
// set clock
|
||||
for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) {
|
||||
if (minClk >= fndClk[clkCnt]-(fndClk[clkCnt]/8) && minClk <= fndClk[clkCnt]+1)
|
||||
return fndClk[clkCnt];
|
||||
}
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// by marshmellow
|
||||
|
@ -856,45 +724,61 @@ int DetectStrongAskClock(uint8_t dest[], size_t size)
|
|||
// return start index of best starting position for that clock and return clock (by reference)
|
||||
int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
|
||||
{
|
||||
size_t i=0;
|
||||
uint8_t clk[]={8,16,32,40,50,64,100,128,255};
|
||||
size_t i=1;
|
||||
uint8_t clk[] = {255,8,16,32,40,50,64,100,128,255};
|
||||
uint8_t clkEnd = 9;
|
||||
uint8_t loopCnt = 255; //don't need to loop through entire array...
|
||||
if (size <= loopCnt) return -1; //not enough samples
|
||||
//if we already have a valid clock quit
|
||||
|
||||
for (;i<8;++i)
|
||||
if (clk[i] == *clock) return 0;
|
||||
|
||||
//if we already have a valid clock
|
||||
uint8_t clockFnd=0;
|
||||
for (;i<clkEnd;++i)
|
||||
if (clk[i] == *clock) clockFnd = i;
|
||||
//clock found but continue to find best startpos
|
||||
|
||||
//get high and low peak
|
||||
int peak, low;
|
||||
if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return -1;
|
||||
|
||||
//test for large clean peaks
|
||||
if (DetectCleanAskWave(dest, size, peak, low)==1){
|
||||
int ans = DetectStrongAskClock(dest, size);
|
||||
for (i=7; i>0; i--){
|
||||
if (clk[i] == ans) {
|
||||
*clock = ans;
|
||||
return 0;
|
||||
if (!clockFnd){
|
||||
if (DetectCleanAskWave(dest, size, peak, low)==1){
|
||||
int ans = DetectStrongAskClock(dest, size, peak, low);
|
||||
for (i=clkEnd-1; i>0; i--){
|
||||
if (clk[i] == ans) {
|
||||
*clock = ans;
|
||||
//clockFnd = i;
|
||||
return 0; // for strong waves i don't use the 'best start position' yet...
|
||||
//break; //clock found but continue to find best startpos [not yet]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ii;
|
||||
uint8_t clkCnt, tol = 0;
|
||||
uint16_t bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
|
||||
uint8_t bestStart[]={0,0,0,0,0,0,0,0,0};
|
||||
size_t errCnt = 0;
|
||||
size_t arrLoc, loopEnd;
|
||||
|
||||
if (clockFnd>0) {
|
||||
clkCnt = clockFnd;
|
||||
clkEnd = clockFnd+1;
|
||||
}
|
||||
else clkCnt=1;
|
||||
|
||||
//test each valid clock from smallest to greatest to see which lines up
|
||||
for(clkCnt=0; clkCnt < 8; clkCnt++){
|
||||
if (clk[clkCnt] == 32){
|
||||
for(; clkCnt < clkEnd; clkCnt++){
|
||||
if (clk[clkCnt] <= 32){
|
||||
tol=1;
|
||||
}else{
|
||||
tol=0;
|
||||
}
|
||||
if (!maxErr) loopCnt=clk[clkCnt]*2;
|
||||
//if no errors allowed - keep start within the first clock
|
||||
if (!maxErr && size > clk[clkCnt]*2 + tol && clk[clkCnt]<128) loopCnt=clk[clkCnt]*2;
|
||||
bestErr[clkCnt]=1000;
|
||||
//try lining up the peaks by moving starting point (try first 256)
|
||||
//try lining up the peaks by moving starting point (try first few clocks)
|
||||
for (ii=0; ii < loopCnt; ii++){
|
||||
if (dest[ii] < peak && dest[ii] > low) continue;
|
||||
|
||||
|
@ -910,11 +794,11 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
|
|||
errCnt++;
|
||||
}
|
||||
}
|
||||
//if we found no errors then we can stop here
|
||||
//if we found no errors then we can stop here and a low clock (common clocks)
|
||||
// this is correct one - return this clock
|
||||
//PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
|
||||
if(errCnt==0 && clkCnt<6) {
|
||||
*clock = clk[clkCnt];
|
||||
if(errCnt==0 && clkCnt<7) {
|
||||
if (!clockFnd) *clock = clk[clkCnt];
|
||||
return ii;
|
||||
}
|
||||
//if we found errors see if it is lowest so far and save it as best run
|
||||
|
@ -924,9 +808,9 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
|
|||
}
|
||||
}
|
||||
}
|
||||
uint8_t iii=0;
|
||||
uint8_t iii;
|
||||
uint8_t best=0;
|
||||
for (iii=0; iii<8; ++iii){
|
||||
for (iii=1; iii<clkEnd; ++iii){
|
||||
if (bestErr[iii] < bestErr[best]){
|
||||
if (bestErr[iii] == 0) bestErr[iii]=1;
|
||||
// current best bit to error ratio vs new bit to error ratio
|
||||
|
@ -935,8 +819,8 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
|
|||
}
|
||||
}
|
||||
}
|
||||
if (bestErr[best] > maxErr) return -1;
|
||||
*clock = clk[best];
|
||||
//if (bestErr[best] > maxErr) return -1;
|
||||
if (!clockFnd) *clock = clk[best];
|
||||
return bestStart[best];
|
||||
}
|
||||
|
||||
|
@ -1115,7 +999,7 @@ void psk1TOpsk2(uint8_t *BitStream, size_t size)
|
|||
size_t i=1;
|
||||
uint8_t lastBit=BitStream[0];
|
||||
for (; i<size; i++){
|
||||
if (BitStream[i]==77){
|
||||
if (BitStream[i]==7){
|
||||
//ignore errors
|
||||
} else if (lastBit!=BitStream[i]){
|
||||
lastBit=BitStream[i];
|
||||
|
@ -1308,7 +1192,7 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int maxErr)
|
|||
if (ignoreCnt == 0){
|
||||
bitHigh = 0;
|
||||
if (errBitHigh == 1){
|
||||
dest[bitnum++] = 77;
|
||||
dest[bitnum++] = 7;
|
||||
errCnt++;
|
||||
}
|
||||
errBitHigh=0;
|
||||
|
@ -1583,7 +1467,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
|
|||
//noise after a phase shift - ignore
|
||||
} else { //phase shift before supposed to based on clock
|
||||
errCnt++;
|
||||
dest[numBits++] = 77;
|
||||
dest[numBits++] = 7;
|
||||
}
|
||||
} else if (i+1 > lastClkBit + *clock + tol + fc){
|
||||
lastClkBit += *clock; //no phase shift but clock bit
|
||||
|
|
|
@ -15,34 +15,39 @@
|
|||
#define LFDEMOD_H__
|
||||
#include <stdint.h>
|
||||
|
||||
int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr);
|
||||
uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low);
|
||||
int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr);
|
||||
uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo);
|
||||
int ManchesterEncode(uint8_t *BitStream, size_t size);
|
||||
int manrawdecode(uint8_t *BitStream, size_t *size);
|
||||
int BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset, int invert);
|
||||
int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp);
|
||||
//generic
|
||||
int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType);
|
||||
int BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset, int invert);
|
||||
uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
|
||||
uint32_t bytebits_to_byteLSBF(uint8_t* src, size_t numbits);
|
||||
uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj);
|
||||
int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr);
|
||||
uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low);
|
||||
uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow);
|
||||
int DetectNRZClock(uint8_t dest[], size_t size, int clock);
|
||||
int DetectPSKClock(uint8_t dest[], size_t size, int clock);
|
||||
int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low);
|
||||
uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo);
|
||||
int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
|
||||
int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
|
||||
int ManchesterEncode(uint8_t *BitStream, size_t size);
|
||||
int manrawdecode(uint8_t *BitStream, size_t *size, uint8_t invert);
|
||||
int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int maxErr);
|
||||
uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType);
|
||||
uint8_t preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx);
|
||||
int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert);
|
||||
void psk2TOpsk1(uint8_t *BitStream, size_t size);
|
||||
void psk1TOpsk2(uint8_t *BitStream, size_t size);
|
||||
size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen);
|
||||
|
||||
//tag specific
|
||||
int FDXBdemodBI(uint8_t *dest, size_t *size);
|
||||
int AWIDdemodFSK(uint8_t *dest, size_t *size);
|
||||
int gProxII_Demod(uint8_t BitStream[], size_t *size);
|
||||
int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
|
||||
int IOdemodFSK(uint8_t *dest, size_t size);
|
||||
int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
|
||||
uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
|
||||
int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int maxErr);
|
||||
void psk1TOpsk2(uint8_t *BitStream, size_t size);
|
||||
void psk2TOpsk1(uint8_t *BitStream, size_t size);
|
||||
int DetectNRZClock(uint8_t dest[], size_t size, int clock);
|
||||
int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert);
|
||||
int PyramiddemodFSK(uint8_t *dest, size_t *size);
|
||||
int AWIDdemodFSK(uint8_t *dest, size_t *size);
|
||||
size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen);
|
||||
uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj);
|
||||
uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow);
|
||||
int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
|
||||
int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
|
||||
uint8_t preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx);
|
||||
uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType);
|
||||
int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert);
|
||||
int DetectPSKClock(uint8_t dest[], size_t size, int clock);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
|
|
@ -123,9 +123,21 @@ NXP/Philips CUSTOM COMMANDS
|
|||
#define MIFARE_CMD_RESTORE 0xC2
|
||||
#define MIFARE_CMD_TRANSFER 0xB0
|
||||
|
||||
#define MIFARE_ULC_WRITE 0xA0
|
||||
#define MIFARE_ULC_WRITE 0xA2
|
||||
//#define MIFARE_ULC__COMP_WRITE 0xA0
|
||||
#define MIFARE_ULC_AUTH_1 0x1A
|
||||
#define MIFARE_ULC_AUTH_2 0xAF
|
||||
#define MIFARE_ULC_AUTH_2 0xAF
|
||||
|
||||
#define MIFARE_ULEV1_AUTH 0x1B
|
||||
#define MIFARE_ULEV1_VERSION 0x60
|
||||
#define MIFARE_ULEV1_FASTREAD 0x3A
|
||||
//#define MIFARE_ULEV1_WRITE 0xA2
|
||||
//#define MIFARE_ULEV1_COMP_WRITE 0xA0
|
||||
#define MIFARE_ULEV1_READ_CNT 0x39
|
||||
#define MIFARE_ULEV1_INCR_CNT 0xA5
|
||||
#define MIFARE_ULEV1_READSIG 0x3C
|
||||
#define MIFARE_ULEV1_CHECKTEAR 0x3E
|
||||
#define MIFARE_ULEV1_VCSL 0x4B
|
||||
|
||||
/**
|
||||
06 00 = INITIATE
|
||||
|
|
665
common/sha1.c
Normal file
665
common/sha1.c
Normal file
|
@ -0,0 +1,665 @@
|
|||
/*
|
||||
* FIPS-180-1 compliant SHA-1 implementation
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* The SHA-1 standard was published by NIST in 1993.
|
||||
*
|
||||
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
//#include "polarssl/config.h"
|
||||
#define POLARSSL_SHA1_C
|
||||
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SHA1_C)
|
||||
|
||||
#include "sha1.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#include "polarssl/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#endif /* POLARSSL_PLATFORM_C */
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void polarssl_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
#if !defined(POLARSSL_SHA1_ALT)
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_BE
|
||||
#define GET_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
void sha1_init( sha1_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof( sha1_context ) );
|
||||
}
|
||||
|
||||
void sha1_free( sha1_context *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
|
||||
polarssl_zeroize( ctx, sizeof( sha1_context ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 context setup
|
||||
*/
|
||||
void sha1_starts( sha1_context *ctx )
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
ctx->state[4] = 0xC3D2E1F0;
|
||||
}
|
||||
|
||||
void sha1_process( sha1_context *ctx, const unsigned char data[64] )
|
||||
{
|
||||
uint32_t temp, W[16], A, B, C, D, E;
|
||||
|
||||
GET_UINT32_BE( W[ 0], data, 0 );
|
||||
GET_UINT32_BE( W[ 1], data, 4 );
|
||||
GET_UINT32_BE( W[ 2], data, 8 );
|
||||
GET_UINT32_BE( W[ 3], data, 12 );
|
||||
GET_UINT32_BE( W[ 4], data, 16 );
|
||||
GET_UINT32_BE( W[ 5], data, 20 );
|
||||
GET_UINT32_BE( W[ 6], data, 24 );
|
||||
GET_UINT32_BE( W[ 7], data, 28 );
|
||||
GET_UINT32_BE( W[ 8], data, 32 );
|
||||
GET_UINT32_BE( W[ 9], data, 36 );
|
||||
GET_UINT32_BE( W[10], data, 40 );
|
||||
GET_UINT32_BE( W[11], data, 44 );
|
||||
GET_UINT32_BE( W[12], data, 48 );
|
||||
GET_UINT32_BE( W[13], data, 52 );
|
||||
GET_UINT32_BE( W[14], data, 56 );
|
||||
GET_UINT32_BE( W[15], data, 60 );
|
||||
|
||||
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define R(t) \
|
||||
( \
|
||||
temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
|
||||
W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
|
||||
( W[t & 0x0F] = S(temp,1) ) \
|
||||
)
|
||||
|
||||
#define P(a,b,c,d,e,x) \
|
||||
{ \
|
||||
e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
|
||||
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||
#define K 0x5A827999
|
||||
|
||||
P( A, B, C, D, E, W[0] );
|
||||
P( E, A, B, C, D, W[1] );
|
||||
P( D, E, A, B, C, W[2] );
|
||||
P( C, D, E, A, B, W[3] );
|
||||
P( B, C, D, E, A, W[4] );
|
||||
P( A, B, C, D, E, W[5] );
|
||||
P( E, A, B, C, D, W[6] );
|
||||
P( D, E, A, B, C, W[7] );
|
||||
P( C, D, E, A, B, W[8] );
|
||||
P( B, C, D, E, A, W[9] );
|
||||
P( A, B, C, D, E, W[10] );
|
||||
P( E, A, B, C, D, W[11] );
|
||||
P( D, E, A, B, C, W[12] );
|
||||
P( C, D, E, A, B, W[13] );
|
||||
P( B, C, D, E, A, W[14] );
|
||||
P( A, B, C, D, E, W[15] );
|
||||
P( E, A, B, C, D, R(16) );
|
||||
P( D, E, A, B, C, R(17) );
|
||||
P( C, D, E, A, B, R(18) );
|
||||
P( B, C, D, E, A, R(19) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0x6ED9EBA1
|
||||
|
||||
P( A, B, C, D, E, R(20) );
|
||||
P( E, A, B, C, D, R(21) );
|
||||
P( D, E, A, B, C, R(22) );
|
||||
P( C, D, E, A, B, R(23) );
|
||||
P( B, C, D, E, A, R(24) );
|
||||
P( A, B, C, D, E, R(25) );
|
||||
P( E, A, B, C, D, R(26) );
|
||||
P( D, E, A, B, C, R(27) );
|
||||
P( C, D, E, A, B, R(28) );
|
||||
P( B, C, D, E, A, R(29) );
|
||||
P( A, B, C, D, E, R(30) );
|
||||
P( E, A, B, C, D, R(31) );
|
||||
P( D, E, A, B, C, R(32) );
|
||||
P( C, D, E, A, B, R(33) );
|
||||
P( B, C, D, E, A, R(34) );
|
||||
P( A, B, C, D, E, R(35) );
|
||||
P( E, A, B, C, D, R(36) );
|
||||
P( D, E, A, B, C, R(37) );
|
||||
P( C, D, E, A, B, R(38) );
|
||||
P( B, C, D, E, A, R(39) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((x & y) | (z & (x | y)))
|
||||
#define K 0x8F1BBCDC
|
||||
|
||||
P( A, B, C, D, E, R(40) );
|
||||
P( E, A, B, C, D, R(41) );
|
||||
P( D, E, A, B, C, R(42) );
|
||||
P( C, D, E, A, B, R(43) );
|
||||
P( B, C, D, E, A, R(44) );
|
||||
P( A, B, C, D, E, R(45) );
|
||||
P( E, A, B, C, D, R(46) );
|
||||
P( D, E, A, B, C, R(47) );
|
||||
P( C, D, E, A, B, R(48) );
|
||||
P( B, C, D, E, A, R(49) );
|
||||
P( A, B, C, D, E, R(50) );
|
||||
P( E, A, B, C, D, R(51) );
|
||||
P( D, E, A, B, C, R(52) );
|
||||
P( C, D, E, A, B, R(53) );
|
||||
P( B, C, D, E, A, R(54) );
|
||||
P( A, B, C, D, E, R(55) );
|
||||
P( E, A, B, C, D, R(56) );
|
||||
P( D, E, A, B, C, R(57) );
|
||||
P( C, D, E, A, B, R(58) );
|
||||
P( B, C, D, E, A, R(59) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0xCA62C1D6
|
||||
|
||||
P( A, B, C, D, E, R(60) );
|
||||
P( E, A, B, C, D, R(61) );
|
||||
P( D, E, A, B, C, R(62) );
|
||||
P( C, D, E, A, B, R(63) );
|
||||
P( B, C, D, E, A, R(64) );
|
||||
P( A, B, C, D, E, R(65) );
|
||||
P( E, A, B, C, D, R(66) );
|
||||
P( D, E, A, B, C, R(67) );
|
||||
P( C, D, E, A, B, R(68) );
|
||||
P( B, C, D, E, A, R(69) );
|
||||
P( A, B, C, D, E, R(70) );
|
||||
P( E, A, B, C, D, R(71) );
|
||||
P( D, E, A, B, C, R(72) );
|
||||
P( C, D, E, A, B, R(73) );
|
||||
P( B, C, D, E, A, R(74) );
|
||||
P( A, B, C, D, E, R(75) );
|
||||
P( E, A, B, C, D, R(76) );
|
||||
P( D, E, A, B, C, R(77) );
|
||||
P( C, D, E, A, B, R(78) );
|
||||
P( B, C, D, E, A, R(79) );
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 process buffer
|
||||
*/
|
||||
void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen )
|
||||
{
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
||||
if( ilen == 0 )
|
||||
return;
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if( ctx->total[0] < (uint32_t) ilen )
|
||||
ctx->total[1]++;
|
||||
|
||||
if( left && ilen >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
sha1_process( ctx, ctx->buffer );
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while( ilen >= 64 )
|
||||
{
|
||||
sha1_process( ctx, input );
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if( ilen > 0 )
|
||||
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
||||
}
|
||||
|
||||
static const unsigned char sha1_padding[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SHA-1 final digest
|
||||
*/
|
||||
void sha1_finish( sha1_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_UINT32_BE( high, msglen, 0 );
|
||||
PUT_UINT32_BE( low, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
sha1_update( ctx, sha1_padding, padn );
|
||||
sha1_update( ctx, msglen, 8 );
|
||||
|
||||
PUT_UINT32_BE( ctx->state[0], output, 0 );
|
||||
PUT_UINT32_BE( ctx->state[1], output, 4 );
|
||||
PUT_UINT32_BE( ctx->state[2], output, 8 );
|
||||
PUT_UINT32_BE( ctx->state[3], output, 12 );
|
||||
PUT_UINT32_BE( ctx->state[4], output, 16 );
|
||||
}
|
||||
|
||||
#endif /* !POLARSSL_SHA1_ALT */
|
||||
|
||||
/*
|
||||
* output = SHA-1( input buffer )
|
||||
*/
|
||||
void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
sha1_init( &ctx );
|
||||
sha1_starts( &ctx );
|
||||
sha1_update( &ctx, input, ilen );
|
||||
sha1_finish( &ctx, output );
|
||||
sha1_free( &ctx );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
/*
|
||||
* output = SHA-1( file contents )
|
||||
*/
|
||||
int sha1_file( const char *path, unsigned char output[20] )
|
||||
{
|
||||
FILE *f;
|
||||
size_t n;
|
||||
sha1_context ctx;
|
||||
unsigned char buf[1024];
|
||||
|
||||
if( ( f = fopen( path, "rb" ) ) == NULL )
|
||||
return( POLARSSL_ERR_SHA1_FILE_IO_ERROR );
|
||||
|
||||
sha1_init( &ctx );
|
||||
sha1_starts( &ctx );
|
||||
|
||||
while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
|
||||
sha1_update( &ctx, buf, n );
|
||||
|
||||
sha1_finish( &ctx, output );
|
||||
sha1_free( &ctx );
|
||||
|
||||
if( ferror( f ) != 0 )
|
||||
{
|
||||
fclose( f );
|
||||
return( POLARSSL_ERR_SHA1_FILE_IO_ERROR );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_FS_IO */
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC context setup
|
||||
*/
|
||||
void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key,
|
||||
size_t keylen )
|
||||
{
|
||||
size_t i;
|
||||
unsigned char sum[20];
|
||||
|
||||
if( keylen > 64 )
|
||||
{
|
||||
sha1( key, keylen, sum );
|
||||
keylen = 20;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 64 );
|
||||
memset( ctx->opad, 0x5C, 64 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
sha1_starts( ctx );
|
||||
sha1_update( ctx, ctx->ipad, 64 );
|
||||
|
||||
polarssl_zeroize( sum, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC process buffer
|
||||
*/
|
||||
void sha1_hmac_update( sha1_context *ctx, const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
sha1_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 HMAC final digest
|
||||
*/
|
||||
void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
unsigned char tmpbuf[20];
|
||||
|
||||
sha1_finish( ctx, tmpbuf );
|
||||
sha1_starts( ctx );
|
||||
sha1_update( ctx, ctx->opad, 64 );
|
||||
sha1_update( ctx, tmpbuf, 20 );
|
||||
sha1_finish( ctx, output );
|
||||
|
||||
polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA1 HMAC context reset
|
||||
*/
|
||||
void sha1_hmac_reset( sha1_context *ctx )
|
||||
{
|
||||
sha1_starts( ctx );
|
||||
sha1_update( ctx, ctx->ipad, 64 );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-SHA-1( hmac key, input buffer )
|
||||
*/
|
||||
void sha1_hmac( const unsigned char *key, size_t keylen,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char output[20] )
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
sha1_init( &ctx );
|
||||
sha1_hmac_starts( &ctx, key, keylen );
|
||||
sha1_hmac_update( &ctx, input, ilen );
|
||||
sha1_hmac_finish( &ctx, output );
|
||||
sha1_free( &ctx );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/*
|
||||
* FIPS-180-1 test vectors
|
||||
*/
|
||||
static const unsigned char sha1_test_buf[3][57] =
|
||||
{
|
||||
{ "abc" },
|
||||
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
|
||||
{ "" }
|
||||
};
|
||||
|
||||
static const int sha1_test_buflen[3] =
|
||||
{
|
||||
3, 56, 1000
|
||||
};
|
||||
|
||||
static const unsigned char sha1_test_sum[3][20] =
|
||||
{
|
||||
{ 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
|
||||
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
|
||||
{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
|
||||
0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
|
||||
{ 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
|
||||
0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
|
||||
};
|
||||
|
||||
/*
|
||||
* RFC 2202 test vectors
|
||||
*/
|
||||
static const unsigned char sha1_hmac_test_key[7][26] =
|
||||
{
|
||||
{ "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
|
||||
"\x0B\x0B\x0B\x0B" },
|
||||
{ "Jefe" },
|
||||
{ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
|
||||
"\xAA\xAA\xAA\xAA" },
|
||||
{ "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
|
||||
"\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
|
||||
{ "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
|
||||
"\x0C\x0C\x0C\x0C" },
|
||||
{ "" }, /* 0xAA 80 times */
|
||||
{ "" }
|
||||
};
|
||||
|
||||
static const int sha1_hmac_test_keylen[7] =
|
||||
{
|
||||
20, 4, 20, 25, 20, 80, 80
|
||||
};
|
||||
|
||||
static const unsigned char sha1_hmac_test_buf[7][74] =
|
||||
{
|
||||
{ "Hi There" },
|
||||
{ "what do ya want for nothing?" },
|
||||
{ "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
|
||||
{ "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
|
||||
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
|
||||
{ "Test With Truncation" },
|
||||
{ "Test Using Larger Than Block-Size Key - Hash Key First" },
|
||||
{ "Test Using Larger Than Block-Size Key and Larger"
|
||||
" Than One Block-Size Data" }
|
||||
};
|
||||
|
||||
static const int sha1_hmac_test_buflen[7] =
|
||||
{
|
||||
8, 28, 50, 50, 20, 54, 73
|
||||
};
|
||||
|
||||
static const unsigned char sha1_hmac_test_sum[7][20] =
|
||||
{
|
||||
{ 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
|
||||
0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 },
|
||||
{ 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
|
||||
0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 },
|
||||
{ 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
|
||||
0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 },
|
||||
{ 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
|
||||
0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA },
|
||||
{ 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
|
||||
0x7B, 0xE1 },
|
||||
{ 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
|
||||
0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 },
|
||||
{ 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
|
||||
0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 }
|
||||
};
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int sha1_self_test( int verbose )
|
||||
{
|
||||
int i, j, buflen, ret = 0;
|
||||
unsigned char buf[1024];
|
||||
unsigned char sha1sum[20];
|
||||
sha1_context ctx;
|
||||
|
||||
sha1_init( &ctx );
|
||||
|
||||
/*
|
||||
* SHA-1
|
||||
*/
|
||||
for( i = 0; i < 3; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " SHA-1 test #%d: ", i + 1 );
|
||||
|
||||
sha1_starts( &ctx );
|
||||
|
||||
if( i == 2 )
|
||||
{
|
||||
memset( buf, 'a', buflen = 1000 );
|
||||
|
||||
for( j = 0; j < 1000; j++ )
|
||||
sha1_update( &ctx, buf, buflen );
|
||||
}
|
||||
else
|
||||
sha1_update( &ctx, sha1_test_buf[i],
|
||||
sha1_test_buflen[i] );
|
||||
|
||||
sha1_finish( &ctx, sha1sum );
|
||||
|
||||
if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "\n" );
|
||||
|
||||
for( i = 0; i < 7; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " HMAC-SHA-1 test #%d: ", i + 1 );
|
||||
|
||||
if( i == 5 || i == 6 )
|
||||
{
|
||||
memset( buf, 0xAA, buflen = 80 );
|
||||
sha1_hmac_starts( &ctx, buf, buflen );
|
||||
}
|
||||
else
|
||||
sha1_hmac_starts( &ctx, sha1_hmac_test_key[i],
|
||||
sha1_hmac_test_keylen[i] );
|
||||
|
||||
sha1_hmac_update( &ctx, sha1_hmac_test_buf[i],
|
||||
sha1_hmac_test_buflen[i] );
|
||||
|
||||
sha1_hmac_finish( &ctx, sha1sum );
|
||||
|
||||
buflen = ( i == 4 ) ? 12 : 20;
|
||||
|
||||
if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "\n" );
|
||||
|
||||
exit:
|
||||
sha1_free( &ctx );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
|
||||
#endif /* POLARSSL_SHA1_C */
|
||||
|
213
common/sha1.h
Normal file
213
common/sha1.h
Normal file
|
@ -0,0 +1,213 @@
|
|||
/**
|
||||
* \file sha1.h
|
||||
*
|
||||
* \brief SHA-1 cryptographic hash function
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_SHA1_H
|
||||
#define POLARSSL_SHA1_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
//#include "config.h"
|
||||
/**
|
||||
* \def POLARSSL_SHA1_C
|
||||
*
|
||||
* Enable the SHA1 cryptographic hash algorithm.
|
||||
*
|
||||
* Module: library/sha1.c
|
||||
* Caller: library/md.c
|
||||
* library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
* library/ssl_tls.c
|
||||
* library/x509write_crt.c
|
||||
*
|
||||
* This module is required for SSL/TLS and SHA1-signed certificates.
|
||||
*/
|
||||
#define POLARSSL_SHA1_C
|
||||
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
||||
#include <basetsd.h>
|
||||
typedef UINT32 uint32_t;
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */
|
||||
|
||||
#if !defined(POLARSSL_SHA1_ALT)
|
||||
// Regular implementation
|
||||
//
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief SHA-1 context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t total[2]; /*!< number of bytes processed */
|
||||
uint32_t state[5]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
|
||||
unsigned char ipad[64]; /*!< HMAC: inner padding */
|
||||
unsigned char opad[64]; /*!< HMAC: outer padding */
|
||||
}
|
||||
sha1_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize SHA-1 context
|
||||
*
|
||||
* \param ctx SHA-1 context to be initialized
|
||||
*/
|
||||
void sha1_init( sha1_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Clear SHA-1 context
|
||||
*
|
||||
* \param ctx SHA-1 context to be cleared
|
||||
*/
|
||||
void sha1_free( sha1_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void sha1_starts( sha1_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 process buffer
|
||||
*
|
||||
* \param ctx SHA-1 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 final digest
|
||||
*
|
||||
* \param ctx SHA-1 context
|
||||
* \param output SHA-1 checksum result
|
||||
*/
|
||||
void sha1_finish( sha1_context *ctx, unsigned char output[20] );
|
||||
|
||||
/* Internal use */
|
||||
void sha1_process( sha1_context *ctx, const unsigned char data[64] );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#else /* POLARSSL_SHA1_ALT */
|
||||
#include "sha1_alt.h"
|
||||
#endif /* POLARSSL_SHA1_ALT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-1( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output SHA-1 checksum result
|
||||
*/
|
||||
void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-1( file contents )
|
||||
*
|
||||
* \param path input file name
|
||||
* \param output SHA-1 checksum result
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR
|
||||
*/
|
||||
int sha1_file( const char *path, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
*/
|
||||
void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key,
|
||||
size_t keylen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha1_hmac_update( sha1_context *ctx, const unsigned char *input,
|
||||
size_t ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output SHA-1 HMAC checksum result
|
||||
*/
|
||||
void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief SHA-1 HMAC context reset
|
||||
*
|
||||
* \param ctx HMAC context to be reset
|
||||
*/
|
||||
void sha1_hmac_reset( sha1_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-SHA-1( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-SHA-1 result
|
||||
*/
|
||||
void sha1_hmac( const unsigned char *key, size_t keylen,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char output[20] );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int sha1_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* sha1.h */
|
Loading…
Add table
Add a link
Reference in a new issue