-
Notifications
You must be signed in to change notification settings - Fork 5
/
aes256.h
90 lines (76 loc) · 2.59 KB
/
aes256.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// A compact byte-oriented AES-256 implementation.
// All lookup tables replaced with 'on the fly' calculations.
//
// Copyright (c) 2007-2011 Literatecode, http://www.literatecode.com
// Copyright (c) 2022 Ilia Levin (ilia@levin.sg)
//
// Other contributors: Hal Finney.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef AES256_H__
#define AES256_H__ 1
#ifndef uint8_t
#define uint8_t unsigned char
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define AES_SUCCESS (0)
#define AES_ERROR (1)
typedef struct aes256_key_t { uint8_t raw[32]; } aes256_key_t;
typedef struct aes256_blk_t { uint8_t raw[16]; } aes256_blk_t;
typedef struct aes256_context_t {
aes256_key_t key;
aes256_key_t enckey;
aes256_key_t deckey;
} aes256_context_t;
/// @function aes256_init
/// @brief Initialize a context structure.
/// @param[in,out] ctx Pointer to a pre-allocated context structure.
/// @param[in] key Pointer to a key initialized buffer.
/// @return AES_SUCCESS on success, AES_ERROR on failure.
///
uint8_t aes256_init(
aes256_context_t *ctx,
aes256_key_t *key
);
/// @brief Clear the context structure.
/// @param[in,out] ctx Pointer to a context structure.
/// @return AES_SUCCESS on success, AES_ERROR on failure.
///
uint8_t aes256_done(
aes256_context_t *ctx
);
/// @brief Encrypt a single data block in place.
/// @param[in] ctx Pointer to an initialized context structure.
/// @param[in,out] buf Plaintext in, ciphertext out.
/// @return AES_SUCCESS on success, AES_ERROR on failure.
///
uint8_t aes256_encrypt_ecb(
aes256_context_t *ctx,
aes256_blk_t *buf
);
/// @brief Decrypt a single data block in place.
/// @param[in] ctx Pointer to an initialized context structure.
/// @param[in,out] buf Ciphertext in, plaintext out.
/// @return AES_SUCCESS on success, AES_ERROR on failure.
///
uint8_t aes256_decrypt_ecb(
aes256_context_t *ctx,
aes256_blk_t *buf
);
#ifdef __cplusplus
}
#endif
#endif // AES256_H__