xref: /third_party/openssl/crypto/des/des_local.h (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2016 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#ifndef OSSL_CRYPTO_DES_LOCAL_H
11e1051a39Sopenharmony_ci# define OSSL_CRYPTO_DES_LOCAL_H
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci# include <openssl/e_os2.h>
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci# include <stdio.h>
16e1051a39Sopenharmony_ci# include <stdlib.h>
17e1051a39Sopenharmony_ci# include <string.h>
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci# include <openssl/des.h>
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci# ifdef OPENSSL_BUILD_SHLIBCRYPTO
22e1051a39Sopenharmony_ci#  undef OPENSSL_EXTERN
23e1051a39Sopenharmony_ci#  define OPENSSL_EXTERN OPENSSL_EXPORT
24e1051a39Sopenharmony_ci# endif
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci# define ITERATIONS 16
27e1051a39Sopenharmony_ci# define HALF_ITERATIONS 8
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci# define c2l(c,l)        (l =((DES_LONG)(*((c)++)))    , \
30e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++)))<< 8L, \
31e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++)))<<16L, \
32e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++)))<<24L)
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per c2l */
35e1051a39Sopenharmony_ci# define c2ln(c,l1,l2,n) { \
36e1051a39Sopenharmony_ci                        c+=n; \
37e1051a39Sopenharmony_ci                        l1=l2=0; \
38e1051a39Sopenharmony_ci                        switch (n) { \
39e1051a39Sopenharmony_ci                        case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
40e1051a39Sopenharmony_ci                        /* fall thru */                          \
41e1051a39Sopenharmony_ci                        case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
42e1051a39Sopenharmony_ci                        /* fall thru */                          \
43e1051a39Sopenharmony_ci                        case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
44e1051a39Sopenharmony_ci                        /* fall thru */                          \
45e1051a39Sopenharmony_ci                        case 5: l2|=((DES_LONG)(*(--(c))));      \
46e1051a39Sopenharmony_ci                        /* fall thru */                          \
47e1051a39Sopenharmony_ci                        case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
48e1051a39Sopenharmony_ci                        /* fall thru */                          \
49e1051a39Sopenharmony_ci                        case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
50e1051a39Sopenharmony_ci                        /* fall thru */                          \
51e1051a39Sopenharmony_ci                        case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
52e1051a39Sopenharmony_ci                        /* fall thru */                          \
53e1051a39Sopenharmony_ci                        case 1: l1|=((DES_LONG)(*(--(c))));      \
54e1051a39Sopenharmony_ci                                } \
55e1051a39Sopenharmony_ci                        }
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci# define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
58e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
59e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
60e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>24L)&0xff))
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci/*
63e1051a39Sopenharmony_ci * replacements for htonl and ntohl since I have no idea what to do when
64e1051a39Sopenharmony_ci * faced with machines with 8 byte longs.
65e1051a39Sopenharmony_ci */
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci# define n2l(c,l)        (l =((DES_LONG)(*((c)++)))<<24L, \
68e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++)))<<16L, \
69e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++)))<< 8L, \
70e1051a39Sopenharmony_ci                         l|=((DES_LONG)(*((c)++))))
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ci# define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
73e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
74e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
75e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)     )&0xff))
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per l2c */
78e1051a39Sopenharmony_ci# define l2cn(l1,l2,c,n) { \
79e1051a39Sopenharmony_ci                        c+=n; \
80e1051a39Sopenharmony_ci                        switch (n) { \
81e1051a39Sopenharmony_ci                        case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
82e1051a39Sopenharmony_ci                        /* fall thru */                                     \
83e1051a39Sopenharmony_ci                        case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
84e1051a39Sopenharmony_ci                        /* fall thru */                                     \
85e1051a39Sopenharmony_ci                        case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
86e1051a39Sopenharmony_ci                        /* fall thru */                                     \
87e1051a39Sopenharmony_ci                        case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
88e1051a39Sopenharmony_ci                        /* fall thru */                                     \
89e1051a39Sopenharmony_ci                        case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
90e1051a39Sopenharmony_ci                        /* fall thru */                                     \
91e1051a39Sopenharmony_ci                        case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
92e1051a39Sopenharmony_ci                        /* fall thru */                                     \
93e1051a39Sopenharmony_ci                        case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
94e1051a39Sopenharmony_ci                        /* fall thru */                                     \
95e1051a39Sopenharmony_ci                        case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
96e1051a39Sopenharmony_ci                                } \
97e1051a39Sopenharmony_ci                        }
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ci# if defined(_MSC_VER)
100e1051a39Sopenharmony_ci#  define ROTATE(a,n)     (_lrotr(a,n))
101e1051a39Sopenharmony_ci# elif defined(__ICC)
102e1051a39Sopenharmony_ci#  define ROTATE(a,n)     (_rotr(a,n))
103e1051a39Sopenharmony_ci# elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
104e1051a39Sopenharmony_ci#  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
105e1051a39Sopenharmony_ci#   define ROTATE(a,n)   ({ register unsigned int ret;   \
106e1051a39Sopenharmony_ci                                asm ("rorl %1,%0"       \
107e1051a39Sopenharmony_ci                                        : "=r"(ret)     \
108e1051a39Sopenharmony_ci                                        : "I"(n),"0"(a) \
109e1051a39Sopenharmony_ci                                        : "cc");        \
110e1051a39Sopenharmony_ci                           ret;                         \
111e1051a39Sopenharmony_ci                        })
112e1051a39Sopenharmony_ci#  endif
113e1051a39Sopenharmony_ci# endif
114e1051a39Sopenharmony_ci# ifndef ROTATE
115e1051a39Sopenharmony_ci#  define ROTATE(a,n)     (((a)>>(n))+((a)<<(32-(n))))
116e1051a39Sopenharmony_ci# endif
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ci/*
119e1051a39Sopenharmony_ci * Don't worry about the LOAD_DATA() stuff, that is used by fcrypt() to add
120e1051a39Sopenharmony_ci * it's little bit to the front
121e1051a39Sopenharmony_ci */
122e1051a39Sopenharmony_ci
123e1051a39Sopenharmony_ci# ifdef DES_FCRYPT
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ci#  define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
126e1051a39Sopenharmony_ci        { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
127e1051a39Sopenharmony_ci
128e1051a39Sopenharmony_ci#  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
129e1051a39Sopenharmony_ci        t=R^(R>>16L); \
130e1051a39Sopenharmony_ci        u=t&E0; t&=E1; \
131e1051a39Sopenharmony_ci        tmp=(u<<16); u^=R^s[S  ]; u^=tmp; \
132e1051a39Sopenharmony_ci        tmp=(t<<16); t^=R^s[S+1]; t^=tmp
133e1051a39Sopenharmony_ci# else
134e1051a39Sopenharmony_ci#  define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
135e1051a39Sopenharmony_ci#  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
136e1051a39Sopenharmony_ci        u=R^s[S  ]; \
137e1051a39Sopenharmony_ci        t=R^s[S+1]
138e1051a39Sopenharmony_ci# endif
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_ci/*
141e1051a39Sopenharmony_ci * It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there is no reason
142e1051a39Sopenharmony_ci * to not xor all the sub items together.  This potentially saves a register
143e1051a39Sopenharmony_ci * since things can be xored directly into L
144e1051a39Sopenharmony_ci */
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci# define D_ENCRYPT(LL,R,S) { \
147e1051a39Sopenharmony_ci        LOAD_DATA_tmp(R,S,u,t,E0,E1); \
148e1051a39Sopenharmony_ci        t=ROTATE(t,4); \
149e1051a39Sopenharmony_ci        LL^= \
150e1051a39Sopenharmony_ci            DES_SPtrans[0][(u>> 2L)&0x3f]^ \
151e1051a39Sopenharmony_ci            DES_SPtrans[2][(u>>10L)&0x3f]^ \
152e1051a39Sopenharmony_ci            DES_SPtrans[4][(u>>18L)&0x3f]^ \
153e1051a39Sopenharmony_ci            DES_SPtrans[6][(u>>26L)&0x3f]^ \
154e1051a39Sopenharmony_ci            DES_SPtrans[1][(t>> 2L)&0x3f]^ \
155e1051a39Sopenharmony_ci            DES_SPtrans[3][(t>>10L)&0x3f]^ \
156e1051a39Sopenharmony_ci            DES_SPtrans[5][(t>>18L)&0x3f]^ \
157e1051a39Sopenharmony_ci            DES_SPtrans[7][(t>>26L)&0x3f]; }
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci        /*-
160e1051a39Sopenharmony_ci         * IP and FP
161e1051a39Sopenharmony_ci         * The problem is more of a geometric problem that random bit fiddling.
162e1051a39Sopenharmony_ci         0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
163e1051a39Sopenharmony_ci         8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
164e1051a39Sopenharmony_ci        16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
165e1051a39Sopenharmony_ci        24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci        32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
168e1051a39Sopenharmony_ci        40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
169e1051a39Sopenharmony_ci        48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
170e1051a39Sopenharmony_ci        56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
171e1051a39Sopenharmony_ci
172e1051a39Sopenharmony_ci        The output has been subject to swaps of the form
173e1051a39Sopenharmony_ci        0 1 -> 3 1 but the odd and even bits have been put into
174e1051a39Sopenharmony_ci        2 3    2 0
175e1051a39Sopenharmony_ci        different words.  The main trick is to remember that
176e1051a39Sopenharmony_ci        t=((l>>size)^r)&(mask);
177e1051a39Sopenharmony_ci        r^=t;
178e1051a39Sopenharmony_ci        l^=(t<<size);
179e1051a39Sopenharmony_ci        can be used to swap and move bits between words.
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_ci        So l =  0  1  2  3  r = 16 17 18 19
182e1051a39Sopenharmony_ci                4  5  6  7      20 21 22 23
183e1051a39Sopenharmony_ci                8  9 10 11      24 25 26 27
184e1051a39Sopenharmony_ci               12 13 14 15      28 29 30 31
185e1051a39Sopenharmony_ci        becomes (for size == 2 and mask == 0x3333)
186e1051a39Sopenharmony_ci           t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
187e1051a39Sopenharmony_ci                 6^20  7^21 -- --        4  5 20 21       6  7 22 23
188e1051a39Sopenharmony_ci                10^24 11^25 -- --        8  9 24 25      10 11 24 25
189e1051a39Sopenharmony_ci                14^28 15^29 -- --       12 13 28 29      14 15 28 29
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci        Thanks for hints from Richard Outerbridge - he told me IP&FP
192e1051a39Sopenharmony_ci        could be done in 15 xor, 10 shifts and 5 ands.
193e1051a39Sopenharmony_ci        When I finally started to think of the problem in 2D
194e1051a39Sopenharmony_ci        I first got ~42 operations without xors.  When I remembered
195e1051a39Sopenharmony_ci        how to use xors :-) I got it to its final state.
196e1051a39Sopenharmony_ci        */
197e1051a39Sopenharmony_ci# define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
198e1051a39Sopenharmony_ci        (b)^=(t),\
199e1051a39Sopenharmony_ci        (a)^=((t)<<(n)))
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ci# define IP(l,r) \
202e1051a39Sopenharmony_ci        { \
203e1051a39Sopenharmony_ci        register DES_LONG tt; \
204e1051a39Sopenharmony_ci        PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
205e1051a39Sopenharmony_ci        PERM_OP(l,r,tt,16,0x0000ffffL); \
206e1051a39Sopenharmony_ci        PERM_OP(r,l,tt, 2,0x33333333L); \
207e1051a39Sopenharmony_ci        PERM_OP(l,r,tt, 8,0x00ff00ffL); \
208e1051a39Sopenharmony_ci        PERM_OP(r,l,tt, 1,0x55555555L); \
209e1051a39Sopenharmony_ci        }
210e1051a39Sopenharmony_ci
211e1051a39Sopenharmony_ci# define FP(l,r) \
212e1051a39Sopenharmony_ci        { \
213e1051a39Sopenharmony_ci        register DES_LONG tt; \
214e1051a39Sopenharmony_ci        PERM_OP(l,r,tt, 1,0x55555555L); \
215e1051a39Sopenharmony_ci        PERM_OP(r,l,tt, 8,0x00ff00ffL); \
216e1051a39Sopenharmony_ci        PERM_OP(l,r,tt, 2,0x33333333L); \
217e1051a39Sopenharmony_ci        PERM_OP(r,l,tt,16,0x0000ffffL); \
218e1051a39Sopenharmony_ci        PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
219e1051a39Sopenharmony_ci        }
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_ciextern const DES_LONG DES_SPtrans[8][64];
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_civoid fcrypt_body(DES_LONG *out, DES_key_schedule *ks,
224e1051a39Sopenharmony_ci                 DES_LONG Eswap0, DES_LONG Eswap1);
225e1051a39Sopenharmony_ci
226e1051a39Sopenharmony_ci#endif
227