Rizin
unix-like reverse engineering framework and cli tools
aes-internal.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2002, 2013 Niels Möller
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 /* aes-internal.h
5 
6  The aes/rijndael block cipher.
7 
8  Copyright (C) 2001, 2013 Niels Möller
9 
10  This file is part of GNU Nettle.
11 
12  GNU Nettle is free software: you can redistribute it and/or
13  modify it under the terms of either:
14 
15  * the GNU Lesser General Public License as published by the Free
16  Software Foundation; either version 3 of the License, or (at your
17  option) any later version.
18 
19  or
20 
21  * the GNU General Public License as published by the Free
22  Software Foundation; either version 2 of the License, or (at your
23  option) any later version.
24 
25  or both in parallel, as here.
26 
27  GNU Nettle is distributed in the hope that it will be useful,
28  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  General Public License for more details.
31 
32  You should have received copies of the GNU General Public License and
33  the GNU Lesser General Public License along with this program. If
34  not, see http://www.gnu.org/licenses/.
35 */
36 
37 #ifndef NETTLE_AES_INTERNAL_H_INCLUDED
38 #define NETTLE_AES_INTERNAL_H_INCLUDED
39 
40 #include "aes.h"
41 
42 /* Define to use only small tables. */
43 #ifndef AES_SMALL
44 # define AES_SMALL 0
45 #endif
46 
47 #if AES_SMALL
48 # define AES_TABLE_SIZE 1
49 #else
50 # define AES_TABLE_SIZE 4
51 #endif
52 
53 struct aes_table
54 {
55  uint8_t sbox[0x100];
57 };
58 
59 void
60 _nettle_aes_set_key(unsigned nr, unsigned nk,
61  uint32_t *subkeys, const uint8_t *key);
62 
63 void
64 _nettle_aes_invert(unsigned rounds, uint32_t *dst, const uint32_t *src);
65 
66 void
67 _nettle_aes_encrypt(unsigned rounds, const uint32_t *keys,
68  const struct aes_table *T,
69  size_t length, uint8_t *dst,
70  const uint8_t *src);
71 
72 void
73 _nettle_aes_decrypt(unsigned rounds, const uint32_t *keys,
74  const struct aes_table *T,
75  size_t length, uint8_t *dst,
76  const uint8_t *src);
77 
78 /* Macros */
79 /* Get the byte with index 0, 1, 2 and 3 */
80 #define B0(x) ((x) & 0xff)
81 #define B1(x) (((x) >> 8) & 0xff)
82 #define B2(x) (((x) >> 16) & 0xff)
83 #define B3(x) (((x) >> 24) & 0xff)
84 
85 #define SUBBYTE(x, box) ((uint32_t)(box)[B0(x)] \
86  | ((uint32_t)(box)[B1(x)] << 8) \
87  | ((uint32_t)(box)[B2(x)] << 16) \
88  | ((uint32_t)(box)[B3(x)] << 24))
89 
90 #define AES_ROUND(T, w0, w1, w2, w3, k) \
91 (( T->table[0][ B0(w0) ] \
92  ^ T->table[1][ B1(w1) ] \
93  ^ T->table[2][ B2(w2) ] \
94  ^ T->table[3][ B3(w3) ]) ^ (k))
95 
96 #define AES_FINAL_ROUND(T, w0, w1, w2, w3, k) \
97 (( (uint32_t) T->sbox[ B0(w0) ] \
98  | ((uint32_t) T->sbox[ B1(w1) ] << 8) \
99  | ((uint32_t) T->sbox[ B2(w2) ] << 16) \
100  | ((uint32_t) T->sbox[ B3(w3) ] << 24)) ^ (k))
101 
102 extern const struct aes_table _nettle_aes_encrypt_table;
103 #define aes_sbox (_nettle_aes_encrypt_table.sbox)
104 extern const struct aes_table _nettle_aes_decrypt_table;
105 
106 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */
#define T(op)
const struct aes_table _nettle_aes_decrypt_table
void _nettle_aes_encrypt(unsigned rounds, const uint32_t *keys, const struct aes_table *T, size_t length, uint8_t *dst, const uint8_t *src)
#define AES_TABLE_SIZE
Definition: aes-internal.h:50
const struct aes_table _nettle_aes_encrypt_table
void _nettle_aes_decrypt(unsigned rounds, const uint32_t *keys, const struct aes_table *T, size_t length, uint8_t *dst, const uint8_t *src)
void _nettle_aes_invert(unsigned rounds, uint32_t *dst, const uint32_t *src)
void _nettle_aes_set_key(unsigned nr, unsigned nk, uint32_t *subkeys, const uint8_t *key)
lzma_index * src
Definition: index.h:567
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
char * dst
Definition: lz4.h:724
static struct @218 keys[]
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
uint32_t table[AES_TABLE_SIZE][0x100]
Definition: aes-internal.h:56