Path: blob/main/crypto/openssl/demos/signature/EVP_ED_Signature_demo.c
113219 views
/*-1* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* This demonstration will calculate and verify an ED25519 signature of11* a message using EVP_DigestSign() and EVP_DigestVerify().12*/1314#include <string.h>15#include <stdio.h>16#include <openssl/err.h>17#include <openssl/evp.h>18#include <openssl/core_names.h>1920/* A test message to be signed (TBS) */21static const unsigned char hamlet[] = "To be, or not to be, that is the question,\n"22"Whether tis nobler in the minde to suffer\n"23"The slings and arrowes of outragious fortune,\n"24"Or to take Armes again in a sea of troubles,\n";2526static int demo_sign(EVP_PKEY *priv,27const unsigned char *tbs, size_t tbs_len,28OSSL_LIB_CTX *libctx,29unsigned char **sig_out_value,30size_t *sig_out_len)31{32int ret = 0;33size_t sig_len;34unsigned char *sig_value = NULL;35EVP_MD_CTX *sign_context = NULL;3637/* Create a signature context */38sign_context = EVP_MD_CTX_new();39if (sign_context == NULL) {40fprintf(stderr, "EVP_MD_CTX_new failed.\n");41goto cleanup;42}4344/*45* Initialize the sign context using an ED25519 private key46* Notice that the digest name must NOT be used.47* In this demo we don't specify any additional parameters via48* OSSL_PARAM, which means it will use default values.49* For more information, refer to doc/man7/EVP_SIGNATURE-ED25519.pod50* "ED25519 and ED448 Signature Parameters"51*/52if (!EVP_DigestSignInit_ex(sign_context, NULL, NULL, libctx, NULL, priv, NULL)) {53fprintf(stderr, "EVP_DigestSignInit_ex failed.\n");54goto cleanup;55}5657/* Calculate the required size for the signature by passing a NULL buffer. */58if (!EVP_DigestSign(sign_context, NULL, &sig_len, tbs, tbs_len)) {59fprintf(stderr, "EVP_DigestSign using NULL buffer failed.\n");60goto cleanup;61}62sig_value = OPENSSL_malloc(sig_len);63if (sig_value == NULL) {64fprintf(stderr, "OPENSSL_malloc failed.\n");65goto cleanup;66}67fprintf(stdout, "Generating signature:\n");68if (!EVP_DigestSign(sign_context, sig_value, &sig_len, tbs, tbs_len)) {69fprintf(stderr, "EVP_DigestSign failed.\n");70goto cleanup;71}72*sig_out_len = sig_len;73*sig_out_value = sig_value;74BIO_dump_indent_fp(stdout, sig_value, sig_len, 2);75fprintf(stdout, "\n");76ret = 1;7778cleanup:79if (!ret)80OPENSSL_free(sig_value);81EVP_MD_CTX_free(sign_context);82return ret;83}8485static int demo_verify(EVP_PKEY *pub,86const unsigned char *tbs, size_t tbs_len,87const unsigned char *sig_value, size_t sig_len,88OSSL_LIB_CTX *libctx)89{90int ret = 0;91EVP_MD_CTX *verify_context = NULL;9293/*94* Make a verify signature context to hold temporary state95* during signature verification96*/97verify_context = EVP_MD_CTX_new();98if (verify_context == NULL) {99fprintf(stderr, "EVP_MD_CTX_new failed.\n");100goto cleanup;101}102/* Initialize the verify context with a ED25519 public key */103if (!EVP_DigestVerifyInit_ex(verify_context, NULL, NULL,104libctx, NULL, pub, NULL)) {105fprintf(stderr, "EVP_DigestVerifyInit_ex failed.\n");106goto cleanup;107}108/*109* ED25519 only supports the one shot interface using EVP_DigestVerify()110* The streaming EVP_DigestVerifyUpdate() API is not supported.111*/112if (!EVP_DigestVerify(verify_context, sig_value, sig_len,113tbs, tbs_len)) {114fprintf(stderr, "EVP_DigestVerify() failed.\n");115goto cleanup;116}117fprintf(stdout, "Signature verified.\n");118ret = 1;119120cleanup:121EVP_MD_CTX_free(verify_context);122return ret;123}124125static int create_key(OSSL_LIB_CTX *libctx,126EVP_PKEY **privout, EVP_PKEY **pubout)127{128int ret = 0;129EVP_PKEY *priv = NULL, *pub = NULL;130unsigned char pubdata[32];131size_t pubdata_len = 0;132133/*134* In this demo we just create a keypair, and extract the135* public key. We could also use EVP_PKEY_new_raw_private_key_ex()136* to create a key from raw data.137*/138priv = EVP_PKEY_Q_keygen(libctx, NULL, "ED25519");139if (priv == NULL) {140fprintf(stderr, "EVP_PKEY_Q_keygen() failed\n");141goto end;142}143144if (!EVP_PKEY_get_octet_string_param(priv,145OSSL_PKEY_PARAM_PUB_KEY,146pubdata,147sizeof(pubdata),148&pubdata_len)) {149fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");150goto end;151}152pub = EVP_PKEY_new_raw_public_key_ex(libctx, "ED25519", NULL, pubdata, pubdata_len);153if (pub == NULL) {154fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");155goto end;156}157ret = 1;158end:159if (ret) {160*pubout = pub;161*privout = priv;162} else {163EVP_PKEY_free(priv);164}165return ret;166}167168int main(void)169{170OSSL_LIB_CTX *libctx = NULL;171size_t sig_len = 0;172unsigned char *sig_value = NULL;173int ret = EXIT_FAILURE;174EVP_PKEY *priv = NULL, *pub = NULL;175176libctx = OSSL_LIB_CTX_new();177if (libctx == NULL) {178fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");179goto cleanup;180}181if (!create_key(libctx, &priv, &pub)) {182fprintf(stderr, "Failed to create key.\n");183goto cleanup;184}185186if (!demo_sign(priv, hamlet, sizeof(hamlet), libctx,187&sig_value, &sig_len)) {188fprintf(stderr, "demo_sign failed.\n");189goto cleanup;190}191if (!demo_verify(pub, hamlet, sizeof(hamlet),192sig_value, sig_len, libctx)) {193fprintf(stderr, "demo_verify failed.\n");194goto cleanup;195}196ret = EXIT_SUCCESS;197198cleanup:199if (ret != EXIT_SUCCESS)200ERR_print_errors_fp(stderr);201EVP_PKEY_free(pub);202EVP_PKEY_free(priv);203OSSL_LIB_CTX_free(libctx);204OPENSSL_free(sig_value);205return ret;206}207208209