• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
18 #include "rec_anti_replay.h"
19 
20 #define REC_SLID_WINDOW_SIZE 64
21 
RecAntiReplayReset(RecSlidWindow * w)22 void RecAntiReplayReset(RecSlidWindow *w)
23 {
24     w->top = 0;
25     w->window = 0;
26     return;
27 }
28 
RecAntiReplayCheck(const RecSlidWindow * w,uint64_t seq)29 bool RecAntiReplayCheck(const RecSlidWindow *w, uint64_t seq)
30 {
31     if (seq > w->top) {
32         return false;
33     }
34 
35     uint64_t bit = w->top - seq;
36     if (bit >= REC_SLID_WINDOW_SIZE) {
37         /* The sequence number must be smaller than or equal to the minimum value of the sliding window */
38         return true;
39     }
40     /* return true: The sequence number is equal to a certain value in the sliding window */
41     return (w->window & ((uint64_t)1 << bit)) != 0;
42 }
43 
RecAntiReplayUpdate(RecSlidWindow * w,uint64_t seq)44 void RecAntiReplayUpdate(RecSlidWindow *w, uint64_t seq)
45 {
46     /* If the sequence number is too small, the flag bit is not updated */
47     if ((seq + REC_SLID_WINDOW_SIZE) <= w->top) {
48         return;
49     }
50 
51     /* If the sequence number is less than or equal to top, update the flag */
52     if (seq <= w->top) {
53         uint64_t bit = w->top - seq;
54         w->window |= (uint64_t)1 << bit;
55         return;
56     }
57 
58     /* If the sequence number is greater than top, update the maximum sliding window size */
59     uint64_t bit = seq - w->top;
60     w->top = seq;
61     if (bit >= REC_SLID_WINDOW_SIZE) {
62         /* If the number exceeds the current number too much, all previous flags are cleared and the maximum value is
63          * updated */
64         w->window = 1;
65     } else {
66         w->window <<= bit;
67         w->window |= 1;
68     }
69     return;
70 }
71 #endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */