21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

for (j = 8; j > 0; j--) {<br />

if (crc & 1) crc = (crc >> 1) ^ CRC_POLY;<br />

else crc >>= 1;<br />

}<br />

crc32_table[i] = crc;<br />

}<br />

return 1;<br />

}<br />

unsigned long crc32_calc(unsigned char *buf, int buf_len) {<br />

int x;<br />

unsigned long crc = 0xFFFFFFFF;<br />

if (!crc32_table[0]) crc32_table_init( );<br />

for (x = 0; x < buf_len; x++) crc = crc32(crc, buf[x]);<br />

return crc;<br />

}<br />

The following program demonstrates the use of the checksum implementation. Note<br />

that the program is first compiled with a printf( ) in main( ) that will print the<br />

checksum to stdout. As long as main( ) is linked into the program after the buffer<br />

being checked, this printf( ) can be removed and the program recompiled without<br />

the value of the checksum changing. Once the checksum is known, a hex editor can<br />

be used to patch the checksum value into the location crc32_stored. In this example,<br />

the four bytes of the checksum are stored between two 0xFEEDFACE markers that<br />

should be overwritten with random bytes before the binary is distributed. Note that<br />

the markers will be stored in little-endian order in the binary, hence the reversed<br />

ordering of the bytes in the C source.<br />

#include <br />

/* warning: replace "crc32_stored" with the real checksum! */<br />

asm(".long 0xCEFAEDFE \n" /* look for 0xFEEDFACE markers */<br />

"crc32_stored: \n"<br />

".long 0xFFFFFFFF \n" /* change this in the binary! */<br />

".long 0xCEFAEDFE \n" /* end marker */<br />

);<br />

CRC_START_BLOCK(test)<br />

int test_routine(int a) {<br />

while (a < 12) a = (a - (a * 3)) + 1;<br />

return a;<br />

}<br />

CRC_END_BLOCK( test )<br />

int main(int argc, char *argv[ ]) {<br />

unsigned long crc;<br />

crc = crc32_calc(CRC_BLOCK_ADDR(test), CRC_BLOCK_LEN(test));<br />

#ifdef TEST_BUILD<br />

/* This printf( ) displays the CRC value that needs to be stored in the program.<br />

* The printf( ) must be removed, and the program recompiled before distribution.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Detecting Modification | 655

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!