0

How to calculate reconciliation factor in CRC32 for CVN number and CALID Can someone help he with this How to calculate reconciliation factor in CRC32 and crc16.

but calculated factor value should be like to get same sum Every time for CVN calculation

I want formula of get offset or Reconciliation factor for CRC32 calculation .

Let me clarify for this : CVN : calibration Vehicle network identification . The solution as i want for ex: I have two different structure where we have 10 paramater and from that check sum value as ex 0xfefeABCD and 0x12345678 and on that structure im have to add one more parameter which calibration factor , When I add this parameter on both structure the checksum value get modify , but I need a algorithm for that to get same checksum value for both the structure by adding calibration factor and offset . NOTE : both structure have same 10 Variables but value is different , Im not having idea about value of these structure ,but still i need same checksum by adding factor value on structure .

Im using this function :

As the data im passing to function is :

final result will be store in buffer Please let me know what thing im missing so to get result as same we want . Im sure that im missing something .

Start code :

 #include <stdio.h>


#define CRCPOLY 0xEDB88320
#define CRCINV 0x5B358FD3 // inverse poly of (x ^N) mod CRCPOLY
#define INITXOR 0xFFFFFFFF
#define FINALXOR 0xFFFFFFFF



void make_crc_revtable ( unsigned int * crc_revtable ) ;
int crc32_bitoriented ( unsigned char * buffer , int length );
unsigned int  crc_table[256];
unsigned char  buffer[]= { 0x0, 0x1, 0x2, 0x3, 0x4,0x5, 0x6, 0x7, 0x8, 0x9,0xA, 0xB, 0xC, 0xD, 0xE,0xF, 0x0, 0x1, 0x2, 0x3,0x0, 0x0, 0x0, 0x0, 0x0 };
unsigned int  crc_revtable [256];
unsigned int   tcrcreg  ;
unsigned int   CRC_32 ;
unsigned int fix_pos = 21;
unsigned int length = 256;
void fix_crc_pos ( unsigned char * buffer ,int length ,unsigned int tcrcreg ,int fix_pos ,unsigned int * crc_table ,unsigned int * crc_revtable )
{
    int i;
    // make sure fix_pos is within 0..( length -1)
    fix_pos = ((fix_pos % length) + length) % length;

    // calculate crc register at position fix_pos ; this is essentially crc32 ()
    unsigned int crcreg = INITXOR ;
    for (i = 0; i < fix_pos ; ++i) 
    {
        crcreg = (crcreg >> 8) ^ crc_table[((crcreg ^ buffer [i]) & 0xFF)];
    }

    // inject crcreg as content
    for (i = 0; i < 4; ++i)
    {
        buffer[fix_pos + i] = ((crcreg >> i * 8) & 0xFF);
    }
    // calculate crc backwards to fix_pos , beginning at the end
    tcrcreg = (tcrcreg ^FINALXOR) ;
    for (i = length - 1; i >= fix_pos ; --i) 
    {
        tcrcreg = ((tcrcreg << 8) ^ (crc_revtable[tcrcreg >> 3*8] ^ buffer[i]));
    }

    // inject new content
    for (i = 0; i < 4; ++i)
    {
        buffer[fix_pos + i] = (tcrcreg >> i * 8) & 0xFF;
    }

}
void make_crc_revtable ( unsigned int *crc_revtable ) 
{
    unsigned int c;
    int n , k;
    for (n = 0; n < 256; n ++) 
    {
        c = n << 3*8;
        for (k = 0; k < 8; k ++) 
        {
            if (( c & 0x80000000 ) != 0) 
            {
                c = ((( c ^ CRCPOLY ) << 1) | 1);
            } 
            else 
            {
                c = (c <<1);
            }
        }
        crc_revtable [n] = c;
    }
}
void make_crc_table ( unsigned int * table ) 
{
    unsigned int c;
    int n , k;
    for (n = 0; n < 256; n ++) 
    {
        c = n ;
        for (k = 0; k < 8; k ++) 
        {
            if (( c & 1) != 0) 
            {
                c = CRCPOLY ^ ( c >> 1);
            } 
            else 
            {
                c = c >> 1;
            }
        }
        table [n] = c;
    }
}
int crc32_bitoriented ( unsigned char * buffer , int length ) 
{
    int i , j;
    unsigned int crcreg = INITXOR ;
    for (j = 0; j < length ; ++ j ) 
    {
        unsigned char b = buffer [ j ];
        for (i = 0; i < 8; ++ i) 
        {
            if (( crcreg ^ b ) & 1) 
            {
                crcreg = ( crcreg >> 1) ^ CRCPOLY ;
            } 
            else 
            {
                crcreg >>= 1;
            }
            b >>= 1;
        }
    }

return crcreg ^ FINALXOR ;
}
int main()
{
    length = sizeof(buffer);
    CRC_32 = crc32_bitoriented( buffer , length );
    
    printf("\nCRC_32 :%x ",CRC_32);
    make_crc_table(&crc_table[0]);

    make_crc_revtable(&crc_revtable[0]);
    
    fix_crc_pos(buffer, length, tcrcreg, fix_pos, &crc_table[0], &crc_revtable[0]);
    printf("\nModified Buffer:\n");

    for(int i=1;i<=length ;i++)
    {
        printf("0x%x  ",buffer[i-1]);
        if(0== (i%5))
        {
            printf("\n");
        }
    }printf("\n");
    
    CRC_32 = crc32_bitoriented( buffer , length );
    printf("\nFinal CRC_32 :%x ",CRC_32);   
    return 0;
    
}

