FreeBSD/sys/netinet/sctp_auth.c
/*-
 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
 *
 * Compute the HMAC digest using the desired hash key, text, and HMAC
 * algorithm.  Resulting digest is placed in 'digest' and digest length
 * is returned, if the HMAC was performed.
 *
 * WARNING: it is up to the caller to supply sufficient space to hold the
 * resultant digest.
 */
Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
Compute the HMAC digest using the desired hash key, text, and HMAC
algorithm.  Resulting digest is placed in 'digest' and digest length
is returned, if the HMAC was performed.

it is up to the caller to supply sufficient space to hold the
resultant digest.
Keyed - Hashing for Message Authentication : FIPS 198 ( RFC 2104 ) Compute the HMAC digest using the desired hash key text and HMAC algorithm . Resulting digest is placed in ' digest ' and digest length is returned if the HMAC was performed . it is up to the caller to supply sufficient space to hold the resultant digest .
uint32_t
sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
    uint8_t * text, uint32_t textlen, uint8_t * digest)
{
	uint32_t digestlen;
	uint32_t blocklen;
	sctp_hash_context_t ctx;
	uint8_t ipad[128], opad[128];	
	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
	uint32_t i;
	if ((key == NULL) || (keylen == 0) || (text == NULL) ||
	    (textlen == 0) || (digest == NULL)) {
		return (0);
	}
	digestlen = sctp_get_hmac_digest_len(hmac_algo);
	if (digestlen == 0)
		return (0);
	blocklen = sctp_get_hmac_block_len(hmac_algo);
	if (keylen > blocklen) {
		sctp_hmac_init(hmac_algo, &ctx);
		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
		sctp_hmac_final(hmac_algo, &ctx, temp);
		keylen = digestlen;
		key = temp;
	}
	bzero(ipad, blocklen);
	bzero(opad, blocklen);
	bcopy(key, ipad, keylen);
	bcopy(key, opad, keylen);
	for (i = 0; i < blocklen; i++) {
		ipad[i] ^= 0x36;
		opad[i] ^= 0x5c;
	}
	sctp_hmac_init(hmac_algo, &ctx);
	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
	sctp_hmac_update(hmac_algo, &ctx, text, textlen);
	sctp_hmac_final(hmac_algo, &ctx, temp);
	sctp_hmac_init(hmac_algo, &ctx);
	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
	sctp_hmac_final(hmac_algo, &ctx, digest);
	return (digestlen);
}
uint32_t sctp_hmac ( uint16_t hmac_algo , uint8_t * key , uint32_t keylen , uint8_t * text , uint32_t textlen , uint8_t * digest ) { uint32_t digestlen ; uint32_t blocklen ; sctp_hash_context_t ctx ; uint8_t ipad [ 128 ] , opad [ 128 ] ; uint8_t temp [ SCTP_AUTH_DIGEST_LEN_MAX ] ; uint32_t i ; if ( ( key == NULL ) || ( keylen == 0 ) || ( text == NULL ) || ( textlen == 0 ) || ( digest == NULL ) ) { return ( 0 ) ; } digestlen = sctp_get_hmac_digest_len ( hmac_algo ) ; if ( digestlen == 0 ) return ( 0 ) ; blocklen = sctp_get_hmac_block_len ( hmac_algo ) ; if ( keylen > blocklen ) { sctp_hmac_init ( hmac_algo , & ctx ) ; sctp_hmac_update ( hmac_algo , & ctx , key , keylen ) ; sctp_hmac_final ( hmac_algo , & ctx , temp ) ; keylen = digestlen ; key = temp ; } bzero ( ipad , blocklen ) ; bzero ( opad , blocklen ) ; bcopy ( key , ipad , keylen ) ; bcopy ( key , opad , keylen ) ; for ( i = 0 ; i < blocklen ; i ++ ) { ipad [ i ] ^= 0x36 ; opad [ i ] ^= 0x5c ; } sctp_hmac_init ( hmac_algo , & ctx ) ; sctp_hmac_update ( hmac_algo , & ctx , ipad , blocklen ) ; sctp_hmac_update ( hmac_algo , & ctx , text , textlen ) ; sctp_hmac_final ( hmac_algo , & ctx , temp ) ; sctp_hmac_init ( hmac_algo , & ctx ) ; sctp_hmac_update ( hmac_algo , & ctx , opad , blocklen ) ; sctp_hmac_update ( hmac_algo , & ctx , temp , digestlen ) ; sctp_hmac_final ( hmac_algo , & ctx , digest ) ; return ( digestlen ) ; }
Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
 Compute the HMAC digest using the desired hash key, text, and HMAC
 algorithm.
Keyed - Hashing for Message Authentication : FIPS 198 ( RFC 2104 ) Compute the HMAC digest using the desired hash key text and HMAC algorithm .
/* keyed hash inner/outer pads */ /* sanity check the material and length */ /* can't do HMAC with empty key or text or digest store */ /* validate the hmac algo and get the digest length */ /* hash the key if it is longer than the hash block size */ /* set the hashed key as the key */ /* initialize the inner/outer pads with the key and "append" zeroes */ /* XOR the key with ipad and opad values */ /* perform inner hash */ /* perform outer hash */