LCOV - code coverage report
Current view: top level - crypto/asymmetric_keys - public_key.c (source / functions) Hit Total Coverage
Test: Real Lines: 44 120 36.7 %
Date: 2020-10-17 15:46:16 Functions: 0 8 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /* In-software asymmetric public-key crypto subtype
       3                 :            :  *
       4                 :            :  * See Documentation/crypto/asymmetric-keys.txt
       5                 :            :  *
       6                 :            :  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
       7                 :            :  * Written by David Howells (dhowells@redhat.com)
       8                 :            :  */
       9                 :            : 
      10                 :            : #define pr_fmt(fmt) "PKEY: "fmt
      11                 :            : #include <linux/module.h>
      12                 :            : #include <linux/export.h>
      13                 :            : #include <linux/kernel.h>
      14                 :            : #include <linux/slab.h>
      15                 :            : #include <linux/seq_file.h>
      16                 :            : #include <linux/scatterlist.h>
      17                 :            : #include <keys/asymmetric-subtype.h>
      18                 :            : #include <crypto/public_key.h>
      19                 :            : #include <crypto/akcipher.h>
      20                 :            : 
      21                 :            : MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
      22                 :            : MODULE_AUTHOR("Red Hat, Inc.");
      23                 :            : MODULE_LICENSE("GPL");
      24                 :            : 
      25                 :            : /*
      26                 :            :  * Provide a part of a description of the key for /proc/keys.
      27                 :            :  */
      28                 :          0 : static void public_key_describe(const struct key *asymmetric_key,
      29                 :            :                                 struct seq_file *m)
      30                 :            : {
      31                 :          0 :         struct public_key *key = asymmetric_key->payload.data[asym_crypto];
      32                 :            : 
      33                 :          0 :         if (key)
      34                 :          0 :                 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
      35                 :          0 : }
      36                 :            : 
      37                 :            : /*
      38                 :            :  * Destroy a public key algorithm key.
      39                 :            :  */
      40                 :          3 : void public_key_free(struct public_key *key)
      41                 :            : {
      42                 :          3 :         if (key) {
      43                 :          3 :                 kfree(key->key);
      44                 :          3 :                 kfree(key->params);
      45                 :          3 :                 kfree(key);
      46                 :            :         }
      47                 :          3 : }
      48                 :            : EXPORT_SYMBOL_GPL(public_key_free);
      49                 :            : 
      50                 :            : /*
      51                 :            :  * Destroy a public key algorithm key.
      52                 :            :  */
      53                 :          0 : static void public_key_destroy(void *payload0, void *payload3)
      54                 :            : {
      55                 :          0 :         public_key_free(payload0);
      56                 :          0 :         public_key_signature_free(payload3);
      57                 :          0 : }
      58                 :            : 
      59                 :            : /*
      60                 :            :  * Determine the crypto algorithm name.
      61                 :            :  */
      62                 :            : static
      63                 :          3 : int software_key_determine_akcipher(const char *encoding,
      64                 :            :                                     const char *hash_algo,
      65                 :            :                                     const struct public_key *pkey,
      66                 :            :                                     char alg_name[CRYPTO_MAX_ALG_NAME])
      67                 :            : {
      68                 :            :         int n;
      69                 :            : 
      70                 :          3 :         if (strcmp(encoding, "pkcs1") == 0) {
      71                 :            :                 /* The data wangled by the RSA algorithm is typically padded
      72                 :            :                  * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
      73                 :            :                  * sec 8.2].
      74                 :            :                  */
      75                 :          3 :                 if (!hash_algo)
      76                 :          0 :                         n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
      77                 :            :                                      "pkcs1pad(%s)",
      78                 :            :                                      pkey->pkey_algo);
      79                 :            :                 else
      80                 :          3 :                         n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
      81                 :            :                                      "pkcs1pad(%s,%s)",
      82                 :            :                                      pkey->pkey_algo, hash_algo);
      83                 :          3 :                 return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
      84                 :            :         }
      85                 :            : 
      86                 :          0 :         if (strcmp(encoding, "raw") == 0) {
      87                 :          0 :                 strcpy(alg_name, pkey->pkey_algo);
      88                 :          0 :                 return 0;
      89                 :            :         }
      90                 :            : 
      91                 :            :         return -ENOPKG;
      92                 :            : }
      93                 :            : 
      94                 :            : static u8 *pkey_pack_u32(u8 *dst, u32 val)
      95                 :            : {
      96                 :          3 :         memcpy(dst, &val, sizeof(val));
      97                 :          3 :         return dst + sizeof(val);
      98                 :            : }
      99                 :            : 
     100                 :            : /*
     101                 :            :  * Query information about a key.
     102                 :            :  */
     103                 :          0 : static int software_key_query(const struct kernel_pkey_params *params,
     104                 :            :                               struct kernel_pkey_query *info)
     105                 :            : {
     106                 :            :         struct crypto_akcipher *tfm;
     107                 :          0 :         struct public_key *pkey = params->key->payload.data[asym_crypto];
     108                 :            :         char alg_name[CRYPTO_MAX_ALG_NAME];
     109                 :            :         u8 *key, *ptr;
     110                 :            :         int ret, len;
     111                 :            : 
     112                 :          0 :         ret = software_key_determine_akcipher(params->encoding,
     113                 :            :                                               params->hash_algo,
     114                 :            :                                               pkey, alg_name);
     115                 :          0 :         if (ret < 0)
     116                 :            :                 return ret;
     117                 :            : 
     118                 :          0 :         tfm = crypto_alloc_akcipher(alg_name, 0, 0);
     119                 :          0 :         if (IS_ERR(tfm))
     120                 :          0 :                 return PTR_ERR(tfm);
     121                 :            : 
     122                 :            :         ret = -ENOMEM;
     123                 :          0 :         key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
     124                 :            :                       GFP_KERNEL);
     125                 :          0 :         if (!key)
     126                 :            :                 goto error_free_tfm;
     127                 :          0 :         memcpy(key, pkey->key, pkey->keylen);
     128                 :          0 :         ptr = key + pkey->keylen;
     129                 :          0 :         ptr = pkey_pack_u32(ptr, pkey->algo);
     130                 :          0 :         ptr = pkey_pack_u32(ptr, pkey->paramlen);
     131                 :          0 :         memcpy(ptr, pkey->params, pkey->paramlen);
     132                 :            : 
     133                 :          0 :         if (pkey->key_is_private)
     134                 :          0 :                 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
     135                 :            :         else
     136                 :          0 :                 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
     137                 :          0 :         if (ret < 0)
     138                 :            :                 goto error_free_key;
     139                 :            : 
     140                 :          0 :         len = crypto_akcipher_maxsize(tfm);
     141                 :          0 :         info->key_size = len * 8;
     142                 :          0 :         info->max_data_size = len;
     143                 :          0 :         info->max_sig_size = len;
     144                 :          0 :         info->max_enc_size = len;
     145                 :          0 :         info->max_dec_size = len;
     146                 :          0 :         info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
     147                 :            :                                KEYCTL_SUPPORTS_VERIFY);
     148                 :          0 :         if (pkey->key_is_private)
     149                 :          0 :                 info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
     150                 :            :                                         KEYCTL_SUPPORTS_SIGN);
     151                 :            :         ret = 0;
     152                 :            : 
     153                 :            : error_free_key:
     154                 :          0 :         kfree(key);
     155                 :            : error_free_tfm:
     156                 :            :         crypto_free_akcipher(tfm);
     157                 :            :         pr_devel("<==%s() = %d\n", __func__, ret);
     158                 :          0 :         return ret;
     159                 :            : }
     160                 :            : 
     161                 :            : /*
     162                 :            :  * Do encryption, decryption and signing ops.
     163                 :            :  */
     164                 :          0 : static int software_key_eds_op(struct kernel_pkey_params *params,
     165                 :            :                                const void *in, void *out)
     166                 :            : {
     167                 :          0 :         const struct public_key *pkey = params->key->payload.data[asym_crypto];
     168                 :            :         struct akcipher_request *req;
     169                 :            :         struct crypto_akcipher *tfm;
     170                 :            :         struct crypto_wait cwait;
     171                 :            :         struct scatterlist in_sg, out_sg;
     172                 :            :         char alg_name[CRYPTO_MAX_ALG_NAME];
     173                 :            :         char *key, *ptr;
     174                 :            :         int ret;
     175                 :            : 
     176                 :            :         pr_devel("==>%s()\n", __func__);
     177                 :            : 
     178                 :          0 :         ret = software_key_determine_akcipher(params->encoding,
     179                 :            :                                               params->hash_algo,
     180                 :            :                                               pkey, alg_name);
     181                 :          0 :         if (ret < 0)
     182                 :            :                 return ret;
     183                 :            : 
     184                 :          0 :         tfm = crypto_alloc_akcipher(alg_name, 0, 0);
     185                 :          0 :         if (IS_ERR(tfm))
     186                 :          0 :                 return PTR_ERR(tfm);
     187                 :            : 
     188                 :            :         ret = -ENOMEM;
     189                 :          0 :         req = akcipher_request_alloc(tfm, GFP_KERNEL);
     190                 :          0 :         if (!req)
     191                 :            :                 goto error_free_tfm;
     192                 :            : 
     193                 :          0 :         key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
     194                 :            :                       GFP_KERNEL);
     195                 :          0 :         if (!key)
     196                 :            :                 goto error_free_req;
     197                 :            : 
     198                 :          0 :         memcpy(key, pkey->key, pkey->keylen);
     199                 :          0 :         ptr = key + pkey->keylen;
     200                 :          0 :         ptr = pkey_pack_u32(ptr, pkey->algo);
     201                 :          0 :         ptr = pkey_pack_u32(ptr, pkey->paramlen);
     202                 :          0 :         memcpy(ptr, pkey->params, pkey->paramlen);
     203                 :            : 
     204                 :          0 :         if (pkey->key_is_private)
     205                 :          0 :                 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
     206                 :            :         else
     207                 :          0 :                 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
     208                 :          0 :         if (ret)
     209                 :            :                 goto error_free_key;
     210                 :            : 
     211                 :          0 :         sg_init_one(&in_sg, in, params->in_len);
     212                 :          0 :         sg_init_one(&out_sg, out, params->out_len);
     213                 :          0 :         akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
     214                 :            :                                    params->out_len);
     215                 :            :         crypto_init_wait(&cwait);
     216                 :            :         akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
     217                 :            :                                       CRYPTO_TFM_REQ_MAY_SLEEP,
     218                 :            :                                       crypto_req_done, &cwait);
     219                 :            : 
     220                 :            :         /* Perform the encryption calculation. */
     221                 :          0 :         switch (params->op) {
     222                 :            :         case kernel_pkey_encrypt:
     223                 :            :                 ret = crypto_akcipher_encrypt(req);
     224                 :          0 :                 break;
     225                 :            :         case kernel_pkey_decrypt:
     226                 :            :                 ret = crypto_akcipher_decrypt(req);
     227                 :          0 :                 break;
     228                 :            :         case kernel_pkey_sign:
     229                 :            :                 ret = crypto_akcipher_sign(req);
     230                 :          0 :                 break;
     231                 :            :         default:
     232                 :          0 :                 BUG();
     233                 :            :         }
     234                 :            : 
     235                 :            :         ret = crypto_wait_req(ret, &cwait);
     236                 :          0 :         if (ret == 0)
     237                 :          0 :                 ret = req->dst_len;
     238                 :            : 
     239                 :            : error_free_key:
     240                 :          0 :         kfree(key);
     241                 :            : error_free_req:
     242                 :            :         akcipher_request_free(req);
     243                 :            : error_free_tfm:
     244                 :            :         crypto_free_akcipher(tfm);
     245                 :            :         pr_devel("<==%s() = %d\n", __func__, ret);
     246                 :          0 :         return ret;
     247                 :            : }
     248                 :            : 
     249                 :            : /*
     250                 :            :  * Verify a signature using a public key.
     251                 :            :  */
     252                 :          3 : int public_key_verify_signature(const struct public_key *pkey,
     253                 :            :                                 const struct public_key_signature *sig)
     254                 :            : {
     255                 :            :         struct crypto_wait cwait;
     256                 :            :         struct crypto_akcipher *tfm;
     257                 :            :         struct akcipher_request *req;
     258                 :            :         struct scatterlist src_sg[2];
     259                 :            :         char alg_name[CRYPTO_MAX_ALG_NAME];
     260                 :            :         char *key, *ptr;
     261                 :            :         int ret;
     262                 :            : 
     263                 :            :         pr_devel("==>%s()\n", __func__);
     264                 :            : 
     265                 :          3 :         BUG_ON(!pkey);
     266                 :          3 :         BUG_ON(!sig);
     267                 :          3 :         BUG_ON(!sig->s);
     268                 :            : 
     269                 :          3 :         ret = software_key_determine_akcipher(sig->encoding,
     270                 :            :                                               sig->hash_algo,
     271                 :            :                                               pkey, alg_name);
     272                 :          3 :         if (ret < 0)
     273                 :            :                 return ret;
     274                 :            : 
     275                 :          3 :         tfm = crypto_alloc_akcipher(alg_name, 0, 0);
     276                 :          3 :         if (IS_ERR(tfm))
     277                 :          0 :                 return PTR_ERR(tfm);
     278                 :            : 
     279                 :            :         ret = -ENOMEM;
     280                 :          3 :         req = akcipher_request_alloc(tfm, GFP_KERNEL);
     281                 :          3 :         if (!req)
     282                 :            :                 goto error_free_tfm;
     283                 :            : 
     284                 :          3 :         key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
     285                 :            :                       GFP_KERNEL);
     286                 :          3 :         if (!key)
     287                 :            :                 goto error_free_req;
     288                 :            : 
     289                 :          3 :         memcpy(key, pkey->key, pkey->keylen);
     290                 :          3 :         ptr = key + pkey->keylen;
     291                 :          3 :         ptr = pkey_pack_u32(ptr, pkey->algo);
     292                 :          3 :         ptr = pkey_pack_u32(ptr, pkey->paramlen);
     293                 :          3 :         memcpy(ptr, pkey->params, pkey->paramlen);
     294                 :            : 
     295                 :          3 :         if (pkey->key_is_private)
     296                 :          0 :                 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
     297                 :            :         else
     298                 :          3 :                 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
     299                 :          3 :         if (ret)
     300                 :            :                 goto error_free_key;
     301                 :            : 
     302                 :          3 :         sg_init_table(src_sg, 2);
     303                 :          3 :         sg_set_buf(&src_sg[0], sig->s, sig->s_size);
     304                 :          3 :         sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
     305                 :          3 :         akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
     306                 :          3 :                                    sig->digest_size);
     307                 :            :         crypto_init_wait(&cwait);
     308                 :            :         akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
     309                 :            :                                       CRYPTO_TFM_REQ_MAY_SLEEP,
     310                 :            :                                       crypto_req_done, &cwait);
     311                 :            :         ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
     312                 :            : 
     313                 :            : error_free_key:
     314                 :          3 :         kfree(key);
     315                 :            : error_free_req:
     316                 :            :         akcipher_request_free(req);
     317                 :            : error_free_tfm:
     318                 :            :         crypto_free_akcipher(tfm);
     319                 :            :         pr_devel("<==%s() = %d\n", __func__, ret);
     320                 :          3 :         if (WARN_ON_ONCE(ret > 0))
     321                 :            :                 ret = -EINVAL;
     322                 :          3 :         return ret;
     323                 :            : }
     324                 :            : EXPORT_SYMBOL_GPL(public_key_verify_signature);
     325                 :            : 
     326                 :          3 : static int public_key_verify_signature_2(const struct key *key,
     327                 :            :                                          const struct public_key_signature *sig)
     328                 :            : {
     329                 :          3 :         const struct public_key *pk = key->payload.data[asym_crypto];
     330                 :          3 :         return public_key_verify_signature(pk, sig);
     331                 :            : }
     332                 :            : 
     333                 :            : /*
     334                 :            :  * Public key algorithm asymmetric key subtype
     335                 :            :  */
     336                 :            : struct asymmetric_key_subtype public_key_subtype = {
     337                 :            :         .owner                  = THIS_MODULE,
     338                 :            :         .name                   = "public_key",
     339                 :            :         .name_len               = sizeof("public_key") - 1,
     340                 :            :         .describe               = public_key_describe,
     341                 :            :         .destroy                = public_key_destroy,
     342                 :            :         .query                  = software_key_query,
     343                 :            :         .eds_op                 = software_key_eds_op,
     344                 :            :         .verify_signature       = public_key_verify_signature_2,
     345                 :            : };
     346                 :            : EXPORT_SYMBOL_GPL(public_key_subtype);
    

Generated by: LCOV version 1.14