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

Bluetooth: Add flexible buffer byte order swapping function

Since the SMP code needs to swap ordering of variable length buffers add
a convenience function that can be used for any length buffer.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>

authored by

Johan Hedberg and committed by
Marcel Holtmann
8a2936f4 533e35d4

+7 -13
+7 -13
net/bluetooth/smp.c
··· 64 64 unsigned long flags; 65 65 }; 66 66 67 - static inline void swap128(const u8 src[16], u8 dst[16]) 67 + static inline void swap_buf(const u8 *src, u8 *dst, size_t len) 68 68 { 69 - int i; 70 - for (i = 0; i < 16; i++) 71 - dst[15 - i] = src[i]; 72 - } 69 + size_t i; 73 70 74 - static inline void swap56(const u8 src[7], u8 dst[7]) 75 - { 76 - int i; 77 - for (i = 0; i < 7; i++) 78 - dst[6 - i] = src[i]; 71 + for (i = 0; i < len; i++) 72 + dst[len - 1 - i] = src[i]; 79 73 } 80 74 81 75 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) ··· 88 94 desc.flags = 0; 89 95 90 96 /* The most significant octet of key corresponds to k[0] */ 91 - swap128(k, tmp); 97 + swap_buf(k, tmp, 16); 92 98 93 99 err = crypto_blkcipher_setkey(tfm, tmp, 16); 94 100 if (err) { ··· 97 103 } 98 104 99 105 /* Most significant octet of plaintextData corresponds to data[0] */ 100 - swap128(r, data); 106 + swap_buf(r, data, 16); 101 107 102 108 sg_init_one(&sg, data, 16); 103 109 ··· 106 112 BT_ERR("Encrypt data error %d", err); 107 113 108 114 /* Most significant octet of encryptedData corresponds to data[0] */ 109 - swap128(data, r); 115 + swap_buf(data, r, 16); 110 116 111 117 return err; 112 118 }