#include "sha1.h" #ifdef __KERNEL__ #define __NO_VERSION__ #endif /* __KERNEL__ */ #define DevccBlockSize 32 /* internal utility functions */ static int copy_16(uint8_t *buf, uint16_t data, int offset) { buf[offset] = (data >> 8) & 0xff; buf[offset+1] = data & 0xff; return offset+2; } static int copy_32(uint8_t *buf, uint32_t data, int offset) { buf[offset] = (data >> 24) & 0xff; buf[offset+1] = (data >> 16) & 0xff; buf[offset+2] = (data >> 8) & 0xff; buf[offset+3] = data & 0xff; return offset+4; } /* * SHA1 * * Description: * This fuction will return the SHA1 hash of a message. * * Parameters: * digest: [out] * Where the digest is returned. * message: [in] * The message to be hashed. * message_length: [in] * The length of the message (in bytes). * * Returns: * 0 on success. * -1 on error. (I don't think this ever happens) */ int SHA1( uint8_t digest[SHA1HashSize], const uint8_t *message, int message_length) { SHA1Context ctx; int err; err = SHA1Reset(&ctx); if (err) return -1; err = SHA1Input(&ctx, message, message_length); if (err) return -1; err = SHA1Result(&ctx,digest); if (err) return -1; return 0; } /* * devcc_validate_checksum * * Description: * This function will compute a checksum by taking the first 4 bytes of * the SHA1 hash of the first 28 bytes of a message. It will then * return 1 if this checksum matches the 29th-32nd bytes of the message. * * Parameters: * message: [in] * The message to be validated. * * Returns: * 1 if the checksum matches bytes 29-32. * 0 otherwise. */ int devcc_validate_checksum(uint8_t *buffer) { uint8_t md[SHA1HashSize]; (void) SHA1(md,buffer,DevccBlockSize-4); if (md[0] == buffer[DevccBlockSize-4] && md[1] == buffer[DevccBlockSize-3] && md[2] == buffer[DevccBlockSize-2] && md[3] == buffer[DevccBlockSize-1]) return 1; else return 0; } /* * devcc_append_checksum * * Description: * This function will compute a checksum by taking the first 4 bytes of * the SHA1 hash of the first 28 bytes of a message. It will then * write this checksum into the 29th-32nd bytes of the message, thus * producing a message that will produce true when passed to * devcc_validate_checksum. * * Parameters: * message: [in/out] * The message to be checksumed. * * Returns: * Nothing. */ void devcc_append_checksum(uint8_t *buffer) { uint8_t md[SHA1HashSize]; (void) SHA1(md,buffer,DevccBlockSize-4); buffer[DevccBlockSize-4] = md[0]; buffer[DevccBlockSize-3] = md[1]; buffer[DevccBlockSize-2] = md[2]; buffer[DevccBlockSize-1] = md[3]; } /* * get_index * * Description: * This fuction will return the index of the bit in the current * message block in terms of the packet headers. * * Parameters: * scrport: [in] * TCP source port as specified in the packet header. * dstport: [in] * TCP destination port as specified in the packet header. * seq: [in] * TCP sequence number as specified in the packet header. * key: [in] * Byte array to be used as the secret key. * keylen: [in] * Length of the key (in bytes). * * Returns: * The index of the bit associated with these packet headers. */ int get_index (uint16_t srcport, uint16_t dstport, uint32_t seq, uint8_t *key, int keylen) { uint8_t buf[8+keylen]; uint8_t md[SHA1HashSize]; int i, off; off = 0; off = copy_16(buf,srcport,off); off = copy_16(buf,dstport,off); off = copy_32(buf,seq,off); for(i=0;i> 7) & 0x01; }