----------------------END Code---------------

How do we get Offset and reconciliation factor value to get same CRC every time ? unchanged data in buffer: 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF 0x0 0x1 0x2 0x3 0x0 0x0 0x0 0x0 0x0

Mehul
  • 3
  • 2
  • What the heck is a "reconciliation factor"? – Mark Adler Jan 04 '23 at 07:01
  • And what the heck is a CVN number and CALID? – kesselhaus Jan 05 '23 at 12:04
  • @MarkAdler - maybe he's trying to modify an encoded message and still pass a CRC check? The OP needs to clarify what he's trying to do. – rcgldr Jan 05 '23 at 20:42
  • Yes, I think he wants to modify a message to have the same CRC-32 (or CRC-16?) as the original message. See this similar question: https://stackoverflow.com/questions/57095668 – Mark Adler Jan 05 '23 at 23:45
  • Let me clarify for this : CVN : calibration vehical network identification – Mehul Jan 06 '23 at 06:29
  • @Mark Adler : Yes the question is almost same , but I didn't get the input for that function to apply on my calculation for CRC32 , what is the input i have to give on that ,So i can apply and try – Mehul Jan 06 '23 at 06:46
  • You need to give an explicit example of an actual structure with the byte values, the modification, and what bytes can be changed to match the CRC, and exactly which CRC is being used. There are many CRC-32s, and many CRC-16s. – Mark Adler Jan 06 '23 at 06:59
  • The input for that function is a sequence of bytes, the desired CRC-32, and the index of the location of four bytes that can be modified so that the modified sequence has the desired CRC. You also need to provide forward and reverse CRC tables for the specific CRC in use, as well as #define's for the initial exclusive-or and final exclusive-or for the specific CRC in use. – Mark Adler Jan 06 '23 at 07:01
  • @Mark Adler : I have added code ,since need clarification on it , Could you verify please for me to ensure that whats a mistake in the code or input parameter , So we can make more efficient for CRC32 calculation. – Mehul Jan 09 '23 at 07:11
  • Maybe you should explain what you actually want to achive. Usually, you have an block and make a CRC to store, and on read the CRC should be ok, so nothing has changed. Because, what you currently want has nothing to do anymore with AUTOSAR at all, except, that AUTOSAR defines certain CRC library which provides certain CRCs and their used polynomial. – kesselhaus Jan 10 '23 at 01:17
  • @mark adler :I have modified the code and added , Could you confirm with RUN if has any issue in output for CRC32 . After adding offset has the same CRC for the buffer . Because im not getting same CRC32. – Mehul Jan 10 '23 at 11:19
  • @kesselhaus :Im finding CRC32 reconciliation factor to get same CRC for my CVN number for all the ECU . So we are finding algorithm to add reconsilation factor on CVN number to get same CRC for All the ECU for CVN calculation of CRC. – Mehul Jan 10 '23 at 11:23
  • You still didn't fix the `25` in `make_crc_revtable()`. It needs to be 256. Your `buffer` needs to be `unsigned char`, _not_ `uint32_t`. Also don't define `uint32_t` yourself, guessing that maybe an `int` is 32 bits. Use `#include `. The `length` of your data is not `256`! It's `25`. Again, to not leave things to chance, define `buffer` using `buffer[]`, and it will get the length from the length of your initialization list. And use `length = sizeof(buffer);` to get the correct length every time. You call `fix_crc_pos()` with `buffer`, not `&buffer`. `buffer` is already a pointer. – Mark Adler Jan 10 '23 at 16:15
  • @MarkAdler: I have modified the code again and added on same place , Could you confirm again and RUN in Compiler to ensure we have output . I have question regarding - How we know that what will be offeset value and reconsilation factor value from this implementation . – Mehul Jan 11 '23 at 07:29
  • No, I'm not going to fix your code again. Just do what I said and it will work. – Mark Adler Jan 11 '23 at 07:39
  • As already fixed , Just need to verify only , becasue in my RUN im loosing CRC value after modifictaion in buffer , getting value 0. – Mehul Jan 11 '23 at 08:11
  • @MarkAdler: Thanks a lot for your support , As prepared algorithm is working . – Mehul Jan 16 '23 at 08:50

1 Answers1

0

Your code lost some things in copying. There needs to be a length parameter in fix_crc_pos(). In make_crc_revtable() you go to 256, not 25. You need a place to put the reverse table: uint32_t crc_revtable[256];. You need to make the forward table:

void make_crc_table(uint32_t *crc_table) {
    for (int n = 0; n < 256; n++) {
        uint32_t crc = n;
        for (int k = 0; k < 8; k++)
            crc = crc & 1 ? (crc >> 1) ^ CRCPOLY : crc >> 1;
        crc_table[n] = crc;
    }
}

and a place to put that somewhere: uint32_t crc_table[256];.

Then fix_crc_pos() will work. You give it the data you want the CRC over, buffer with length length. You give it the CRC you want to force the data to have, tcrcreg and the offset in the data, fix_pos, where there are four bytes you are allowing it to modify to get that CRC. And you provide it with the two tables you built by calling the make functions.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • One more question came ,because some changes in project structure . Do we can add this reconciliation factor in any position of the structure ? if yes then what else to be considered while calculating this factor . – Mehul May 09 '23 at 02:42
  • I can't make any sense of your question, I don't know what you mean by "reconciliation factor", and comments aren't the place for questions. Ask a new question. – Mark Adler May 09 '23 at 02:53