OLD | NEW |
1 /* | 1 /* |
2 * cipher.h | 2 * cipher.h |
3 * | 3 * |
4 * common interface to ciphers | 4 * common interface to ciphers |
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,2013 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 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 */ | 73 */ |
74 | 74 |
75 typedef struct cipher_type_t *cipher_type_pointer_t; | 75 typedef struct cipher_type_t *cipher_type_pointer_t; |
76 typedef struct cipher_t *cipher_pointer_t; | 76 typedef struct cipher_t *cipher_pointer_t; |
77 | 77 |
78 /* | 78 /* |
79 * a cipher_alloc_func_t allocates (but does not initialize) a cipher_t | 79 * a cipher_alloc_func_t allocates (but does not initialize) a cipher_t |
80 */ | 80 */ |
81 | 81 |
82 typedef err_status_t (*cipher_alloc_func_t) | 82 typedef err_status_t (*cipher_alloc_func_t) |
83 (cipher_pointer_t *cp, int key_len); | 83 (cipher_pointer_t *cp, int key_len, int tag_len); |
84 | 84 |
85 /* | 85 /* |
86 * a cipher_init_func_t [re-]initializes a cipher_t with a given key | 86 * a cipher_init_func_t [re-]initializes a cipher_t with a given key |
87 * and direction (i.e., encrypt or decrypt) | |
88 */ | 87 */ |
89 | 88 |
90 typedef err_status_t (*cipher_init_func_t) | 89 typedef err_status_t (*cipher_init_func_t) |
91 (void *state, const uint8_t *key, int key_len, cipher_direction_t dir); | 90 (void *state, const uint8_t *key, int key_len); |
92 | 91 |
93 /* a cipher_dealloc_func_t de-allocates a cipher_t */ | 92 /* a cipher_dealloc_func_t de-allocates a cipher_t */ |
94 | 93 |
95 typedef err_status_t (*cipher_dealloc_func_t)(cipher_pointer_t cp); | 94 typedef err_status_t (*cipher_dealloc_func_t)(cipher_pointer_t cp); |
96 | 95 |
97 /* a cipher_set_segment_func_t sets the segment index of a cipher_t */ | 96 /* a cipher_set_segment_func_t sets the segment index of a cipher_t */ |
98 | 97 |
99 typedef err_status_t (*cipher_set_segment_func_t) | 98 typedef err_status_t (*cipher_set_segment_func_t) |
100 (void *state, xtd_seq_num_t idx); | 99 (void *state, xtd_seq_num_t idx); |
101 | 100 |
| 101 /* |
| 102 * a cipher_set_aad_func_t processes the AAD data for AEAD ciphers |
| 103 */ |
| 104 typedef err_status_t (*cipher_set_aad_func_t) |
| 105 (void *state, uint8_t *aad, unsigned int aad_len); |
| 106 |
| 107 |
102 /* a cipher_encrypt_func_t encrypts data in-place */ | 108 /* a cipher_encrypt_func_t encrypts data in-place */ |
103 | 109 |
104 typedef err_status_t (*cipher_encrypt_func_t) | 110 typedef err_status_t (*cipher_encrypt_func_t) |
105 (void *state, uint8_t *buffer, unsigned int *octets_to_encrypt); | 111 (void *state, uint8_t *buffer, unsigned int *octets_to_encrypt); |
106 | 112 |
107 /* a cipher_decrypt_func_t decrypts data in-place */ | 113 /* a cipher_decrypt_func_t decrypts data in-place */ |
108 | 114 |
109 typedef err_status_t (*cipher_decrypt_func_t) | 115 typedef err_status_t (*cipher_decrypt_func_t) |
110 (void *state, uint8_t *buffer, unsigned int *octets_to_decrypt); | 116 (void *state, uint8_t *buffer, unsigned int *octets_to_decrypt); |
111 | 117 |
112 /* | 118 /* |
113 * a cipher_set_iv_func_t function sets the current initialization vector | 119 * a cipher_set_iv_func_t function sets the current initialization vector |
114 */ | 120 */ |
115 | 121 |
116 typedef err_status_t (*cipher_set_iv_func_t) | 122 typedef err_status_t (*cipher_set_iv_func_t) |
117 (cipher_pointer_t cp, void *iv); | 123 (cipher_pointer_t cp, void *iv, cipher_direction_t direction); |
| 124 |
| 125 /* |
| 126 * a cipher_get_tag_funct_t function is used to get the authentication |
| 127 * tag that was calculated by an AEAD cipher. |
| 128 */ |
| 129 typedef err_status_t (*cipher_get_tag_func_t) |
| 130 (void *state, void *tag, int *len); |
| 131 |
118 | 132 |
119 /* | 133 /* |
120 * cipher_test_case_t is a (list of) key, salt, xtd_seq_num_t, | 134 * cipher_test_case_t is a (list of) key, salt, xtd_seq_num_t, |
121 * plaintext, and ciphertext values that are known to be correct for a | 135 * plaintext, and ciphertext values that are known to be correct for a |
122 * particular cipher. this data can be used to test an implementation | 136 * particular cipher. this data can be used to test an implementation |
123 * in an on-the-fly self test of the correcness of the implementation. | 137 * in an on-the-fly self test of the correcness of the implementation. |
124 * (see the cipher_type_self_test() function below) | 138 * (see the cipher_type_self_test() function below) |
125 */ | 139 */ |
126 | 140 |
127 typedef struct cipher_test_case_t { | 141 typedef struct cipher_test_case_t { |
128 int key_length_octets; /* octets in key */ | 142 int key_length_octets; /* octets in key */ |
129 uint8_t *key; /* key */ | 143 uint8_t *key; /* key */ |
130 uint8_t *idx; /* packet index */ | 144 uint8_t *idx; /* packet index */ |
131 int plaintext_length_octets; /* octets in plaintext */ | 145 int plaintext_length_octets; /* octets in plaintext */ |
132 uint8_t *plaintext; /* plaintext */ | 146 uint8_t *plaintext; /* plaintext */ |
133 int ciphertext_length_octets; /* octets in plaintext */ | 147 int ciphertext_length_octets; /* octets in plaintext */ |
134 uint8_t *ciphertext; /* ciphertext */ | 148 uint8_t *ciphertext; /* ciphertext */ |
| 149 int aad_length_octets; /* octets in AAD */ |
| 150 uint8_t *aad; /* AAD */ |
| 151 int tag_length_octets; /* Length of AEAD tag */ |
135 struct cipher_test_case_t *next_test_case; /* pointer to next testcase */ | 152 struct cipher_test_case_t *next_test_case; /* pointer to next testcase */ |
136 } cipher_test_case_t; | 153 } cipher_test_case_t; |
137 | 154 |
138 /* cipher_type_t defines the 'metadata' for a particular cipher type */ | 155 /* cipher_type_t defines the 'metadata' for a particular cipher type */ |
139 | 156 |
140 typedef struct cipher_type_t { | 157 typedef struct cipher_type_t { |
141 cipher_alloc_func_t alloc; | 158 cipher_alloc_func_t alloc; |
142 cipher_dealloc_func_t dealloc; | 159 cipher_dealloc_func_t dealloc; |
143 cipher_init_func_t init; | 160 cipher_init_func_t init; |
| 161 cipher_set_aad_func_t set_aad; |
144 cipher_encrypt_func_t encrypt; | 162 cipher_encrypt_func_t encrypt; |
145 cipher_encrypt_func_t decrypt; | 163 cipher_encrypt_func_t decrypt; |
146 cipher_set_iv_func_t set_iv; | 164 cipher_set_iv_func_t set_iv; |
| 165 cipher_get_tag_func_t get_tag; |
147 char *description; | 166 char *description; |
148 int ref_count; | 167 int ref_count; |
149 cipher_test_case_t *test_data; | 168 cipher_test_case_t *test_data; |
150 debug_module_t *debug; | 169 debug_module_t *debug; |
151 cipher_type_id_t id; | 170 cipher_type_id_t id; |
152 } cipher_type_t; | 171 } cipher_type_t; |
153 | 172 |
154 /* | 173 /* |
155 * cipher_t defines an instantiation of a particular cipher, with fixed | 174 * cipher_t defines an instantiation of a particular cipher, with fixed |
156 * key length, key and salt values | 175 * key length, key and salt values |
157 */ | 176 */ |
158 | 177 |
159 typedef struct cipher_t { | 178 typedef struct cipher_t { |
160 cipher_type_t *type; | 179 cipher_type_t *type; |
161 void *state; | 180 void *state; |
162 int key_len; | 181 int key_len; |
163 #ifdef FORCE_64BIT_ALIGN | 182 int» » algorithm; |
164 int pad; | |
165 #endif | |
166 } cipher_t; | 183 } cipher_t; |
167 | 184 |
168 /* some syntactic sugar on these function types */ | 185 /* some syntactic sugar on these function types */ |
169 | 186 |
170 #define cipher_type_alloc(ct, c, klen) ((ct)->alloc((c), (klen))) | 187 #define cipher_type_alloc(ct, c, klen, tlen) ((ct)->alloc((c), (klen), (tlen))) |
171 | 188 |
172 #define cipher_dealloc(c) (((c)->type)->dealloc(c)) | 189 #define cipher_dealloc(c) (((c)->type)->dealloc(c)) |
173 | 190 |
174 #define cipher_init(c, k, dir) (((c)->type)->init(((c)->state), (k), ((c)->key_l
en), (dir))) | 191 #define cipher_init(c, k) (((c)->type)->init(((c)->state), (k), ((c)->key_len))) |
175 | 192 |
176 #define cipher_encrypt(c, buf, len) \ | 193 #define cipher_encrypt(c, buf, len) \ |
177 (((c)->type)->encrypt(((c)->state), (buf), (len))) | 194 (((c)->type)->encrypt(((c)->state), (buf), (len))) |
178 | 195 |
| 196 #define cipher_get_tag(c, buf, len) \ |
| 197 (((c)->type)->get_tag(((c)->state), (buf), (len))) |
| 198 |
179 #define cipher_decrypt(c, buf, len) \ | 199 #define cipher_decrypt(c, buf, len) \ |
180 (((c)->type)->decrypt(((c)->state), (buf), (len))) | 200 (((c)->type)->decrypt(((c)->state), (buf), (len))) |
181 | 201 |
182 #define cipher_set_iv(c, n) \ | 202 #define cipher_set_iv(c, n, dir) \ |
183 ((c) ? (((c)->type)->set_iv(((cipher_pointer_t)(c)->state), (n))) : \ | 203 ((c) ? (((c)->type)->set_iv(((cipher_pointer_t)(c)->state), (n), (dir))) : \ |
| 204 err_status_no_such_op) |
| 205 #define cipher_set_aad(c, a, l) \ |
| 206 (((c) && (((c)->type)->set_aad)) ? \ |
| 207 (((c)->type)->set_aad(((c)->state), (a), (l))) : \ |
184 err_status_no_such_op) | 208 err_status_no_such_op) |
185 | 209 |
186 err_status_t | 210 err_status_t |
187 cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output); | 211 cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output); |
188 | 212 |
189 | 213 |
190 /* some bookkeeping functions */ | 214 /* some bookkeeping functions */ |
191 | 215 |
192 int | 216 int |
193 cipher_get_key_length(const cipher_t *c); | 217 cipher_get_key_length(const cipher_t *c); |
(...skipping 27 matching lines...) Expand all Loading... |
221 * is the length in octets of the test data to be encrypted, and t is | 245 * is the length in octets of the test data to be encrypted, and t is |
222 * the number of trials | 246 * the number of trials |
223 * | 247 * |
224 * if an error is encountered, then the value 0 is returned | 248 * if an error is encountered, then the value 0 is returned |
225 */ | 249 */ |
226 | 250 |
227 uint64_t | 251 uint64_t |
228 cipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials); | 252 cipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials); |
229 | 253 |
230 #endif /* CIPHER_H */ | 254 #endif /* CIPHER_H */ |
OLD | NEW |