Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/modes/ofb128.c
Line
Count
Source
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
131
{
29
131
    unsigned int n;
30
131
    size_t l = 0;
31
32
131
    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
131
    n = *num;
38
39
131
#if !defined(OPENSSL_SMALL_FOOTPRINT)
40
131
    if (16 % sizeof(size_t) == 0) { /* always true actually */
41
131
        do {
42
131
            while (n && len) {
43
0
                *(out++) = *(in++) ^ ivec[n];
44
0
                --len;
45
0
                n = (n + 1) % 16;
46
0
            }
47
131
#if defined(STRICT_ALIGNMENT)
48
131
            if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0)
49
11
                break;
50
120
#endif
51
1.63k
            while (len >= 16) {
52
1.51k
                (*block)(ivec, ivec, key);
53
4.55k
                for (; n < 16; n += sizeof(size_t))
54
3.03k
                    *(size_t_aX *)(out + n) = *(size_t_aX *)(in + n)
55
3.03k
                        ^ *(size_t_aX *)(ivec + n);
56
1.51k
                len -= 16;
57
1.51k
                out += 16;
58
1.51k
                in += 16;
59
1.51k
                n = 0;
60
1.51k
            }
61
120
            if (len) {
62
96
                (*block)(ivec, ivec, key);
63
192
                while (len--) {
64
96
                    out[n] = in[n] ^ ivec[n];
65
96
                    ++n;
66
96
                }
67
96
            }
68
120
            *num = n;
69
120
            return;
70
131
        } while (0);
71
131
    }
72
    /* the rest would be commonly eliminated by x86* compiler */
73
11
#endif
74
55
    while (l < len) {
75
44
        if (n == 0) {
76
11
            (*block)(ivec, ivec, key);
77
11
        }
78
44
        out[l] = in[l] ^ ivec[n];
79
44
        ++l;
80
44
        n = (n + 1) % 16;
81
44
    }
82
83
11
    *num = n;
84
11
}