summaryrefslogtreecommitdiff
path: root/security/nss/lib/freebl/alg2268.c
blob: 54c6f4dff1b58b84e0b96550c9d9c3c5f34a96e7 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * alg2268.c - implementation of the algorithm in RFC 2268
 *
 * 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 "blapi.h"
#include "blapii.h"
#include "secerr.h"
#ifdef XP_UNIX_XXX
#include <stddef.h> /* for ptrdiff_t */
#endif

/*
** RC2 symmetric block cypher
*/

typedef SECStatus(rc2Func)(RC2Context *cx, unsigned char *output,
                           const unsigned char *input, unsigned int inputLen);

/* forward declarations */
static rc2Func rc2_EncryptECB;
static rc2Func rc2_DecryptECB;
static rc2Func rc2_EncryptCBC;
static rc2Func rc2_DecryptCBC;

typedef union {
    PRUint32 l[2];
    PRUint16 s[4];
    PRUint8 b[8];
} RC2Block;

struct RC2ContextStr {
    union {
        PRUint8 Kb[128];
        PRUint16 Kw[64];
    } u;
    RC2Block iv;
    rc2Func *enc;
    rc2Func *dec;
};

#define B u.Kb
#define K u.Kw
#define BYTESWAP(x) ((x) << 8 | (x) >> 8)
#define SWAPK(i) cx->K[i] = (tmpS = cx->K[i], BYTESWAP(tmpS))
#define RC2_BLOCK_SIZE 8

#define LOAD_HARD(R)                           \
    R[0] = (PRUint16)input[1] << 8 | input[0]; \
    R[1] = (PRUint16)input[3] << 8 | input[2]; \
    R[2] = (PRUint16)input[5] << 8 | input[4]; \
    R[3] = (PRUint16)input[7] << 8 | input[6];
#define LOAD_EASY(R)               \
    R[0] = ((PRUint16 *)input)[0]; \
    R[1] = ((PRUint16 *)input)[1]; \
    R[2] = ((PRUint16 *)input)[2]; \
    R[3] = ((PRUint16 *)input)[3];
#define STORE_HARD(R)                 \
    output[0] = (PRUint8)(R[0]);      \
    output[1] = (PRUint8)(R[0] >> 8); \
    output[2] = (PRUint8)(R[1]);      \
    output[3] = (PRUint8)(R[1] >> 8); \
    output[4] = (PRUint8)(R[2]);      \
    output[5] = (PRUint8)(R[2] >> 8); \
    output[6] = (PRUint8)(R[3]);      \
    output[7] = (PRUint8)(R[3] >> 8);
#define STORE_EASY(R)               \
    ((PRUint16 *)output)[0] = R[0]; \
    ((PRUint16 *)output)[1] = R[1]; \
    ((PRUint16 *)output)[2] = R[2]; \
    ((PRUint16 *)output)[3] = R[3];

#if defined(NSS_X86_OR_X64)
#define LOAD(R) LOAD_EASY(R)
#define STORE(R) STORE_EASY(R)
#elif !defined(IS_LITTLE_ENDIAN)
#define LOAD(R) LOAD_HARD(R)
#define STORE(R) STORE_HARD(R)
#else
#define LOAD(R)                 \
    if ((ptrdiff_t)input & 1) { \
        LOAD_HARD(R)            \
    } else {                    \
        LOAD_EASY(R)            \
    }
#define STORE(R)                \
    if ((ptrdiff_t)input & 1) { \
        STORE_HARD(R)           \
    } else {                    \
        STORE_EASY(R)           \
    }
#endif

