14e5f2c40SSalvatore Benedetto /* 24e5f2c40SSalvatore Benedetto * Key-agreement Protocol Primitives (KPP) 34e5f2c40SSalvatore Benedetto * 44e5f2c40SSalvatore Benedetto * Copyright (c) 2016, Intel Corporation 54e5f2c40SSalvatore Benedetto * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> 64e5f2c40SSalvatore Benedetto * 74e5f2c40SSalvatore Benedetto * This program is free software; you can redistribute it and/or modify it 84e5f2c40SSalvatore Benedetto * under the terms of the GNU General Public License as published by the Free 94e5f2c40SSalvatore Benedetto * Software Foundation; either version 2 of the License, or (at your option) 104e5f2c40SSalvatore Benedetto * any later version. 114e5f2c40SSalvatore Benedetto * 124e5f2c40SSalvatore Benedetto */ 134e5f2c40SSalvatore Benedetto 144e5f2c40SSalvatore Benedetto #ifndef _CRYPTO_KPP_ 154e5f2c40SSalvatore Benedetto #define _CRYPTO_KPP_ 164e5f2c40SSalvatore Benedetto #include <linux/crypto.h> 174e5f2c40SSalvatore Benedetto 184e5f2c40SSalvatore Benedetto /** 194e5f2c40SSalvatore Benedetto * struct kpp_request 204e5f2c40SSalvatore Benedetto * 214e5f2c40SSalvatore Benedetto * @base: Common attributes for async crypto requests 224e5f2c40SSalvatore Benedetto * @src: Source data 234e5f2c40SSalvatore Benedetto * @dst: Destination data 244e5f2c40SSalvatore Benedetto * @src_len: Size of the input buffer 254e5f2c40SSalvatore Benedetto * @dst_len: Size of the output buffer. It needs to be at least 264e5f2c40SSalvatore Benedetto * as big as the expected result depending on the operation 274e5f2c40SSalvatore Benedetto * After operation it will be updated with the actual size of the 284e5f2c40SSalvatore Benedetto * result. In case of error where the dst sgl size was insufficient, 294e5f2c40SSalvatore Benedetto * it will be updated to the size required for the operation. 304e5f2c40SSalvatore Benedetto * @__ctx: Start of private context data 314e5f2c40SSalvatore Benedetto */ 324e5f2c40SSalvatore Benedetto struct kpp_request { 334e5f2c40SSalvatore Benedetto struct crypto_async_request base; 344e5f2c40SSalvatore Benedetto struct scatterlist *src; 354e5f2c40SSalvatore Benedetto struct scatterlist *dst; 364e5f2c40SSalvatore Benedetto unsigned int src_len; 374e5f2c40SSalvatore Benedetto unsigned int dst_len; 384e5f2c40SSalvatore Benedetto void *__ctx[] CRYPTO_MINALIGN_ATTR; 394e5f2c40SSalvatore Benedetto }; 404e5f2c40SSalvatore Benedetto 414e5f2c40SSalvatore Benedetto /** 424e5f2c40SSalvatore Benedetto * struct crypto_kpp - user-instantiated object which encapsulate 434e5f2c40SSalvatore Benedetto * algorithms and core processing logic 444e5f2c40SSalvatore Benedetto * 454e5f2c40SSalvatore Benedetto * @base: Common crypto API algorithm data structure 464e5f2c40SSalvatore Benedetto */ 474e5f2c40SSalvatore Benedetto struct crypto_kpp { 484e5f2c40SSalvatore Benedetto struct crypto_tfm base; 494e5f2c40SSalvatore Benedetto }; 504e5f2c40SSalvatore Benedetto 514e5f2c40SSalvatore Benedetto /** 524e5f2c40SSalvatore Benedetto * struct kpp_alg - generic key-agreement protocol primitives 534e5f2c40SSalvatore Benedetto * 544e5f2c40SSalvatore Benedetto * @set_secret: Function invokes the protocol specific function to 554e5f2c40SSalvatore Benedetto * store the secret private key along with parameters. 564e5f2c40SSalvatore Benedetto * The implementation knows how to decode thie buffer 574e5f2c40SSalvatore Benedetto * @generate_public_key: Function generate the public key to be sent to the 584e5f2c40SSalvatore Benedetto * counterpart. In case of error, where output is not big 594e5f2c40SSalvatore Benedetto * enough req->dst_len will be updated to the size 604e5f2c40SSalvatore Benedetto * required 614e5f2c40SSalvatore Benedetto * @compute_shared_secret: Function compute the shared secret as defined by 624e5f2c40SSalvatore Benedetto * the algorithm. The result is given back to the user. 634e5f2c40SSalvatore Benedetto * In case of error, where output is not big enough, 644e5f2c40SSalvatore Benedetto * req->dst_len will be updated to the size required 654e5f2c40SSalvatore Benedetto * @max_size: Function returns the size of the output buffer 664e5f2c40SSalvatore Benedetto * @init: Initialize the object. This is called only once at 674e5f2c40SSalvatore Benedetto * instantiation time. In case the cryptographic hardware 684e5f2c40SSalvatore Benedetto * needs to be initialized. Software fallback should be 694e5f2c40SSalvatore Benedetto * put in place here. 704e5f2c40SSalvatore Benedetto * @exit: Undo everything @init did. 714e5f2c40SSalvatore Benedetto * 724e5f2c40SSalvatore Benedetto * @reqsize: Request context size required by algorithm 734e5f2c40SSalvatore Benedetto * implementation 744e5f2c40SSalvatore Benedetto * @base Common crypto API algorithm data structure 754e5f2c40SSalvatore Benedetto */ 764e5f2c40SSalvatore Benedetto struct kpp_alg { 774e5f2c40SSalvatore Benedetto int (*set_secret)(struct crypto_kpp *tfm, void *buffer, 784e5f2c40SSalvatore Benedetto unsigned int len); 794e5f2c40SSalvatore Benedetto int (*generate_public_key)(struct kpp_request *req); 804e5f2c40SSalvatore Benedetto int (*compute_shared_secret)(struct kpp_request *req); 814e5f2c40SSalvatore Benedetto 824e5f2c40SSalvatore Benedetto int (*max_size)(struct crypto_kpp *tfm); 834e5f2c40SSalvatore Benedetto 844e5f2c40SSalvatore Benedetto int (*init)(struct crypto_kpp *tfm); 854e5f2c40SSalvatore Benedetto void (*exit)(struct crypto_kpp *tfm); 864e5f2c40SSalvatore Benedetto 874e5f2c40SSalvatore Benedetto unsigned int reqsize; 884e5f2c40SSalvatore Benedetto struct crypto_alg base; 894e5f2c40SSalvatore Benedetto }; 904e5f2c40SSalvatore Benedetto 914e5f2c40SSalvatore Benedetto /** 924e5f2c40SSalvatore Benedetto * DOC: Generic Key-agreement Protocol Primitevs API 934e5f2c40SSalvatore Benedetto * 944e5f2c40SSalvatore Benedetto * The KPP API is used with the algorithm type 954e5f2c40SSalvatore Benedetto * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto) 964e5f2c40SSalvatore Benedetto */ 974e5f2c40SSalvatore Benedetto 984e5f2c40SSalvatore Benedetto /** 994e5f2c40SSalvatore Benedetto * crypto_alloc_kpp() - allocate KPP tfm handle 1004e5f2c40SSalvatore Benedetto * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh") 1014e5f2c40SSalvatore Benedetto * @type: specifies the type of the algorithm 1024e5f2c40SSalvatore Benedetto * @mask: specifies the mask for the algorithm 1034e5f2c40SSalvatore Benedetto * 1044e5f2c40SSalvatore Benedetto * Allocate a handle for kpp algorithm. The returned struct crypto_kpp 1054e5f2c40SSalvatore Benedetto * is requeried for any following API invocation 1064e5f2c40SSalvatore Benedetto * 1074e5f2c40SSalvatore Benedetto * Return: allocated handle in case of success; IS_ERR() is true in case of 1084e5f2c40SSalvatore Benedetto * an error, PTR_ERR() returns the error code. 1094e5f2c40SSalvatore Benedetto */ 1104e5f2c40SSalvatore Benedetto struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask); 1114e5f2c40SSalvatore Benedetto 1124e5f2c40SSalvatore Benedetto static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm) 1134e5f2c40SSalvatore Benedetto { 1144e5f2c40SSalvatore Benedetto return &tfm->base; 1154e5f2c40SSalvatore Benedetto } 1164e5f2c40SSalvatore Benedetto 1174e5f2c40SSalvatore Benedetto static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg) 1184e5f2c40SSalvatore Benedetto { 1194e5f2c40SSalvatore Benedetto return container_of(alg, struct kpp_alg, base); 1204e5f2c40SSalvatore Benedetto } 1214e5f2c40SSalvatore Benedetto 1224e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm) 1234e5f2c40SSalvatore Benedetto { 1244e5f2c40SSalvatore Benedetto return container_of(tfm, struct crypto_kpp, base); 1254e5f2c40SSalvatore Benedetto } 1264e5f2c40SSalvatore Benedetto 1274e5f2c40SSalvatore Benedetto static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm) 1284e5f2c40SSalvatore Benedetto { 1294e5f2c40SSalvatore Benedetto return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg); 1304e5f2c40SSalvatore Benedetto } 1314e5f2c40SSalvatore Benedetto 1324e5f2c40SSalvatore Benedetto static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm) 1334e5f2c40SSalvatore Benedetto { 1344e5f2c40SSalvatore Benedetto return crypto_kpp_alg(tfm)->reqsize; 1354e5f2c40SSalvatore Benedetto } 1364e5f2c40SSalvatore Benedetto 1374e5f2c40SSalvatore Benedetto static inline void kpp_request_set_tfm(struct kpp_request *req, 1384e5f2c40SSalvatore Benedetto struct crypto_kpp *tfm) 1394e5f2c40SSalvatore Benedetto { 1404e5f2c40SSalvatore Benedetto req->base.tfm = crypto_kpp_tfm(tfm); 1414e5f2c40SSalvatore Benedetto } 1424e5f2c40SSalvatore Benedetto 1434e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req) 1444e5f2c40SSalvatore Benedetto { 1454e5f2c40SSalvatore Benedetto return __crypto_kpp_tfm(req->base.tfm); 1464e5f2c40SSalvatore Benedetto } 1474e5f2c40SSalvatore Benedetto 1484e5f2c40SSalvatore Benedetto /** 1494e5f2c40SSalvatore Benedetto * crypto_free_kpp() - free KPP tfm handle 1504e5f2c40SSalvatore Benedetto * 1514e5f2c40SSalvatore Benedetto * @tfm: KPP tfm handle allocated with crypto_alloc_kpp() 1524e5f2c40SSalvatore Benedetto */ 1534e5f2c40SSalvatore Benedetto static inline void crypto_free_kpp(struct crypto_kpp *tfm) 1544e5f2c40SSalvatore Benedetto { 1554e5f2c40SSalvatore Benedetto crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm)); 1564e5f2c40SSalvatore Benedetto } 1574e5f2c40SSalvatore Benedetto 1584e5f2c40SSalvatore Benedetto /** 1594e5f2c40SSalvatore Benedetto * kpp_request_alloc() - allocates kpp request 1604e5f2c40SSalvatore Benedetto * 1614e5f2c40SSalvatore Benedetto * @tfm: KPP tfm handle allocated with crypto_alloc_kpp() 1624e5f2c40SSalvatore Benedetto * @gfp: allocation flags 1634e5f2c40SSalvatore Benedetto * 1644e5f2c40SSalvatore Benedetto * Return: allocated handle in case of success or NULL in case of an error. 1654e5f2c40SSalvatore Benedetto */ 1664e5f2c40SSalvatore Benedetto static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm, 1674e5f2c40SSalvatore Benedetto gfp_t gfp) 1684e5f2c40SSalvatore Benedetto { 1694e5f2c40SSalvatore Benedetto struct kpp_request *req; 1704e5f2c40SSalvatore Benedetto 1714e5f2c40SSalvatore Benedetto req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp); 1724e5f2c40SSalvatore Benedetto if (likely(req)) 1734e5f2c40SSalvatore Benedetto kpp_request_set_tfm(req, tfm); 1744e5f2c40SSalvatore Benedetto 1754e5f2c40SSalvatore Benedetto return req; 1764e5f2c40SSalvatore Benedetto } 1774e5f2c40SSalvatore Benedetto 1784e5f2c40SSalvatore Benedetto /** 1794e5f2c40SSalvatore Benedetto * kpp_request_free() - zeroize and free kpp request 1804e5f2c40SSalvatore Benedetto * 1814e5f2c40SSalvatore Benedetto * @req: request to free 1824e5f2c40SSalvatore Benedetto */ 1834e5f2c40SSalvatore Benedetto static inline void kpp_request_free(struct kpp_request *req) 1844e5f2c40SSalvatore Benedetto { 1854e5f2c40SSalvatore Benedetto kzfree(req); 1864e5f2c40SSalvatore Benedetto } 1874e5f2c40SSalvatore Benedetto 1884e5f2c40SSalvatore Benedetto /** 1894e5f2c40SSalvatore Benedetto * kpp_request_set_callback() - Sets an asynchronous callback. 1904e5f2c40SSalvatore Benedetto * 1914e5f2c40SSalvatore Benedetto * Callback will be called when an asynchronous operation on a given 1924e5f2c40SSalvatore Benedetto * request is finished. 1934e5f2c40SSalvatore Benedetto * 1944e5f2c40SSalvatore Benedetto * @req: request that the callback will be set for 1954e5f2c40SSalvatore Benedetto * @flgs: specify for instance if the operation may backlog 1964e5f2c40SSalvatore Benedetto * @cmpl: callback which will be called 1974e5f2c40SSalvatore Benedetto * @data: private data used by the caller 1984e5f2c40SSalvatore Benedetto */ 1994e5f2c40SSalvatore Benedetto static inline void kpp_request_set_callback(struct kpp_request *req, 2004e5f2c40SSalvatore Benedetto u32 flgs, 2014e5f2c40SSalvatore Benedetto crypto_completion_t cmpl, 2024e5f2c40SSalvatore Benedetto void *data) 2034e5f2c40SSalvatore Benedetto { 2044e5f2c40SSalvatore Benedetto req->base.complete = cmpl; 2054e5f2c40SSalvatore Benedetto req->base.data = data; 2064e5f2c40SSalvatore Benedetto req->base.flags = flgs; 2074e5f2c40SSalvatore Benedetto } 2084e5f2c40SSalvatore Benedetto 2094e5f2c40SSalvatore Benedetto /** 2104e5f2c40SSalvatore Benedetto * kpp_request_set_input() - Sets input buffer 2114e5f2c40SSalvatore Benedetto * 2124e5f2c40SSalvatore Benedetto * Sets parameters required by generate_public_key 2134e5f2c40SSalvatore Benedetto * 2144e5f2c40SSalvatore Benedetto * @req: kpp request 2154e5f2c40SSalvatore Benedetto * @input: ptr to input scatter list 2164e5f2c40SSalvatore Benedetto * @input_len: size of the input scatter list 2174e5f2c40SSalvatore Benedetto */ 2184e5f2c40SSalvatore Benedetto static inline void kpp_request_set_input(struct kpp_request *req, 2194e5f2c40SSalvatore Benedetto struct scatterlist *input, 2204e5f2c40SSalvatore Benedetto unsigned int input_len) 2214e5f2c40SSalvatore Benedetto { 2224e5f2c40SSalvatore Benedetto req->src = input; 2234e5f2c40SSalvatore Benedetto req->src_len = input_len; 2244e5f2c40SSalvatore Benedetto } 2254e5f2c40SSalvatore Benedetto 2264e5f2c40SSalvatore Benedetto /** 2274e5f2c40SSalvatore Benedetto * kpp_request_set_output() - Sets output buffer 2284e5f2c40SSalvatore Benedetto * 2294e5f2c40SSalvatore Benedetto * Sets parameters required by kpp operation 2304e5f2c40SSalvatore Benedetto * 2314e5f2c40SSalvatore Benedetto * @req: kpp request 2324e5f2c40SSalvatore Benedetto * @output: ptr to output scatter list 2334e5f2c40SSalvatore Benedetto * @output_len: size of the output scatter list 2344e5f2c40SSalvatore Benedetto */ 2354e5f2c40SSalvatore Benedetto static inline void kpp_request_set_output(struct kpp_request *req, 2364e5f2c40SSalvatore Benedetto struct scatterlist *output, 2374e5f2c40SSalvatore Benedetto unsigned int output_len) 2384e5f2c40SSalvatore Benedetto { 2394e5f2c40SSalvatore Benedetto req->dst = output; 2404e5f2c40SSalvatore Benedetto req->dst_len = output_len; 2414e5f2c40SSalvatore Benedetto } 2424e5f2c40SSalvatore Benedetto 2434e5f2c40SSalvatore Benedetto enum { 2444e5f2c40SSalvatore Benedetto CRYPTO_KPP_SECRET_TYPE_UNKNOWN, 245802c7f1cSSalvatore Benedetto CRYPTO_KPP_SECRET_TYPE_DH, 2463c4b2390SSalvatore Benedetto CRYPTO_KPP_SECRET_TYPE_ECDH, 2474e5f2c40SSalvatore Benedetto }; 2484e5f2c40SSalvatore Benedetto 2494e5f2c40SSalvatore Benedetto /** 2504e5f2c40SSalvatore Benedetto * struct kpp_secret - small header for packing secret buffer 2514e5f2c40SSalvatore Benedetto * 2524e5f2c40SSalvatore Benedetto * @type: define type of secret. Each kpp type will define its own 2534e5f2c40SSalvatore Benedetto * @len: specify the len of the secret, include the header, that 2544e5f2c40SSalvatore Benedetto * follows the struct 2554e5f2c40SSalvatore Benedetto */ 2564e5f2c40SSalvatore Benedetto struct kpp_secret { 2574e5f2c40SSalvatore Benedetto unsigned short type; 2584e5f2c40SSalvatore Benedetto unsigned short len; 2594e5f2c40SSalvatore Benedetto }; 2604e5f2c40SSalvatore Benedetto 2614e5f2c40SSalvatore Benedetto /** 2624e5f2c40SSalvatore Benedetto * crypto_kpp_set_secret() - Invoke kpp operation 2634e5f2c40SSalvatore Benedetto * 2644e5f2c40SSalvatore Benedetto * Function invokes the specific kpp operation for a given alg. 2654e5f2c40SSalvatore Benedetto * 2664e5f2c40SSalvatore Benedetto * @tfm: tfm handle 2674e5f2c40SSalvatore Benedetto * 2684e5f2c40SSalvatore Benedetto * Return: zero on success; error code in case of error 2694e5f2c40SSalvatore Benedetto */ 2704e5f2c40SSalvatore Benedetto static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm, void *buffer, 2714e5f2c40SSalvatore Benedetto unsigned int len) 2724e5f2c40SSalvatore Benedetto { 2734e5f2c40SSalvatore Benedetto struct kpp_alg *alg = crypto_kpp_alg(tfm); 2744e5f2c40SSalvatore Benedetto 2754e5f2c40SSalvatore Benedetto return alg->set_secret(tfm, buffer, len); 2764e5f2c40SSalvatore Benedetto } 2774e5f2c40SSalvatore Benedetto 2784e5f2c40SSalvatore Benedetto /** 2794e5f2c40SSalvatore Benedetto * crypto_kpp_generate_public_key() - Invoke kpp operation 2804e5f2c40SSalvatore Benedetto * 2814e5f2c40SSalvatore Benedetto * Function invokes the specific kpp operation for generating the public part 2824e5f2c40SSalvatore Benedetto * for a given kpp algorithm 2834e5f2c40SSalvatore Benedetto * 2844e5f2c40SSalvatore Benedetto * @req: kpp key request 2854e5f2c40SSalvatore Benedetto * 2864e5f2c40SSalvatore Benedetto * Return: zero on success; error code in case of error 2874e5f2c40SSalvatore Benedetto */ 2884e5f2c40SSalvatore Benedetto static inline int crypto_kpp_generate_public_key(struct kpp_request *req) 2894e5f2c40SSalvatore Benedetto { 2904e5f2c40SSalvatore Benedetto struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); 2914e5f2c40SSalvatore Benedetto struct kpp_alg *alg = crypto_kpp_alg(tfm); 2924e5f2c40SSalvatore Benedetto 2934e5f2c40SSalvatore Benedetto return alg->generate_public_key(req); 2944e5f2c40SSalvatore Benedetto } 2954e5f2c40SSalvatore Benedetto 2964e5f2c40SSalvatore Benedetto /** 2974e5f2c40SSalvatore Benedetto * crypto_kpp_compute_shared_secret() - Invoke kpp operation 2984e5f2c40SSalvatore Benedetto * 2994e5f2c40SSalvatore Benedetto * Function invokes the specific kpp operation for computing the shared secret 3004e5f2c40SSalvatore Benedetto * for a given kpp algorithm. 3014e5f2c40SSalvatore Benedetto * 3024e5f2c40SSalvatore Benedetto * @req: kpp key request 3034e5f2c40SSalvatore Benedetto * 3044e5f2c40SSalvatore Benedetto * Return: zero on success; error code in case of error 3054e5f2c40SSalvatore Benedetto */ 3064e5f2c40SSalvatore Benedetto static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req) 3074e5f2c40SSalvatore Benedetto { 3084e5f2c40SSalvatore Benedetto struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); 3094e5f2c40SSalvatore Benedetto struct kpp_alg *alg = crypto_kpp_alg(tfm); 3104e5f2c40SSalvatore Benedetto 3114e5f2c40SSalvatore Benedetto return alg->compute_shared_secret(req); 3124e5f2c40SSalvatore Benedetto } 3134e5f2c40SSalvatore Benedetto 3144e5f2c40SSalvatore Benedetto /** 3154e5f2c40SSalvatore Benedetto * crypto_kpp_maxsize() - Get len for output buffer 3164e5f2c40SSalvatore Benedetto * 3174e5f2c40SSalvatore Benedetto * Function returns the output buffer size required 3184e5f2c40SSalvatore Benedetto * 3194e5f2c40SSalvatore Benedetto * @tfm: KPP tfm handle allocated with crypto_alloc_kpp() 3204e5f2c40SSalvatore Benedetto * 3214e5f2c40SSalvatore Benedetto * Return: minimum len for output buffer or error code if key hasn't been set 3224e5f2c40SSalvatore Benedetto */ 3234e5f2c40SSalvatore Benedetto static inline int crypto_kpp_maxsize(struct crypto_kpp *tfm) 3244e5f2c40SSalvatore Benedetto { 3254e5f2c40SSalvatore Benedetto struct kpp_alg *alg = crypto_kpp_alg(tfm); 3264e5f2c40SSalvatore Benedetto 3274e5f2c40SSalvatore Benedetto return alg->max_size(tfm); 3284e5f2c40SSalvatore Benedetto } 3294e5f2c40SSalvatore Benedetto 3304e5f2c40SSalvatore Benedetto #endif 331