0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _CRYPTO_KPP_
0010 #define _CRYPTO_KPP_
0011 #include <linux/crypto.h>
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 struct kpp_request {
0028 struct crypto_async_request base;
0029 struct scatterlist *src;
0030 struct scatterlist *dst;
0031 unsigned int src_len;
0032 unsigned int dst_len;
0033 void *__ctx[] CRYPTO_MINALIGN_ATTR;
0034 };
0035
0036
0037
0038
0039
0040
0041
0042 struct crypto_kpp {
0043 struct crypto_tfm base;
0044 };
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 struct kpp_alg {
0072 int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
0073 unsigned int len);
0074 int (*generate_public_key)(struct kpp_request *req);
0075 int (*compute_shared_secret)(struct kpp_request *req);
0076
0077 unsigned int (*max_size)(struct crypto_kpp *tfm);
0078
0079 int (*init)(struct crypto_kpp *tfm);
0080 void (*exit)(struct crypto_kpp *tfm);
0081
0082 unsigned int reqsize;
0083 struct crypto_alg base;
0084 };
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
0106
0107 int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
0108
0109 static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
0110 {
0111 return &tfm->base;
0112 }
0113
0114 static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
0115 {
0116 return container_of(alg, struct kpp_alg, base);
0117 }
0118
0119 static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
0120 {
0121 return container_of(tfm, struct crypto_kpp, base);
0122 }
0123
0124 static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
0125 {
0126 return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
0127 }
0128
0129 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
0130 {
0131 return crypto_kpp_alg(tfm)->reqsize;
0132 }
0133
0134 static inline void kpp_request_set_tfm(struct kpp_request *req,
0135 struct crypto_kpp *tfm)
0136 {
0137 req->base.tfm = crypto_kpp_tfm(tfm);
0138 }
0139
0140 static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
0141 {
0142 return __crypto_kpp_tfm(req->base.tfm);
0143 }
0144
0145 static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
0146 {
0147 return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
0148 }
0149
0150 static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
0151 {
0152 crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162 static inline void crypto_free_kpp(struct crypto_kpp *tfm)
0163 {
0164 crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
0165 }
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
0176 gfp_t gfp)
0177 {
0178 struct kpp_request *req;
0179
0180 req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
0181 if (likely(req))
0182 kpp_request_set_tfm(req, tfm);
0183
0184 return req;
0185 }
0186
0187
0188
0189
0190
0191
0192 static inline void kpp_request_free(struct kpp_request *req)
0193 {
0194 kfree_sensitive(req);
0195 }
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 static inline void kpp_request_set_callback(struct kpp_request *req,
0209 u32 flgs,
0210 crypto_completion_t cmpl,
0211 void *data)
0212 {
0213 req->base.complete = cmpl;
0214 req->base.data = data;
0215 req->base.flags = flgs;
0216 }
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227 static inline void kpp_request_set_input(struct kpp_request *req,
0228 struct scatterlist *input,
0229 unsigned int input_len)
0230 {
0231 req->src = input;
0232 req->src_len = input_len;
0233 }
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 static inline void kpp_request_set_output(struct kpp_request *req,
0245 struct scatterlist *output,
0246 unsigned int output_len)
0247 {
0248 req->dst = output;
0249 req->dst_len = output_len;
0250 }
0251
0252 enum {
0253 CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
0254 CRYPTO_KPP_SECRET_TYPE_DH,
0255 CRYPTO_KPP_SECRET_TYPE_ECDH,
0256 };
0257
0258
0259
0260
0261
0262
0263
0264
0265 struct kpp_secret {
0266 unsigned short type;
0267 unsigned short len;
0268 };
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285 static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
0286 const void *buffer, unsigned int len)
0287 {
0288 struct kpp_alg *alg = crypto_kpp_alg(tfm);
0289 struct crypto_alg *calg = tfm->base.__crt_alg;
0290 int ret;
0291
0292 crypto_stats_get(calg);
0293 ret = alg->set_secret(tfm, buffer, len);
0294 crypto_stats_kpp_set_secret(calg, ret);
0295 return ret;
0296 }
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
0312 {
0313 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
0314 struct kpp_alg *alg = crypto_kpp_alg(tfm);
0315 struct crypto_alg *calg = tfm->base.__crt_alg;
0316 int ret;
0317
0318 crypto_stats_get(calg);
0319 ret = alg->generate_public_key(req);
0320 crypto_stats_kpp_generate_public_key(calg, ret);
0321 return ret;
0322 }
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334 static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
0335 {
0336 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
0337 struct kpp_alg *alg = crypto_kpp_alg(tfm);
0338 struct crypto_alg *calg = tfm->base.__crt_alg;
0339 int ret;
0340
0341 crypto_stats_get(calg);
0342 ret = alg->compute_shared_secret(req);
0343 crypto_stats_kpp_compute_shared_secret(calg, ret);
0344 return ret;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357 static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
0358 {
0359 struct kpp_alg *alg = crypto_kpp_alg(tfm);
0360
0361 return alg->max_size(tfm);
0362 }
0363
0364 #endif