xref: /aosp_15_r20/external/boringssl/src/crypto/des/internal.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #ifndef OPENSSL_HEADER_DES_INTERNAL_H
58 #define OPENSSL_HEADER_DES_INTERNAL_H
59 
60 #include <openssl/base.h>
61 #include <openssl/des.h>
62 
63 #include "../internal.h"
64 
65 #if defined(__cplusplus)
66 extern "C" {
67 #endif
68 
69 
70 // TODO(davidben): Ideally these macros would be replaced with
71 // |CRYPTO_load_u32_le| and |CRYPTO_store_u32_le|.
72 
73 #define c2l(c, l)                         \
74   do {                                    \
75     (l) = ((uint32_t)(*((c)++)));         \
76     (l) |= ((uint32_t)(*((c)++))) << 8L;  \
77     (l) |= ((uint32_t)(*((c)++))) << 16L; \
78     (l) |= ((uint32_t)(*((c)++))) << 24L; \
79   } while (0)
80 
81 #define l2c(l, c)                                    \
82   do {                                               \
83     *((c)++) = (unsigned char)(((l)) & 0xff);        \
84     *((c)++) = (unsigned char)(((l) >> 8L) & 0xff);  \
85     *((c)++) = (unsigned char)(((l) >> 16L) & 0xff); \
86     *((c)++) = (unsigned char)(((l) >> 24L) & 0xff); \
87   } while (0)
88 
89 // NOTE - c is not incremented as per c2l
90 #define c2ln(c, l1, l2, n)                     \
91   do {                                         \
92     (c) += (n);                                \
93     (l1) = (l2) = 0;                           \
94     switch (n) {                               \
95       case 8:                                  \
96         (l2) = ((uint32_t)(*(--(c)))) << 24L;  \
97         OPENSSL_FALLTHROUGH;                   \
98       case 7:                                  \
99         (l2) |= ((uint32_t)(*(--(c)))) << 16L; \
100         OPENSSL_FALLTHROUGH;                   \
101       case 6:                                  \
102         (l2) |= ((uint32_t)(*(--(c)))) << 8L;  \
103         OPENSSL_FALLTHROUGH;                   \
104       case 5:                                  \
105         (l2) |= ((uint32_t)(*(--(c))));        \
106         OPENSSL_FALLTHROUGH;                   \
107       case 4:                                  \
108         (l1) = ((uint32_t)(*(--(c)))) << 24L;  \
109         OPENSSL_FALLTHROUGH;                   \
110       case 3:                                  \
111         (l1) |= ((uint32_t)(*(--(c)))) << 16L; \
112         OPENSSL_FALLTHROUGH;                   \
113       case 2:                                  \
114         (l1) |= ((uint32_t)(*(--(c)))) << 8L;  \
115         OPENSSL_FALLTHROUGH;                   \
116       case 1:                                  \
117         (l1) |= ((uint32_t)(*(--(c))));        \
118     }                                          \
119   } while (0)
120 
121 // NOTE - c is not incremented as per l2c
122 #define l2cn(l1, l2, c, n)                                \
123   do {                                                    \
124     (c) += (n);                                           \
125     switch (n) {                                          \
126       case 8:                                             \
127         *(--(c)) = (unsigned char)(((l2) >> 24L) & 0xff); \
128         OPENSSL_FALLTHROUGH;                              \
129       case 7:                                             \
130         *(--(c)) = (unsigned char)(((l2) >> 16L) & 0xff); \
131         OPENSSL_FALLTHROUGH;                              \
132       case 6:                                             \
133         *(--(c)) = (unsigned char)(((l2) >> 8L) & 0xff);  \
134         OPENSSL_FALLTHROUGH;                              \
135       case 5:                                             \
136         *(--(c)) = (unsigned char)(((l2)) & 0xff);        \
137         OPENSSL_FALLTHROUGH;                              \
138       case 4:                                             \
139         *(--(c)) = (unsigned char)(((l1) >> 24L) & 0xff); \
140         OPENSSL_FALLTHROUGH;                              \
141       case 3:                                             \
142         *(--(c)) = (unsigned char)(((l1) >> 16L) & 0xff); \
143         OPENSSL_FALLTHROUGH;                              \
144       case 2:                                             \
145         *(--(c)) = (unsigned char)(((l1) >> 8L) & 0xff);  \
146         OPENSSL_FALLTHROUGH;                              \
147       case 1:                                             \
148         *(--(c)) = (unsigned char)(((l1)) & 0xff);        \
149     }                                                     \
150   } while (0)
151 
152 
153 // Correctly-typed versions of DES functions.
154 //
155 // See https://crbug.com/boringssl/683.
156 
157 void DES_set_key_ex(const uint8_t key[8], DES_key_schedule *schedule);
158 void DES_ecb_encrypt_ex(const uint8_t in[8], uint8_t out[8],
159                         const DES_key_schedule *schedule, int is_encrypt);
160 void DES_ncbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len,
161                          const DES_key_schedule *schedule, uint8_t ivec[8],
162                          int enc);
163 void DES_ecb3_encrypt_ex(const uint8_t input[8], uint8_t output[8],
164                          const DES_key_schedule *ks1,
165                          const DES_key_schedule *ks2,
166                          const DES_key_schedule *ks3, int enc);
167 void DES_ede3_cbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len,
168                              const DES_key_schedule *ks1,
169                              const DES_key_schedule *ks2,
170                              const DES_key_schedule *ks3, uint8_t ivec[8],
171                              int enc);
172 
173 
174 // Private functions.
175 //
176 // These functions are only exported for use in |decrepit|.
177 
178 OPENSSL_EXPORT void DES_decrypt3(uint32_t data[2], const DES_key_schedule *ks1,
179                                  const DES_key_schedule *ks2,
180                                  const DES_key_schedule *ks3);
181 
182 OPENSSL_EXPORT void DES_encrypt3(uint32_t data[2], const DES_key_schedule *ks1,
183                                  const DES_key_schedule *ks2,
184                                  const DES_key_schedule *ks3);
185 
186 
187 #if defined(__cplusplus)
188 }  // extern C
189 #endif
190 
191 #endif  // OPENSSL_HEADER_DES_INTERNAL_H
192