Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Bluetooth: Use skcipher and hash

This patch replaces uses of blkcipher with skcipher and the long
obsolete hash interface with shash.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Acked-by: Marcel Holtmann <marcel@holtmann.org>

+63 -72
+63 -72
net/bluetooth/smp.c
··· 21 21 */ 22 22 23 23 #include <linux/debugfs.h> 24 - #include <linux/crypto.h> 25 24 #include <linux/scatterlist.h> 26 25 #include <crypto/b128ops.h> 26 + #include <crypto/hash.h> 27 + #include <crypto/skcipher.h> 27 28 28 29 #include <net/bluetooth/bluetooth.h> 29 30 #include <net/bluetooth/hci_core.h> ··· 88 87 u8 min_key_size; 89 88 u8 max_key_size; 90 89 91 - struct crypto_blkcipher *tfm_aes; 92 - struct crypto_hash *tfm_cmac; 90 + struct crypto_skcipher *tfm_aes; 91 + struct crypto_shash *tfm_cmac; 93 92 }; 94 93 95 94 struct smp_chan { ··· 127 126 u8 dhkey[32]; 128 127 u8 mackey[16]; 129 128 130 - struct crypto_blkcipher *tfm_aes; 131 - struct crypto_hash *tfm_cmac; 129 + struct crypto_skcipher *tfm_aes; 130 + struct crypto_shash *tfm_cmac; 132 131 }; 133 132 134 133 /* These debug key values are defined in the SMP section of the core ··· 166 165 * AES-CMAC, f4, f5, f6, g2 and h6. 167 166 */ 168 167 169 - static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m, 168 + static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m, 170 169 size_t len, u8 mac[16]) 171 170 { 172 171 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; 173 - struct hash_desc desc; 174 - struct scatterlist sg; 172 + SHASH_DESC_ON_STACK(desc, tfm); 175 173 int err; 176 174 177 175 if (len > CMAC_MSG_MAX) ··· 181 181 return -EINVAL; 182 182 } 183 183 184 - desc.tfm = tfm; 185 - desc.flags = 0; 186 - 187 - crypto_hash_init(&desc); 184 + desc->tfm = tfm; 185 + desc->flags = 0; 188 186 189 187 /* Swap key and message from LSB to MSB */ 190 188 swap_buf(k, tmp, 16); ··· 191 193 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m); 192 194 SMP_DBG("key %16phN", k); 193 195 194 - err = crypto_hash_setkey(tfm, tmp, 16); 196 + err = crypto_shash_setkey(tfm, tmp, 16); 195 197 if (err) { 196 198 BT_ERR("cipher setkey failed: %d", err); 197 199 return err; 198 200 } 199 201 200 - sg_init_one(&sg, msg_msb, len); 201 - 202 - err = crypto_hash_update(&desc, &sg, len); 202 + err = crypto_shash_digest(desc, msg_msb, len, mac_msb); 203 + shash_desc_zero(desc); 203 204 if (err) { 204 - BT_ERR("Hash update error %d", err); 205 - return err; 206 - } 207 - 208 - err = crypto_hash_final(&desc, mac_msb); 209 - if (err) { 210 - BT_ERR("Hash final error %d", err); 205 + BT_ERR("Hash computation error %d", err); 211 206 return err; 212 207 } 213 208 ··· 211 220 return 0; 212 221 } 213 222 214 - static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], 215 - const u8 x[16], u8 z, u8 res[16]) 223 + static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32], 224 + const u8 v[32], const u8 x[16], u8 z, u8 res[16]) 216 225 { 217 226 u8 m[65]; 218 227 int err; ··· 234 243 return err; 235 244 } 236 245 237 - static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32], 246 + static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32], 238 247 const u8 n1[16], const u8 n2[16], const u8 a1[7], 239 248 const u8 a2[7], u8 mackey[16], u8 ltk[16]) 240 249 { ··· 287 296 return 0; 288 297 } 289 298 290 - static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16], 299 + static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16], 291 300 const u8 n1[16], const u8 n2[16], const u8 r[16], 292 301 const u8 io_cap[3], const u8 a1[7], const u8 a2[7], 293 302 u8 res[16]) ··· 315 324 return err; 316 325 } 317 326 318 - static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], 327 + static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32], 319 328 const u8 x[16], const u8 y[16], u32 *val) 320 329 { 321 330 u8 m[80], tmp[16]; ··· 341 350 return 0; 342 351 } 343 352 344 - static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16], 353 + static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16], 345 354 const u8 key_id[4], u8 res[16]) 346 355 { 347 356 int err; ··· 361 370 * s1 and ah. 362 371 */ 363 372 364 - static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) 373 + static int smp_e(struct crypto_skcipher *tfm, const u8 *k, u8 *r) 365 374 { 366 - struct blkcipher_desc desc; 375 + SKCIPHER_REQUEST_ON_STACK(req, tfm); 367 376 struct scatterlist sg; 368 377 uint8_t tmp[16], data[16]; 369 378 int err; ··· 375 384 return -EINVAL; 376 385 } 377 386 378 - desc.tfm = tfm; 379 - desc.flags = 0; 380 - 381 387 /* The most significant octet of key corresponds to k[0] */ 382 388 swap_buf(k, tmp, 16); 383 389 384 - err = crypto_blkcipher_setkey(tfm, tmp, 16); 390 + err = crypto_skcipher_setkey(tfm, tmp, 16); 385 391 if (err) { 386 392 BT_ERR("cipher setkey failed: %d", err); 387 393 return err; ··· 389 401 390 402 sg_init_one(&sg, data, 16); 391 403 392 - err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); 404 + skcipher_request_set_tfm(req, tfm); 405 + skcipher_request_set_callback(req, 0, NULL, NULL); 406 + skcipher_request_set_crypt(req, &sg, &sg, 16, NULL); 407 + 408 + err = crypto_skcipher_encrypt(req); 409 + skcipher_request_zero(req); 393 410 if (err) 394 411 BT_ERR("Encrypt data error %d", err); 395 412 ··· 406 413 return err; 407 414 } 408 415 409 - static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16], 416 + static int smp_c1(struct crypto_skcipher *tfm_aes, const u8 k[16], 410 417 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat, 411 418 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16]) 412 419 { ··· 455 462 return err; 456 463 } 457 464 458 - static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16], 465 + static int smp_s1(struct crypto_skcipher *tfm_aes, const u8 k[16], 459 466 const u8 r1[16], const u8 r2[16], u8 _r[16]) 460 467 { 461 468 int err; ··· 471 478 return err; 472 479 } 473 480 474 - static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16], 481 + static int smp_ah(struct crypto_skcipher *tfm, const u8 irk[16], 475 482 const u8 r[3], u8 res[3]) 476 483 { 477 484 u8 _res[16]; ··· 759 766 kzfree(smp->slave_csrk); 760 767 kzfree(smp->link_key); 761 768 762 - crypto_free_blkcipher(smp->tfm_aes); 763 - crypto_free_hash(smp->tfm_cmac); 769 + crypto_free_skcipher(smp->tfm_aes); 770 + crypto_free_shash(smp->tfm_cmac); 764 771 765 772 /* Ensure that we don't leave any debug key around if debug key 766 773 * support hasn't been explicitly enabled. ··· 1375 1382 if (!smp) 1376 1383 return NULL; 1377 1384 1378 - smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 1385 + smp->tfm_aes = crypto_alloc_skcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 1379 1386 if (IS_ERR(smp->tfm_aes)) { 1380 1387 BT_ERR("Unable to create ECB crypto context"); 1381 1388 kzfree(smp); 1382 1389 return NULL; 1383 1390 } 1384 1391 1385 - smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); 1392 + smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0); 1386 1393 if (IS_ERR(smp->tfm_cmac)) { 1387 1394 BT_ERR("Unable to create CMAC crypto context"); 1388 - crypto_free_blkcipher(smp->tfm_aes); 1395 + crypto_free_skcipher(smp->tfm_aes); 1389 1396 kzfree(smp); 1390 1397 return NULL; 1391 1398 } ··· 3136 3143 { 3137 3144 struct l2cap_chan *chan; 3138 3145 struct smp_dev *smp; 3139 - struct crypto_blkcipher *tfm_aes; 3140 - struct crypto_hash *tfm_cmac; 3146 + struct crypto_skcipher *tfm_aes; 3147 + struct crypto_shash *tfm_cmac; 3141 3148 3142 3149 if (cid == L2CAP_CID_SMP_BREDR) { 3143 3150 smp = NULL; ··· 3148 3155 if (!smp) 3149 3156 return ERR_PTR(-ENOMEM); 3150 3157 3151 - tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 3158 + tfm_aes = crypto_alloc_skcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 3152 3159 if (IS_ERR(tfm_aes)) { 3153 3160 BT_ERR("Unable to create ECB crypto context"); 3154 3161 kzfree(smp); 3155 3162 return ERR_CAST(tfm_aes); 3156 3163 } 3157 3164 3158 - tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); 3165 + tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0); 3159 3166 if (IS_ERR(tfm_cmac)) { 3160 3167 BT_ERR("Unable to create CMAC crypto context"); 3161 - crypto_free_blkcipher(tfm_aes); 3168 + crypto_free_skcipher(tfm_aes); 3162 3169 kzfree(smp); 3163 3170 return ERR_CAST(tfm_cmac); 3164 3171 } ··· 3172 3179 chan = l2cap_chan_create(); 3173 3180 if (!chan) { 3174 3181 if (smp) { 3175 - crypto_free_blkcipher(smp->tfm_aes); 3176 - crypto_free_hash(smp->tfm_cmac); 3182 + crypto_free_skcipher(smp->tfm_aes); 3183 + crypto_free_shash(smp->tfm_cmac); 3177 3184 kzfree(smp); 3178 3185 } 3179 3186 return ERR_PTR(-ENOMEM); ··· 3219 3226 smp = chan->data; 3220 3227 if (smp) { 3221 3228 chan->data = NULL; 3222 - if (smp->tfm_aes) 3223 - crypto_free_blkcipher(smp->tfm_aes); 3224 - if (smp->tfm_cmac) 3225 - crypto_free_hash(smp->tfm_cmac); 3229 + crypto_free_skcipher(smp->tfm_aes); 3230 + crypto_free_shash(smp->tfm_cmac); 3226 3231 kzfree(smp); 3227 3232 } 3228 3233 ··· 3456 3465 3457 3466 #if IS_ENABLED(CONFIG_BT_SELFTEST_SMP) 3458 3467 3459 - static int __init test_ah(struct crypto_blkcipher *tfm_aes) 3468 + static int __init test_ah(struct crypto_skcipher *tfm_aes) 3460 3469 { 3461 3470 const u8 irk[16] = { 3462 3471 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, ··· 3476 3485 return 0; 3477 3486 } 3478 3487 3479 - static int __init test_c1(struct crypto_blkcipher *tfm_aes) 3488 + static int __init test_c1(struct crypto_skcipher *tfm_aes) 3480 3489 { 3481 3490 const u8 k[16] = { 3482 3491 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ··· 3506 3515 return 0; 3507 3516 } 3508 3517 3509 - static int __init test_s1(struct crypto_blkcipher *tfm_aes) 3518 + static int __init test_s1(struct crypto_skcipher *tfm_aes) 3510 3519 { 3511 3520 const u8 k[16] = { 3512 3521 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ··· 3531 3540 return 0; 3532 3541 } 3533 3542 3534 - static int __init test_f4(struct crypto_hash *tfm_cmac) 3543 + static int __init test_f4(struct crypto_shash *tfm_cmac) 3535 3544 { 3536 3545 const u8 u[32] = { 3537 3546 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, ··· 3563 3572 return 0; 3564 3573 } 3565 3574 3566 - static int __init test_f5(struct crypto_hash *tfm_cmac) 3575 + static int __init test_f5(struct crypto_shash *tfm_cmac) 3567 3576 { 3568 3577 const u8 w[32] = { 3569 3578 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86, ··· 3600 3609 return 0; 3601 3610 } 3602 3611 3603 - static int __init test_f6(struct crypto_hash *tfm_cmac) 3612 + static int __init test_f6(struct crypto_shash *tfm_cmac) 3604 3613 { 3605 3614 const u8 w[16] = { 3606 3615 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd, ··· 3633 3642 return 0; 3634 3643 } 3635 3644 3636 - static int __init test_g2(struct crypto_hash *tfm_cmac) 3645 + static int __init test_g2(struct crypto_shash *tfm_cmac) 3637 3646 { 3638 3647 const u8 u[32] = { 3639 3648 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, ··· 3665 3674 return 0; 3666 3675 } 3667 3676 3668 - static int __init test_h6(struct crypto_hash *tfm_cmac) 3677 + static int __init test_h6(struct crypto_shash *tfm_cmac) 3669 3678 { 3670 3679 const u8 w[16] = { 3671 3680 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, ··· 3702 3711 .llseek = default_llseek, 3703 3712 }; 3704 3713 3705 - static int __init run_selftests(struct crypto_blkcipher *tfm_aes, 3706 - struct crypto_hash *tfm_cmac) 3714 + static int __init run_selftests(struct crypto_skcipher *tfm_aes, 3715 + struct crypto_shash *tfm_cmac) 3707 3716 { 3708 3717 ktime_t calltime, delta, rettime; 3709 3718 unsigned long long duration; ··· 3780 3789 3781 3790 int __init bt_selftest_smp(void) 3782 3791 { 3783 - struct crypto_blkcipher *tfm_aes; 3784 - struct crypto_hash *tfm_cmac; 3792 + struct crypto_skcipher *tfm_aes; 3793 + struct crypto_shash *tfm_cmac; 3785 3794 int err; 3786 3795 3787 - tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 3796 + tfm_aes = crypto_alloc_skcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 3788 3797 if (IS_ERR(tfm_aes)) { 3789 3798 BT_ERR("Unable to create ECB crypto context"); 3790 3799 return PTR_ERR(tfm_aes); 3791 3800 } 3792 3801 3793 - tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); 3802 + tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); 3794 3803 if (IS_ERR(tfm_cmac)) { 3795 3804 BT_ERR("Unable to create CMAC crypto context"); 3796 - crypto_free_blkcipher(tfm_aes); 3805 + crypto_free_skcipher(tfm_aes); 3797 3806 return PTR_ERR(tfm_cmac); 3798 3807 } 3799 3808 3800 3809 err = run_selftests(tfm_aes, tfm_cmac); 3801 3810 3802 - crypto_free_hash(tfm_cmac); 3803 - crypto_free_blkcipher(tfm_aes); 3811 + crypto_free_shash(tfm_cmac); 3812 + crypto_free_skcipher(tfm_aes); 3804 3813 3805 3814 return err; 3806 3815 }