static const PRUint8 S[256] = {
    0331, 0170, 0371, 0304, 0031, 0335, 0265, 0355, 0050, 0351, 0375, 0171, 0112, 0240, 0330, 0235,
    0306, 0176, 0067, 0203, 0053, 0166, 0123, 0216, 0142, 0114, 0144, 0210, 0104, 0213, 0373, 0242,
    0027, 0232, 0131, 0365, 0207, 0263, 0117, 0023, 0141, 0105, 0155, 0215, 0011, 0201, 0175, 0062,
    0275, 0217, 0100, 0353, 0206, 0267, 0173, 0013, 0360, 0225, 0041, 0042, 0134, 0153, 0116, 0202,
    0124, 0326, 0145, 0223, 0316, 0140, 0262, 0034, 0163, 0126, 0300, 0024, 0247, 0214, 0361, 0334,
    0022, 0165, 0312, 0037, 0073, 0276, 0344, 0321, 0102, 0075, 0324, 0060, 0243, 0074, 0266, 0046,
    0157, 0277, 0016, 0332, 0106, 0151, 0007, 0127, 0047, 0362, 0035, 0233, 0274, 0224, 0103, 0003,
    0370, 0021, 0307, 0366, 0220, 0357, 0076, 0347, 0006, 0303, 0325, 0057, 0310, 0146, 0036, 0327,
    0010, 0350, 0352, 0336, 0200, 0122, 0356, 0367, 0204, 0252, 0162, 0254, 0065, 0115, 0152, 0052,
    0226, 0032, 0322, 0161, 0132, 0025, 0111, 0164, 0113, 0237, 0320, 0136, 0004, 0030, 0244, 0354,
    0302, 0340, 0101, 0156, 0017, 0121, 0313, 0314, 0044, 0221, 0257, 0120, 0241, 0364, 0160, 0071,
    0231, 0174, 0072, 0205, 0043, 0270, 0264, 0172, 0374, 0002, 0066, 0133, 0045, 0125, 0227, 0061,
    0055, 0135, 0372, 0230, 0343, 0212, 0222, 0256, 0005, 0337, 0051, 0020, 0147, 0154, 0272, 0311,
    0323, 0000, 0346, 0317, 0341, 0236, 0250, 0054, 0143, 0026, 0001, 0077, 0130, 0342, 0211, 0251,
    0015, 0070, 0064, 0033, 0253, 0063, 0377, 0260, 0273, 0110, 0014, 0137, 0271, 0261, 0315, 0056,
    0305, 0363, 0333, 0107, 0345, 0245, 0234, 0167, 0012, 0246, 0040, 0150, 0376, 0177, 0301, 0255
};

RC2Context *
RC2_AllocateContext(void)
{
    return PORT_ZNew(RC2Context);
}
SECStatus
RC2_InitContext(RC2Context *cx, const unsigned char *key, unsigned int len,
                const unsigned char *input, int mode, unsigned int efLen8,
                unsigned int unused)
{
    PRUint8 *L, *L2;
    int i;
#if !defined(IS_LITTLE_ENDIAN)
    PRUint16 tmpS;
#endif
    PRUint8 tmpB;

    if (!key || !cx || !len || len > (sizeof cx->B) ||
        efLen8 > (sizeof cx->B)) {
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
        return SECFailure;
    }
    if (mode == NSS_RC2) {
        /* groovy */
    } else if (mode == NSS_RC2_CBC) {
        if (!input) {
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
            return SECFailure;
        }
    } else {
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
        return SECFailure;
    }

    if (mode == NSS_RC2_CBC) {
        cx->enc = &rc2_EncryptCBC;
        cx->dec = &rc2_DecryptCBC;
        LOAD(cx->iv.s);
    } else {
        cx->enc = &rc2_EncryptECB;
        cx->dec = &rc2_DecryptECB;
    }

    /* Step 0. Copy key into table. */
    memcpy(cx->B, key, len);

    /* Step 1. Compute all values to the right of the key. */
    L2 = cx->B;
    L = L2 + len;
    tmpB = L[-1];
    for (i = (sizeof cx->B) - len; i > 0; --i) {
        *L++ = tmpB = S[(PRUint8)(tmpB + *L2++)];
    }

    /* step 2. Adjust left most byte of effective key. */
    i = (sizeof cx->B) - efLen8;
    L = cx->B + i;
    *L = tmpB = S[*L]; /* mask is always 0xff */

    /* step 3. Recompute all values to the left of effective key. */
    L2 = --L + efLen8;
    while (L >= cx->B) {
        *L-- = tmpB = S[tmpB ^ *L2--];
    }

#if !defined(IS_LITTLE_ENDIAN)
    for (i = 63; i >= 0; --i) {
        SWAPK(i); /* candidate for unrolling */
    }
#endif
    return SECSuccess;
}

/*
** Create a new RC2 context suitable for RC2 encryption/decryption.
**  "key" raw key data
**  "len" the number of bytes of key data
**  "iv" is the CBC initialization vector (if mode is NSS_RC2_CBC)
**  "mode" one of NSS_RC2 or NSS_RC2_CBC
**  "effectiveKeyLen" in bytes, not bits.
**
** When mode is set to NSS_RC2_CBC the RC2 cipher is run in "cipher block
** chaining" mode.
*/
RC2Context *
RC2_CreateContext(const unsigned char *key, unsigned int len,
                  const unsigned char *iv, int mode, unsigned efLen8)
{
    RC2Context *cx = PORT_ZNew(RC2Context);
    if (cx) {
        SECStatus rv = RC2_InitContext(cx, key, len, iv, mode, efLen8, 0);
        if (rv != SECSuccess) {
            RC2_DestroyContext(cx, PR_TRUE);
            cx = NULL;
        }
    }
    return cx;
}

/*
** Destroy an RC2 encryption/decryption context.
**  "cx" the context
**  "freeit" if PR_TRUE then free the object as well as its sub-objects
*/
void
RC2_DestroyContext(RC2Context *cx, PRBool freeit)
{
    if (cx) {
        memset(cx, 0, sizeof *cx);
        if (freeit) {
            PORT_Free(cx);
        }
    }
}

