Use zlib to calculate Ethernet frame FCS

I’m doing a small project which involves sending Ethernet frames using FPGA. The FPGA is connected to a PHY using RGMII. All signals are generated by pure logic. When calculating the FCS (Frame checksum sequence), I googled for some reference implementations, and got multiple results, each would calculate a different result. I was quite confused. Finally, I had to implemented some logic that can capture raw Ethernet frames from RGMII interface. With some know-good frames, I was able to work out a C program which can calculate the FCS given a frame. This program can be used to verify the logic which generates Ethernet frames.


/* Ethernet frame with payload, padding.
* the generage crc should be 0x58 0x1e 0xed 0x47
*/
unsigned char eth_frame[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x50,
0xb6, 0x07, 0x86, 0x5a, 0x08, 0x06, 0x00, 0x01,
0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x50,
0xb6, 0x07, 0x86, 0x5a, 0xc0, 0xa8, 0x64, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xa8,
0x64, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};

int main()
{
int t;
u_int32_t crc = 0;

crc = crc32(0, eth_frame, sizeof(eth_frame));

printf("CRC32: ");
for(t=0;t<4;t++) { printf("0x%02X ", (crc>>(t*8))&0xFF);
}
printf("\n");

}

I found this online calculator can also generate the same result: https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/ For Ethernet FCS, look at table CRC-32, row “Reversed 0xEDB88320” and column “Big Endian (ABCD)”.