Skip to content

Commit 4e5f2c4

Browse files
Salvatore Benedettoherbertx
authored andcommitted
crypto: kpp - Key-agreement Protocol Primitives API (KPP)
Add key-agreement protocol primitives (kpp) API which allows to implement primitives required by protocols such as DH and ECDH. The API is composed mainly by the following functions * set_secret() - It allows the user to set his secret, also referred to as his private key, along with the parameters known to both parties involved in the key-agreement session. * generate_public_key() - It generates the public key to be sent to the other counterpart involved in the key-agreement session. The function has to be called after set_params() and set_secret() * generate_secret() - It generates the shared secret for the session Other functions such as init() and exit() are provided for allowing cryptographic hardware to be inizialized properly before use Signed-off-by: Salvatore Benedetto <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 331bf73 commit 4e5f2c4

File tree

8 files changed

+552
-0
lines changed

8 files changed

+552
-0
lines changed

crypto/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ config CRYPTO_AKCIPHER
9393
select CRYPTO_AKCIPHER2
9494
select CRYPTO_ALGAPI
9595

96+
config CRYPTO_KPP2
97+
tristate
98+
select CRYPTO_ALGAPI2
99+
100+
config CRYPTO_KPP
101+
tristate
102+
select CRYPTO_ALGAPI
103+
select CRYPTO_KPP2
104+
96105
config CRYPTO_RSA
97106
tristate "RSA algorithm"
98107
select CRYPTO_AKCIPHER
@@ -115,6 +124,7 @@ config CRYPTO_MANAGER2
115124
select CRYPTO_HASH2
116125
select CRYPTO_BLKCIPHER2
117126
select CRYPTO_AKCIPHER2
127+
select CRYPTO_KPP2
118128

119129
config CRYPTO_USER
120130
tristate "Userspace cryptographic algorithm configuration"

crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ crypto_hash-y += shash.o
3030
obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
3131

3232
obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
33+
obj-$(CONFIG_CRYPTO_KPP2) += kpp.o
3334

3435
$(obj)/rsapubkey-asn1.o: $(obj)/rsapubkey-asn1.c $(obj)/rsapubkey-asn1.h
3536
$(obj)/rsaprivkey-asn1.o: $(obj)/rsaprivkey-asn1.c $(obj)/rsaprivkey-asn1.h

crypto/crypto_user.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <crypto/internal/skcipher.h>
2929
#include <crypto/internal/rng.h>
3030
#include <crypto/akcipher.h>
31+
#include <crypto/kpp.h>
3132

3233
#include "internal.h"
3334

@@ -126,6 +127,21 @@ static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
126127
return -EMSGSIZE;
127128
}
128129

