mirror of
https://github.com/torvalds/linux.git
synced 2024-11-30 16:11:38 +00:00
5a0e3ad6af
percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
410 lines
10 KiB
C
410 lines
10 KiB
C
|
|
#include "ceph_debug.h"
|
|
|
|
#include <linux/err.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/slab.h>
|
|
#include <crypto/hash.h>
|
|
|
|
#include "crypto.h"
|
|
#include "decode.h"
|
|
|
|
int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
|
|
{
|
|
if (*p + sizeof(u16) + sizeof(key->created) +
|
|
sizeof(u16) + key->len > end)
|
|
return -ERANGE;
|
|
ceph_encode_16(p, key->type);
|
|
ceph_encode_copy(p, &key->created, sizeof(key->created));
|
|
ceph_encode_16(p, key->len);
|
|
ceph_encode_copy(p, key->key, key->len);
|
|
return 0;
|
|
}
|
|
|
|
int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
|
|
{
|
|
ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
|
|
key->type = ceph_decode_16(p);
|
|
ceph_decode_copy(p, &key->created, sizeof(key->created));
|
|
key->len = ceph_decode_16(p);
|
|
ceph_decode_need(p, end, key->len, bad);
|
|
key->key = kmalloc(key->len, GFP_NOFS);
|
|
if (!key->key)
|
|
return -ENOMEM;
|
|
ceph_decode_copy(p, key->key, key->len);
|
|
return 0;
|
|
|
|
bad:
|
|
dout("failed to decode crypto key\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
|
|
{
|
|
int inlen = strlen(inkey);
|
|
int blen = inlen * 3 / 4;
|
|
void *buf, *p;
|
|
int ret;
|
|
|
|
dout("crypto_key_unarmor %s\n", inkey);
|
|
buf = kmalloc(blen, GFP_NOFS);
|
|
if (!buf)
|
|
return -ENOMEM;
|
|
blen = ceph_unarmor(buf, inkey, inkey+inlen);
|
|
if (blen < 0) {
|
|
kfree(buf);
|
|
return blen;
|
|
}
|
|
|
|
p = buf;
|
|
ret = ceph_crypto_key_decode(key, &p, p + blen);
|
|
kfree(buf);
|
|
if (ret)
|
|
return ret;
|
|
dout("crypto_key_unarmor key %p type %d len %d\n", key,
|
|
key->type, key->len);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
#define AES_KEY_SIZE 16
|
|
|
|
static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void)
|
|
{
|
|
return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
|
|
}
|
|
|
|
const u8 *aes_iv = "cephsageyudagreg";
|
|
|
|
int ceph_aes_encrypt(const void *key, int key_len, void *dst, size_t *dst_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
struct scatterlist sg_in[2], sg_out[1];
|
|
struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
|
|
struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
|
|
int ret;
|
|
void *iv;
|
|
int ivsize;
|
|
size_t zero_padding = (0x10 - (src_len & 0x0f));
|
|
char pad[16];
|
|
|
|
if (IS_ERR(tfm))
|
|
return PTR_ERR(tfm);
|
|
|
|
memset(pad, zero_padding, zero_padding);
|
|
|
|
*dst_len = src_len + zero_padding;
|
|
|
|
crypto_blkcipher_setkey((void *)tfm, key, key_len);
|
|
sg_init_table(sg_in, 2);
|
|
sg_set_buf(&sg_in[0], src, src_len);
|
|
sg_set_buf(&sg_in[1], pad, zero_padding);
|
|
sg_init_table(sg_out, 1);
|
|
sg_set_buf(sg_out, dst, *dst_len);
|
|
iv = crypto_blkcipher_crt(tfm)->iv;
|
|
ivsize = crypto_blkcipher_ivsize(tfm);
|
|
|
|
memcpy(iv, aes_iv, ivsize);
|
|
/*
|
|
print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
|
|
key, key_len, 1);
|
|
print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
|
|
src, src_len, 1);
|
|
print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
|
|
pad, zero_padding, 1);
|
|
*/
|
|
ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
|
|
src_len + zero_padding);
|
|
crypto_free_blkcipher(tfm);
|
|
if (ret < 0)
|
|
pr_err("ceph_aes_crypt failed %d\n", ret);
|
|
/*
|
|
print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
|
|
dst, *dst_len, 1);
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
int ceph_aes_encrypt2(const void *key, int key_len, void *dst, size_t *dst_len,
|
|
const void *src1, size_t src1_len,
|
|
const void *src2, size_t src2_len)
|
|
{
|
|
struct scatterlist sg_in[3], sg_out[1];
|
|
struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
|
|
struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
|
|
int ret;
|
|
void *iv;
|
|
int ivsize;
|
|
size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f));
|
|
char pad[16];
|
|
|
|
if (IS_ERR(tfm))
|
|
return PTR_ERR(tfm);
|
|
|
|
memset(pad, zero_padding, zero_padding);
|
|
|
|
*dst_len = src1_len + src2_len + zero_padding;
|
|
|
|
crypto_blkcipher_setkey((void *)tfm, key, key_len);
|
|
sg_init_table(sg_in, 3);
|
|
sg_set_buf(&sg_in[0], src1, src1_len);
|
|
sg_set_buf(&sg_in[1], src2, src2_len);
|
|
sg_set_buf(&sg_in[2], pad, zero_padding);
|
|
sg_init_table(sg_out, 1);
|
|
sg_set_buf(sg_out, dst, *dst_len);
|
|
iv = crypto_blkcipher_crt(tfm)->iv;
|
|
ivsize = crypto_blkcipher_ivsize(tfm);
|
|
|
|
memcpy(iv, aes_iv, ivsize);
|
|
/*
|
|
print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
|
|
key, key_len, 1);
|
|
print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
|
|
src1, src1_len, 1);
|
|
print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1,
|
|
src2, src2_len, 1);
|
|
print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
|
|
pad, zero_padding, 1);
|
|
*/
|
|
ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
|
|
src1_len + src2_len + zero_padding);
|
|
crypto_free_blkcipher(tfm);
|
|
if (ret < 0)
|
|
pr_err("ceph_aes_crypt2 failed %d\n", ret);
|
|
/*
|
|
print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
|
|
dst, *dst_len, 1);
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
int ceph_aes_decrypt(const void *key, int key_len, void *dst, size_t *dst_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
struct scatterlist sg_in[1], sg_out[2];
|
|
struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
|
|
struct blkcipher_desc desc = { .tfm = tfm };
|
|
char pad[16];
|
|
void *iv;
|
|
int ivsize;
|
|
int ret;
|
|
int last_byte;
|
|
|
|
if (IS_ERR(tfm))
|
|
return PTR_ERR(tfm);
|
|
|
|
crypto_blkcipher_setkey((void *)tfm, key, key_len);
|
|
sg_init_table(sg_in, 1);
|
|
sg_init_table(sg_out, 2);
|
|
sg_set_buf(sg_in, src, src_len);
|
|
sg_set_buf(&sg_out[0], dst, *dst_len);
|
|
sg_set_buf(&sg_out[1], pad, sizeof(pad));
|
|
|
|
iv = crypto_blkcipher_crt(tfm)->iv;
|
|
ivsize = crypto_blkcipher_ivsize(tfm);
|
|
|
|
memcpy(iv, aes_iv, ivsize);
|
|
|
|
/*
|
|
print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
|
|
key, key_len, 1);
|
|
print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
|
|
src, src_len, 1);
|
|
*/
|
|
|
|
ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len);
|
|
crypto_free_blkcipher(tfm);
|
|
if (ret < 0) {
|
|
pr_err("ceph_aes_decrypt failed %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
if (src_len <= *dst_len)
|
|
last_byte = ((char *)dst)[src_len - 1];
|
|
else
|
|
last_byte = pad[src_len - *dst_len - 1];
|
|
if (last_byte <= 16 && src_len >= last_byte) {
|
|
*dst_len = src_len - last_byte;
|
|
} else {
|
|
pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
|
|
last_byte, (int)src_len);
|
|
return -EPERM; /* bad padding */
|
|
}
|
|
/*
|
|
print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
|
|
dst, *dst_len, 1);
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
int ceph_aes_decrypt2(const void *key, int key_len,
|
|
void *dst1, size_t *dst1_len,
|
|
void *dst2, size_t *dst2_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
struct scatterlist sg_in[1], sg_out[3];
|
|
struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
|
|
struct blkcipher_desc desc = { .tfm = tfm };
|
|
char pad[16];
|
|
void *iv;
|
|
int ivsize;
|
|
int ret;
|
|
int last_byte;
|
|
|
|
if (IS_ERR(tfm))
|
|
return PTR_ERR(tfm);
|
|
|
|
sg_init_table(sg_in, 1);
|
|
sg_set_buf(sg_in, src, src_len);
|
|
sg_init_table(sg_out, 3);
|
|
sg_set_buf(&sg_out[0], dst1, *dst1_len);
|
|
sg_set_buf(&sg_out[1], dst2, *dst2_len);
|
|
sg_set_buf(&sg_out[2], pad, sizeof(pad));
|
|
|
|
crypto_blkcipher_setkey((void *)tfm, key, key_len);
|
|
iv = crypto_blkcipher_crt(tfm)->iv;
|
|
ivsize = crypto_blkcipher_ivsize(tfm);
|
|
|
|
memcpy(iv, aes_iv, ivsize);
|
|
|
|
/*
|
|
print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
|
|
key, key_len, 1);
|
|
print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
|
|
src, src_len, 1);
|
|
*/
|
|
|
|
ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len);
|
|
crypto_free_blkcipher(tfm);
|
|
if (ret < 0) {
|
|
pr_err("ceph_aes_decrypt failed %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
if (src_len <= *dst1_len)
|
|
last_byte = ((char *)dst1)[src_len - 1];
|
|
else if (src_len <= *dst1_len + *dst2_len)
|
|
last_byte = ((char *)dst2)[src_len - *dst1_len - 1];
|
|
else
|
|
last_byte = pad[src_len - *dst1_len - *dst2_len - 1];
|
|
if (last_byte <= 16 && src_len >= last_byte) {
|
|
src_len -= last_byte;
|
|
} else {
|
|
pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
|
|
last_byte, (int)src_len);
|
|
return -EPERM; /* bad padding */
|
|
}
|
|
|
|
if (src_len < *dst1_len) {
|
|
*dst1_len = src_len;
|
|
*dst2_len = 0;
|
|
} else {
|
|
*dst2_len = src_len - *dst1_len;
|
|
}
|
|
/*
|
|
print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
|
|
dst1, *dst1_len, 1);
|
|
print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
|
|
dst2, *dst2_len, 1);
|
|
*/
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int ceph_decrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
switch (secret->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
if (*dst_len < src_len)
|
|
return -ERANGE;
|
|
memcpy(dst, src, src_len);
|
|
*dst_len = src_len;
|
|
return 0;
|
|
|
|
case CEPH_CRYPTO_AES:
|
|
return ceph_aes_decrypt(secret->key, secret->len, dst,
|
|
dst_len, src, src_len);
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
int ceph_decrypt2(struct ceph_crypto_key *secret,
|
|
void *dst1, size_t *dst1_len,
|
|
void *dst2, size_t *dst2_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
size_t t;
|
|
|
|
switch (secret->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
if (*dst1_len + *dst2_len < src_len)
|
|
return -ERANGE;
|
|
t = min(*dst1_len, src_len);
|
|
memcpy(dst1, src, t);
|
|
*dst1_len = t;
|
|
src += t;
|
|
src_len -= t;
|
|
if (src_len) {
|
|
t = min(*dst2_len, src_len);
|
|
memcpy(dst2, src, t);
|
|
*dst2_len = t;
|
|
}
|
|
return 0;
|
|
|
|
case CEPH_CRYPTO_AES:
|
|
return ceph_aes_decrypt2(secret->key, secret->len,
|
|
dst1, dst1_len, dst2, dst2_len,
|
|
src, src_len);
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
int ceph_encrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
|
|
const void *src, size_t src_len)
|
|
{
|
|
switch (secret->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
if (*dst_len < src_len)
|
|
return -ERANGE;
|
|
memcpy(dst, src, src_len);
|
|
*dst_len = src_len;
|
|
return 0;
|
|
|
|
case CEPH_CRYPTO_AES:
|
|
return ceph_aes_encrypt(secret->key, secret->len, dst,
|
|
dst_len, src, src_len);
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
|
|
const void *src1, size_t src1_len,
|
|
const void *src2, size_t src2_len)
|
|
{
|
|
switch (secret->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
if (*dst_len < src1_len + src2_len)
|
|
return -ERANGE;
|
|
memcpy(dst, src1, src1_len);
|
|
memcpy(dst + src1_len, src2, src2_len);
|
|
*dst_len = src1_len + src2_len;
|
|
return 0;
|
|
|
|
case CEPH_CRYPTO_AES:
|
|
return ceph_aes_encrypt2(secret->key, secret->len, dst, dst_len,
|
|
src1, src1_len, src2, src2_len);
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|