#define ROL(x, k) (x << k | x >> (16 - k))
#define MIX(j)                                           \
    R0 = R0 + cx->K[4 * j + 0] + (R3 & R2) + (~R3 & R1); \
    R0 = ROL(R0, 1);                                     \
    R1 = R1 + cx->K[4 * j + 1] + (R0 & R3) + (~R0 & R2); \
    R1 = ROL(R1, 2);                                     \
    R2 = R2 + cx->K[4 * j + 2] + (R1 & R0) + (~R1 & R3); \
    R2 = ROL(R2, 3);                                     \
    R3 = R3 + cx->K[4 * j + 3] + (R2 & R1) + (~R2 & R0); \
    R3 = ROL(R3, 5)
#define MASH                  \
    R0 = R0 + cx->K[R3 & 63]; \
    R1 = R1 + cx->K[R0 & 63]; \
    R2 = R2 + cx->K[R1 & 63]; \
    R3 = R3 + cx->K[R2 & 63]

/* Encrypt one block */
static void
rc2_Encrypt1Block(RC2Context *cx, RC2Block *output, RC2Block *input)
{
    register PRUint16 R0, R1, R2, R3;

    /* step 1. Initialize input. */
    R0 = input->s[0];
    R1 = input->s[1];
    R2 = input->s[2];
    R3 = input->s[3];

    /* step 2.  Expand Key (already done, in context) */
    /* step 3.  j = 0 */
    /* step 4.  Perform 5 mixing rounds. */

    MIX(0);
    MIX(1);
    MIX(2);
    MIX(3);
    MIX(4);

    /* step 5. Perform 1 mashing round. */
    MASH;

    /* step 6. Perform 6 mixing rounds. */

    MIX(5);
    MIX(6);
    MIX(7);
    MIX(8);
    MIX(9);
    MIX(10);

    /* step 7. Perform 1 mashing round. */
    MASH;

    /* step 8. Perform 5 mixing rounds. */

    MIX(11);
    MIX(12);
    MIX(13);
    MIX(14);
    MIX(15);

    /* output results */
    output->s[0] = R0;
    output->s[1] = R1;
    output->s[2] = R2;
    output->s[3] = R3;
}

#define ROR(x, k) (x >> k | x << (16 - k))
#define R_MIX(j)                                         \
    R3 = ROR(R3, 5);                                     \
    R3 = R3 - cx->K[4 * j + 3] - (R2 & R1) - (~R2 & R0); \
    R2 = ROR(R2, 3);                                     \
    R2 = R2 - cx->K[4 * j + 2] - (R1 & R0) - (~R1 & R3); \
    R1 = ROR(R1, 2);                                     \
    R1 = R1 - cx->K[4 * j + 1] - (R0 & R3) - (~R0 & R2); \
    R0 = ROR(R0, 1);                                     \
    R0 = R0 - cx->K[4 * j + 0] - (R3 & R2) - (~R3 & R1)
#define R_MASH                \
    R3 = R3 - cx->K[R2 & 63]; \
    R2 = R2 - cx->K[R1 & 63]; \
    R1 = R1 - cx->K[R0 & 63]; \
    R0 = R0 - cx->K[R3 & 63]

/* Encrypt one block */
static void
rc2_Decrypt1Block(RC2Context *cx, RC2Block *output, RC2Block *input)
{
    register PRUint16 R0, R1, R2, R3;

    /* step 1. Initialize input. */
    R0 = input->s[0];
    R1 = input->s[1];
    R2 = input->s[2];
    R3 = input->s[3];

    /* step 2.  Expand Key (already done, in context) */
    /* step 3.  j = 63 */
    /* step 4.  Perform 5 r_mixing rounds. */
    R_MIX(15);
    R_MIX(14);
    R_MIX(13);
    R_MIX(12);
    R_MIX(11);

    /* step 5.  Perform 1 r_mashing round. */
    R_MASH;

    /* step 6.  Perform 6 r_mixing rounds. */
    R_MIX(10);
    R_MIX(9);
    R_MIX(8);
    R_MIX(7);
    R_MIX(6);
    R_MIX(5);

    /* step 7.  Perform 1 r_mashing round. */
    R_MASH;

    /* step 8.  Perform 5 r_mixing rounds. */
    R_MIX(4);
    R_MIX(3);
    R_MIX(2);
    R_MIX(1);
    R_MIX(0);

    /* output results */
    output->s[0] = R0;
    output->s[1] = R1;
    output->s[2] = R2;
    output->s[3] = R3;
}

