Chapter 7 Public-Key Operations 257
Performing Diffie-Hellman Key Agreement
Step 3: Init
Initialize the algorithm object with B_KeyAgreeInit. The Reference Manual Chapter 4
entry on this function indicates it takes four arguments. The first is the algorithm
object,
dhKeyAgreeAlg
. The second is a key object. The Diffie-Hellman key agreement
algorithm does not require a key, so use a properly cast NULL_PTR for this argument.
The third argument is an algorithm chooser, and the last is a surrender context. This
function is fast, so it is reasonable to pass a properly cast NULL_PTR for the surrender
context.
Step 4: Phase 1
In Phase 1, you generate a random private value and compute a public value from
that private value and the parameters. The Reference Manual Chapter 4 entry on
B_KeyAgreePhase1 describes the format of its six arguments.
The first is the algorithm object.
The second is output. This output is the public value, which will be the same size as
the prime. You are responsible for allocating the memory for the buffer to contain the
public value. In this example, you do not know how big the prime is; just set the
algorithm with the BER-encoded
info
. That
info
does contain the size of the prime,
but you would have to know exactly where to look. An easier way to find the prime
size is by getting the algorithm
info
as AI_DHKeyAgree.
The third argument for the Phase 1 call is the address of an unsigned int. Crypto-C
will place the length in bytes of the public value at that address.
if ((status = B_SetAlgorithmInfo
(dhKeyAgreeAlg, AI_DHKeyAgreeBER,
(POINTER)&dhParametersBER)) != 0)
break;
B_ALGORITHM_METHOD *DH_AGREE_SAMPLE_CHOOSER[] = {
&AM_DH_KEY_AGREE,
(B_ALGORITHM_METHOD *)NULL_PTR
};
if ((status = B_KeyAgreeInit
(dhKeyAgreeAlg, (B_KEY_OBJ)NULL_PTR, DH_AGREE_SAMPLE_CHOOSER,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;