qdf_crypto.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2017-2018, 2020 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /**
  20. * DOC: qdf_crypto.h
  21. * This file provides OS abstraction for crypto APIs.
  22. */
  23. #if !defined(__QDF_CRYPTO_H)
  24. #define __QDF_CRYPTO_H
  25. /* Include Files */
  26. #include "qdf_status.h"
  27. #include <qdf_types.h>
  28. #include <qdf_trace.h>
  29. /* Preprocessor definitions and constants */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. #define AES_BLOCK_SIZE 16
  34. #define HMAC_SHA256_CRYPTO_TYPE "hmac(sha256)"
  35. #define HMAC_SHA386_CRYPTO_TYPE "hmac(sha384)"
  36. #define SHA256_CRYPTO_TYPE "sha256"
  37. #define SHA386_CRYPTO_TYPE "sha384"
  38. #define SHA256_DIGEST_SIZE 32
  39. #define SHA384_DIGEST_SIZE 48
  40. #define FIXED_PARAM_OFFSET_ASSOC_REQ 4
  41. #define FIXED_PARAM_OFFSET_ASSOC_RSP 6
  42. #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
  43. #define AAD_LEN 20
  44. #define IEEE80211_MMIE_GMAC_MICLEN 16
  45. #define IS_VALID_CTR_KEY_LEN(len) ((((len) == 16) || ((len) == 32) || \
  46. ((len) == 48)) ? 1 : 0)
  47. #define WLAN_MAX_PRF_INTERATIONS_COUNT 255
  48. /* Function declarations and documentation */
  49. /**
  50. * qdf_get_hash: API to get hash using specific crypto and scatterlist
  51. * @type: crypto type
  52. * @element_cnt: scatterlist element count
  53. * @addr: scatterlist element array
  54. * @addr_len: element length array
  55. * @hash: new hash
  56. *
  57. * Return: 0 if success else error code
  58. */
  59. int qdf_get_hash(uint8_t *type, uint8_t element_cnt,
  60. uint8_t *addr[], uint32_t *addr_len,
  61. int8_t *hash);
  62. /**
  63. * qdf_get_hmac_hash: API to get hmac hash using specific crypto and
  64. * scatterlist elements.
  65. * @type: crypto type
  66. * @key: key needs to be used for hmac api
  67. * @keylen: length of key
  68. * @element_cnt: scatterlist element count
  69. * @addr: scatterlist element array
  70. * @addr_len: element length array
  71. * @hash: new hash
  72. *
  73. * Return: 0 if success else error code
  74. */
  75. int qdf_get_hmac_hash(uint8_t *type, uint8_t *key,
  76. uint32_t keylen, uint8_t element_cnt,
  77. uint8_t *addr[], uint32_t *addr_len, int8_t *hash);
  78. /**
  79. * qdf_default_hmac_sha256_kdf()- This API calculates key data using default kdf
  80. * defined in RFC4306.
  81. * @secret: key which needs to be used in crypto
  82. * @secret_len: key_len of secret
  83. * @label: PRF label
  84. * @optional_data: Data used for hash
  85. * @optional_data_len: data length
  86. * @key: key data output
  87. * @keylen: key data length
  88. *
  89. * This API creates default KDF as defined in RFC4306
  90. * PRF+ (K,S) = T1 | T2 | T3 | T4 | ...
  91. * T1 = PRF (K, S | 0x01)
  92. * T2 = PRF (K, T1 | S | 0x02)
  93. * T3 = PRF (K, T2 | S | 0x03)
  94. * T4 = PRF (K, T3 | S | 0x04)
  95. *
  96. * for every iteration its creates 32 bit of hash
  97. *
  98. * Return: QDF_STATUS
  99. */
  100. QDF_STATUS
  101. qdf_default_hmac_sha256_kdf(uint8_t *secret, uint32_t secret_len,
  102. uint8_t *label, uint8_t *optional_data,
  103. uint32_t optional_data_len, uint8_t *key,
  104. uint32_t keylen);
  105. /**
  106. * qdf_get_keyed_hash: API to get hash using specific crypto and
  107. * scatterlist elements.
  108. * @alg: crypto type
  109. * @key: key needs to be used for hmac api
  110. * @key_len: length of key
  111. * @src: scatterlist element array
  112. * @src_len: scatterlist element length array
  113. * @num_elements: scatterlist element count
  114. * @out: calculated hash
  115. *
  116. * Return: 0 if success else error code
  117. */
  118. int qdf_get_keyed_hash(const char *alg, const uint8_t *key,
  119. unsigned int key_len, const uint8_t *src[],
  120. size_t *src_len, size_t num_elements, uint8_t *out);
  121. /**
  122. * qdf_update_dbl: This API does the doubling operation as defined in RFC5297
  123. * @d: input for doubling
  124. *
  125. * Return: None
  126. */
  127. void qdf_update_dbl(uint8_t *d);
  128. /**
  129. * qdf_aes_s2v: This API gets vector from AES string as defined in RFC5297
  130. * output length will be AES_BLOCK_SIZE.
  131. * @key: key used for operation
  132. * @key_len: key len
  133. * @s: addresses of elements to be used
  134. * @s_len: array of element length
  135. * @num_s: number of elements
  136. * @out: pointer to output vector
  137. *
  138. * Return: 0 if success else Error number
  139. */
  140. int qdf_aes_s2v(const uint8_t *key, unsigned int key_len, const uint8_t *s[],
  141. size_t s_len[], size_t num_s, uint8_t *out);
  142. /**
  143. * qdf_aes_ctr: This API defines AES Counter Mode
  144. * @key: key used for operation
  145. * @key_len: key len
  146. * @siv: Initialization vector
  147. * @src: input
  148. * @src_len: input len
  149. * @dest: output
  150. * @enc: if encryption needs to be done or decryption
  151. *
  152. * Return: 0 if success else Error number
  153. */
  154. int qdf_aes_ctr(const uint8_t *key, unsigned int key_len, uint8_t *siv,
  155. const uint8_t *src, size_t src_len, uint8_t *dest, bool enc);
  156. /**
  157. * qdf_crypto_aes_gmac: This API calculates MIC for GMAC
  158. * @key: key used for operation
  159. * @key_length: key length
  160. * @iv: Initialization vector
  161. * @aad: Additional authentication data
  162. * @data: Pointer to data
  163. * @data_len: Length of data
  164. * @mic: Pointer to MIC
  165. *
  166. * Return: 0 if success else Error number
  167. */
  168. int qdf_crypto_aes_gmac(const uint8_t *key, uint16_t key_length,
  169. uint8_t *iv, const uint8_t *aad,
  170. const uint8_t *data, uint16_t data_len, uint8_t *mic);
  171. /**
  172. * qdf_crypto_aes_128_cmac: This API calculates MIC for AES 128 CMAC
  173. * @key: key used for operation
  174. * @data: Pointer to data
  175. * @len: Length of data
  176. * @mic: Pointer to MIC
  177. *
  178. * Return: 0 if success else Error number
  179. */
  180. int qdf_crypto_aes_128_cmac(const uint8_t *key, const uint8_t *data,
  181. uint16_t len, uint8_t *mic);
  182. #ifdef __cplusplus
  183. }
  184. #endif /* __cplusplus */
  185. #endif /* __QDF_CRYPTO_H */