Path: blob/main/crypto/openssl/demos/signature/EVP_ED_Signature_demo.c
34890 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[] =22"To be, or not to be, that is the question,\n"23"Whether tis nobler in the minde to suffer\n"24"The slings and arrowes of outragious fortune,\n"25"Or to take Armes again in a sea of troubles,\n";2627static int demo_sign(EVP_PKEY *priv,28const unsigned char *tbs, size_t tbs_len,29OSSL_LIB_CTX *libctx,30unsigned char **sig_out_value,31size_t *sig_out_len)32{33int ret = 0;34size_t sig_len;35unsigned char *sig_value = NULL;36EVP_MD_CTX *sign_context = NULL;3738/* Create a signature context */39sign_context = EVP_MD_CTX_new();40if (sign_context == NULL) {41fprintf(stderr, "EVP_MD_CTX_new failed.\n");42goto cleanup;43}4445/*46* Initialize the sign context using an ED25519 private key47* Notice that the digest name must NOT be used.48* In this demo we don't specify any additional parameters via49* OSSL_PARAM, which means it will use default values.50* For more information, refer to doc/man7/EVP_SIGNATURE-ED25519.pod51* "ED25519 and ED448 Signature Parameters"52*/53if (!EVP_DigestSignInit_ex(sign_context, NULL, NULL, libctx, NULL, priv, NULL)) {54fprintf(stderr, "EVP_DigestSignInit_ex failed.\n");55goto cleanup;56}5758/* Calculate the required size for the signature by passing a NULL buffer. */59if (!EVP_DigestSign(sign_context, NULL, &sig_len, tbs, tbs_len)) {60fprintf(stderr, "EVP_DigestSign using NULL buffer failed.\n");61goto cleanup;62}63sig_value = OPENSSL_malloc(sig_len);64if (sig_value == NULL) {65fprintf(stderr, "OPENSSL_malloc failed.\n");66goto cleanup;67}68fprintf(stdout, "Generating signature:\n");69if (!EVP_DigestSign(sign_context, sig_value, &sig_len, tbs, tbs_len)) {70fprintf(stderr, "EVP_DigestSign failed.\n");71goto cleanup;72}73*sig_out_len = sig_len;74*sig_out_value = sig_value;75BIO_dump_indent_fp(stdout, sig_value, sig_len, 2);76fprintf(stdout, "\n");77ret = 1;7879cleanup:80if (!ret)81OPENSSL_free(sig_value);82EVP_MD_CTX_free(sign_context);83return ret;84}8586static int demo_verify(EVP_PKEY *pub,87const unsigned char *tbs, size_t tbs_len,88const unsigned char *sig_value, size_t sig_len,89OSSL_LIB_CTX *libctx)90{91int ret = 0;92EVP_MD_CTX *verify_context = NULL;9394/*95* Make a verify signature context to hold temporary state96* during signature verification97*/98verify_context = EVP_MD_CTX_new();99if (verify_context == NULL) {100fprintf(stderr, "EVP_MD_CTX_new failed.\n");101goto cleanup;102}103/* Initialize the verify context with a ED25519 public key */104if (!EVP_DigestVerifyInit_ex(verify_context, NULL, NULL,105libctx, NULL, pub, NULL)) {106fprintf(stderr, "EVP_DigestVerifyInit_ex failed.\n");107goto cleanup;108}109/*110* ED25519 only supports the one shot interface using EVP_DigestVerify()111* The streaming EVP_DigestVerifyUpdate() API is not supported.112*/113if (!EVP_DigestVerify(verify_context, sig_value, sig_len,114tbs, tbs_len)) {115fprintf(stderr, "EVP_DigestVerify() failed.\n");116goto cleanup;117}118fprintf(stdout, "Signature verified.\n");119ret = 1;120121cleanup:122EVP_MD_CTX_free(verify_context);123return ret;124}125126static int create_key(OSSL_LIB_CTX *libctx,127EVP_PKEY **privout, EVP_PKEY **pubout)128{129int ret = 0;130EVP_PKEY *priv = NULL, *pub = NULL;131unsigned char pubdata[32];132size_t pubdata_len = 0;133134/*135* In this demo we just create a keypair, and extract the136* public key. We could also use EVP_PKEY_new_raw_private_key_ex()137* to create a key from raw data.138*/139priv = EVP_PKEY_Q_keygen(libctx, NULL, "ED25519");140if (priv == NULL) {141fprintf(stderr, "EVP_PKEY_Q_keygen() failed\n");142goto end;143}144145if (!EVP_PKEY_get_octet_string_param(priv,146OSSL_PKEY_PARAM_PUB_KEY,147pubdata,148sizeof(pubdata),149&pubdata_len)) {150fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");151goto end;152}153pub = EVP_PKEY_new_raw_public_key_ex(libctx, "ED25519", NULL, pubdata, pubdata_len);154if (pub == NULL) {155fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");156goto end;157}158ret = 1;159end:160if (ret) {161*pubout = pub;162*privout = priv;163} else {164EVP_PKEY_free(priv);165}166return ret;167}168169int main(void)170{171OSSL_LIB_CTX *libctx = NULL;172size_t sig_len = 0;173unsigned char *sig_value = NULL;174int ret = EXIT_FAILURE;175EVP_PKEY *priv = NULL, *pub = NULL;176177libctx = OSSL_LIB_CTX_new();178if (libctx == NULL) {179fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");180goto cleanup;181}182if (!create_key(libctx, &priv, &pub)) {183fprintf(stderr, "Failed to create key.\n");184goto cleanup;185}186187if (!demo_sign(priv, hamlet, sizeof(hamlet), libctx,188&sig_value, &sig_len)) {189fprintf(stderr, "demo_sign failed.\n");190goto cleanup;191}192if (!demo_verify(pub, hamlet, sizeof(hamlet),193sig_value, sig_len, libctx)) {194fprintf(stderr, "demo_verify failed.\n");195goto cleanup;196}197ret = EXIT_SUCCESS;198199cleanup:200if (ret != EXIT_SUCCESS)201ERR_print_errors_fp(stderr);202EVP_PKEY_free(pub);203EVP_PKEY_free(priv);204OSSL_LIB_CTX_free(libctx);205OPENSSL_free(sig_value);206return ret;207}208209210