DtlsWindowing.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file
  26. *
  27. * \brief This file implements the APIs, types used in the
  28. * for DTLS windowing.
  29. *
  30. * \addtogroup grMutualAuth
  31. * @{
  32. */
  33. #include <stdint.h>
  34. #include "optiga/dtls/DtlsWindowing.h"
  35. #include "optiga/dtls/DtlsRecordLayer.h"
  36. #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
  37. /// @cond hidden
  38. ///Maximum window size supported
  39. #define MAX_WINDOW_SIZE 64
  40. /// @endcond
  41. /**
  42. * Implementation for Record Replay Detection.<br>
  43. * Return status as #OCP_RL_WINDOW_IGNORE if record is already received or record sequence number is less then lower bound of window.<br>
  44. * Under some erroneous conditions, error codes from Record Layer can also be returned.<br>
  45. *
  46. * \param[in] PpsWindow Pointer to the structure that contains details required for windowing like
  47. * record sequence number, lower and higher boundaries.
  48. *
  49. * \retval OCP_RL_WINDOW_UPDATED Valid record is received and window is updated.
  50. * \retval OCP_RL_WINDOW_MOVED Valid record is received and window is updated and moved.
  51. * \retval OCP_RL_WINDOW_IGNORE Failure in execution and record already received.
  52. *
  53. */
  54. int32_t DtlsCheckReplay(sWindow_d *PpsWindow)
  55. {
  56. int32_t i4Status = (int32_t) OCP_RL_WINDOW_IGNORE;
  57. int32_t i4Retval;
  58. sUint64 sIntermidateVal;
  59. do
  60. {
  61. #ifdef ENABLE_NULL_CHECKS
  62. if((NULL == PpsWindow) || (NULL == PpsWindow->fValidateRecord))
  63. {
  64. break;
  65. }
  66. #endif
  67. if((MAX_WINDOW_SIZE < PpsWindow->bWindowSize) || (WORD_SIZE > PpsWindow->bWindowSize))
  68. {
  69. break;
  70. }
  71. //Compare the received sequence number with the Lower window boundary
  72. i4Retval = CompareUint64(&PpsWindow->sRecvSeqNumber, &PpsWindow->sLowerBound);
  73. //If sequence number is lesser than the low bound of window
  74. if(LESSER_THAN == i4Retval)
  75. {
  76. break;
  77. }
  78. //If sequence number is greater than low bound window
  79. //Compare the received sequence number with the Higher window boundary
  80. i4Retval = CompareUint64(&PpsWindow->sRecvSeqNumber, &PpsWindow->sHigherBound);
  81. //If Sequence number is greater than high bound of the window
  82. //Slide the window
  83. if(GREATER_THAN == i4Retval)
  84. {
  85. //Record validation
  86. i4Retval = PpsWindow->fValidateRecord(PpsWindow->pValidateArgs);
  87. //If record validation fails
  88. if(OCP_RL_OK != i4Retval)
  89. {
  90. if(((int32_t)CMD_LIB_DECRYPT_FAILURE == i4Retval) || ((int32_t)OCP_RL_MALLOC_FAILURE == i4Retval))
  91. {
  92. i4Status = i4Retval;
  93. }
  94. break;
  95. }
  96. else
  97. {
  98. //Calculate the count to slide the window
  99. //lint --e{534} suppress "The return value check is suppressed as this function always return Success.Only error condition where
  100. //RecvSeqNumber < sHigherBound is not possible as it will enter this path only when RecvSeqNumber > sHigherBound"
  101. i4Retval = SubtractUint64(&PpsWindow->sRecvSeqNumber, &PpsWindow->sHigherBound, &sIntermidateVal);
  102. //Slide the window
  103. i4Retval = ShiftLeftUint64(&PpsWindow->sWindowFrame, sIntermidateVal, PpsWindow->bWindowSize, (uint8_t)MAX_WINDOW_SIZE);
  104. if(UTIL_SUCCESS != i4Retval)
  105. {
  106. break;
  107. }
  108. //Set the sequence number received as the Higher Bound
  109. PpsWindow->sHigherBound = PpsWindow->sRecvSeqNumber;
  110. sIntermidateVal.dwHigherByte = DEFAULT_LOWBOUND_DOUBLEWORD ;
  111. sIntermidateVal.dwLowerByte = (uint32_t)PpsWindow->bWindowSize - 1;
  112. //Difference of Higher bound and window size is set as lower bound
  113. i4Retval = SubtractUint64(&PpsWindow->sHigherBound, &sIntermidateVal, &PpsWindow->sLowerBound);
  114. if(UTIL_SUCCESS != i4Retval)
  115. {
  116. break;
  117. }
  118. //Set the bit position of sequence number to 1 which is the MSB of the window frame
  119. i4Retval = Utility_SetBitUint64(&PpsWindow->sWindowFrame, PpsWindow->bWindowSize, PpsWindow->bWindowSize);
  120. if(UTIL_SUCCESS != i4Retval)
  121. {
  122. break;
  123. }
  124. i4Status = (int32_t) OCP_RL_WINDOW_MOVED;
  125. break;
  126. }
  127. }
  128. //Compare the received sequence number with the Higher and Lower window boundary
  129. //lint --e{534} suppress "The return value check is suppressed as this function always return Success.Only error condition where
  130. //RecvSeqNumber > sHigherBound is not possible as it will enter this path only when RecvSeqNumber < sHigherBound"
  131. //Calculate bit position of sequence number from high bound of the window
  132. i4Retval = SubtractUint64(&PpsWindow->sHigherBound, &PpsWindow->sRecvSeqNumber, &sIntermidateVal);
  133. //If window size is equal to 32
  134. if(WORD_SIZE == PpsWindow->bWindowSize)
  135. {
  136. if((MOST_SIGNIFICANT_BIT_HIGH == ((PpsWindow->sWindowFrame.dwHigherByte << (uint32_t)((WORD_SIZE - sIntermidateVal.dwLowerByte) - 1))
  137. & MOST_SIGNIFICANT_BIT_HIGH)))
  138. {
  139. break;
  140. }
  141. }
  142. else
  143. {
  144. //Received sequence number is in the lower byte of the window frame
  145. if((DEFAULT_LOWBOUND_DOUBLEWORD == sIntermidateVal.dwHigherByte) && (sIntermidateVal.dwLowerByte < WORD_SIZE))
  146. {
  147. if((MOST_SIGNIFICANT_BIT_HIGH == ((PpsWindow->sWindowFrame.dwLowerByte << (uint32_t)((WORD_SIZE - sIntermidateVal.dwLowerByte) - 1 )) & MOST_SIGNIFICANT_BIT_HIGH)))
  148. {
  149. break;
  150. }
  151. }
  152. //Received sequence number is in the higher byte of the window frame
  153. else if((DEFAULT_LOWBOUND_DOUBLEWORD == sIntermidateVal.dwHigherByte) && (sIntermidateVal.dwLowerByte >= WORD_SIZE))
  154. {
  155. if((MOST_SIGNIFICANT_BIT_HIGH == ((PpsWindow->sWindowFrame.dwHigherByte << (uint32_t)((MAX_WINDOW_SIZE - sIntermidateVal.dwLowerByte) - 1)) & MOST_SIGNIFICANT_BIT_HIGH)))
  156. {
  157. break;
  158. }
  159. }
  160. }
  161. //Record validation
  162. i4Retval = PpsWindow->fValidateRecord(PpsWindow->pValidateArgs);
  163. //If record validation fails
  164. if(OCP_RL_OK != i4Retval)
  165. {
  166. if(((int32_t)CMD_LIB_DECRYPT_FAILURE == i4Retval) || ((int32_t)OCP_RL_MALLOC_FAILURE == i4Retval))
  167. {
  168. i4Status = i4Retval;
  169. }
  170. break;
  171. }
  172. else
  173. {
  174. i4Retval = SubtractUint64(&PpsWindow->sRecvSeqNumber, &PpsWindow->sLowerBound,&sIntermidateVal);
  175. if(UTIL_SUCCESS != i4Retval)
  176. {
  177. break;
  178. }
  179. //Set the bit position of sequence number to 1
  180. i4Retval = Utility_SetBitUint64(&PpsWindow->sWindowFrame, PpsWindow->bWindowSize, (uint8_t)sIntermidateVal.dwLowerByte);
  181. if(UTIL_SUCCESS != i4Retval)
  182. {
  183. break;
  184. }
  185. if(PpsWindow->bWindowSize > WORD_SIZE)
  186. {
  187. PpsWindow->sWindowFrame.dwHigherByte &= MASK_DOUBLE_WORD >> (MAX_WINDOW_SIZE - PpsWindow->bWindowSize);
  188. }
  189. i4Status = (int32_t)OCP_RL_WINDOW_UPDATED;
  190. }
  191. }while(0);
  192. return i4Status;
  193. }
  194. #endif /*MODULE_ENABLE_DTLS_MUTUAL_AUTH*/