Back to home page

LXR

 
 

    


0001 /*
0002  * RSA key extract helper
0003  *
0004  * Copyright (c) 2015, Intel Corporation
0005  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
0006  *
0007  * This program is free software; you can redistribute it and/or modify it
0008  * under the terms of the GNU General Public License as published by the Free
0009  * Software Foundation; either version 2 of the License, or (at your option)
0010  * any later version.
0011  *
0012  */
0013 #include <linux/kernel.h>
0014 #include <linux/export.h>
0015 #include <linux/err.h>
0016 #include <linux/fips.h>
0017 #include <crypto/internal/rsa.h>
0018 #include "rsapubkey-asn1.h"
0019 #include "rsaprivkey-asn1.h"
0020 
0021 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
0022           const void *value, size_t vlen)
0023 {
0024     struct rsa_key *key = context;
0025     const u8 *ptr = value;
0026     size_t n_sz = vlen;
0027 
0028     /* invalid key provided */
0029     if (!value || !vlen)
0030         return -EINVAL;
0031 
0032     if (fips_enabled) {
0033         while (!*ptr && n_sz) {
0034             ptr++;
0035             n_sz--;
0036         }
0037 
0038         /* In FIPS mode only allow key size 2K and higher */
0039         if (n_sz < 256) {
0040             pr_err("RSA: key size not allowed in FIPS mode\n");
0041             return -EINVAL;
0042         }
0043     }
0044 
0045     key->n = value;
0046     key->n_sz = vlen;
0047 
0048     return 0;
0049 }
0050 
0051 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
0052           const void *value, size_t vlen)
0053 {
0054     struct rsa_key *key = context;
0055 
0056     /* invalid key provided */
0057     if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
0058         return -EINVAL;
0059 
0060     key->e = value;
0061     key->e_sz = vlen;
0062 
0063     return 0;
0064 }
0065 
0066 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
0067           const void *value, size_t vlen)
0068 {
0069     struct rsa_key *key = context;
0070 
0071     /* invalid key provided */
0072     if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
0073         return -EINVAL;
0074 
0075     key->d = value;
0076     key->d_sz = vlen;
0077 
0078     return 0;
0079 }
0080 
0081 int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
0082           const void *value, size_t vlen)
0083 {
0084     struct rsa_key *key = context;
0085 
0086     /* invalid key provided */
0087     if (!value || !vlen || vlen > key->n_sz)
0088         return -EINVAL;
0089 
0090     key->p = value;
0091     key->p_sz = vlen;
0092 
0093     return 0;
0094 }
0095 
0096 int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
0097           const void *value, size_t vlen)
0098 {
0099     struct rsa_key *key = context;
0100 
0101     /* invalid key provided */
0102     if (!value || !vlen || vlen > key->n_sz)
0103         return -EINVAL;
0104 
0105     key->q = value;
0106     key->q_sz = vlen;
0107 
0108     return 0;
0109 }
0110 
0111 int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
0112            const void *value, size_t vlen)
0113 {
0114     struct rsa_key *key = context;
0115 
0116     /* invalid key provided */
0117     if (!value || !vlen || vlen > key->n_sz)
0118         return -EINVAL;
0119 
0120     key->dp = value;
0121     key->dp_sz = vlen;
0122 
0123     return 0;
0124 }
0125 
0126 int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
0127            const void *value, size_t vlen)
0128 {
0129     struct rsa_key *key = context;
0130 
0131     /* invalid key provided */
0132     if (!value || !vlen || vlen > key->n_sz)
0133         return -EINVAL;
0134 
0135     key->dq = value;
0136     key->dq_sz = vlen;
0137 
0138     return 0;
0139 }
0140 
0141 int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
0142          const void *value, size_t vlen)
0143 {
0144     struct rsa_key *key = context;
0145 
0146     /* invalid key provided */
0147     if (!value || !vlen || vlen > key->n_sz)
0148         return -EINVAL;
0149 
0150     key->qinv = value;
0151     key->qinv_sz = vlen;
0152 
0153     return 0;
0154 }
0155 
0156 /**
0157  * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
0158  *                       provided struct rsa_key, pointers to the raw key as is,
0159  *                       so that the caller can copy it or MPI parse it, etc.
0160  *
0161  * @rsa_key:    struct rsa_key key representation
0162  * @key:    key in BER format
0163  * @key_len:    length of key
0164  *
0165  * Return:  0 on success or error code in case of error
0166  */
0167 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
0168               unsigned int key_len)
0169 {
0170     return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
0171 }
0172 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
0173 
0174 /**
0175  * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
0176  *                        provided struct rsa_key, pointers to the raw key
0177  *                        as is, so that the caller can copy it or MPI parse it,
0178  *                        etc.
0179  *
0180  * @rsa_key:    struct rsa_key key representation
0181  * @key:    key in BER format
0182  * @key_len:    length of key
0183  *
0184  * Return:  0 on success or error code in case of error
0185  */
0186 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
0187                unsigned int key_len)
0188 {
0189     return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
0190 }
0191 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);