summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/desblapi.c
blob: c03ab27cceff32f1e74a634b629076d47fd163e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *  desblapi.c
 *
 *  core source file for DES-150 library
 *  Implement DES Modes of Operation and Triple-DES.
 *  Adapt DES-150 to blapi API.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef FREEBL_NO_DEPEND
#include "stubs.h"
#endif

#include "des.h"
#include "blapii.h"
#include <stddef.h>
#include "secerr.h"

#if defined(NSS_X86_OR_X64)
/* Intel X86 CPUs do unaligned loads and stores without complaint. */
#define COPY8B(to, from, ptr) \
    HALFPTR(to)               \
    [0] = HALFPTR(from)[0];   \
    HALFPTR(to)               \
    [1] = HALFPTR(from)[1];
#else
#define COPY8B(to, from, ptr) memcpy(to, from, 8)
#endif
#define COPY8BTOHALF(to, from) COPY8B(to, from, from)
#define COPY8BFROMHALF(to, from) COPY8B(to, from, to)

static void
DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    while (len) {
        DES_Do1Block(cx->ks0, in, out);
        len -= 8;
        in += 8;
        out += 8;
    }
}

static void
DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    while (len) {
        DES_Do1Block(cx->ks0, in, out);
        len -= 8;
        in += 8;
        DES_Do1Block(cx->ks1, out, out);
        DES_Do1Block(cx->ks2, out, out);
        out += 8;
    }
}

static void NO_SANITIZE_ALIGNMENT
DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    const BYTE *bufend = in + len;
    HALF vec[2];

    while (in != bufend) {
        COPY8BTOHALF(vec, in);
        in += 8;
        vec[0] ^= cx->iv[0];
        vec[1] ^= cx->iv[1];
        DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
        COPY8BFROMHALF(out, cx->iv);
        out += 8;
    }
}

static void NO_SANITIZE_ALIGNMENT
DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    const BYTE *bufend;
    HALF oldciphertext[2];
    HALF plaintext[2];

    for (bufend = in + len; in != bufend;) {
        oldciphertext[0] = cx->iv[0];
        oldciphertext[1] = cx->iv[1];
        COPY8BTOHALF(cx->iv, in);
        in += 8;
        DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
        plaintext[0] ^= oldciphertext[0];
        plaintext[1] ^= oldciphertext[1];
        COPY8BFROMHALF(out, plaintext);
        out += 8;
    }
}

static void NO_SANITIZE_ALIGNMENT
DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    const BYTE *bufend = in + len;
    HALF vec[2];

    while (in != bufend) {
        COPY8BTOHALF(vec, in);
        in += 8;
        vec[0] ^= cx->iv[0];
        vec[1] ^= cx->iv[1];
        DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
        DES_Do1Block(cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv);
        DES_Do1Block(cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv);
        COPY8BFROMHALF(out, cx->iv);
        out += 8;
    }
}

static void NO_SANITIZE_ALIGNMENT
DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
    const BYTE *bufend;
    HALF oldciphertext[2];
    HALF plaintext[2];

    for (bufend = in + len; in != bufend;) {
        oldciphertext[0] = cx->iv[0];
        oldciphertext[1] = cx->iv[1];
        COPY8BTOHALF(cx->iv, in);
        in += 8;
        DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
        DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext);
        DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext);
        plaintext[0] ^= oldciphertext[0];
        plaintext[1] ^= oldciphertext[1];
        COPY8BFROMHALF(out, plaintext);
        out += 8;
    }
}

DESContext *
DES_AllocateContext(void)
{
    return PORT_ZNew(DESContext);
}

SECStatus
DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen,
                const unsigned char *iv, int mode, unsigned int encrypt,
                unsigned int unused)
{
    DESDirection opposite;
    if (!cx) {
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
        return SECFailure;
    }
    cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT;
    opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT;
    switch (mode) {
        case NSS_DES: /* DES ECB */
            DES_MakeSchedule(cx->ks0, key, cx->direction);
            cx->worker = &DES_ECB;
            break;

        case NSS_DES_EDE3: /* DES EDE ECB */
            cx->worker = &DES_EDE3_ECB;
            if (encrypt) {
                DES_MakeSchedule(cx->ks0, key, cx->direction);
                DES_MakeSchedule(cx->ks1, key + 8, opposite);
                DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
            } else {
                DES_MakeSchedule(cx->ks2, key, cx->direction);
                DES_MakeSchedule(cx->ks1, key + 8, opposite);
                DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
            }
            break;

        case NSS_DES_CBC: /* DES CBC */
            COPY8BTOHALF(cx->iv, iv);
            cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe;
            DES_MakeSchedule(cx->ks0, key, cx->direction);
            break;

        case NSS_DES_EDE3_CBC: /* DES EDE CBC */
            COPY8BTOHALF(cx->iv, iv);
            if (encrypt) {
                cx->worker = &DES_EDE3CBCEn;
                DES_MakeSchedule(cx->ks0, key, cx->direction);
                DES_MakeSchedule(cx->ks1, key + 8, opposite);
                DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
            } else {
                cx->worker = &DES_EDE3CBCDe;
                DES_MakeSchedule(cx->ks2, key, cx->direction);
                DES_MakeSchedule(cx->ks1, key + 8, opposite);
                DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
            }
            break;

        default:
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
            return SECFailure;
    }
    return SECSuccess;
}

DESContext *
DES_CreateContext(const BYTE *key, const BYTE *iv, int mode, PRBool encrypt)
{
    DESContext *cx = PORT_ZNew(DESContext);
    SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0);

    if (rv != SECSuccess) {
        PORT_ZFree(cx, sizeof *cx);
        cx = NULL;
    }
    return cx;
}

void
DES_DestroyContext(DESContext *cx, PRBool freeit)
{
    if (cx) {
        memset(cx, 0, sizeof *cx);
        if (freeit)
            PORT_Free(cx);
    }
}

SECStatus
DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
            unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
{

    if ((inLen % 8) != 0 || maxOutLen < inLen || !cx ||
        cx->direction != DES_ENCRYPT) {
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
        return SECFailure;
    }

    cx->worker(cx, out, in, inLen);
    if (outLen)
        *outLen = inLen;
    return SECSuccess;
}

SECStatus
DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
            unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
{

    if ((inLen % 8) != 0 || maxOutLen < inLen || !cx ||
        cx->direction != DES_DECRYPT) {
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
        return SECFailure;
    }

    cx->worker(cx, out, in, inLen);
    if (outLen)
        *outLen = inLen;
    return SECSuccess;
}