130+
static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
131+
{
132+
struct crypto_report_kpp rkpp;
133+
134+
strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
135+
136+
if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
137+
sizeof(struct crypto_report_kpp), &rkpp))
138+
goto nla_put_failure;
139+
return 0;
140+
141+
nla_put_failure:
142+
return -EMSGSIZE;
143+
}
144+
129145
static int crypto_report_one(struct crypto_alg *alg,
130146
struct crypto_user_alg *ualg, struct sk_buff *skb)
131147
{
@@ -176,6 +192,10 @@ static int crypto_report_one(struct crypto_alg *alg,
176192
goto nla_put_failure;
177193

178194
break;
195+
case CRYPTO_ALG_TYPE_KPP:
196+
if (crypto_report_kpp(skb, alg))
197+
goto nla_put_failure;
198+
break;
179199
}
180200

181201
out:

crypto/kpp.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Key-agreement Protocol Primitives (KPP)
3+
*
4+
* Copyright (c) 2016, Intel Corporation
5+
* Authors: Salvatore Benedetto <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU General Public License as published by the Free
9+
* Software Foundation; either version 2 of the License, or (at your option)
10+
* any later version.
11+
*
12+
*/
13+
#include <linux/errno.h>
14+
#include <linux/kernel.h>
15+
#include <linux/module.h>
16+
#include <linux/seq_file.h>
17+
#include <linux/slab.h>
18+
#include <linux/string.h>
19+
#include <linux/crypto.h>
20+
#include <crypto/algapi.h>
21+
#include <linux/cryptouser.h>
22+
#include <net/netlink.h>
23+
#include <crypto/kpp.h>
24+
#include <crypto/internal/kpp.h>
25+
#include "internal.h"
26+
27+
#ifdef CONFIG_NET
28+
static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
29+
{
30+
struct crypto_report_kpp rkpp;
31+
32+
strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
33+
34+
if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
35+
sizeof(struct crypto_report_kpp), &rkpp))
36+
goto nla_put_failure;
37+
return 0;
38+
39+
nla_put_failure:
40+
return -EMSGSIZE;
41+
}
42+
#else
43+
static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
44+
{
45+
return -ENOSYS;
46+
}
47+
#endif
48+
49+
static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
50+
__attribute__ ((unused));
51+
52+
static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
53+
{
54+
seq_puts(m, "type : kpp\n");
55+
}
56+
57+
static void crypto_kpp_exit_tfm(struct crypto_tfm *tfm)
58+
{
59+
struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
60+
struct kpp_alg *alg = crypto_kpp_alg(kpp);
61+
62+
alg->exit(kpp);
63+
}
64+
65+
static int crypto_kpp_init_tfm(struct crypto_tfm *tfm)
66+
{
67+
struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
68+
struct kpp_alg *alg = crypto_kpp_alg(kpp);
69+
70+
if (alg->exit)
71+
kpp->base.exit = crypto_kpp_exit_tfm;
72+
73+
if (alg->init)
74+
return alg->init(kpp);
75+
76+
return 0;
77+
}
78+
79+
static const struct crypto_type crypto_kpp_type = {
80+
.extsize = crypto_alg_extsize,
81+
.init_tfm = crypto_kpp_init_tfm,
82+
#ifdef CONFIG_PROC_FS
83+
.show = crypto_kpp_show,
84+
#endif
85+
.report = crypto_kpp_report,
86+
.maskclear = ~CRYPTO_ALG_TYPE_MASK,
87+
.maskset = CRYPTO_ALG_TYPE_MASK,
88+
.type = CRYPTO_ALG_TYPE_KPP,
89+
.tfmsize = offsetof(struct crypto_kpp, base),
90+
};
91+
92+
struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
93+
{
94+
return crypto_alloc_tfm(alg_name, &crypto_kpp_type, type, mask);
95+
}
96+
EXPORT_SYMBOL_GPL(crypto_alloc_kpp);
97+
98+
static void kpp_prepare_alg(struct kpp_alg *alg)
99+
{
100+
struct crypto_alg *base = &alg->base;
101+
102+
base->cra_type = &crypto_kpp_type;
103+
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
104+
base->cra_flags |= CRYPTO_ALG_TYPE_KPP;
105+
}
106+
107+
int crypto_register_kpp(struct kpp_alg *alg)
108+
{
109+
struct crypto_alg *base = &alg->base;
110+
111+
kpp_prepare_alg(alg);
112+
return crypto_register_alg(base);
113+
}
114+
EXPORT_SYMBOL_GPL(crypto_register_kpp);
115+
116+
void crypto_unregister_kpp(struct kpp_alg *alg)
117+
{
118+
crypto_unregister_alg(&alg->base);
119+
}
120+
EXPORT_SYMBOL_GPL(crypto_unregister_kpp);
121+
122+
MODULE_LICENSE("GPL");
123+
MODULE_DESCRIPTION("Key-agreement Protocol Primitives");

include/crypto/internal/kpp.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Key-agreement Protocol Primitives (KPP)
3+
*
4+
* Copyright (c) 2016, Intel Corporation
5+
* Authors: Salvatore Benedetto <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU General Public License as published by the Free
9+
* Software Foundation; either version 2 of the License, or (at your option)
10+
* any later version.
11+
*
12+
*/
13+
#ifndef _CRYPTO_KPP_INT_H
14+
#define _CRYPTO_KPP_INT_H
15+
#include <crypto/kpp.h>
16+
#include <crypto/algapi.h>
17+
18+
/*
19+
* Transform internal helpers.
20+
*/
21+
static inline void *kpp_request_ctx(struct kpp_request *req)
22+
{
23+
return req->__ctx;
24+
}
25+
26+
static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
27+
{
28+
return tfm->base.__crt_ctx;
29+
}
30+
31+
static inline void kpp_request_complete(struct kpp_request *req, int err)
32+
{
33+
req->base.complete(&req->base, err);
34+
}
35+
36+
static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
37+
{
38+
return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
39+
}
40+
41+
/**
42+
* crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
43+
*
44+
* Function registers an implementation of a key-agreement protocol primitive
45+
* algorithm
46+
*
47+
* @alg: algorithm definition
48+
*
49+
* Return: zero on success; error code in case of error
50+
*/
51+
int crypto_register_kpp(struct kpp_alg *alg);
52+
53+
/**
54+
* crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
55+
* algorithm
56+
*
57+
* Function unregisters an implementation of a key-agreement protocol primitive
58+
* algorithm
59+
*
60+
* @alg: algorithm definition
61+
*/
62+
void crypto_unregister_kpp(struct kpp_alg *alg);
63+
64+
#endif

0 commit comments

Comments
 (0)