2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/core_dispatch.h>
11 #include <openssl/err.h>
12 #include <openssl/pem.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
15 #include "crypto/ecx.h"
17 #include "prov/implementations.h"
18 #include "prov/provider_ctx.h"
19 #include "encoder_local.h"
21 static OSSL_FUNC_encoder_newctx_fn x25519_pub_newctx
;
22 static OSSL_FUNC_encoder_newctx_fn x448_pub_newctx
;
23 static OSSL_FUNC_encoder_newctx_fn ed25519_pub_newctx
;
24 static OSSL_FUNC_encoder_newctx_fn ed448_pub_newctx
;
25 static OSSL_FUNC_encoder_freectx_fn ecx_pub_freectx
;
26 static OSSL_FUNC_encoder_encode_data_fn ecx_pub_der_data
;
27 static OSSL_FUNC_encoder_encode_object_fn ecx_pub_der
;
28 static OSSL_FUNC_encoder_encode_data_fn ecx_pub_pem_data
;
29 static OSSL_FUNC_encoder_encode_object_fn ecx_pub_pem
;
31 static OSSL_FUNC_encoder_encode_data_fn ecx_pub_print_data
;
32 static OSSL_FUNC_encoder_encode_object_fn ecx_pub_print
;
35 * Context used for public key encoding.
37 struct ecx_pub_ctx_st
{
42 /* Public key : context */
43 static void *ecx_pub_newctx(void *provctx
, ECX_KEY_TYPE type
)
45 struct ecx_pub_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
48 ctx
->provctx
= provctx
;
54 static void *x25519_pub_newctx(void *provctx
)
56 return ecx_pub_newctx(provctx
, ECX_KEY_TYPE_X25519
);
59 static void *x448_pub_newctx(void *provctx
)
61 return ecx_pub_newctx(provctx
, ECX_KEY_TYPE_X448
);
64 static void *ed25519_pub_newctx(void *provctx
)
66 return ecx_pub_newctx(provctx
, ECX_KEY_TYPE_ED25519
);
69 static void *ed448_pub_newctx(void *provctx
)
71 return ecx_pub_newctx(provctx
, ECX_KEY_TYPE_ED448
);
74 static void ecx_pub_freectx(void *ctx
)
79 /* Public key : DER */
80 static int ecx_pub_der_data(void *vctx
, const OSSL_PARAM params
[],
82 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
84 struct ecx_pub_ctx_st
*ctx
= vctx
;
85 OSSL_FUNC_keymgmt_new_fn
*ecx_new
;
86 OSSL_FUNC_keymgmt_free_fn
*ecx_free
;
87 OSSL_FUNC_keymgmt_import_fn
*ecx_import
;
90 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
92 if (ecx_import
!= NULL
) {
95 if ((ecxkey
= ecx_new(ctx
->provctx
)) != NULL
96 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
, params
)
97 && ecx_pub_der(ctx
, ecxkey
, out
, cb
, cbarg
))
104 static int ecx_pub_der(void *vctx
, void *ecxkey
, OSSL_CORE_BIO
*cout
,
105 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
107 struct ecx_pub_ctx_st
*ctx
= vctx
;
108 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
114 ret
= ossl_prov_write_pub_der_from_obj(out
, ecxkey
,
115 KEYTYPE2NID(ctx
->type
),
117 ossl_prov_ecx_pub_to_der
);
123 /* Public key : PEM */
124 static int ecx_pub_pem_data(void *vctx
, const OSSL_PARAM params
[],
126 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
128 struct ecx_pub_ctx_st
*ctx
= vctx
;
129 OSSL_FUNC_keymgmt_new_fn
*ecx_new
;
130 OSSL_FUNC_keymgmt_free_fn
*ecx_free
;
131 OSSL_FUNC_keymgmt_import_fn
*ecx_import
;
134 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
136 if (ecx_import
!= NULL
) {
139 if ((ecxkey
= ecx_new(ctx
->provctx
)) != NULL
140 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
, params
)
141 && ecx_pub_pem(ctx
, ecxkey
, out
, cb
, cbarg
))
148 static int ecx_pub_pem(void *vctx
, void *ecxkey
, OSSL_CORE_BIO
*cout
,
149 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
151 struct ecx_pub_ctx_st
*ctx
= vctx
;
152 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
158 ret
= ossl_prov_write_pub_pem_from_obj(out
, ecxkey
,
159 KEYTYPE2NID(ctx
->type
),
161 ossl_prov_ecx_pub_to_der
);
167 static int ecx_pub_print_data(void *vctx
, const OSSL_PARAM params
[],
169 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
171 struct ecx_pub_ctx_st
*ctx
= vctx
;
172 OSSL_FUNC_keymgmt_new_fn
*ecx_new
;
173 OSSL_FUNC_keymgmt_free_fn
*ecx_free
;
174 OSSL_FUNC_keymgmt_import_fn
*ecx_import
;
177 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
179 if (ecx_import
!= NULL
) {
182 if ((ecxkey
= ecx_new(ctx
)) != NULL
183 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
, params
)
184 && ecx_pub_print(ctx
, ecxkey
, out
, cb
, cbarg
))
191 static int ecx_pub_print(void *vctx
, void *ecxkey
, OSSL_CORE_BIO
*cout
,
192 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
194 struct ecx_pub_ctx_st
*ctx
= vctx
;
195 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
201 ret
= ossl_prov_print_ecx(out
, ecxkey
, ecx_print_pub
);
207 #define MAKE_ENCODER_FUNCTIONS(alg, type) \
208 const OSSL_DISPATCH alg##_pub_##type##_encoder_functions[] = { \
209 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))alg##_pub_newctx }, \
210 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ecx_pub_freectx }, \
211 { OSSL_FUNC_ENCODER_ENCODE_DATA, \
212 (void (*)(void))ecx_pub_##type##_data }, \
213 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, \
214 (void (*)(void))ecx_pub_##type }, \
218 #define MAKE_ENCODER_FUNCTIONS_GROUP(alg) \
219 MAKE_ENCODER_FUNCTIONS(alg, der) \
220 MAKE_ENCODER_FUNCTIONS(alg, pem) \
221 MAKE_ENCODER_FUNCTIONS(alg, print)
223 MAKE_ENCODER_FUNCTIONS_GROUP(x25519
)
224 MAKE_ENCODER_FUNCTIONS_GROUP(x448
)
225 MAKE_ENCODER_FUNCTIONS_GROUP(ed25519
)
226 MAKE_ENCODER_FUNCTIONS_GROUP(ed448
)