1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2007-2021 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 * Copyright (c) 2007 KISA(Korea Information Security Agency). All rights reserved.
12e1051a39Sopenharmony_ci *
13e1051a39Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
14e1051a39Sopenharmony_ci * modification, are permitted provided that the following conditions
15e1051a39Sopenharmony_ci * are met:
16e1051a39Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
17e1051a39Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
18e1051a39Sopenharmony_ci * 2. Neither the name of author nor the names of its contributors may
19e1051a39Sopenharmony_ci *    be used to endorse or promote products derived from this software
20e1051a39Sopenharmony_ci *    without specific prior written permission.
21e1051a39Sopenharmony_ci *
22e1051a39Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23e1051a39Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24e1051a39Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25e1051a39Sopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
26e1051a39Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27e1051a39Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28e1051a39Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29e1051a39Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30e1051a39Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31e1051a39Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e1051a39Sopenharmony_ci * SUCH DAMAGE.
33e1051a39Sopenharmony_ci *
34e1051a39Sopenharmony_ci */
35e1051a39Sopenharmony_ci#ifndef OSSL_CRYPTO_SEED_LOCAL_H
36e1051a39Sopenharmony_ci# define OSSL_CRYPTO_SEED_LOCAL_H
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci# include <openssl/e_os2.h>
39e1051a39Sopenharmony_ci# include <openssl/seed.h>
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci# ifdef SEED_LONG               /* need 32-bit type */
42e1051a39Sopenharmony_citypedef unsigned long seed_word;
43e1051a39Sopenharmony_ci# else
44e1051a39Sopenharmony_citypedef unsigned int seed_word;
45e1051a39Sopenharmony_ci# endif
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci# define char2word(c, i)  \
49e1051a39Sopenharmony_ci        (i) = ((((seed_word)(c)[0]) << 24) | (((seed_word)(c)[1]) << 16) | (((seed_word)(c)[2]) << 8) | ((seed_word)(c)[3]))
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci# define word2char(l, c)  \
52e1051a39Sopenharmony_ci        *((c)+0) = (unsigned char)((l)>>24) & 0xff; \
53e1051a39Sopenharmony_ci        *((c)+1) = (unsigned char)((l)>>16) & 0xff; \
54e1051a39Sopenharmony_ci        *((c)+2) = (unsigned char)((l)>> 8) & 0xff; \
55e1051a39Sopenharmony_ci        *((c)+3) = (unsigned char)((l))     & 0xff
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci# define KEYSCHEDULE_UPDATE0(T0, T1, X1, X2, X3, X4, KC)  \
58e1051a39Sopenharmony_ci        (T0) = (X3);                                     \
59e1051a39Sopenharmony_ci        (X3) = (((X3)<<8) ^ ((X4)>>24)) & 0xffffffff;    \
60e1051a39Sopenharmony_ci        (X4) = (((X4)<<8) ^ ((T0)>>24)) & 0xffffffff;    \
61e1051a39Sopenharmony_ci        (T0) = ((X1) + (X3) - (KC))     & 0xffffffff;    \
62e1051a39Sopenharmony_ci        (T1) = ((X2) + (KC) - (X4))     & 0xffffffff
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci# define KEYSCHEDULE_UPDATE1(T0, T1, X1, X2, X3, X4, KC)  \
65e1051a39Sopenharmony_ci        (T0) = (X1);                                     \
66e1051a39Sopenharmony_ci        (X1) = (((X1)>>8) ^ ((X2)<<24)) & 0xffffffff;    \
67e1051a39Sopenharmony_ci        (X2) = (((X2)>>8) ^ ((T0)<<24)) & 0xffffffff;    \
68e1051a39Sopenharmony_ci        (T0) = ((X1) + (X3) - (KC))     & 0xffffffff;     \
69e1051a39Sopenharmony_ci        (T1) = ((X2) + (KC) - (X4))     & 0xffffffff
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci# define KEYUPDATE_TEMP(T0, T1, K)   \
72e1051a39Sopenharmony_ci        (K)[0] = G_FUNC((T0));      \
73e1051a39Sopenharmony_ci        (K)[1] = G_FUNC((T1))
74e1051a39Sopenharmony_ci
75e1051a39Sopenharmony_ci# define XOR_SEEDBLOCK(DST, SRC)      \
76e1051a39Sopenharmony_ci        ((DST))[0] ^= ((SRC))[0];    \
77e1051a39Sopenharmony_ci        ((DST))[1] ^= ((SRC))[1];    \
78e1051a39Sopenharmony_ci        ((DST))[2] ^= ((SRC))[2];    \
79e1051a39Sopenharmony_ci        ((DST))[3] ^= ((SRC))[3]
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci# define MOV_SEEDBLOCK(DST, SRC)      \
82e1051a39Sopenharmony_ci        ((DST))[0] = ((SRC))[0];     \
83e1051a39Sopenharmony_ci        ((DST))[1] = ((SRC))[1];     \
84e1051a39Sopenharmony_ci        ((DST))[2] = ((SRC))[2];     \
85e1051a39Sopenharmony_ci        ((DST))[3] = ((SRC))[3]
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci# define CHAR2WORD(C, I)              \
88e1051a39Sopenharmony_ci        char2word((C),    (I)[0]);    \
89e1051a39Sopenharmony_ci        char2word((C+4),  (I)[1]);    \
90e1051a39Sopenharmony_ci        char2word((C+8),  (I)[2]);    \
91e1051a39Sopenharmony_ci        char2word((C+12), (I)[3])
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ci# define WORD2CHAR(I, C)              \
94e1051a39Sopenharmony_ci        word2char((I)[0], (C));       \
95e1051a39Sopenharmony_ci        word2char((I)[1], (C+4));     \
96e1051a39Sopenharmony_ci        word2char((I)[2], (C+8));     \
97e1051a39Sopenharmony_ci        word2char((I)[3], (C+12))
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ci# define E_SEED(T0, T1, X1, X2, X3, X4, rbase)   \
100e1051a39Sopenharmony_ci        (T0) = (X3) ^ (ks->data)[(rbase)];       \
101e1051a39Sopenharmony_ci        (T1) = (X4) ^ (ks->data)[(rbase)+1];     \
102e1051a39Sopenharmony_ci        (T1) ^= (T0);                            \
103e1051a39Sopenharmony_ci        (T1) = G_FUNC((T1));                     \
104e1051a39Sopenharmony_ci        (T0) = ((T0) + (T1)) & 0xffffffff;       \
105e1051a39Sopenharmony_ci        (T0) = G_FUNC((T0));                     \
106e1051a39Sopenharmony_ci        (T1) = ((T1) + (T0)) & 0xffffffff;       \
107e1051a39Sopenharmony_ci        (T1) = G_FUNC((T1));                     \
108e1051a39Sopenharmony_ci        (T0) = ((T0) + (T1)) & 0xffffffff;       \
109e1051a39Sopenharmony_ci        (X1) ^= (T0);                            \
110e1051a39Sopenharmony_ci        (X2) ^= (T1)
111e1051a39Sopenharmony_ci
112e1051a39Sopenharmony_ci#endif                          /* OSSL_CRYPTO_SEED_LOCAL_H */
113