Bluetooth: Introduce public big-endian AES API

There may be use cases where input and output is in big-endian rather
than little-endian. Introduce a native big-endian API to avoid
excessive byte order reversals in these cases.

Change-Id: Ia7b3e01bb0a07c4560b23f60c2f615ec614eb431
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-03-16 10:56:35 +02:00
commit 94831a7589
4 changed files with 58 additions and 11 deletions

View file

@ -37,7 +37,7 @@ extern "C" {
*/
int bt_rand(void *buf, size_t len);
/** @brief AES encrypt data.
/** @brief AES encrypt little-endian data.
*
* An AES encrypt helper is used to request the Bluetooth controller's own
* hardware to encrypt the plaintext using the key and returns the encrypted
@ -49,8 +49,23 @@ int bt_rand(void *buf, size_t len);
*
* @return Zero on success or error code otherwise.
*/
int bt_encrypt(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16]);
int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16]);
/** @brief AES encrypt big-endian data.
*
* An AES encrypt helper is used to request the Bluetooth controller's own
* hardware to encrypt the plaintext using the key and returns the encrypted
* data.
*
* @param key 128 bit MS byte first key for the encryption of the plaintext
* @param plaintext 128 bit MS byte first plaintext data block to be encrypted
* @param enc_data 128 bit MS byte first encrypted data block
*
* @return Zero on success or error code otherwise.
*/
int bt_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16]);
#ifdef __cplusplus
}

View file

@ -29,8 +29,8 @@ int bt_rand(void *buf, size_t len)
return 0;
}
int bt_encrypt(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
{
BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
@ -40,3 +40,15 @@ int bt_encrypt(const uint8_t key[16], const uint8_t plaintext[16],
return 0;
}
int bt_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
{
BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
ecb_encrypt_be(key, plaintext, enc_data);
BT_DBG("enc_data %s", bt_hex(enc_data, 16));
return 0;
}

View file

@ -107,8 +107,8 @@ int bt_rand(void *buf, size_t len)
return -EIO;
}
int bt_encrypt(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
{
struct tc_aes_key_sched_struct s;
uint8_t tmp[16];
@ -133,3 +133,23 @@ int bt_encrypt(const uint8_t key[16], const uint8_t plaintext[16],
return 0;
}
int bt_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16],
uint8_t enc_data[16])
{
struct tc_aes_key_sched_struct s;
BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) {
return -EINVAL;
}
if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) {
return -EINVAL;
}
BT_DBG("enc_data %s", bt_hex(enc_data, 16));
return 0;
}

View file

@ -312,7 +312,7 @@ static int smp_ah(const uint8_t irk[16], const uint8_t r[3], uint8_t out[3])
memcpy(res, r, 3);
memset(res + 3, 0, 13);
err = bt_encrypt(irk, res, res);
err = bt_encrypt_le(irk, res, res);
if (err) {
return err;
}
@ -1552,7 +1552,7 @@ static int smp_c1(const uint8_t k[16], const uint8_t r[16],
/* Using enc_data as temporary output buffer */
xor_128(r, p1, enc_data);
err = bt_encrypt(k, enc_data, enc_data);
err = bt_encrypt_le(k, enc_data, enc_data);
if (err) {
return err;
}
@ -1566,7 +1566,7 @@ static int smp_c1(const uint8_t k[16], const uint8_t r[16],
xor_128(enc_data, p2, enc_data);
return bt_encrypt(k, enc_data, enc_data);
return bt_encrypt_le(k, enc_data, enc_data);
}
#endif /* !CONFIG_BLUETOOTH_SMP_SC_ONLY */
@ -1797,7 +1797,7 @@ static int smp_s1(const uint8_t k[16], const uint8_t r1[16],
memcpy(out + 8, r1, 8);
/* s1(k, r1 , r2) = e(k, r') */
return bt_encrypt(k, out, out);
return bt_encrypt_le(k, out, out);
}
static uint8_t legacy_get_pair_method(struct bt_smp *smp, uint8_t remote_io)