/src/openssl/crypto/modes/ofb128.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include "crypto/modes.h" |
13 | | |
14 | | #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) |
15 | | typedef size_t size_t_aX __attribute((__aligned__(1))); |
16 | | #else |
17 | | typedef size_t size_t_aX; |
18 | | #endif |
19 | | |
20 | | /* |
21 | | * The input and output encrypted as though 128bit ofb mode is being used. |
22 | | * The extra state information to record how much of the 128bit block we have |
23 | | * used is contained in *num; |
24 | | */ |
25 | | void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out, |
26 | | size_t len, const void *key, |
27 | | unsigned char ivec[16], int *num, block128_f block) |
28 | 2.48k | { |
29 | 2.48k | unsigned int n; |
30 | 2.48k | size_t l = 0; |
31 | | |
32 | 2.48k | if (*num < 0) { |
33 | | /* There is no good way to signal an error return from here */ |
34 | 0 | *num = -1; |
35 | 0 | return; |
36 | 0 | } |
37 | 2.48k | n = *num; |
38 | | |
39 | 2.48k | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
40 | 2.48k | if (16 % sizeof(size_t) == 0) { /* always true actually */ |
41 | 2.48k | do { |
42 | 6.42k | while (n && len) { |
43 | 3.93k | *(out++) = *(in++) ^ ivec[n]; |
44 | 3.93k | --len; |
45 | 3.93k | n = (n + 1) % 16; |
46 | 3.93k | } |
47 | | # if defined(STRICT_ALIGNMENT) |
48 | | if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != |
49 | | 0) |
50 | | break; |
51 | | # endif |
52 | 7.52k | while (len >= 16) { |
53 | 5.03k | (*block) (ivec, ivec, key); |
54 | 15.1k | for (; n < 16; n += sizeof(size_t)) |
55 | 10.0k | *(size_t_aX *)(out + n) = |
56 | 10.0k | *(size_t_aX *)(in + n) |
57 | 10.0k | ^ *(size_t_aX *)(ivec + n); |
58 | 5.03k | len -= 16; |
59 | 5.03k | out += 16; |
60 | 5.03k | in += 16; |
61 | 5.03k | n = 0; |
62 | 5.03k | } |
63 | 2.48k | if (len) { |
64 | 800 | (*block) (ivec, ivec, key); |
65 | 2.89k | while (len--) { |
66 | 2.09k | out[n] = in[n] ^ ivec[n]; |
67 | 2.09k | ++n; |
68 | 2.09k | } |
69 | 800 | } |
70 | 2.48k | *num = n; |
71 | 2.48k | return; |
72 | 2.48k | } while (0); |
73 | 2.48k | } |
74 | | /* the rest would be commonly eliminated by x86* compiler */ |
75 | 0 | #endif |
76 | 0 | while (l < len) { |
77 | 0 | if (n == 0) { |
78 | 0 | (*block) (ivec, ivec, key); |
79 | 0 | } |
80 | 0 | out[l] = in[l] ^ ivec[n]; |
81 | 0 | ++l; |
82 | 0 | n = (n + 1) % 16; |
83 | 0 | } |
84 | |
|
85 | 0 | *num = n; |
86 | 0 | } |