Rename <openssl/core_numbers.h> -> <openssl/core_dispatch.h>
[oweals/openssl.git] / providers / implementations / serializers / serializer_ecx_pub.c
index 384d75e6b33818c9054e8351d1acf2d8d1857465..3e2c7620ac80af6bfc7b1dc43bd423ab14666ffa 100644 (file)
@@ -7,17 +7,21 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/core_numbers.h>
+#include <openssl/core_dispatch.h>
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/types.h>
 #include <openssl/params.h>
+#include "crypto/ecx.h"
 #include "prov/bio.h"
 #include "prov/implementations.h"
+#include "prov/provider_ctx.h"
 #include "serializer_local.h"
 
 static OSSL_OP_serializer_newctx_fn x25519_pub_newctx;
 static OSSL_OP_serializer_newctx_fn x448_pub_newctx;
+static OSSL_OP_serializer_newctx_fn ed25519_pub_newctx;
+static OSSL_OP_serializer_newctx_fn ed448_pub_newctx;
 static OSSL_OP_serializer_freectx_fn ecx_pub_freectx;
 static OSSL_OP_serializer_serialize_data_fn ecx_pub_der_data;
 static OSSL_OP_serializer_serialize_object_fn ecx_pub_der;
@@ -57,13 +61,24 @@ static void *x448_pub_newctx(void *provctx)
     return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
 }
 
+static void *ed25519_pub_newctx(void *provctx)
+{
+    return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED25519);
+}
+
+static void *ed448_pub_newctx(void *provctx)
+{
+    return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED448);
+}
+
 static void ecx_pub_freectx(void *ctx)
 {
     OPENSSL_free(ctx);
 }
 
 /* Public key : DER */
-static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
+static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[],
+                            OSSL_CORE_BIO *out,
                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
     struct ecx_pub_ctx_st *ctx = vctx;
@@ -86,20 +101,28 @@ static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
     return ok;
 }
 
-static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out,
+static int ecx_pub_der(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
     struct ecx_pub_ctx_st *ctx = vctx;
+    BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
+    int ret;
+
+    if (out == NULL)
+        return 0;
 
-    return ossl_prov_write_pub_der_from_obj(out, ecxkey,
-                                            ctx->type == ECX_KEY_TYPE_X25519
-                                            ? EVP_PKEY_X25519 : EVP_PKEY_X448,
-                                            NULL,
-                                            ossl_prov_ecx_pub_to_der);
+    ret = ossl_prov_write_pub_der_from_obj(out, ecxkey,
+                                           KEYTYPE2NID(ctx->type),
+                                           NULL,
+                                           ossl_prov_ecx_pub_to_der);
+    BIO_free(out);
+
+    return ret;
 }
 
 /* Public key : PEM */
-static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
+static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[],
+                            OSSL_CORE_BIO *out,
                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
     struct ecx_pub_ctx_st *ctx = vctx;
@@ -122,20 +145,27 @@ static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
     return ok;
 }
 
-static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out,
+static int ecx_pub_pem(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
     struct ecx_pub_ctx_st *ctx = vctx;
+    BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
+    int ret;
+
+    if (out == NULL)
+        return 0;
 
-    return ossl_prov_write_pub_pem_from_obj(out, ecxkey,
-                                            ctx->type == ECX_KEY_TYPE_X25519
-                                            ? EVP_PKEY_X25519 : EVP_PKEY_X448,
-                                            NULL,
-                                            ossl_prov_ecx_pub_to_der);
+    ret = ossl_prov_write_pub_pem_from_obj(out, ecxkey,
+                                           KEYTYPE2NID(ctx->type),
+                                           NULL,
+                                           ossl_prov_ecx_pub_to_der);
+    BIO_free(out);
 
+    return ret;
 }
 
-static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
+static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[],
+                              OSSL_CORE_BIO *out,
                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
     struct ecx_pub_ctx_st *ctx = vctx;
@@ -158,10 +188,20 @@ static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
     return ok;
 }
 
-static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
+static int ecx_pub_print(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
 {
-    return ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
+    struct ecx_pub_ctx_st *ctx = vctx;
+    BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
+    int ret;
+
+    if (out == NULL)
+        return 0;
+
+    ret = ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
+    BIO_free(out);
+
+    return ret;
 }
 
 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
@@ -182,3 +222,5 @@ static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
 
 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
+MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
+MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)