OLD | NEW |
1 /* | 1 /* |
2 * key.c | 2 * key.c |
3 * | 3 * |
4 * key usage limits enforcement | 4 * key usage limits enforcement |
5 * | 5 * |
6 * David A. Mcgrew | 6 * David A. Mcgrew |
7 * Cisco Systems, Inc. | 7 * Cisco Systems, Inc. |
8 */ | 8 */ |
9 /* | 9 /* |
10 *» | 10 * |
11 * Copyright (c) 2001-2006 Cisco Systems, Inc. | 11 * Copyright (c) 2001-2006 Cisco Systems, Inc. |
12 * All rights reserved. | 12 * All rights reserved. |
13 * | 13 * |
14 * Redistribution and use in source and binary forms, with or without | 14 * Redistribution and use in source and binary forms, with or without |
15 * modification, are permitted provided that the following conditions | 15 * modification, are permitted provided that the following conditions |
16 * are met: | 16 * are met: |
17 * | 17 * |
18 * Redistributions of source code must retain the above copyright | 18 * Redistributions of source code must retain the above copyright |
19 * notice, this list of conditions and the following disclaimer. | 19 * notice, this list of conditions and the following disclaimer. |
20 * | 20 * |
21 * Redistributions in binary form must reproduce the above | 21 * Redistributions in binary form must reproduce the above |
22 * copyright notice, this list of conditions and the following | 22 * copyright notice, this list of conditions and the following |
23 * disclaimer in the documentation and/or other materials provided | 23 * disclaimer in the documentation and/or other materials provided |
24 * with the distribution. | 24 * with the distribution. |
25 * | 25 * |
26 * Neither the name of the Cisco Systems, Inc. nor the names of its | 26 * Neither the name of the Cisco Systems, Inc. nor the names of its |
27 * contributors may be used to endorse or promote products derived | 27 * contributors may be used to endorse or promote products derived |
28 * from this software without specific prior written permission. | 28 * from this software without specific prior written permission. |
29 * | 29 * |
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | 34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
41 * OF THE POSSIBILITY OF SUCH DAMAGE. | 41 * OF THE POSSIBILITY OF SUCH DAMAGE. |
42 * | 42 * |
43 */ | 43 */ |
44 | 44 |
45 #ifdef HAVE_CONFIG_H | 45 #ifdef HAVE_CONFIG_H |
46 #include <config.h> | 46 #include <config.h> |
47 #endif | 47 #endif |
48 | 48 |
49 #include "key.h" | 49 #include "key.h" |
50 | 50 |
51 #define soft_limit 0x10000 | 51 #define soft_limit 0x10000 |
52 | 52 |
53 err_status_t | 53 srtp_err_status_t srtp_key_limit_set (srtp_key_limit_t key, const srtp_xtd_seq_n
um_t s) |
54 key_limit_set(key_limit_t key, const xtd_seq_num_t s) { | 54 { |
55 #ifdef NO_64BIT_MATH | 55 #ifdef NO_64BIT_MATH |
56 if (high32(s) == 0 && low32(s) < soft_limit) | 56 if (high32(s) == 0 && low32(s) < soft_limit) { |
57 return err_status_bad_param; | 57 return srtp_err_status_bad_param; |
| 58 } |
58 #else | 59 #else |
59 if (s < soft_limit) | 60 if (s < soft_limit) { |
60 return err_status_bad_param; | 61 return srtp_err_status_bad_param; |
| 62 } |
61 #endif | 63 #endif |
62 key->num_left = s; | 64 key->num_left = s; |
63 key->state = key_state_normal; | 65 key->state = srtp_key_state_normal; |
64 return err_status_ok; | 66 return srtp_err_status_ok; |
65 } | 67 } |
66 | 68 |
67 err_status_t | 69 srtp_err_status_t srtp_key_limit_clone (srtp_key_limit_t original, srtp_key_limi
t_t *new_key) |
68 key_limit_clone(key_limit_t original, key_limit_t *new_key) { | 70 { |
69 if (original == NULL) | 71 if (original == NULL) { |
70 return err_status_bad_param; | 72 return srtp_err_status_bad_param; |
71 *new_key = original; | 73 } |
72 return err_status_ok; | 74 *new_key = original; |
| 75 return srtp_err_status_ok; |
73 } | 76 } |
74 | 77 |
75 err_status_t | 78 srtp_err_status_t srtp_key_limit_check (const srtp_key_limit_t key) |
76 key_limit_check(const key_limit_t key) { | 79 { |
77 if (key->state == key_state_expired) | 80 if (key->state == srtp_key_state_expired) { |
78 return err_status_key_expired; | 81 return srtp_err_status_key_expired; |
79 return err_status_ok; | 82 } |
| 83 return srtp_err_status_ok; |
80 } | 84 } |
81 | 85 |
82 key_event_t | 86 srtp_key_event_t srtp_key_limit_update (srtp_key_limit_t key) |
83 key_limit_update(key_limit_t key) { | 87 { |
84 #ifdef NO_64BIT_MATH | 88 #ifdef NO_64BIT_MATH |
85 if (low32(key->num_left) == 0) | 89 if (low32(key->num_left) == 0) { |
86 { | 90 // carry |
87 » // carry | 91 key->num_left = make64(high32(key->num_left) - 1, low32(key->num_left) -
1); |
88 » key->num_left = make64(high32(key->num_left)-1,low32(key->num_left) -
1); | 92 }else { |
89 } | 93 // no carry |
90 else | 94 key->num_left = make64(high32(key->num_left), low32(key->num_left) - 1); |
91 { | 95 } |
92 » // no carry | 96 if (high32(key->num_left) != 0 || low32(key->num_left) >= soft_limit) { |
93 » key->num_left = make64(high32(key->num_left),low32(key->num_left) - 1)
; | 97 return srtp_key_event_normal; /* we're above the soft limit */ |
94 } | 98 } |
95 if (high32(key->num_left) != 0 || low32(key->num_left) >= soft_limit) { | |
96 return key_event_normal; /* we're above the soft limit */ | |
97 } | |
98 #else | 99 #else |
99 key->num_left--; | 100 key->num_left--; |
100 if (key->num_left >= soft_limit) { | 101 if (key->num_left >= soft_limit) { |
101 return key_event_normal; /* we're above the soft limit */ | 102 return srtp_key_event_normal; /* we're above the soft limit */ |
102 } | 103 } |
103 #endif | 104 #endif |
104 if (key->state == key_state_normal) { | 105 if (key->state == srtp_key_state_normal) { |
105 /* we just passed the soft limit, so change the state */ | 106 /* we just passed the soft limit, so change the state */ |
106 key->state = key_state_past_soft_limit; | 107 key->state = srtp_key_state_past_soft_limit; |
107 } | 108 } |
108 #ifdef NO_64BIT_MATH | 109 #ifdef NO_64BIT_MATH |
109 if (low32(key->num_left) == 0 && high32(key->num_left == 0)) | 110 if (low32(key->num_left) == 0 && high32(key->num_left == 0)) |
110 #else | 111 #else |
111 if (key->num_left < 1) | 112 if (key->num_left < 1) |
112 #endif | 113 #endif |
113 { /* we just hit the hard limit */ | 114 { /* we just hit the hard limit */ |
114 key->state = key_state_expired; | 115 key->state = srtp_key_state_expired; |
115 return key_event_hard_limit; | 116 return srtp_key_event_hard_limit; |
116 } | 117 } |
117 return key_event_soft_limit; | 118 return srtp_key_event_soft_limit; |
118 } | 119 } |
119 | 120 |
OLD | NEW |