Asmcodes: CRC16, CRC32, CRC32C checksums

Introduction

The concept of error detection codes was first proposed by mathematician and computer scientist W. Wesley Peterson in his 1961 publication Cyclic Codes for Error Detection.

CRC-32

crc32x:    
_crc32x:
    pushad
    lea    esi, [esp+32+4]
    lodsd
    xchg   edx, eax          ; edx = crc
    not    edx
    lodsd
    xchg   ecx, eax          ; ecx = inlen
    lodsd
    xchg   esi, eax          ; esi = in
    xor    eax, eax          ; eax = 0
    jecxz  crc_l3            ; if (inlen==0) return crc;
crc_l0:
    lodsb                    ; al = *p++
    xor    dl, al            ; crc ^= al
    xchg   eax, ecx
    mov    cl, 8
crc_l1:
    shr    edx, 1            ; crc >>= 1
    jnc    crc_l2
    xor    edx, 0xEDB88320
crc_l2:
    loop   crc_l1
    xchg   eax, ecx
    loop   crc_l0
crc_l3:    
    not    edx
    mov    [esp+28], edx     ; return ~crc    
    popad
    ret

CRC-32C (Castagnoli)

In 2007, two years before Peterson’s death, Intel added a new instruction simply called CRC32. It expects a byte in AL before computing CRC32 in EDX using the polynomial 0x82F63B78. Here’s code to emulate the instruction should it be unavailable.

crc32cx:    
_crc32cx:
    pushad
    lea    esi, [esp+32+4]
    lodsd
    xchg   edx, eax          ; edx = crc
    lodsd
    xchg   ecx, eax          ; ecx = inlen
    lodsd
    xchg   esi, eax          ; esi = in
    xor    eax, eax          ; eax = 0
    jecxz  crcc_l3            ; if (inlen==0) return crc;
crcc_l0:
    lodsb                    ; al = *p++
    xor    dl, al            ; crc ^= al
    xchg   eax, ecx
    mov    cl, 8
crcc_l1:
    shr    edx, 1            ; crc >>= 1
    jnc    crcc_l2
    xor    edx, 0x82F63B78
crcc_l2:
    loop   crcc_l1
    xchg   eax, ecx
    loop   crcc_l0
crcc_l3:
    mov    [esp+28], edx     ; return crc    
    popad
    ret

Summary

A CRC function is really only suitable for short inputs. They are not collision resistant.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s