1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2010-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/* This header can move into provider when legacy support is removed */
11e1051a39Sopenharmony_ci#include <openssl/modes.h>
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
14e1051a39Sopenharmony_citypedef __int64 i64;
15e1051a39Sopenharmony_citypedef unsigned __int64 u64;
16e1051a39Sopenharmony_ci# define U64(C) C##UI64
17e1051a39Sopenharmony_ci#elif defined(__arch64__)
18e1051a39Sopenharmony_citypedef long i64;
19e1051a39Sopenharmony_citypedef unsigned long u64;
20e1051a39Sopenharmony_ci# define U64(C) C##UL
21e1051a39Sopenharmony_ci#else
22e1051a39Sopenharmony_citypedef long long i64;
23e1051a39Sopenharmony_citypedef unsigned long long u64;
24e1051a39Sopenharmony_ci# define U64(C) C##ULL
25e1051a39Sopenharmony_ci#endif
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_citypedef unsigned int u32;
28e1051a39Sopenharmony_citypedef unsigned char u8;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci#define STRICT_ALIGNMENT 1
31e1051a39Sopenharmony_ci#ifndef PEDANTIC
32e1051a39Sopenharmony_ci# if defined(__i386)    || defined(__i386__)    || \
33e1051a39Sopenharmony_ci     defined(__x86_64)  || defined(__x86_64__)  || \
34e1051a39Sopenharmony_ci     defined(_M_IX86)   || defined(_M_AMD64)    || defined(_M_X64) || \
35e1051a39Sopenharmony_ci     defined(__aarch64__)                       || \
36e1051a39Sopenharmony_ci     defined(__s390__)  || defined(__s390x__)
37e1051a39Sopenharmony_ci#  undef STRICT_ALIGNMENT
38e1051a39Sopenharmony_ci# endif
39e1051a39Sopenharmony_ci#endif
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci#if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
42e1051a39Sopenharmony_ci# if defined(__GNUC__) && __GNUC__>=2
43e1051a39Sopenharmony_ci#  if defined(__x86_64) || defined(__x86_64__)
44e1051a39Sopenharmony_ci#   define BSWAP8(x) ({ u64 ret_=(x);                   \
45e1051a39Sopenharmony_ci                        asm ("bswapq %0"                \
46e1051a39Sopenharmony_ci                        : "+r"(ret_));   ret_;          })
47e1051a39Sopenharmony_ci#   define BSWAP4(x) ({ u32 ret_=(x);                   \
48e1051a39Sopenharmony_ci                        asm ("bswapl %0"                \
49e1051a39Sopenharmony_ci                        : "+r"(ret_));   ret_;          })
50e1051a39Sopenharmony_ci#  elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
51e1051a39Sopenharmony_ci#   define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x);   \
52e1051a39Sopenharmony_ci                        asm ("bswapl %0; bswapl %1"     \
53e1051a39Sopenharmony_ci                        : "+r"(hi_),"+r"(lo_));         \
54e1051a39Sopenharmony_ci                        (u64)hi_<<32|lo_;               })
55e1051a39Sopenharmony_ci#   define BSWAP4(x) ({ u32 ret_=(x);                   \
56e1051a39Sopenharmony_ci                        asm ("bswapl %0"                \
57e1051a39Sopenharmony_ci                        : "+r"(ret_));   ret_;          })
58e1051a39Sopenharmony_ci#  elif defined(__aarch64__)
59e1051a39Sopenharmony_ci#   if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
60e1051a39Sopenharmony_ci       __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
61e1051a39Sopenharmony_ci#    define BSWAP8(x) ({ u64 ret_;                       \
62e1051a39Sopenharmony_ci                        asm ("rev %0,%1"                \
63e1051a39Sopenharmony_ci                        : "=r"(ret_) : "r"(x)); ret_;   })
64e1051a39Sopenharmony_ci#    define BSWAP4(x) ({ u32 ret_;                       \
65e1051a39Sopenharmony_ci                        asm ("rev %w0,%w1"              \
66e1051a39Sopenharmony_ci                        : "=r"(ret_) : "r"(x)); ret_;   })
67e1051a39Sopenharmony_ci#   endif
68e1051a39Sopenharmony_ci#  elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
69e1051a39Sopenharmony_ci#   define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x);   \
70e1051a39Sopenharmony_ci                        asm ("rev %0,%0; rev %1,%1"     \
71e1051a39Sopenharmony_ci                        : "+r"(hi_),"+r"(lo_));         \
72e1051a39Sopenharmony_ci                        (u64)hi_<<32|lo_;               })
73e1051a39Sopenharmony_ci#   define BSWAP4(x) ({ u32 ret_;                       \
74e1051a39Sopenharmony_ci                        asm ("rev %0,%1"                \
75e1051a39Sopenharmony_ci                        : "=r"(ret_) : "r"((u32)(x)));  \
76e1051a39Sopenharmony_ci                        ret_;                           })
77e1051a39Sopenharmony_ci#  endif
78e1051a39Sopenharmony_ci# elif defined(_MSC_VER)
79e1051a39Sopenharmony_ci#  if _MSC_VER>=1300
80e1051a39Sopenharmony_ci#   include <stdlib.h>
81e1051a39Sopenharmony_ci#   pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
82e1051a39Sopenharmony_ci#   define BSWAP8(x)    _byteswap_uint64((u64)(x))
83e1051a39Sopenharmony_ci#   define BSWAP4(x)    _byteswap_ulong((u32)(x))
84e1051a39Sopenharmony_ci#  elif defined(_M_IX86)
85e1051a39Sopenharmony_ci__inline u32 _bswap4(u32 val)
86e1051a39Sopenharmony_ci{
87e1051a39Sopenharmony_ci_asm mov eax, val _asm bswap eax}
88e1051a39Sopenharmony_ci#   define BSWAP4(x)    _bswap4(x)
89e1051a39Sopenharmony_ci#  endif
90e1051a39Sopenharmony_ci# endif
91e1051a39Sopenharmony_ci#endif
92e1051a39Sopenharmony_ci#if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
93e1051a39Sopenharmony_ci# define GETU32(p)       BSWAP4(*(const u32 *)(p))
94e1051a39Sopenharmony_ci# define PUTU32(p,v)     *(u32 *)(p) = BSWAP4(v)
95e1051a39Sopenharmony_ci#else
96e1051a39Sopenharmony_ci# define GETU32(p)       ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
97e1051a39Sopenharmony_ci# define PUTU32(p,v)     ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
98e1051a39Sopenharmony_ci#endif
99e1051a39Sopenharmony_ci/*- GCM definitions */ typedef struct {
100e1051a39Sopenharmony_ci    u64 hi, lo;
101e1051a39Sopenharmony_ci} u128;
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci#ifdef  TABLE_BITS
104e1051a39Sopenharmony_ci# undef  TABLE_BITS
105e1051a39Sopenharmony_ci#endif
106e1051a39Sopenharmony_ci/*
107e1051a39Sopenharmony_ci * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
108e1051a39Sopenharmony_ci * never be set to 8 [or 1]. For further information see gcm128.c.
109e1051a39Sopenharmony_ci */
110e1051a39Sopenharmony_ci#define TABLE_BITS 4
111e1051a39Sopenharmony_ci
112e1051a39Sopenharmony_cistruct gcm128_context {
113e1051a39Sopenharmony_ci    /* Following 6 names follow names in GCM specification */
114e1051a39Sopenharmony_ci    union {
115e1051a39Sopenharmony_ci        u64 u[2];
116e1051a39Sopenharmony_ci        u32 d[4];
117e1051a39Sopenharmony_ci        u8 c[16];
118e1051a39Sopenharmony_ci        size_t t[16 / sizeof(size_t)];
119e1051a39Sopenharmony_ci    } Yi, EKi, EK0, len, Xi, H;
120e1051a39Sopenharmony_ci    /*
121e1051a39Sopenharmony_ci     * Relative position of Xi, H and pre-computed Htable is used in some
122e1051a39Sopenharmony_ci     * assembler modules, i.e. don't change the order!
123e1051a39Sopenharmony_ci     */
124e1051a39Sopenharmony_ci#if TABLE_BITS==8
125e1051a39Sopenharmony_ci    u128 Htable[256];
126e1051a39Sopenharmony_ci#else
127e1051a39Sopenharmony_ci    u128 Htable[16];
128e1051a39Sopenharmony_ci    void (*gmult) (u64 Xi[2], const u128 Htable[16]);
129e1051a39Sopenharmony_ci    void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
130e1051a39Sopenharmony_ci                   size_t len);
131e1051a39Sopenharmony_ci#endif
132e1051a39Sopenharmony_ci    unsigned int mres, ares;
133e1051a39Sopenharmony_ci    block128_f block;
134e1051a39Sopenharmony_ci    void *key;
135e1051a39Sopenharmony_ci#if !defined(OPENSSL_SMALL_FOOTPRINT)
136e1051a39Sopenharmony_ci    unsigned char Xn[48];
137e1051a39Sopenharmony_ci#endif
138e1051a39Sopenharmony_ci};
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_ci/*
141e1051a39Sopenharmony_ci * The maximum permitted number of cipher blocks per data unit in XTS mode.
142e1051a39Sopenharmony_ci * Reference IEEE Std 1619-2018.
143e1051a39Sopenharmony_ci */
144e1051a39Sopenharmony_ci#define XTS_MAX_BLOCKS_PER_DATA_UNIT            (1<<20)
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_cistruct xts128_context {
147e1051a39Sopenharmony_ci    void *key1, *key2;
148e1051a39Sopenharmony_ci    block128_f block1, block2;
149e1051a39Sopenharmony_ci};
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_cistruct ccm128_context {
152e1051a39Sopenharmony_ci    union {
153e1051a39Sopenharmony_ci        u64 u[2];
154e1051a39Sopenharmony_ci        u8 c[16];
155e1051a39Sopenharmony_ci    } nonce, cmac;
156e1051a39Sopenharmony_ci    u64 blocks;
157e1051a39Sopenharmony_ci    block128_f block;
158e1051a39Sopenharmony_ci    void *key;
159e1051a39Sopenharmony_ci};
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCB
162e1051a39Sopenharmony_ci
163e1051a39Sopenharmony_citypedef union {
164e1051a39Sopenharmony_ci    u64 a[2];
165e1051a39Sopenharmony_ci    unsigned char c[16];
166e1051a39Sopenharmony_ci} OCB_BLOCK;
167e1051a39Sopenharmony_ci# define ocb_block16_xor(in1,in2,out) \
168e1051a39Sopenharmony_ci    ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
169e1051a39Sopenharmony_ci      (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
170e1051a39Sopenharmony_ci# if STRICT_ALIGNMENT
171e1051a39Sopenharmony_ci#  define ocb_block16_xor_misaligned(in1,in2,out) \
172e1051a39Sopenharmony_ci    ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
173e1051a39Sopenharmony_ci# else
174e1051a39Sopenharmony_ci#  define ocb_block16_xor_misaligned ocb_block16_xor
175e1051a39Sopenharmony_ci# endif
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_cistruct ocb128_context {
178e1051a39Sopenharmony_ci    /* Need both encrypt and decrypt key schedules for decryption */
179e1051a39Sopenharmony_ci    block128_f encrypt;
180e1051a39Sopenharmony_ci    block128_f decrypt;
181e1051a39Sopenharmony_ci    void *keyenc;
182e1051a39Sopenharmony_ci    void *keydec;
183e1051a39Sopenharmony_ci    ocb128_f stream;    /* direction dependent */
184e1051a39Sopenharmony_ci    /* Key dependent variables. Can be reused if key remains the same */
185e1051a39Sopenharmony_ci    size_t l_index;
186e1051a39Sopenharmony_ci    size_t max_l_index;
187e1051a39Sopenharmony_ci    OCB_BLOCK l_star;
188e1051a39Sopenharmony_ci    OCB_BLOCK l_dollar;
189e1051a39Sopenharmony_ci    OCB_BLOCK *l;
190e1051a39Sopenharmony_ci    /* Must be reset for each session */
191e1051a39Sopenharmony_ci    struct {
192e1051a39Sopenharmony_ci        u64 blocks_hashed;
193e1051a39Sopenharmony_ci        u64 blocks_processed;
194e1051a39Sopenharmony_ci        OCB_BLOCK offset_aad;
195e1051a39Sopenharmony_ci        OCB_BLOCK sum;
196e1051a39Sopenharmony_ci        OCB_BLOCK offset;
197e1051a39Sopenharmony_ci        OCB_BLOCK checksum;
198e1051a39Sopenharmony_ci    } sess;
199e1051a39Sopenharmony_ci};
200e1051a39Sopenharmony_ci#endif                          /* OPENSSL_NO_OCB */
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SIV
203e1051a39Sopenharmony_ci
204e1051a39Sopenharmony_ci#define SIV_LEN 16
205e1051a39Sopenharmony_ci
206e1051a39Sopenharmony_citypedef union siv_block_u {
207e1051a39Sopenharmony_ci    uint64_t word[SIV_LEN/sizeof(uint64_t)];
208e1051a39Sopenharmony_ci    unsigned char byte[SIV_LEN];
209e1051a39Sopenharmony_ci} SIV_BLOCK;
210e1051a39Sopenharmony_ci
211e1051a39Sopenharmony_cistruct siv128_context {
212e1051a39Sopenharmony_ci    /* d stores intermediate results of S2V; it corresponds to D from the
213e1051a39Sopenharmony_ci       pseudocode in section 2.4 of RFC 5297. */
214e1051a39Sopenharmony_ci    SIV_BLOCK d;
215e1051a39Sopenharmony_ci    SIV_BLOCK tag;
216e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *cipher_ctx;
217e1051a39Sopenharmony_ci    EVP_MAC *mac;
218e1051a39Sopenharmony_ci    EVP_MAC_CTX *mac_ctx_init;
219e1051a39Sopenharmony_ci    int final_ret;
220e1051a39Sopenharmony_ci    int crypto_ok;
221e1051a39Sopenharmony_ci};
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_SIV */
224