-- C module extracted from ITU-T J.170 (11/2005)
/* Demo of IPCablecom MMH16 and MMH32 MAC algorithms.
This program has been tested using Microsoft C/C++ Version 5.0.
It is believed to port easily to other compilers, but this has
not been tested. When porting, be sure to pick the definitions
for int16, int32, uint16, and uint32 carefully.
*/
#include
/*
Define signed and unsigned integers having 16 and 32 bits.
This is machine/compiler dependent, so pick carefully.
*/
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
/*
Define this symbol to see intermediate values.
Comment it out for clean display.
*/
#define VERBOSE
int32 reduceModF4(int32 x) {
/*
Routine to reduce an int32 value modulo F4, where F4 = 0x10001.
Result is in range [0, 0x10000].
*/
int32 xHi, xLo;
/* Range of x is [0x80000000, 0x7fffffff]. */
/*
If x is negative, add a multiple of F4 to make it non-negative.
This loop executes no more than two times.
*/
while (x < 0) x += 0x7fff7fff;
/* Range of x is [0, 0x7fffffff]. */
/* Subtract high 16 bits of x from low 16 bits. */
xHi = x >> 16;
xLo = x & 0xffff;
x = xLo - xHi;
/* Range of x is [0xffff8001, 0x0000ffff]. */
/* If x is negative, add F4. */
if (x < 0) x += 0x10001;
/* Range of x is [0, 0x10000]. */
return x;
}
uint16 mmh16(
unsigned char *message,
unsigned char *key,
unsigned char *pad,
int msgLen) {
/*
Compute and return the MMH16 MAC of the message using the indicated key and pad.
The length of the message is msgLen bytes; msgLen must be even.
The length of the key must be at least msgLen bytes.
The length of the pad is two bytes. The pad must be freshly picked from a secure random source.
*/
int16 x, y;
uint16 u, v;
int32 sum;
int i;
sum = 0;
for (i=0; i>= 8;
macBuf[0] = (unsigned char)mac16;
show("MMH16 MAC", macBuf, 2);
printf("\n");
printf("Example of MMH32 computation\n");
show("message", message, sizeof(message));
show("key", key, sizeof(message)+2);
show("pad", pad32, 4);
mac32 = mmh32(message, key, pad32, sizeof(message));
macBuf[3] = (unsigned char)mac32; mac32 >>= 8;
macBuf[2] = (unsigned char)mac32; mac32 >>= 8;
macBuf[1] = (unsigned char)mac32; mac32 >>= 8;
macBuf[0] = (unsigned char)mac32;
show("MMH32 MAC", macBuf, 4);
printf("\n");
return 0;
}