aboutsummaryrefslogtreecommitdiffstats
path: root/net/ceph/crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ceph/crypto.c')
-rw-r--r--net/ceph/crypto.c101
1 files changed, 56 insertions, 45 deletions
diff --git a/net/ceph/crypto.c b/net/ceph/crypto.c
index 42e8649c6e79..db2847ac5f12 100644
--- a/net/ceph/crypto.c
+++ b/net/ceph/crypto.c
@@ -4,7 +4,8 @@
4#include <linux/err.h> 4#include <linux/err.h>
5#include <linux/scatterlist.h> 5#include <linux/scatterlist.h>
6#include <linux/slab.h> 6#include <linux/slab.h>
7#include <crypto/hash.h> 7#include <crypto/aes.h>
8#include <crypto/skcipher.h>
8#include <linux/key-type.h> 9#include <linux/key-type.h>
9 10
10#include <keys/ceph-type.h> 11#include <keys/ceph-type.h>
@@ -79,9 +80,9 @@ int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
79 return 0; 80 return 0;
80} 81}
81 82
82static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void) 83static struct crypto_skcipher *ceph_crypto_alloc_cipher(void)
83{ 84{
84 return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC); 85 return crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
85} 86}
86 87
87static const u8 *aes_iv = (u8 *)CEPH_AES_IV; 88static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
@@ -162,11 +163,10 @@ static int ceph_aes_encrypt(const void *key, int key_len,
162{ 163{
163 struct scatterlist sg_in[2], prealloc_sg; 164 struct scatterlist sg_in[2], prealloc_sg;
164 struct sg_table sg_out; 165 struct sg_table sg_out;
165 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); 166 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher();
166 struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 }; 167 SKCIPHER_REQUEST_ON_STACK(req, tfm);
167 int ret; 168 int ret;
168 void *iv; 169 char iv[AES_BLOCK_SIZE];
169 int ivsize;
170 size_t zero_padding = (0x10 - (src_len & 0x0f)); 170 size_t zero_padding = (0x10 - (src_len & 0x0f));
171 char pad[16]; 171 char pad[16];
172 172
@@ -184,10 +184,13 @@ static int ceph_aes_encrypt(const void *key, int key_len,
184 if (ret) 184 if (ret)
185 goto out_tfm; 185 goto out_tfm;
186 186
187 crypto_blkcipher_setkey((void *)tfm, key, key_len); 187 crypto_skcipher_setkey((void *)tfm, key, key_len);
188 iv = crypto_blkcipher_crt(tfm)->iv; 188 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
189 ivsize = crypto_blkcipher_ivsize(tfm); 189
190 memcpy(iv, aes_iv, ivsize); 190 skcipher_request_set_tfm(req, tfm);
191 skcipher_request_set_callback(req, 0, NULL, NULL);
192 skcipher_request_set_crypt(req, sg_in, sg_out.sgl,
193 src_len + zero_padding, iv);
191 194
192 /* 195 /*
193 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1, 196 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
@@ -197,8 +200,8 @@ static int ceph_aes_encrypt(const void *key, int key_len,
197 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1, 200 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
198 pad, zero_padding, 1); 201 pad, zero_padding, 1);
199 */ 202 */
200 ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in, 203 ret = crypto_skcipher_encrypt(req);
201 src_len + zero_padding); 204 skcipher_request_zero(req);
202 if (ret < 0) { 205 if (ret < 0) {
203 pr_err("ceph_aes_crypt failed %d\n", ret); 206 pr_err("ceph_aes_crypt failed %d\n", ret);
204 goto out_sg; 207 goto out_sg;
@@ -211,7 +214,7 @@ static int ceph_aes_encrypt(const void *key, int key_len,
211out_sg: 214out_sg:
212 teardown_sgtable(&sg_out); 215 teardown_sgtable(&sg_out);
213out_tfm: 216out_tfm:
214 crypto_free_blkcipher(tfm); 217 crypto_free_skcipher(tfm);
215 return ret; 218 return ret;
216} 219}
217 220
@@ -222,11 +225,10 @@ static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
222{ 225{
223 struct scatterlist sg_in[3], prealloc_sg; 226 struct scatterlist sg_in[3], prealloc_sg;
224 struct sg_table sg_out; 227 struct sg_table sg_out;
225 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); 228 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher();
226 struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 }; 229 SKCIPHER_REQUEST_ON_STACK(req, tfm);
227 int ret; 230 int ret;
228 void *iv; 231 char iv[AES_BLOCK_SIZE];
229 int ivsize;
230 size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f)); 232 size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f));
231 char pad[16]; 233 char pad[16];
232 234
@@ -245,10 +247,13 @@ static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
245 if (ret) 247 if (ret)
246 goto out_tfm; 248 goto out_tfm;
247 249
248 crypto_blkcipher_setkey((void *)tfm, key, key_len); 250 crypto_skcipher_setkey((void *)tfm, key, key_len);
249 iv = crypto_blkcipher_crt(tfm)->iv; 251 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
250 ivsize = crypto_blkcipher_ivsize(tfm); 252
251 memcpy(iv, aes_iv, ivsize); 253 skcipher_request_set_tfm(req, tfm);
254 skcipher_request_set_callback(req, 0, NULL, NULL);
255 skcipher_request_set_crypt(req, sg_in, sg_out.sgl,
256 src1_len + src2_len + zero_padding, iv);
252 257
253 /* 258 /*
254 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1, 259 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
@@ -260,8 +265,8 @@ static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
260 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1, 265 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
261 pad, zero_padding, 1); 266 pad, zero_padding, 1);
262 */ 267 */
263 ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in, 268 ret = crypto_skcipher_encrypt(req);
264 src1_len + src2_len + zero_padding); 269 skcipher_request_zero(req);
265 if (ret < 0) { 270 if (ret < 0) {
266 pr_err("ceph_aes_crypt2 failed %d\n", ret); 271 pr_err("ceph_aes_crypt2 failed %d\n", ret);
267 goto out_sg; 272 goto out_sg;
@@ -274,7 +279,7 @@ static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
274out_sg: 279out_sg:
275 teardown_sgtable(&sg_out); 280 teardown_sgtable(&sg_out);
276out_tfm: 281out_tfm:
277 crypto_free_blkcipher(tfm); 282 crypto_free_skcipher(tfm);
278 return ret; 283 return ret;
279} 284}
280 285
@@ -284,11 +289,10 @@ static int ceph_aes_decrypt(const void *key, int key_len,
284{ 289{
285 struct sg_table sg_in; 290 struct sg_table sg_in;
286 struct scatterlist sg_out[2], prealloc_sg; 291 struct scatterlist sg_out[2], prealloc_sg;
287 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); 292 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher();
288 struct blkcipher_desc desc = { .tfm = tfm }; 293 SKCIPHER_REQUEST_ON_STACK(req, tfm);
289 char pad[16]; 294 char pad[16];
290 void *iv; 295 char iv[AES_BLOCK_SIZE];
291 int ivsize;
292 int ret; 296 int ret;
293 int last_byte; 297 int last_byte;
294 298
@@ -302,10 +306,13 @@ static int ceph_aes_decrypt(const void *key, int key_len,
302 if (ret) 306 if (ret)
303 goto out_tfm; 307 goto out_tfm;
304 308
305 crypto_blkcipher_setkey((void *)tfm, key, key_len); 309 crypto_skcipher_setkey((void *)tfm, key, key_len);
306 iv = crypto_blkcipher_crt(tfm)->iv; 310 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
307 ivsize = crypto_blkcipher_ivsize(tfm); 311
308 memcpy(iv, aes_iv, ivsize); 312 skcipher_request_set_tfm(req, tfm);
313 skcipher_request_set_callback(req, 0, NULL, NULL);
314 skcipher_request_set_crypt(req, sg_in.sgl, sg_out,
315 src_len, iv);
309 316
310 /* 317 /*
311 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1, 318 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
@@ -313,7 +320,8 @@ static int ceph_aes_decrypt(const void *key, int key_len,
313 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1, 320 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
314 src, src_len, 1); 321 src, src_len, 1);
315 */ 322 */
316 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len); 323 ret = crypto_skcipher_decrypt(req);
324 skcipher_request_zero(req);
317 if (ret < 0) { 325 if (ret < 0) {
318 pr_err("ceph_aes_decrypt failed %d\n", ret); 326 pr_err("ceph_aes_decrypt failed %d\n", ret);
319 goto out_sg; 327 goto out_sg;
@@ -338,7 +346,7 @@ static int ceph_aes_decrypt(const void *key, int key_len,
338out_sg: 346out_sg:
339 teardown_sgtable(&sg_in); 347 teardown_sgtable(&sg_in);
340out_tfm: 348out_tfm:
341 crypto_free_blkcipher(tfm); 349 crypto_free_skcipher(tfm);
342 return ret; 350 return ret;
343} 351}
344 352
@@ -349,11 +357,10 @@ static int ceph_aes_decrypt2(const void *key, int key_len,
349{ 357{
350 struct sg_table sg_in; 358 struct sg_table sg_in;
351 struct scatterlist sg_out[3], prealloc_sg; 359 struct scatterlist sg_out[3], prealloc_sg;
352 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); 360 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher();
353 struct blkcipher_desc desc = { .tfm = tfm }; 361 SKCIPHER_REQUEST_ON_STACK(req, tfm);
354 char pad[16]; 362 char pad[16];
355 void *iv; 363 char iv[AES_BLOCK_SIZE];
356 int ivsize;
357 int ret; 364 int ret;
358 int last_byte; 365 int last_byte;
359 366
@@ -368,10 +375,13 @@ static int ceph_aes_decrypt2(const void *key, int key_len,
368 if (ret) 375 if (ret)
369 goto out_tfm; 376 goto out_tfm;
370 377
371 crypto_blkcipher_setkey((void *)tfm, key, key_len); 378 crypto_skcipher_setkey((void *)tfm, key, key_len);
372 iv = crypto_blkcipher_crt(tfm)->iv; 379 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
373 ivsize = crypto_blkcipher_ivsize(tfm); 380
374 memcpy(iv, aes_iv, ivsize); 381 skcipher_request_set_tfm(req, tfm);
382 skcipher_request_set_callback(req, 0, NULL, NULL);
383 skcipher_request_set_crypt(req, sg_in.sgl, sg_out,
384 src_len, iv);
375 385
376 /* 386 /*
377 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1, 387 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
@@ -379,7 +389,8 @@ static int ceph_aes_decrypt2(const void *key, int key_len,
379 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1, 389 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
380 src, src_len, 1); 390 src, src_len, 1);
381 */ 391 */
382 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len); 392 ret = crypto_skcipher_decrypt(req);
393 skcipher_request_zero(req);
383 if (ret < 0) { 394 if (ret < 0) {
384 pr_err("ceph_aes_decrypt failed %d\n", ret); 395 pr_err("ceph_aes_decrypt failed %d\n", ret);
385 goto out_sg; 396 goto out_sg;
@@ -415,7 +426,7 @@ static int ceph_aes_decrypt2(const void *key, int key_len,
415out_sg: 426out_sg:
416 teardown_sgtable(&sg_in); 427 teardown_sgtable(&sg_in);
417out_tfm: 428out_tfm:
418 crypto_free_blkcipher(tfm); 429 crypto_free_skcipher(tfm);
419 return ret; 430 return ret;
420} 431}
421 432