Back to home page

LXR

 
 

    


0001 /* RSA asymmetric public-key algorithm [RFC3447]
0002  *
0003  * Copyright (c) 2015, Intel Corporation
0004  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public Licence
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the Licence, or (at your option) any later version.
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/mpi.h>
0014 #include <crypto/internal/rsa.h>
0015 #include <crypto/internal/akcipher.h>
0016 #include <crypto/akcipher.h>
0017 #include <crypto/algapi.h>
0018 
0019 struct rsa_mpi_key {
0020     MPI n;
0021     MPI e;
0022     MPI d;
0023 };
0024 
0025 /*
0026  * RSAEP function [RFC3447 sec 5.1.1]
0027  * c = m^e mod n;
0028  */
0029 static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
0030 {
0031     /* (1) Validate 0 <= m < n */
0032     if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
0033         return -EINVAL;
0034 
0035     /* (2) c = m^e mod n */
0036     return mpi_powm(c, m, key->e, key->n);
0037 }
0038 
0039 /*
0040  * RSADP function [RFC3447 sec 5.1.2]
0041  * m = c^d mod n;
0042  */
0043 static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
0044 {
0045     /* (1) Validate 0 <= c < n */
0046     if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
0047         return -EINVAL;
0048 
0049     /* (2) m = c^d mod n */
0050     return mpi_powm(m, c, key->d, key->n);
0051 }
0052 
0053 /*
0054  * RSASP1 function [RFC3447 sec 5.2.1]
0055  * s = m^d mod n
0056  */
0057 static int _rsa_sign(const struct rsa_mpi_key *key, MPI s, MPI m)
0058 {
0059     /* (1) Validate 0 <= m < n */
0060     if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
0061         return -EINVAL;
0062 
0063     /* (2) s = m^d mod n */
0064     return mpi_powm(s, m, key->d, key->n);
0065 }
0066 
0067 /*
0068  * RSAVP1 function [RFC3447 sec 5.2.2]
0069  * m = s^e mod n;
0070  */
0071 static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s)
0072 {
0073     /* (1) Validate 0 <= s < n */
0074     if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
0075         return -EINVAL;
0076 
0077     /* (2) m = s^e mod n */
0078     return mpi_powm(m, s, key->e, key->n);
0079 }
0080 
0081 static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
0082 {
0083     return akcipher_tfm_ctx(tfm);
0084 }
0085 
0086 static int rsa_enc(struct akcipher_request *req)
0087 {
0088     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0089     const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
0090     MPI m, c = mpi_alloc(0);
0091     int ret = 0;
0092     int sign;
0093 
0094     if (!c)
0095         return -ENOMEM;
0096 
0097     if (unlikely(!pkey->n || !pkey->e)) {
0098         ret = -EINVAL;
0099         goto err_free_c;
0100     }
0101 
0102     ret = -ENOMEM;
0103     m = mpi_read_raw_from_sgl(req->src, req->src_len);
0104     if (!m)
0105         goto err_free_c;
0106 
0107     ret = _rsa_enc(pkey, c, m);
0108     if (ret)
0109         goto err_free_m;
0110 
0111     ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
0112     if (ret)
0113         goto err_free_m;
0114 
0115     if (sign < 0)
0116         ret = -EBADMSG;
0117 
0118 err_free_m:
0119     mpi_free(m);
0120 err_free_c:
0121     mpi_free(c);
0122     return ret;
0123 }
0124 
0125 static int rsa_dec(struct akcipher_request *req)
0126 {
0127     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0128     const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
0129     MPI c, m = mpi_alloc(0);
0130     int ret = 0;
0131     int sign;
0132 
0133     if (!m)
0134         return -ENOMEM;
0135 
0136     if (unlikely(!pkey->n || !pkey->d)) {
0137         ret = -EINVAL;
0138         goto err_free_m;
0139     }
0140 
0141     ret = -ENOMEM;
0142     c = mpi_read_raw_from_sgl(req->src, req->src_len);
0143     if (!c)
0144         goto err_free_m;
0145 
0146     ret = _rsa_dec(pkey, m, c);
0147     if (ret)
0148         goto err_free_c;
0149 
0150     ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
0151     if (ret)
0152         goto err_free_c;
0153 
0154     if (sign < 0)
0155         ret = -EBADMSG;
0156 err_free_c:
0157     mpi_free(c);
0158 err_free_m:
0159     mpi_free(m);
0160     return ret;
0161 }
0162 
0163 static int rsa_sign(struct akcipher_request *req)
0164 {
0165     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0166     const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
0167     MPI m, s = mpi_alloc(0);
0168     int ret = 0;
0169     int sign;
0170 
0171     if (!s)
0172         return -ENOMEM;
0173 
0174     if (unlikely(!pkey->n || !pkey->d)) {
0175         ret = -EINVAL;
0176         goto err_free_s;
0177     }
0178 
0179     ret = -ENOMEM;
0180     m = mpi_read_raw_from_sgl(req->src, req->src_len);
0181     if (!m)
0182         goto err_free_s;
0183 
0184     ret = _rsa_sign(pkey, s, m);
0185     if (ret)
0186         goto err_free_m;
0187 
0188     ret = mpi_write_to_sgl(s, req->dst, req->dst_len, &sign);
0189     if (ret)
0190         goto err_free_m;
0191 
0192     if (sign < 0)
0193         ret = -EBADMSG;
0194 
0195 err_free_m:
0196     mpi_free(m);
0197 err_free_s:
0198     mpi_free(s);
0199     return ret;
0200 }
0201 
0202 static int rsa_verify(struct akcipher_request *req)
0203 {
0204     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0205     const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
0206     MPI s, m = mpi_alloc(0);
0207     int ret = 0;
0208     int sign;
0209 
0210     if (!m)
0211         return -ENOMEM;
0212 
0213     if (unlikely(!pkey->n || !pkey->e)) {
0214         ret = -EINVAL;
0215         goto err_free_m;
0216     }
0217 
0218     ret = -ENOMEM;
0219     s = mpi_read_raw_from_sgl(req->src, req->src_len);
0220     if (!s) {
0221         ret = -ENOMEM;
0222         goto err_free_m;
0223     }
0224 
0225     ret = _rsa_verify(pkey, m, s);
0226     if (ret)
0227         goto err_free_s;
0228 
0229     ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
0230     if (ret)
0231         goto err_free_s;
0232 
0233     if (sign < 0)
0234         ret = -EBADMSG;
0235 
0236 err_free_s:
0237     mpi_free(s);
0238 err_free_m:
0239     mpi_free(m);
0240     return ret;
0241 }
0242 
0243 static void rsa_free_mpi_key(struct rsa_mpi_key *key)
0244 {
0245     mpi_free(key->d);
0246     mpi_free(key->e);
0247     mpi_free(key->n);
0248     key->d = NULL;
0249     key->e = NULL;
0250     key->n = NULL;
0251 }
0252 
0253 static int rsa_check_key_length(unsigned int len)
0254 {
0255     switch (len) {
0256     case 512:
0257     case 1024:
0258     case 1536:
0259     case 2048:
0260     case 3072:
0261     case 4096:
0262         return 0;
0263     }
0264 
0265     return -EINVAL;
0266 }
0267 
0268 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
0269                unsigned int keylen)
0270 {
0271     struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
0272     struct rsa_key raw_key = {0};
0273     int ret;
0274 
0275     /* Free the old MPI key if any */
0276     rsa_free_mpi_key(mpi_key);
0277 
0278     ret = rsa_parse_pub_key(&raw_key, key, keylen);
0279     if (ret)
0280         return ret;
0281 
0282     mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
0283     if (!mpi_key->e)
0284         goto err;
0285 
0286     mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
0287     if (!mpi_key->n)
0288         goto err;
0289 
0290     if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
0291         rsa_free_mpi_key(mpi_key);
0292         return -EINVAL;
0293     }
0294 
0295     return 0;
0296 
0297 err:
0298     rsa_free_mpi_key(mpi_key);
0299     return -ENOMEM;
0300 }
0301 
0302 static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
0303                 unsigned int keylen)
0304 {
0305     struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
0306     struct rsa_key raw_key = {0};
0307     int ret;
0308 
0309     /* Free the old MPI key if any */
0310     rsa_free_mpi_key(mpi_key);
0311 
0312     ret = rsa_parse_priv_key(&raw_key, key, keylen);
0313     if (ret)
0314         return ret;
0315 
0316     mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
0317     if (!mpi_key->d)
0318         goto err;
0319 
0320     mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
0321     if (!mpi_key->e)
0322         goto err;
0323 
0324     mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
0325     if (!mpi_key->n)
0326         goto err;
0327 
0328     if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
0329         rsa_free_mpi_key(mpi_key);
0330         return -EINVAL;
0331     }
0332 
0333     return 0;
0334 
0335 err:
0336     rsa_free_mpi_key(mpi_key);
0337     return -ENOMEM;
0338 }
0339 
0340 static int rsa_max_size(struct crypto_akcipher *tfm)
0341 {
0342     struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
0343 
0344     return pkey->n ? mpi_get_size(pkey->n) : -EINVAL;
0345 }
0346 
0347 static void rsa_exit_tfm(struct crypto_akcipher *tfm)
0348 {
0349     struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
0350 
0351     rsa_free_mpi_key(pkey);
0352 }
0353 
0354 static struct akcipher_alg rsa = {
0355     .encrypt = rsa_enc,
0356     .decrypt = rsa_dec,
0357     .sign = rsa_sign,
0358     .verify = rsa_verify,
0359     .set_priv_key = rsa_set_priv_key,
0360     .set_pub_key = rsa_set_pub_key,
0361     .max_size = rsa_max_size,
0362     .exit = rsa_exit_tfm,
0363     .base = {
0364         .cra_name = "rsa",
0365         .cra_driver_name = "rsa-generic",
0366         .cra_priority = 100,
0367         .cra_module = THIS_MODULE,
0368         .cra_ctxsize = sizeof(struct rsa_mpi_key),
0369     },
0370 };
0371 
0372 static int rsa_init(void)
0373 {
0374     int err;
0375 
0376     err = crypto_register_akcipher(&rsa);
0377     if (err)
0378         return err;
0379 
0380     err = crypto_register_template(&rsa_pkcs1pad_tmpl);
0381     if (err) {
0382         crypto_unregister_akcipher(&rsa);
0383         return err;
0384     }
0385 
0386     return 0;
0387 }
0388 
0389 static void rsa_exit(void)
0390 {
0391     crypto_unregister_template(&rsa_pkcs1pad_tmpl);
0392     crypto_unregister_akcipher(&rsa);
0393 }
0394 
0395 module_init(rsa_init);
0396 module_exit(rsa_exit);
0397 MODULE_ALIAS_CRYPTO("rsa");
0398 MODULE_LICENSE("GPL");
0399 MODULE_DESCRIPTION("RSA generic algorithm");