]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/modes/cbc128.c
2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include "crypto/modes.h"
14 #if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC)
15 # define STRICT_ALIGNMENT 0
18 void CRYPTO_cbc128_encrypt(const unsigned char *in
, unsigned char *out
,
19 size_t len
, const void *key
,
20 unsigned char ivec
[16], block128_f block
)
23 const unsigned char *iv
= ivec
;
28 #if !defined(OPENSSL_SMALL_FOOTPRINT)
29 if (STRICT_ALIGNMENT
&&
30 ((size_t)in
| (size_t)out
| (size_t)ivec
) % sizeof(size_t) != 0) {
32 for (n
= 0; n
< 16; ++n
)
33 out
[n
] = in
[n
] ^ iv
[n
];
34 (*block
) (out
, out
, key
);
42 for (n
= 0; n
< 16; n
+= sizeof(size_t))
43 *(size_t *)(out
+ n
) =
44 *(size_t *)(in
+ n
) ^ *(size_t *)(iv
+ n
);
45 (*block
) (out
, out
, key
);
54 for (n
= 0; n
< 16 && n
< len
; ++n
)
55 out
[n
] = in
[n
] ^ iv
[n
];
58 (*block
) (out
, out
, key
);
69 void CRYPTO_cbc128_decrypt(const unsigned char *in
, unsigned char *out
,
70 size_t len
, const void *key
,
71 unsigned char ivec
[16], block128_f block
)
75 size_t t
[16 / sizeof(size_t)];
82 #if !defined(OPENSSL_SMALL_FOOTPRINT)
84 const unsigned char *iv
= ivec
;
86 if (STRICT_ALIGNMENT
&&
87 ((size_t)in
| (size_t)out
| (size_t)ivec
) % sizeof(size_t) != 0) {
89 (*block
) (in
, out
, key
);
90 for (n
= 0; n
< 16; ++n
)
97 } else if (16 % sizeof(size_t) == 0) { /* always true */
99 size_t *out_t
= (size_t *)out
, *iv_t
= (size_t *)iv
;
101 (*block
) (in
, out
, key
);
102 for (n
= 0; n
< 16 / sizeof(size_t); n
++)
110 memcpy(ivec
, iv
, 16);
112 if (STRICT_ALIGNMENT
&&
113 ((size_t)in
| (size_t)out
| (size_t)ivec
) % sizeof(size_t) != 0) {
116 (*block
) (in
, tmp
.c
, key
);
117 for (n
= 0; n
< 16; ++n
) {
119 out
[n
] = tmp
.c
[n
] ^ ivec
[n
];
126 } else if (16 % sizeof(size_t) == 0) { /* always true */
128 size_t c
, *out_t
= (size_t *)out
, *ivec_t
= (size_t *)ivec
;
129 const size_t *in_t
= (const size_t *)in
;
131 (*block
) (in
, tmp
.c
, key
);
132 for (n
= 0; n
< 16 / sizeof(size_t); n
++) {
134 out_t
[n
] = tmp
.t
[n
] ^ ivec_t
[n
];
146 (*block
) (in
, tmp
.c
, key
);
147 for (n
= 0; n
< 16 && n
< len
; ++n
) {
149 out
[n
] = tmp
.c
[n
] ^ ivec
[n
];