James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file srtp/replay.c SRTP replay protection |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #include <re_types.h> |
| 7 | #include <re_mbuf.h> |
| 8 | #include <re_list.h> |
| 9 | #include <re_aes.h> |
| 10 | #include <re_srtp.h> |
| 11 | #include "srtp.h" |
| 12 | |
| 13 | |
| 14 | enum { |
| 15 | SRTP_WINDOW_SIZE = 64 |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | void srtp_replay_init(struct replay *replay) |
| 20 | { |
| 21 | if (!replay) |
| 22 | return; |
| 23 | |
| 24 | replay->bitmap = 0; |
| 25 | replay->lix = 0; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /* |
| 30 | * Returns false if packet disallowed, true if packet permitted |
| 31 | */ |
| 32 | bool srtp_replay_check(struct replay *replay, uint64_t ix) |
| 33 | { |
| 34 | uint64_t diff; |
| 35 | |
| 36 | if (!replay) |
| 37 | return false; |
| 38 | |
| 39 | if (ix > replay->lix) { |
| 40 | diff = ix - replay->lix; |
| 41 | |
| 42 | if (diff < SRTP_WINDOW_SIZE) { /* In window */ |
| 43 | replay->bitmap <<= diff; |
| 44 | replay->bitmap |= 1; /* set bit for this packet */ |
| 45 | } |
| 46 | else |
| 47 | replay->bitmap = 1; |
| 48 | |
| 49 | replay->lix = ix; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | diff = replay->lix - ix; |
| 54 | if (diff >= SRTP_WINDOW_SIZE) |
| 55 | return false; |
| 56 | |
| 57 | if (replay->bitmap & (1ULL << diff)) |
| 58 | return false; /* already seen */ |
| 59 | |
| 60 | /* mark as seen */ |
| 61 | replay->bitmap |= (1ULL << diff); |
| 62 | |
| 63 | return true; |
| 64 | } |