Search code examples
iosopensslcryptographydiffie-hellmanpublic-key-exchange

I want to change the values of P and G for diffiehellman in openssl


Since parameter generation can be an expensive process this is normally done once in advance and then the same set of parameters are used over many key exchanges. Is there any way to modify p and g before its advance set up?


Solution

  • If you use openssl command, you can try asn1...

    # cat dh.conf
    asn1=SEQUENCE:dh_key
    
    [dh_key]
    p=INTEGER:0x97
    g=INTEGER:5
    
    # openssl dh -in newkey.der -inform der -text -check
        DH Parameters: (7 bit)
        prime: 97 (0x61)
        generator: 5 (0x5)
    p value is not a safe prime
    -----BEGIN DH PARAMETERS-----
    MAYCAWECAQU=
    -----END DH PARAMETERS-----
    

    the above implements the simple dh math, i.e.

    > Alice Bob wants to change key...
    > Let p =97 and g=5
    > and XA=36 and XB=58
    > so YA=g^XA mod p = 50
    >    YB=g^XB mod p = 44
    > thus Alice:K=(YB) ^XA mod p=75 
    >      Bob:  K=(YA) ^XB mod p=75