ruby / openssl

Provides SSL, TLS and general purpose cryptography.
Other
240 stars 167 forks source link

Group Key Agreement using OpenSSL #637

Closed Waleed1031 closed 1 year ago

Waleed1031 commented 1 year ago

Hi. I am working on Group Key Agreement using openssl. I am a bit weak in programming. So, needed a help. The issue is that i am deriving shared secret using following code.

`/ Generate shared secret / EVP_PKEY_CTX ctx; unsigned char skey; size_t skeylen; ctx = EVP_PKEY_CTX_new(pkey, NULL);

    if (!ctx) {
        /* Error */
        printf("CTX is empty");
    }

    if (EVP_PKEY_derive_init(ctx) <= 0) {
        /* Error */
        printf("EVP derive initialization failed\n");
    }

    if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0) {
        /* Error */
        printf("EVP derive set peer failed\n");
    }

    /* Determine buffer length */
    if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0) {
        /* Error */
        printf("EVP derive failed\n");
    }
    skey = OPENSSL_malloc(skeylen);

    if (!skey) {
        /* Malloc failure */
        printf("OpenSSL Malloc failed");
    }

    if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0) {
        /* Error */
        printf("Shared key derivation failed");
    }
    printf("\nShared secret:\n");

    for (size_t i = 0; i < skeylen; i++) {
        printf("%02x", skey[i]);
    }

`

Now, i want to save this skey generated to some EVP structure so that i can use it as a peerkey while deriving shared secret with another client's keyshare. It is in raw form. i need a snippet which saves it in an EVP structure and save it in a file inn PEM format so that it can be used afterwards. I tried EVP_write_PrivateKey or Pubkey functions but they extract the private or public key from the context. An assistance will be appreciated. For clarification, i am using X25519 curve and ECDH for key agreement.

Thank you in advance.

rhenium commented 1 year ago

Is this a question regarding OpenSSL's C API? This repository handles the openssl library for Ruby.

Check out OpenSSL's GitHub page (https://github.com/openssl/openssl/) and the mailing lists (https://www.openssl.org/community/mailinglists.html).