diff options
author | jsing <> | 2014-04-15 14:38:10 +0000 |
---|---|---|
committer | jsing <> | 2014-04-15 14:38:10 +0000 |
commit | fd934c882a62cc862a41725de129e7a623ce16a3 (patch) | |
tree | 0afadc32450a98b257d0902261d2c1f7ba6b56be /src/lib/libcrypto/aes/aes_wrap.c | |
parent | 69a8d2dcc19aed0d1116ba2ecd81cce5ae736c94 (diff) | |
download | openbsd-fd934c882a62cc862a41725de129e7a623ce16a3.tar.gz openbsd-fd934c882a62cc862a41725de129e7a623ce16a3.tar.bz2 openbsd-fd934c882a62cc862a41725de129e7a623ce16a3.zip |
First pass at applying KNF to the OpenSSL code, which almost makes it
readable. This pass is whitespace only and can readily be verified using
tr and md5.
Diffstat (limited to 'src/lib/libcrypto/aes/aes_wrap.c')
-rw-r--r-- | src/lib/libcrypto/aes/aes_wrap.c | 186 |
1 files changed, 88 insertions, 98 deletions
diff --git a/src/lib/libcrypto/aes/aes_wrap.c b/src/lib/libcrypto/aes/aes_wrap.c index e2d73d37ce..198b0be333 100644 --- a/src/lib/libcrypto/aes/aes_wrap.c +++ b/src/lib/libcrypto/aes/aes_wrap.c | |||
@@ -56,13 +56,13 @@ | |||
56 | #include <openssl/bio.h> | 56 | #include <openssl/bio.h> |
57 | 57 | ||
58 | static const unsigned char default_iv[] = { | 58 | static const unsigned char default_iv[] = { |
59 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, | 59 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, |
60 | }; | 60 | }; |
61 | 61 | ||
62 | int AES_wrap_key(AES_KEY *key, const unsigned char *iv, | 62 | int |
63 | unsigned char *out, | 63 | AES_wrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, |
64 | const unsigned char *in, unsigned int inlen) | 64 | const unsigned char *in, unsigned int inlen) |
65 | { | 65 | { |
66 | unsigned char *A, B[16], *R; | 66 | unsigned char *A, B[16], *R; |
67 | unsigned int i, j, t; | 67 | unsigned int i, j, t; |
68 | if ((inlen & 0x7) || (inlen < 8)) | 68 | if ((inlen & 0x7) || (inlen < 8)) |
@@ -75,31 +75,28 @@ int AES_wrap_key(AES_KEY *key, const unsigned char *iv, | |||
75 | 75 | ||
76 | memcpy(A, iv, 8); | 76 | memcpy(A, iv, 8); |
77 | 77 | ||
78 | for (j = 0; j < 6; j++) | 78 | for (j = 0; j < 6; j++) { |
79 | { | ||
80 | R = out + 8; | 79 | R = out + 8; |
81 | for (i = 0; i < inlen; i += 8, t++, R += 8) | 80 | for (i = 0; i < inlen; i += 8, t++, R += 8) { |
82 | { | ||
83 | memcpy(B + 8, R, 8); | 81 | memcpy(B + 8, R, 8); |
84 | AES_encrypt(B, B, key); | 82 | AES_encrypt(B, B, key); |
85 | A[7] ^= (unsigned char)(t & 0xff); | 83 | A[7] ^= (unsigned char)(t & 0xff); |
86 | if (t > 0xff) | 84 | if (t > 0xff) { |
87 | { | ||
88 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | 85 | A[6] ^= (unsigned char)((t >> 8) & 0xff); |
89 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | 86 | A[5] ^= (unsigned char)((t >> 16) & 0xff); |
90 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | 87 | A[4] ^= (unsigned char)((t >> 24) & 0xff); |
91 | } | ||
92 | memcpy(R, B + 8, 8); | ||
93 | } | 88 | } |
89 | memcpy(R, B + 8, 8); | ||
94 | } | 90 | } |
91 | } | ||
95 | memcpy(out, A, 8); | 92 | memcpy(out, A, 8); |
96 | return inlen + 8; | 93 | return inlen + 8; |
97 | } | 94 | } |
98 | 95 | ||
99 | int AES_unwrap_key(AES_KEY *key, const unsigned char *iv, | 96 | int |
100 | unsigned char *out, | 97 | AES_unwrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, |
101 | const unsigned char *in, unsigned int inlen) | 98 | const unsigned char *in, unsigned int inlen) |
102 | { | 99 | { |
103 | unsigned char *A, B[16], *R; | 100 | unsigned char *A, B[16], *R; |
104 | unsigned int i, j, t; | 101 | unsigned int i, j, t; |
105 | inlen -= 8; | 102 | inlen -= 8; |
@@ -108,43 +105,39 @@ int AES_unwrap_key(AES_KEY *key, const unsigned char *iv, | |||
108 | if (inlen < 8) | 105 | if (inlen < 8) |
109 | return -1; | 106 | return -1; |
110 | A = B; | 107 | A = B; |
111 | t = 6 * (inlen >> 3); | 108 | t = 6 * (inlen >> 3); |
112 | memcpy(A, in, 8); | 109 | memcpy(A, in, 8); |
113 | memcpy(out, in + 8, inlen); | 110 | memcpy(out, in + 8, inlen); |
114 | for (j = 0; j < 6; j++) | 111 | for (j = 0; j < 6; j++) { |
115 | { | ||
116 | R = out + inlen - 8; | 112 | R = out + inlen - 8; |
117 | for (i = 0; i < inlen; i += 8, t--, R -= 8) | 113 | for (i = 0; i < inlen; i += 8, t--, R -= 8) { |
118 | { | ||
119 | A[7] ^= (unsigned char)(t & 0xff); | 114 | A[7] ^= (unsigned char)(t & 0xff); |
120 | if (t > 0xff) | 115 | if (t > 0xff) { |
121 | { | ||
122 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | 116 | A[6] ^= (unsigned char)((t >> 8) & 0xff); |
123 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | 117 | A[5] ^= (unsigned char)((t >> 16) & 0xff); |
124 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | 118 | A[4] ^= (unsigned char)((t >> 24) & 0xff); |
125 | } | 119 | } |
126 | memcpy(B + 8, R, 8); | 120 | memcpy(B + 8, R, 8); |
127 | AES_decrypt(B, B, key); | 121 | AES_decrypt(B, B, key); |
128 | memcpy(R, B + 8, 8); | 122 | memcpy(R, B + 8, 8); |
129 | } | ||
130 | } | 123 | } |
124 | } | ||
131 | if (!iv) | 125 | if (!iv) |
132 | iv = default_iv; | 126 | iv = default_iv; |
133 | if (memcmp(A, iv, 8)) | 127 | if (memcmp(A, iv, 8)) { |
134 | { | ||
135 | OPENSSL_cleanse(out, inlen); | 128 | OPENSSL_cleanse(out, inlen); |
136 | return 0; | 129 | return 0; |
137 | } | ||
138 | return inlen; | ||
139 | } | 130 | } |
131 | return inlen; | ||
132 | } | ||
140 | 133 | ||
141 | #ifdef AES_WRAP_TEST | 134 | #ifdef AES_WRAP_TEST |
142 | 135 | ||
143 | int AES_wrap_unwrap_test(const unsigned char *kek, int keybits, | 136 | int |
144 | const unsigned char *iv, | 137 | AES_wrap_unwrap_test(const unsigned char *kek, int keybits, |
145 | const unsigned char *eout, | 138 | const unsigned char *iv, const unsigned char *eout, |
146 | const unsigned char *key, int keylen) | 139 | const unsigned char *key, int keylen) |
147 | { | 140 | { |
148 | unsigned char *otmp = NULL, *ptmp = NULL; | 141 | unsigned char *otmp = NULL, *ptmp = NULL; |
149 | int r, ret = 0; | 142 | int r, ret = 0; |
150 | AES_KEY wctx; | 143 | AES_KEY wctx; |
@@ -160,7 +153,7 @@ int AES_wrap_unwrap_test(const unsigned char *kek, int keybits, | |||
160 | 153 | ||
161 | if (eout && memcmp(eout, otmp, keylen)) | 154 | if (eout && memcmp(eout, otmp, keylen)) |
162 | goto err; | 155 | goto err; |
163 | 156 | ||
164 | if (AES_set_decrypt_key(kek, keybits, &wctx)) | 157 | if (AES_set_decrypt_key(kek, keybits, &wctx)) |
165 | goto err; | 158 | goto err; |
166 | r = AES_unwrap_key(&wctx, iv, ptmp, otmp, r); | 159 | r = AES_unwrap_key(&wctx, iv, ptmp, otmp, r); |
@@ -170,74 +163,72 @@ int AES_wrap_unwrap_test(const unsigned char *kek, int keybits, | |||
170 | 163 | ||
171 | ret = 1; | 164 | ret = 1; |
172 | 165 | ||
173 | err: | 166 | err: |
174 | if (otmp) | 167 | if (otmp) |
175 | OPENSSL_free(otmp); | 168 | OPENSSL_free(otmp); |
176 | if (ptmp) | 169 | if (ptmp) |
177 | OPENSSL_free(ptmp); | 170 | OPENSSL_free(ptmp); |
178 | 171 | ||
179 | return ret; | 172 | return ret; |
180 | 173 | } | |
181 | } | ||
182 | |||
183 | 174 | ||
184 | 175 | ||
185 | int main(int argc, char **argv) | 176 | int |
177 | main(int argc, char **argv) | ||
186 | { | 178 | { |
187 | 179 | static const unsigned char kek[] = { | |
188 | static const unsigned char kek[] = { | 180 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
189 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 181 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
190 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | 182 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
191 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | 183 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
192 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f | 184 | }; |
193 | }; | 185 | |
194 | 186 | static const unsigned char key[] = { | |
195 | static const unsigned char key[] = { | 187 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
196 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, | 188 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
197 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, | 189 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
198 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 190 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
199 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | 191 | }; |
200 | }; | 192 | |
201 | 193 | static const unsigned char e1[] = { | |
202 | static const unsigned char e1[] = { | 194 | 0x1f, 0xa6, 0x8b, 0x0a, 0x81, 0x12, 0xb4, 0x47, |
203 | 0x1f, 0xa6, 0x8b, 0x0a, 0x81, 0x12, 0xb4, 0x47, | 195 | 0xae, 0xf3, 0x4b, 0xd8, 0xfb, 0x5a, 0x7b, 0x82, |
204 | 0xae, 0xf3, 0x4b, 0xd8, 0xfb, 0x5a, 0x7b, 0x82, | 196 | 0x9d, 0x3e, 0x86, 0x23, 0x71, 0xd2, 0xcf, 0xe5 |
205 | 0x9d, 0x3e, 0x86, 0x23, 0x71, 0xd2, 0xcf, 0xe5 | 197 | }; |
206 | }; | 198 | |
207 | 199 | static const unsigned char e2[] = { | |
208 | static const unsigned char e2[] = { | 200 | 0x96, 0x77, 0x8b, 0x25, 0xae, 0x6c, 0xa4, 0x35, |
209 | 0x96, 0x77, 0x8b, 0x25, 0xae, 0x6c, 0xa4, 0x35, | 201 | 0xf9, 0x2b, 0x5b, 0x97, 0xc0, 0x50, 0xae, 0xd2, |
210 | 0xf9, 0x2b, 0x5b, 0x97, 0xc0, 0x50, 0xae, 0xd2, | 202 | 0x46, 0x8a, 0xb8, 0xa1, 0x7a, 0xd8, 0x4e, 0x5d |
211 | 0x46, 0x8a, 0xb8, 0xa1, 0x7a, 0xd8, 0x4e, 0x5d | 203 | }; |
212 | }; | 204 | |
213 | 205 | static const unsigned char e3[] = { | |
214 | static const unsigned char e3[] = { | 206 | 0x64, 0xe8, 0xc3, 0xf9, 0xce, 0x0f, 0x5b, 0xa2, |
215 | 0x64, 0xe8, 0xc3, 0xf9, 0xce, 0x0f, 0x5b, 0xa2, | 207 | 0x63, 0xe9, 0x77, 0x79, 0x05, 0x81, 0x8a, 0x2a, |
216 | 0x63, 0xe9, 0x77, 0x79, 0x05, 0x81, 0x8a, 0x2a, | 208 | 0x93, 0xc8, 0x19, 0x1e, 0x7d, 0x6e, 0x8a, 0xe7 |
217 | 0x93, 0xc8, 0x19, 0x1e, 0x7d, 0x6e, 0x8a, 0xe7 | 209 | }; |
218 | }; | 210 | |
219 | 211 | static const unsigned char e4[] = { | |
220 | static const unsigned char e4[] = { | 212 | 0x03, 0x1d, 0x33, 0x26, 0x4e, 0x15, 0xd3, 0x32, |
221 | 0x03, 0x1d, 0x33, 0x26, 0x4e, 0x15, 0xd3, 0x32, | 213 | 0x68, 0xf2, 0x4e, 0xc2, 0x60, 0x74, 0x3e, 0xdc, |
222 | 0x68, 0xf2, 0x4e, 0xc2, 0x60, 0x74, 0x3e, 0xdc, | 214 | 0xe1, 0xc6, 0xc7, 0xdd, 0xee, 0x72, 0x5a, 0x93, |
223 | 0xe1, 0xc6, 0xc7, 0xdd, 0xee, 0x72, 0x5a, 0x93, | 215 | 0x6b, 0xa8, 0x14, 0x91, 0x5c, 0x67, 0x62, 0xd2 |
224 | 0x6b, 0xa8, 0x14, 0x91, 0x5c, 0x67, 0x62, 0xd2 | 216 | }; |
225 | }; | 217 | |
226 | 218 | static const unsigned char e5[] = { | |
227 | static const unsigned char e5[] = { | 219 | 0xa8, 0xf9, 0xbc, 0x16, 0x12, 0xc6, 0x8b, 0x3f, |
228 | 0xa8, 0xf9, 0xbc, 0x16, 0x12, 0xc6, 0x8b, 0x3f, | 220 | 0xf6, 0xe6, 0xf4, 0xfb, 0xe3, 0x0e, 0x71, 0xe4, |
229 | 0xf6, 0xe6, 0xf4, 0xfb, 0xe3, 0x0e, 0x71, 0xe4, | 221 | 0x76, 0x9c, 0x8b, 0x80, 0xa3, 0x2c, 0xb8, 0x95, |
230 | 0x76, 0x9c, 0x8b, 0x80, 0xa3, 0x2c, 0xb8, 0x95, | 222 | 0x8c, 0xd5, 0xd1, 0x7d, 0x6b, 0x25, 0x4d, 0xa1 |
231 | 0x8c, 0xd5, 0xd1, 0x7d, 0x6b, 0x25, 0x4d, 0xa1 | 223 | }; |
232 | }; | 224 | |
233 | 225 | static const unsigned char e6[] = { | |
234 | static const unsigned char e6[] = { | 226 | 0x28, 0xc9, 0xf4, 0x04, 0xc4, 0xb8, 0x10, 0xf4, |
235 | 0x28, 0xc9, 0xf4, 0x04, 0xc4, 0xb8, 0x10, 0xf4, | 227 | 0xcb, 0xcc, 0xb3, 0x5c, 0xfb, 0x87, 0xf8, 0x26, |
236 | 0xcb, 0xcc, 0xb3, 0x5c, 0xfb, 0x87, 0xf8, 0x26, | 228 | 0x3f, 0x57, 0x86, 0xe2, 0xd8, 0x0e, 0xd3, 0x26, |
237 | 0x3f, 0x57, 0x86, 0xe2, 0xd8, 0x0e, 0xd3, 0x26, | 229 | 0xcb, 0xc7, 0xf0, 0xe7, 0x1a, 0x99, 0xf4, 0x3b, |
238 | 0xcb, 0xc7, 0xf0, 0xe7, 0x1a, 0x99, 0xf4, 0x3b, | 230 | 0xfb, 0x98, 0x8b, 0x9b, 0x7a, 0x02, 0xdd, 0x21 |
239 | 0xfb, 0x98, 0x8b, 0x9b, 0x7a, 0x02, 0xdd, 0x21 | 231 | }; |
240 | }; | ||
241 | 232 | ||
242 | AES_KEY wctx, xctx; | 233 | AES_KEY wctx, xctx; |
243 | int ret; | 234 | int ret; |
@@ -254,6 +245,5 @@ static const unsigned char e6[] = { | |||
254 | ret = AES_wrap_unwrap_test(kek, 256, NULL, e6, key, 32); | 245 | ret = AES_wrap_unwrap_test(kek, 256, NULL, e6, key, 32); |
255 | fprintf(stderr, "Key test result %d\n", ret); | 246 | fprintf(stderr, "Key test result %d\n", ret); |
256 | } | 247 | } |
257 | 248 | ||
258 | |||
259 | #endif | 249 | #endif |