FreeBSD/contrib/ntp/ntpd/ntp_crypto.c
/*
 * session_key - generate session key
 *
 * This routine generates a session key from the source address,
 * destination address, key ID and private value. The value of the
 * session key is the MD5 hash of these values, while the next key ID is
 * the first four octets of the hash.
 *
 * Returns the next key ID or 0 if there is no destination address.
 */
generate session key
This routine generates a session key from the source address,
destination address, key ID and private value. The value of the
session key is the MD5 hash of these values, while the next key ID is
the first four octets of the hash.

Returns the next key ID or 0 if there is no destination address.
generate session key This routine generates a session key from the source address destination address key ID and private value . The value of the session key is the MD5 hash of these values while the next key ID is the first four octets of the hash . Returns the next key ID or 0 if there is no destination address .
keyid_t
session_key(
	sockaddr_u *srcadr, 	
	sockaddr_u *dstadr, 	
	keyid_t	keyno,		
	keyid_t	private,	
	u_long	lifetime 	
	)
{
	EVP_MD_CTX *ctx;	
	u_char dgst[EVP_MAX_MD_SIZE]; 
	keyid_t	keyid;		
	u_int32	header[10];	
	u_int	hdlen, len;
	if (!dstadr)
		return 0;
	hdlen = 0;
	switch(AF(srcadr)) {
	case AF_INET:
		header[0] = NSRCADR(srcadr);
		header[1] = NSRCADR(dstadr);
		header[2] = htonl(keyno);
		header[3] = htonl(private);
		hdlen = 4 * sizeof(u_int32);
		break;
	case AF_INET6:
		memcpy(&header[0], PSOCK_ADDR6(srcadr),
		    sizeof(struct in6_addr));
		memcpy(&header[4], PSOCK_ADDR6(dstadr),
		    sizeof(struct in6_addr));
		header[8] = htonl(keyno);
		header[9] = htonl(private);
		hdlen = 10 * sizeof(u_int32);
		break;
	}
	ctx = EVP_MD_CTX_new();
	EVP_DigestInit(ctx, EVP_get_digestbynid(crypto_nid));
	EVP_DigestUpdate(ctx, (u_char *)header, hdlen);
	EVP_DigestFinal(ctx, dgst, &len);
	EVP_MD_CTX_free(ctx);
	memcpy(&keyid, dgst, 4);
	keyid = ntohl(keyid);
	if (lifetime != 0) {
		MD5auth_setkey(keyno, crypto_nid, dgst, len, NULL);
		authtrust(keyno, lifetime);
	}
	DPRINTF(2, ("session_key: %s > %s %08x %08x hash %08x life %lu\n",
		    stoa(srcadr), stoa(dstadr), keyno,
		    private, keyid, lifetime));
	return (keyid);
}
keyid_t session_key ( sockaddr_u * srcadr , sockaddr_u * dstadr , keyid_t keyno , keyid_t private , u_long lifetime ) { EVP_MD_CTX * ctx ; u_char dgst [ EVP_MAX_MD_SIZE ] ; keyid_t keyid ; u_int32 header [ 10 ] ; u_int hdlen , len ; if ( ! dstadr ) return 0 ; hdlen = 0 ; switch ( AF ( srcadr ) ) { case AF_INET : header [ 0 ] = NSRCADR ( srcadr ) ; header [ 1 ] = NSRCADR ( dstadr ) ; header [ 2 ] = htonl ( keyno ) ; header [ 3 ] = htonl ( private ) ; hdlen = 4 * sizeof ( u_int32 ) ; break ; case AF_INET6 : memcpy ( & header [ 0 ] , PSOCK_ADDR6 ( srcadr ) , sizeof ( struct in6_addr ) ) ; memcpy ( & header [ 4 ] , PSOCK_ADDR6 ( dstadr ) , sizeof ( struct in6_addr ) ) ; header [ 8 ] = htonl ( keyno ) ; header [ 9 ] = htonl ( private ) ; hdlen = 10 * sizeof ( u_int32 ) ; break ; } ctx = EVP_MD_CTX_new ( ) ; EVP_DigestInit ( ctx , EVP_get_digestbynid ( crypto_nid ) ) ; EVP_DigestUpdate ( ctx , ( u_char * ) header , hdlen ) ; EVP_DigestFinal ( ctx , dgst , & len ) ; EVP_MD_CTX_free ( ctx ) ; memcpy ( & keyid , dgst , 4 ) ; keyid = ntohl ( keyid ) ; if ( lifetime != 0 ) { MD5auth_setkey ( keyno , crypto_nid , dgst , len , NULL ) ; authtrust ( keyno , lifetime ) ; } DPRINTF ( 2 , ( " \n " , stoa ( srcadr ) , stoa ( dstadr ) , keyno , private , keyid , lifetime ) ) ; return ( keyid ) ; }
session_key - generate session key
 This routine generates a session key from the source address,
 destination address, key ID and private value.
session_key - generate session key This routine generates a session key from the source address destination address key ID and private value .
/* source address */ /* destination address */ /* key ID */ /* private value */ /* key lifetime */ /* message digest context */ /* message digest */ /* key identifer */ /* data in network byte order */ /*
	 * Generate the session key and key ID. If the lifetime is
	 * greater than zero, install the key and call it trusted.
	 */