1 // SPDX-License-Identifier: GPL-2.0
3 * ECDSA P1363 signature encoding
5 * Copyright (c) 2024 Intel Corporation
9 #include <linux/module.h>
10 #include <crypto/algapi.h>
11 #include <crypto/sig.h>
12 #include <crypto/internal/ecc.h>
13 #include <crypto/internal/sig.h>
15 struct ecdsa_p1363_ctx
{
16 struct crypto_sig
*child
;
19 static int ecdsa_p1363_verify(struct crypto_sig
*tfm
,
20 const void *src
, unsigned int slen
,
21 const void *digest
, unsigned int dlen
)
23 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
24 unsigned int keylen
= crypto_sig_keysize(ctx
->child
);
25 unsigned int ndigits
= DIV_ROUND_UP(keylen
, sizeof(u64
));
26 struct ecdsa_raw_sig sig
;
28 if (slen
!= 2 * keylen
)
31 ecc_digits_from_bytes(src
, keylen
, sig
.r
, ndigits
);
32 ecc_digits_from_bytes(src
+ keylen
, keylen
, sig
.s
, ndigits
);
34 return crypto_sig_verify(ctx
->child
, &sig
, sizeof(sig
), digest
, dlen
);
37 static unsigned int ecdsa_p1363_key_size(struct crypto_sig
*tfm
)
39 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
41 return crypto_sig_keysize(ctx
->child
);
44 static unsigned int ecdsa_p1363_max_size(struct crypto_sig
*tfm
)
46 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
48 return 2 * crypto_sig_keysize(ctx
->child
);
51 static unsigned int ecdsa_p1363_digest_size(struct crypto_sig
*tfm
)
53 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
55 return crypto_sig_digestsize(ctx
->child
);
58 static int ecdsa_p1363_set_pub_key(struct crypto_sig
*tfm
,
59 const void *key
, unsigned int keylen
)
61 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
63 return crypto_sig_set_pubkey(ctx
->child
, key
, keylen
);
66 static int ecdsa_p1363_init_tfm(struct crypto_sig
*tfm
)
68 struct sig_instance
*inst
= sig_alg_instance(tfm
);
69 struct crypto_sig_spawn
*spawn
= sig_instance_ctx(inst
);
70 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
71 struct crypto_sig
*child_tfm
;
73 child_tfm
= crypto_spawn_sig(spawn
);
74 if (IS_ERR(child_tfm
))
75 return PTR_ERR(child_tfm
);
77 ctx
->child
= child_tfm
;
82 static void ecdsa_p1363_exit_tfm(struct crypto_sig
*tfm
)
84 struct ecdsa_p1363_ctx
*ctx
= crypto_sig_ctx(tfm
);
86 crypto_free_sig(ctx
->child
);
89 static void ecdsa_p1363_free(struct sig_instance
*inst
)
91 struct crypto_sig_spawn
*spawn
= sig_instance_ctx(inst
);
93 crypto_drop_sig(spawn
);
97 static int ecdsa_p1363_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
99 struct crypto_sig_spawn
*spawn
;
100 struct sig_instance
*inst
;
101 struct sig_alg
*ecdsa_alg
;
105 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SIG
, &mask
);
109 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
113 spawn
= sig_instance_ctx(inst
);
115 err
= crypto_grab_sig(spawn
, sig_crypto_instance(inst
),
116 crypto_attr_alg_name(tb
[1]), 0, mask
);
120 ecdsa_alg
= crypto_spawn_sig_alg(spawn
);
123 if (strncmp(ecdsa_alg
->base
.cra_name
, "ecdsa", 5) != 0)
126 err
= crypto_inst_setname(sig_crypto_instance(inst
), tmpl
->name
,
131 inst
->alg
.base
.cra_priority
= ecdsa_alg
->base
.cra_priority
;
132 inst
->alg
.base
.cra_ctxsize
= sizeof(struct ecdsa_p1363_ctx
);
134 inst
->alg
.init
= ecdsa_p1363_init_tfm
;
135 inst
->alg
.exit
= ecdsa_p1363_exit_tfm
;
137 inst
->alg
.verify
= ecdsa_p1363_verify
;
138 inst
->alg
.key_size
= ecdsa_p1363_key_size
;
139 inst
->alg
.max_size
= ecdsa_p1363_max_size
;
140 inst
->alg
.digest_size
= ecdsa_p1363_digest_size
;
141 inst
->alg
.set_pub_key
= ecdsa_p1363_set_pub_key
;
143 inst
->free
= ecdsa_p1363_free
;
145 err
= sig_register_instance(tmpl
, inst
);
148 ecdsa_p1363_free(inst
);
153 struct crypto_template ecdsa_p1363_tmpl
= {
155 .create
= ecdsa_p1363_create
,
156 .module
= THIS_MODULE
,
159 MODULE_ALIAS_CRYPTO("p1363");