Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_des_hw.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
/*
11
 * DES low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include "prov/ciphercommon.h"
17
#include "cipher_des.h"
18
19
static int cipher_hw_des_initkey(PROV_CIPHER_CTX *ctx,
20
    const unsigned char *key, size_t keylen)
21
0
{
22
0
    PROV_DES_CTX *dctx = (PROV_DES_CTX *)ctx;
23
0
    DES_cblock *deskey = (DES_cblock *)key;
24
0
    DES_key_schedule *ks = &dctx->dks.ks;
25
26
0
    dctx->dstream.cbc = NULL;
27
#if defined(SPARC_DES_CAPABLE)
28
    if (SPARC_DES_CAPABLE) {
29
        if (ctx->mode == EVP_CIPH_CBC_MODE) {
30
            des_t4_key_expand(&deskey[0], ks);
31
            dctx->dstream.cbc = ctx->enc ? des_t4_cbc_encrypt : des_t4_cbc_decrypt;
32
            return 1;
33
        }
34
    }
35
#endif
36
0
    DES_set_key_unchecked(deskey, ks);
37
0
    return 1;
38
0
}
39
40
static void cipher_hw_des_copyctx(PROV_CIPHER_CTX *dst,
41
    const PROV_CIPHER_CTX *src)
42
0
{
43
0
    PROV_DES_CTX *sctx = (PROV_DES_CTX *)src;
44
0
    PROV_DES_CTX *dctx = (PROV_DES_CTX *)dst;
45
46
0
    *dctx = *sctx;
47
0
    dst->ks = &dctx->dks.ks;
48
0
}
49
50
static int cipher_hw_des_ecb_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
51
    const unsigned char *in, size_t len)
52
0
{
53
0
    size_t i, bl = ctx->blocksize;
54
0
    DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
55
56
0
    if (len < bl)
57
0
        return 1;
58
0
    for (i = 0, len -= bl; i <= len; i += bl)
59
0
        DES_ecb_encrypt((const_DES_cblock *)(in + i),
60
0
            (const_DES_cblock *)(out + i), key, ctx->enc);
61
0
    return 1;
62
0
}
63
64
static int cipher_hw_des_cbc_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
65
    const unsigned char *in, size_t len)
66
0
{
67
0
    PROV_DES_CTX *dctx = (PROV_DES_CTX *)ctx;
68
0
    DES_key_schedule *key = &(dctx->dks.ks);
69
70
0
    if (dctx->dstream.cbc != NULL) {
71
0
        (*dctx->dstream.cbc)(in, out, len, key, ctx->iv);
72
0
        return 1;
73
0
    }
74
75
0
    while (len >= MAXCHUNK) {
76
0
        DES_ncbc_encrypt(in, out, MAXCHUNK, key, (DES_cblock *)ctx->iv,
77
0
            ctx->enc);
78
0
        len -= MAXCHUNK;
79
0
        in += MAXCHUNK;
80
0
        out += MAXCHUNK;
81
0
    }
82
0
    if (len > 0)
83
0
        DES_ncbc_encrypt(in, out, (long)len, key, (DES_cblock *)ctx->iv,
84
0
            ctx->enc);
85
0
    return 1;
86
0
}
87
88
static int cipher_hw_des_ofb64_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
89
    const unsigned char *in, size_t len)
90
0
{
91
0
    int num = ctx->num;
92
0
    DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
93
94
0
    while (len >= MAXCHUNK) {
95
0
        DES_ofb64_encrypt(in, out, MAXCHUNK, key, (DES_cblock *)ctx->iv, &num);
96
0
        len -= MAXCHUNK;
97
0
        in += MAXCHUNK;
98
0
        out += MAXCHUNK;
99
0
    }
100
0
    if (len > 0) {
101
0
        DES_ofb64_encrypt(in, out, (long)len, key, (DES_cblock *)ctx->iv, &num);
102
0
    }
103
0
    ctx->num = num;
104
0
    return 1;
105
0
}
106
107
static int cipher_hw_des_cfb64_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
108
    const unsigned char *in, size_t len)
109
0
{
110
0
    size_t chunk = MAXCHUNK;
111
0
    DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
112
0
    int num = ctx->num;
113
114
0
    if (len < chunk)
115
0
        chunk = len;
116
0
    while (len > 0 && len >= chunk) {
117
0
        DES_cfb64_encrypt(in, out, (long)chunk, key, (DES_cblock *)ctx->iv,
118
0
            &num, ctx->enc);
119
0
        len -= chunk;
120
0
        in += chunk;
121
0
        out += chunk;
122
0
        if (len < chunk)
123
0
            chunk = len;
124
0
    }
125
0
    ctx->num = num;
126
0
    return 1;
127
0
}
128
129
/*
130
 * Although we have a CFB-r implementation for DES, it doesn't pack the right
131
 * way, so wrap it here
132
 */
