xref: /third_party/openssl/crypto/cast/c_skey.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * CAST low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/cast.h>
17e1051a39Sopenharmony_ci#include "cast_local.h"
18e1051a39Sopenharmony_ci#include "cast_s.h"
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci#define CAST_exp(l,A,a,n) \
21e1051a39Sopenharmony_ci        A[n/4]=l; \
22e1051a39Sopenharmony_ci        a[n+3]=(l    )&0xff; \
23e1051a39Sopenharmony_ci        a[n+2]=(l>> 8)&0xff; \
24e1051a39Sopenharmony_ci        a[n+1]=(l>>16)&0xff; \
25e1051a39Sopenharmony_ci        a[n+0]=(l>>24)&0xff;
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci#define S4 CAST_S_table4
28e1051a39Sopenharmony_ci#define S5 CAST_S_table5
29e1051a39Sopenharmony_ci#define S6 CAST_S_table6
30e1051a39Sopenharmony_ci#define S7 CAST_S_table7
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_civoid CAST_set_key(CAST_KEY *key, int len, const unsigned char *data)
33e1051a39Sopenharmony_ci{
34e1051a39Sopenharmony_ci    CAST_LONG x[16];
35e1051a39Sopenharmony_ci    CAST_LONG z[16];
36e1051a39Sopenharmony_ci    CAST_LONG k[32];
37e1051a39Sopenharmony_ci    CAST_LONG X[4], Z[4];
38e1051a39Sopenharmony_ci    CAST_LONG l, *K;
39e1051a39Sopenharmony_ci    int i;
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    for (i = 0; i < 16; i++)
42e1051a39Sopenharmony_ci        x[i] = 0;
43e1051a39Sopenharmony_ci    if (len > 16)
44e1051a39Sopenharmony_ci        len = 16;
45e1051a39Sopenharmony_ci    for (i = 0; i < len; i++)
46e1051a39Sopenharmony_ci        x[i] = data[i];
47e1051a39Sopenharmony_ci    if (len <= 10)
48e1051a39Sopenharmony_ci        key->short_key = 1;
49e1051a39Sopenharmony_ci    else
50e1051a39Sopenharmony_ci        key->short_key = 0;
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    K = &k[0];
53e1051a39Sopenharmony_ci    X[0] = ((x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]) & 0xffffffffL;
54e1051a39Sopenharmony_ci    X[1] = ((x[4] << 24) | (x[5] << 16) | (x[6] << 8) | x[7]) & 0xffffffffL;
55e1051a39Sopenharmony_ci    X[2] = ((x[8] << 24) | (x[9] << 16) | (x[10] << 8) | x[11]) & 0xffffffffL;
56e1051a39Sopenharmony_ci    X[3] =
57e1051a39Sopenharmony_ci        ((x[12] << 24) | (x[13] << 16) | (x[14] << 8) | x[15]) & 0xffffffffL;
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ci    for (;;) {
60e1051a39Sopenharmony_ci        l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
61e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 0);
62e1051a39Sopenharmony_ci        l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
63e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 4);
64e1051a39Sopenharmony_ci        l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
65e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 8);
66e1051a39Sopenharmony_ci        l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
67e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 12);
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci        K[0] = S4[z[8]] ^ S5[z[9]] ^ S6[z[7]] ^ S7[z[6]] ^ S4[z[2]];
70e1051a39Sopenharmony_ci        K[1] = S4[z[10]] ^ S5[z[11]] ^ S6[z[5]] ^ S7[z[4]] ^ S5[z[6]];
71e1051a39Sopenharmony_ci        K[2] = S4[z[12]] ^ S5[z[13]] ^ S6[z[3]] ^ S7[z[2]] ^ S6[z[9]];
72e1051a39Sopenharmony_ci        K[3] = S4[z[14]] ^ S5[z[15]] ^ S6[z[1]] ^ S7[z[0]] ^ S7[z[12]];
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci        l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
75e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 0);
76e1051a39Sopenharmony_ci        l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
77e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 4);
78e1051a39Sopenharmony_ci        l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
79e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 8);
80e1051a39Sopenharmony_ci        l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
81e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 12);
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci        K[4] = S4[x[3]] ^ S5[x[2]] ^ S6[x[12]] ^ S7[x[13]] ^ S4[x[8]];
84e1051a39Sopenharmony_ci        K[5] = S4[x[1]] ^ S5[x[0]] ^ S6[x[14]] ^ S7[x[15]] ^ S5[x[13]];
85e1051a39Sopenharmony_ci        K[6] = S4[x[7]] ^ S5[x[6]] ^ S6[x[8]] ^ S7[x[9]] ^ S6[x[3]];
86e1051a39Sopenharmony_ci        K[7] = S4[x[5]] ^ S5[x[4]] ^ S6[x[10]] ^ S7[x[11]] ^ S7[x[7]];
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci        l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
89e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 0);
90e1051a39Sopenharmony_ci        l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
91e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 4);
92e1051a39Sopenharmony_ci        l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
93e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 8);
94e1051a39Sopenharmony_ci        l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
95e1051a39Sopenharmony_ci        CAST_exp(l, Z, z, 12);
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ci        K[8] = S4[z[3]] ^ S5[z[2]] ^ S6[z[12]] ^ S7[z[13]] ^ S4[z[9]];
98e1051a39Sopenharmony_ci        K[9] = S4[z[1]] ^ S5[z[0]] ^ S6[z[14]] ^ S7[z[15]] ^ S5[z[12]];
99e1051a39Sopenharmony_ci        K[10] = S4[z[7]] ^ S5[z[6]] ^ S6[z[8]] ^ S7[z[9]] ^ S6[z[2]];
100e1051a39Sopenharmony_ci        K[11] = S4[z[5]] ^ S5[z[4]] ^ S6[z[10]] ^ S7[z[11]] ^ S7[z[6]];
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci        l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
103e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 0);
104e1051a39Sopenharmony_ci        l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
105e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 4);
106e1051a39Sopenharmony_ci        l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
107e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 8);
108e1051a39Sopenharmony_ci        l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
109e1051a39Sopenharmony_ci        CAST_exp(l, X, x, 12);
110e1051a39Sopenharmony_ci
111e1051a39Sopenharmony_ci        K[12] = S4[x[8]] ^ S5[x[9]] ^ S6[x[7]] ^ S7[x[6]] ^ S4[x[3]];
112e1051a39Sopenharmony_ci        K[13] = S4[x[10]] ^ S5[x[11]] ^ S6[x[5]] ^ S7[x[4]] ^ S5[x[7]];
113e1051a39Sopenharmony_ci        K[14] = S4[x[12]] ^ S5[x[13]] ^ S6[x[3]] ^ S7[x[2]] ^ S6[x[8]];
114e1051a39Sopenharmony_ci        K[15] = S4[x[14]] ^ S5[x[15]] ^ S6[x[1]] ^ S7[x[0]] ^ S7[x[13]];
115e1051a39Sopenharmony_ci        if (K != k)
116e1051a39Sopenharmony_ci            break;
117e1051a39Sopenharmony_ci        K += 16;
118e1051a39Sopenharmony_ci    }
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci    for (i = 0; i < 16; i++) {
121e1051a39Sopenharmony_ci        key->data[i * 2] = k[i];
122e1051a39Sopenharmony_ci        key->data[i * 2 + 1] = ((k[i + 16]) + 16) & 0x1f;
123e1051a39Sopenharmony_ci    }
124e1051a39Sopenharmony_ci}
125