static SECStatus NO_SANITIZE_ALIGNMENT
rc2_EncryptECB(RC2Context *cx, unsigned char *output,
               const unsigned char *input, unsigned int inputLen)
{
    RC2Block iBlock;

    while (inputLen > 0) {
        LOAD(iBlock.s)
        rc2_Encrypt1Block(cx, &iBlock, &iBlock);
        STORE(iBlock.s)
        output += RC2_BLOCK_SIZE;
        input += RC2_BLOCK_SIZE;
        inputLen -= RC2_BLOCK_SIZE;
    }
    return SECSuccess;
}

static SECStatus NO_SANITIZE_ALIGNMENT
rc2_DecryptECB(RC2Context *cx, unsigned char *output,
               const unsigned char *input, unsigned int inputLen)
{
    RC2Block iBlock;

    while (inputLen > 0) {
        LOAD(iBlock.s)
        rc2_Decrypt1Block(cx, &iBlock, &iBlock);
        STORE(iBlock.s)
        output += RC2_BLOCK_SIZE;
        input += RC2_BLOCK_SIZE;
        inputLen -= RC2_BLOCK_SIZE;
    }
    return SECSuccess;
}

static SECStatus NO_SANITIZE_ALIGNMENT
rc2_EncryptCBC(RC2Context *cx, unsigned char *output,
               const unsigned char *input, unsigned int inputLen)
{
    RC2Block iBlock;

    while (inputLen > 0) {

        LOAD(iBlock.s)
        iBlock.l[0] ^= cx->iv.l[0];
        iBlock.l[1] ^= cx->iv.l[1];
        rc2_Encrypt1Block(cx, &iBlock, &iBlock);
        cx->iv = iBlock;
        STORE(iBlock.s)
        output += RC2_BLOCK_SIZE;
        input += RC2_BLOCK_SIZE;
        inputLen -= RC2_BLOCK_SIZE;
    }
    return SECSuccess;
}

static SECStatus NO_SANITIZE_ALIGNMENT
rc2_DecryptCBC(RC2Context *cx, unsigned char *output,
               const unsigned char *input, unsigned int inputLen)
{
    RC2Block iBlock;
    RC2Block oBlock;

    while (inputLen > 0) {
        LOAD(iBlock.s)
        rc2_Decrypt1Block(cx, &oBlock, &iBlock);
        oBlock.l[0] ^= cx->iv.l[0];
        oBlock.l[1] ^= cx->iv.l[1];
        cx->iv = iBlock;
        STORE(oBlock.s)
        output += RC2_BLOCK_SIZE;
        input += RC2_BLOCK_SIZE;
        inputLen -= RC2_BLOCK_SIZE;
    }
    return SECSuccess;
}

/*
** Perform RC2 encryption.
**  "cx" the context
**  "output" the output buffer to store the encrypted data.
**  "outputLen" how much data is stored in "output". Set by the routine
**     after some data is stored in output.
**  "maxOutputLen" the maximum amount of data that can ever be
**     stored in "output"
**  "input" the input data
**  "inputLen" the amount of input data
*/
SECStatus
RC2_Encrypt(RC2Context *cx, unsigned char *output,
            unsigned int *outputLen, unsigned int maxOutputLen,
            const unsigned char *input, unsigned int inputLen)
{
    SECStatus rv = SECSuccess;
    if (inputLen) {
        if (inputLen % RC2_BLOCK_SIZE) {
            PORT_SetError(SEC_ERROR_INPUT_LEN);
            return SECFailure;
        }
        if (maxOutputLen < inputLen) {
            PORT_SetError(SEC_ERROR_OUTPUT_LEN);
            return SECFailure;
        }
        rv = (*cx->enc)(cx, output, input, inputLen);
    }
    if (rv == SECSuccess) {
        *outputLen = inputLen;
    }
    return rv;
}

/*
** Perform RC2 decryption.
**  "cx" the context
**  "output" the output buffer to store the decrypted data.
**  "outputLen" how much data is stored in "output". Set by the routine
**     after some data is stored in output.
**  "maxOutputLen" the maximum amount of data that can ever be
**     stored in "output"
**  "input" the input data
**  "inputLen" the amount of input data
*/
SECStatus
RC2_Decrypt(RC2Context *cx, unsigned char *output,
            unsigned int *outputLen, unsigned int maxOutputLen,
            const unsigned char *input, unsigned int inputLen)
{
    SECStatus rv = SECSuccess;
    if (inputLen) {
        if (inputLen % RC2_BLOCK_SIZE) {
            PORT_SetError(SEC_ERROR_INPUT_LEN);
            return SECFailure;
        }
        if (maxOutputLen < inputLen) {
            PORT_SetError(SEC_ERROR_OUTPUT_LEN);
            return SECFailure;
        }
        rv = (*cx->dec)(cx, output, input, inputLen);
    }
    if (rv == SECSuccess) {
        *outputLen = inputLen;
    }
    return rv;
}