FreeBSD/contrib/ntp/ntpd/ntp_crypto.c
/*
 * crypto_mv - verify Bob's response to Alice's challenge
 *
 * Returns
 * XEVNT_OK	success
 * XEVNT_ERR	protocol error
 * XEVNT_FSP	bad filestamp
 * XEVNT_ID	bad or missing group key
 * XEVNT_PUB	bad or missing public key
 */
verify Bob's response to Alice's challenge
Returns
XEVNT_OK     success
XEVNT_ERR    protocol error
XEVNT_FSP    bad filestamp
XEVNT_ID     bad or missing group key
XEVNT_PUB    bad or missing public key
verify Bob ' s response to Alice ' s challenge Returns XEVNT_OK success XEVNT_ERR protocol error XEVNT_FSP bad filestamp XEVNT_ID bad or missing group key XEVNT_PUB bad or missing public key
int
crypto_mv(
	struct exten *ep,	
	struct peer *peer	
	)
{
	DSA	*dsa;		
	DSA	*sdsa;		
	BN_CTX	*bctx;		
	BIGNUM	*k, *u, *v;
	u_int	len;
	const u_char *ptr;
	int	temp;
	const BIGNUM *p;
	const BIGNUM *pub_key, *priv_key;
	const BIGNUM *sp, *sq, *sg;
	if (peer->ident_pkey == NULL) {
		msyslog(LOG_NOTICE, "crypto_mv: scheme unavailable");
		return (XEVNT_ID);
	}
	if (ntohl(ep->fstamp) != peer->ident_pkey->fstamp) {
		msyslog(LOG_NOTICE, "crypto_mv: invalid filestamp %u",
		    ntohl(ep->fstamp));
		return (XEVNT_FSP);
	}
	if ((dsa = EVP_PKEY_get0_DSA(peer->ident_pkey->pkey)) == NULL) {
		msyslog(LOG_NOTICE, "crypto_mv: defective key");
		return (XEVNT_PUB);
	}
	DSA_get0_pqg(dsa, &p, NULL, NULL);
	DSA_get0_key(dsa, &pub_key, &priv_key);
	if (peer->iffval == NULL) {
		msyslog(LOG_NOTICE, "crypto_mv: missing challenge");
		return (XEVNT_ID);
	}
	bctx = BN_CTX_new(); k = BN_new(); u = BN_new(); v = BN_new();
	len = ntohl(ep->vallen);
	ptr = (u_char *)ep->pkt;
	if ((sdsa = d2i_DSAparams(NULL, &ptr, len)) == NULL) {
		msyslog(LOG_ERR, "crypto_mv: %s",
		    ERR_error_string(ERR_get_error(), NULL));
		return (XEVNT_ERR);
	}
	DSA_get0_pqg(sdsa, &sp, &sq, &sg);
	BN_mod_exp(u, sq, pub_key, p, bctx);
	BN_mod_exp(v, sg, priv_key, p, bctx);
	BN_mod_mul(u, u, v, p, bctx);
	BN_mod_mul(u, u, sp, p, bctx);
	temp = BN_cmp(u, peer->iffval);
	BN_CTX_free(bctx); BN_free(k); BN_free(u); BN_free(v);
	BN_free(peer->iffval);
	peer->iffval = NULL;
	DSA_free(sdsa);
	if (temp == 0)
		return (XEVNT_OK);
	msyslog(LOG_NOTICE, "crypto_mv: identity not verified");
	return (XEVNT_ID);
}
int crypto_mv ( struct exten * ep , struct peer * peer ) { DSA * dsa ; DSA * sdsa ; BN_CTX * bctx ; BIGNUM * k , * u , * v ; u_int len ; const u_char * ptr ; int temp ; const BIGNUM * p ; const BIGNUM * pub_key , * priv_key ; const BIGNUM * sp , * sq , * sg ; if ( peer -> ident_pkey == NULL ) { msyslog ( LOG_NOTICE , " " ) ; return ( XEVNT_ID ) ; } if ( ntohl ( ep -> fstamp ) != peer -> ident_pkey -> fstamp ) { msyslog ( LOG_NOTICE , " " , ntohl ( ep -> fstamp ) ) ; return ( XEVNT_FSP ) ; } if ( ( dsa = EVP_PKEY_get0_DSA ( peer -> ident_pkey -> pkey ) ) == NULL ) { msyslog ( LOG_NOTICE , " " ) ; return ( XEVNT_PUB ) ; } DSA_get0_pqg ( dsa , & p , NULL , NULL ) ; DSA_get0_key ( dsa , & pub_key , & priv_key ) ; if ( peer -> iffval == NULL ) { msyslog ( LOG_NOTICE , " " ) ; return ( XEVNT_ID ) ; } bctx = BN_CTX_new ( ) ; k = BN_new ( ) ; u = BN_new ( ) ; v = BN_new ( ) ; len = ntohl ( ep -> vallen ) ; ptr = ( u_char * ) ep -> pkt ; if ( ( sdsa = d2i_DSAparams ( NULL , & ptr , len ) ) == NULL ) { msyslog ( LOG_ERR , " " , ERR_error_string ( ERR_get_error ( ) , NULL ) ) ; return ( XEVNT_ERR ) ; } DSA_get0_pqg ( sdsa , & sp , & sq , & sg ) ; BN_mod_exp ( u , sq , pub_key , p , bctx ) ; BN_mod_exp ( v , sg , priv_key , p , bctx ) ; BN_mod_mul ( u , u , v , p , bctx ) ; BN_mod_mul ( u , u , sp , p , bctx ) ; temp = BN_cmp ( u , peer -> iffval ) ; BN_CTX_free ( bctx ) ; BN_free ( k ) ; BN_free ( u ) ; BN_free ( v ) ; BN_free ( peer -> iffval ) ; peer -> iffval = NULL ; DSA_free ( sdsa ) ; if ( temp == 0 ) return ( XEVNT_OK ) ; msyslog ( LOG_NOTICE , " " ) ; return ( XEVNT_ID ) ; }
crypto_mv - verify Bob's response to Alice's challenge
 Returns
 XEVNT_OK     success
 XEVNT_ERR    protocol error
 XEVNT_FSP    bad filestamp
 XEVNT_ID     bad or missing group key
 XEVNT_PUB    bad or missing public key
crypto_mv - verify Bob ' s response to Alice ' s challenge Returns XEVNT_OK success XEVNT_ERR protocol error XEVNT_FSP bad filestamp XEVNT_ID bad or missing group key XEVNT_PUB bad or missing public key
/* extension pointer */ /* peer structure pointer */ /* MV parameters */ /* DSA parameters */ /* BIGNUM context */ /*
	 * If the MV parameters are not valid or no challenge was sent,
	 * something awful happened or we are being tormented.
	 */ /*
	 * Extract the y, gbar and ghat values from the response.
	 */ /*
	 * Compute (gbar^xhat ghat^xbar) mod p.
	 */ /*
	 * The result should match r.
	 */