qdf_crypto.h 5.9 KB

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