diff options
author | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2023-03-02 13:47:44 -0800 |
---|---|---|
committer | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2023-03-10 13:41:06 -0800 |
commit | c1dd94cc7f810e72ded5ef3886208fbe7b35483c (patch) | |
tree | 5b65f1d5bd197afcc08b736d8e25f6dfe86d0f61 /src/shared | |
parent | 31ab084f072c335b5fbaab1c44ecaf899f5d5dda (diff) | |
download | bluez-c1dd94cc7f810e72ded5ef3886208fbe7b35483c.tar.gz |
shared/crypto: Add bt_crypto_sirk
This adds bt_crypto_sirk which attempts to generate a unique SIRK using
the following steps:
- Generate a hash (k) using the str as input
- Generate a hash (sirk) using vendor, product, version and source as input
- Encrypt sirk using k as LTK with sef function.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/crypto.c | 40 | ||||
-rw-r--r-- | src/shared/crypto.h | 3 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/shared/crypto.c b/src/shared/crypto.c index 4cb2ea857..5449621b5 100644 --- a/src/shared/crypto.c +++ b/src/shared/crypto.c @@ -926,3 +926,43 @@ bool bt_crypto_sef(struct bt_crypto *crypto, const uint8_t k[16], return true; } + +/* Generates a SIRK from a string using the following steps: + * - Generate a hash (k) using the str as input + * - Generate a hash (sirk) using vendor, product, version and source as input + * - Encrypt sirk using k as LTK with sef function. + */ +bool bt_crypto_sirk(struct bt_crypto *crypto, const char *str, uint16_t vendor, + uint16_t product, uint16_t version, uint16_t source, + uint8_t sirk[16]) +{ + struct iovec iov[4]; + uint8_t k[16]; + uint8_t sirk_plaintext[16]; + + if (!crypto) + return false; + + iov[0].iov_base = (void *)str; + iov[0].iov_len = strlen(str); + + /* Generate a k using the str as input */ + if (!bt_crypto_gatt_hash(crypto, iov, 1, k)) + return false; + + iov[0].iov_base = &vendor; + iov[0].iov_len = sizeof(vendor); + iov[1].iov_base = &product; + iov[1].iov_len = sizeof(product); + iov[2].iov_base = &version; + iov[2].iov_len = sizeof(version); + iov[3].iov_base = &source; + iov[3].iov_len = sizeof(source); + + /* Generate a sirk using vendor, product, version and source as input */ + if (!bt_crypto_gatt_hash(crypto, iov, 4, sirk_plaintext)) + return false; + + /* Encrypt sirk using k as LTK with sef function */ + return bt_crypto_sef(crypto, k, sirk_plaintext, sirk); +} diff --git a/src/shared/crypto.h b/src/shared/crypto.h index fc1ba0c4f..d45308abf 100644 --- a/src/shared/crypto.h +++ b/src/shared/crypto.h @@ -57,3 +57,6 @@ bool bt_crypto_sef(struct bt_crypto *crypto, const uint8_t k[16], const uint8_t sirk[16], uint8_t out[16]); bool bt_crypto_sih(struct bt_crypto *crypto, const uint8_t k[16], const uint8_t r[3], uint8_t hash[3]); +bool bt_crypto_sirk(struct bt_crypto *crypto, const char *str, uint16_t vendor, + uint16_t product, uint16_t version, uint16_t source, + uint8_t sirk[16]); |