diff options
Diffstat (limited to 'src/lib/libcrypto/modes/cbc128.c')
| -rw-r--r-- | src/lib/libcrypto/modes/cbc128.c | 104 |
1 files changed, 57 insertions, 47 deletions
diff --git a/src/lib/libcrypto/modes/cbc128.c b/src/lib/libcrypto/modes/cbc128.c index f2eebc6e7a..27a2241ad4 100644 --- a/src/lib/libcrypto/modes/cbc128.c +++ b/src/lib/libcrypto/modes/cbc128.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: cbc128.c,v 1.6 2022/11/26 16:08:53 tb Exp $ */ | 1 | /* $OpenBSD: cbc128.c,v 1.7 2023/07/08 14:55:36 beck Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright (c) 2008 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 2008 The OpenSSL Project. All rights reserved. |
| 4 | * | 4 | * |
| @@ -7,7 +7,7 @@ | |||
| 7 | * are met: | 7 | * are met: |
| 8 | * | 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright | 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. | 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * | 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in | 13 | * notice, this list of conditions and the following disclaimer in |
| @@ -66,117 +66,127 @@ | |||
| 66 | #define STRICT_ALIGNMENT 0 | 66 | #define STRICT_ALIGNMENT 0 |
| 67 | #endif | 67 | #endif |
| 68 | 68 | ||
| 69 | void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out, | 69 | void |
| 70 | size_t len, const void *key, | 70 | CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out, |
| 71 | unsigned char ivec[16], block128_f block) | 71 | size_t len, const void *key, |
| 72 | unsigned char ivec[16], block128_f block) | ||
| 72 | { | 73 | { |
| 73 | size_t n; | 74 | size_t n; |
| 74 | const unsigned char *iv = ivec; | 75 | const unsigned char *iv = ivec; |
| 75 | 76 | ||
| 76 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | 77 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
| 77 | if (STRICT_ALIGNMENT && | 78 | if (STRICT_ALIGNMENT && |
| 78 | ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) { | 79 | ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 0) { |
| 79 | while (len>=16) { | 80 | while (len >= 16) { |
| 80 | for(n=0; n<16; ++n) | 81 | for (n = 0; n < 16; ++n) |
| 81 | out[n] = in[n] ^ iv[n]; | 82 | out[n] = in[n] ^ iv[n]; |
| 82 | (*block)(out, out, key); | 83 | (*block)(out, out, key); |
| 83 | iv = out; | 84 | iv = out; |
| 84 | len -= 16; | 85 | len -= 16; |
| 85 | in += 16; | 86 | in += 16; |
| 86 | out += 16; | 87 | out += 16; |
| 87 | } | 88 | } |
| 88 | } else { | 89 | } else { |
| 89 | while (len>=16) { | 90 | while (len >= 16) { |
| 90 | for(n=0; n<16; n+=sizeof(size_t)) | 91 | for (n = 0; n < 16; n += sizeof(size_t)) |
| 91 | *(size_t*)(out+n) = | 92 | *(size_t *)(out + n) = |
| 92 | *(size_t*)(in+n) ^ *(size_t*)(iv+n); | 93 | *(size_t *)(in + n) ^ *(size_t *)(iv + n); |
| 93 | (*block)(out, out, key); | 94 | (*block)(out, out, key); |
| 94 | iv = out; | 95 | iv = out; |
| 95 | len -= 16; | 96 | len -= 16; |
| 96 | in += 16; | 97 | in += 16; |
| 97 | out += 16; | 98 | out += 16; |
| 98 | } | 99 | } |
| 99 | } | 100 | } |
| 100 | #endif | 101 | #endif |
| 101 | while (len) { | 102 | while (len) { |
| 102 | for(n=0; n<16 && n<len; ++n) | 103 | for (n = 0; n < 16 && n < len; ++n) |
| 103 | out[n] = in[n] ^ iv[n]; | 104 | out[n] = in[n] ^ iv[n]; |
| 104 | for(; n<16; ++n) | 105 | for (; n < 16; ++n) |
| 105 | out[n] = iv[n]; | 106 | out[n] = iv[n]; |
| 106 | (*block)(out, out, key); | 107 | (*block)(out, out, key); |
| 107 | iv = out; | 108 | iv = out; |
| 108 | if (len<=16) break; | 109 | if (len <= 16) |
| 110 | break; | ||
| 109 | len -= 16; | 111 | len -= 16; |
| 110 | in += 16; | 112 | in += 16; |
| 111 | out += 16; | 113 | out += 16; |
| 112 | } | 114 | } |
| 113 | memmove(ivec,iv,16); | 115 | memmove(ivec, iv, 16); |
| 114 | } | 116 | } |
| 115 | 117 | ||
| 116 | void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, | 118 | void |
| 117 | size_t len, const void *key, | 119 | CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, |
| 118 | unsigned char ivec[16], block128_f block) | 120 | size_t len, const void *key, |
| 121 | unsigned char ivec[16], block128_f block) | ||
| 119 | { | 122 | { |
| 120 | size_t n; | 123 | size_t n; |
| 121 | union { size_t t[16/sizeof(size_t)]; unsigned char c[16]; } tmp; | 124 | union { |
| 125 | size_t t[16/sizeof(size_t)]; | ||
| 126 | unsigned char c[16]; | ||
| 127 | } tmp; | ||
| 122 | 128 | ||
| 123 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | 129 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
| 124 | if (in != out) { | 130 | if (in != out) { |
| 125 | const unsigned char *iv = ivec; | 131 | const unsigned char *iv = ivec; |
| 126 | 132 | ||
| 127 | if (STRICT_ALIGNMENT && | 133 | if (STRICT_ALIGNMENT && |
| 128 | ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) { | 134 | ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != |
| 129 | while (len>=16) { | 135 | 0) { |
| 136 | while (len >= 16) { | ||
| 130 | (*block)(in, out, key); | 137 | (*block)(in, out, key); |
| 131 | for(n=0; n<16; ++n) | 138 | for (n = 0; n < 16; ++n) |
| 132 | out[n] ^= iv[n]; | 139 | out[n] ^= iv[n]; |
| 133 | iv = in; | 140 | iv = in; |
| 134 | len -= 16; | 141 | len -= 16; |
| 135 | in += 16; | 142 | in += 16; |
| 136 | out += 16; | 143 | out += 16; |
| 137 | } | 144 | } |
| 138 | } else if (16%sizeof(size_t) == 0) { /* always true */ | 145 | } else if (16 % sizeof(size_t) == 0) { /* always true */ |
| 139 | while (len>=16) { | 146 | while (len >= 16) { |
| 140 | size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv; | 147 | size_t *out_t = (size_t *)out, |
| 148 | *iv_t = (size_t *)iv; | ||
| 141 | 149 | ||
| 142 | (*block)(in, out, key); | 150 | (*block)(in, out, key); |
| 143 | for(n=0; n<16/sizeof(size_t); n++) | 151 | for (n = 0; n < 16/sizeof(size_t); n++) |
| 144 | out_t[n] ^= iv_t[n]; | 152 | out_t[n] ^= iv_t[n]; |
| 145 | iv = in; | 153 | iv = in; |
| 146 | len -= 16; | 154 | len -= 16; |
| 147 | in += 16; | 155 | in += 16; |
| 148 | out += 16; | 156 | out += 16; |
| 149 | } | 157 | } |
| 150 | } | 158 | } |
| 151 | memmove(ivec,iv,16); | 159 | memmove(ivec, iv, 16); |
| 152 | } else { | 160 | } else { |
| 153 | if (STRICT_ALIGNMENT && | 161 | if (STRICT_ALIGNMENT && |
| 154 | ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) { | 162 | ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != |
| 163 | 0) { | ||
| 155 | unsigned char c; | 164 | unsigned char c; |
| 156 | while (len>=16) { | 165 | while (len >= 16) { |
| 157 | (*block)(in, tmp.c, key); | 166 | (*block)(in, tmp.c, key); |
| 158 | for(n=0; n<16; ++n) { | 167 | for (n = 0; n < 16; ++n) { |
| 159 | c = in[n]; | 168 | c = in[n]; |
| 160 | out[n] = tmp.c[n] ^ ivec[n]; | 169 | out[n] = tmp.c[n] ^ ivec[n]; |
| 161 | ivec[n] = c; | 170 | ivec[n] = c; |
| 162 | } | 171 | } |
| 163 | len -= 16; | 172 | len -= 16; |
| 164 | in += 16; | 173 | in += 16; |
| 165 | out += 16; | 174 | out += 16; |
| 166 | } | 175 | } |
| 167 | } else if (16%sizeof(size_t) == 0) { /* always true */ | 176 | } else if (16 % sizeof(size_t) == 0) { /* always true */ |
| 168 | while (len>=16) { | 177 | while (len >= 16) { |
| 169 | size_t c, *out_t=(size_t *)out, *ivec_t=(size_t *)ivec; | 178 | size_t c, *out_t = (size_t *)out, |
| 170 | const size_t *in_t=(const size_t *)in; | 179 | *ivec_t = (size_t *)ivec; |
| 180 | const size_t *in_t = (const size_t *)in; | ||
| 171 | 181 | ||
| 172 | (*block)(in, tmp.c, key); | 182 | (*block)(in, tmp.c, key); |
| 173 | for(n=0; n<16/sizeof(size_t); n++) { | 183 | for (n = 0; n < 16/sizeof(size_t); n++) { |
| 174 | c = in_t[n]; | 184 | c = in_t[n]; |
| 175 | out_t[n] = tmp.t[n] ^ ivec_t[n]; | 185 | out_t[n] = tmp.t[n] ^ ivec_t[n]; |
| 176 | ivec_t[n] = c; | 186 | ivec_t[n] = c; |
| 177 | } | 187 | } |
| 178 | len -= 16; | 188 | len -= 16; |
| 179 | in += 16; | 189 | in += 16; |
| 180 | out += 16; | 190 | out += 16; |
| 181 | } | 191 | } |
| 182 | } | 192 | } |
| @@ -185,18 +195,18 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, | |||
| 185 | while (len) { | 195 | while (len) { |
| 186 | unsigned char c; | 196 | unsigned char c; |
| 187 | (*block)(in, tmp.c, key); | 197 | (*block)(in, tmp.c, key); |
| 188 | for(n=0; n<16 && n<len; ++n) { | 198 | for (n = 0; n < 16 && n < len; ++n) { |
| 189 | c = in[n]; | 199 | c = in[n]; |
| 190 | out[n] = tmp.c[n] ^ ivec[n]; | 200 | out[n] = tmp.c[n] ^ ivec[n]; |
| 191 | ivec[n] = c; | 201 | ivec[n] = c; |
| 192 | } | 202 | } |
| 193 | if (len<=16) { | 203 | if (len <= 16) { |
| 194 | for (; n<16; ++n) | 204 | for (; n < 16; ++n) |
| 195 | ivec[n] = in[n]; | 205 | ivec[n] = in[n]; |
| 196 | break; | 206 | break; |
| 197 | } | 207 | } |
| 198 | len -= 16; | 208 | len -= 16; |
| 199 | in += 16; | 209 | in += 16; |
| 200 | out += 16; | 210 | out += 16; |
| 201 | } | 211 | } |
| 202 | } | 212 | } |