133
static int cipher_hw_des_cfb1_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
134
    const unsigned char *in, size_t inl)
135
0
{
136
0
    size_t n, chunk = MAXCHUNK / 8;
137
0
    DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
138
0
    unsigned char c[1];
139
0
    unsigned char d[1] = { 0 };
140
141
0
    if (inl < chunk)
142
0
        chunk = inl;
143
144
0
    while (inl && inl >= chunk) {
145
0
        for (n = 0; n < chunk * 8; ++n) {
146
0
            c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
147
0
            DES_cfb_encrypt(c, d, 1, 1, key, (DES_cblock *)ctx->iv, ctx->enc);
148
0
            out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) | ((d[0] & 0x80) >> (unsigned int)(n % 8));
149
0
        }
150
0
        inl -= chunk;
151
0
        in += chunk;
152
0
        out += chunk;
153
0
        if (inl < chunk)
154
0
            chunk = inl;
155
0
    }
156
157
0
    return 1;
158
0
}
159
160
static int cipher_hw_des_cfb8_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
161
    const unsigned char *in, size_t inl)
162
0
{
163
0
    DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
164
165
0
    while (inl >= MAXCHUNK) {
166
0
        DES_cfb_encrypt(in, out, 8, (long)MAXCHUNK, key,
167
0
            (DES_cblock *)ctx->iv, ctx->enc);
168
0
        inl -= MAXCHUNK;
169
0
        in += MAXCHUNK;
170
0
        out += MAXCHUNK;
171
0
    }
172
0
    if (inl > 0)
173
0
        DES_cfb_encrypt(in, out, 8, (long)inl, key,
174
0
            (DES_cblock *)ctx->iv, ctx->enc);
175
0
    return 1;
176
0
}
177
178
#define PROV_CIPHER_HW_des_mode(mode)                          \
179
    static const PROV_CIPHER_HW des_##mode = {                 \
180
        cipher_hw_des_initkey,                                 \
181
        cipher_hw_des_##mode##_cipher,                         \
182
        cipher_hw_des_copyctx                                  \
183
    };                                                         \
184
    const PROV_CIPHER_HW *ossl_prov_cipher_hw_des_##mode(void) \
185
0
    {                                                          \
186
0
        return &des_##mode;                                    \
187
0
    }
Unexecuted instantiation: ossl_prov_cipher_hw_des_ecb
Unexecuted instantiation: ossl_prov_cipher_hw_des_cbc
Unexecuted instantiation: ossl_prov_cipher_hw_des_ofb64
Unexecuted instantiation: ossl_prov_cipher_hw_des_cfb64
Unexecuted instantiation: ossl_prov_cipher_hw_des_cfb1
Unexecuted instantiation: ossl_prov_cipher_hw_des_cfb8
188
189
PROV_CIPHER_HW_des_mode(ecb)
190
    PROV_CIPHER_HW_des_mode(cbc)
191
        PROV_CIPHER_HW_des_mode(ofb64)
192
            PROV_CIPHER_HW_des_mode(cfb64)
193
                PROV_CIPHER_HW_des_mode(cfb1)
194
                    PROV_CIPHER_HW_des_mode(cfb8)