Debian Codegraph @

contrib/aes.h

raw symbols SCIP
1 /*********************************************************************
2 * Filename: aes.h
3 * Author: Brad Conte (brad AT bradconte.com)
4 * Copyright:
5 * Disclaimer: This code is presented "as is" without any guarantees.
6 * Details: Defines the API for the corresponding AES implementation.
7 *
8 * File copied from the project: https://github.com/B-Con/crypto-algorithms
9 * License (quoting from author's github project):
10 * This code is released into the public domain free of any restrictions. The author requests
11 * acknowledgement if the code is used, but does not require it. This code is provided free of any
12 * liability and without any quality claims by the author.
13 *
14 *********************************************************************/
15
16 #ifndef AES_H
17 #define AES_H
18
19 /*************************** HEADER FILES ***************************/
20 #include <stddef.h>
21 #include <stdint.h>
22
23 /****************************** MACROS ******************************/
24 #define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time
25
26 /*********************** FUNCTION DECLARATIONS **********************/
27 ///////////////////
28 // AES
29 ///////////////////
30 // Key setup must be done before any AES en/de-cryption functions can be used.
31 int aes_key_setup(const uint8_t key[], // The key, must be 128, 192, or 256 bits
32 uint32_t w[], // Output key schedule to be used later
33 int keysize); // Bit length of the key, 128, 192, or 256
34
35 void aes_encrypt(const uint8_t in[], // 16 bytes of plaintext
36 uint8_t out[], // 16 bytes of ciphertext
37 const uint32_t key[], // From the key setup
38 int keysize); // Bit length of the key, 128, 192, or 256
39
40 void aes_decrypt(const uint8_t in[], // 16 bytes of ciphertext
41 uint8_t out[], // 16 bytes of plaintext
42 const uint32_t key[], // From the key setup
43 int keysize); // Bit length of the key, 128, 192, or 256
44
45 ///////////////////
46 // AES - CBC
47 ///////////////////
48 int aes_encrypt_cbc(const uint8_t in[], // Plaintext
49 size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
50 uint8_t out[], // Ciphertext, same length as plaintext
51 const uint32_t key[], // From the key setup
52 int keysize, // Bit length of the key, 128, 192, or 256
53 const uint8_t iv[]); // IV, must be AES_BLOCK_SIZE bytes long
54
55 // Only output the CBC-MAC of the input.
56 int aes_encrypt_cbc_mac(const uint8_t in[], // plaintext
57 size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
58 uint8_t out[], // Output MAC
59 const uint32_t key[], // From the key setup
60 int keysize, // Bit length of the key, 128, 192, or 256
61 const uint8_t iv[]); // IV, must be AES_BLOCK_SIZE bytes long
62
63 ///////////////////
64 // AES - CTR
65 ///////////////////
66 void increment_iv(uint8_t iv[], // Must be a multiple of AES_BLOCK_SIZE
67 int counter_size); // Bytes of the IV used for counting (low end)
68
69 void aes_encrypt_ctr(const uint8_t in[], // Plaintext
70 size_t in_len, // Any byte length
71 uint8_t out[], // Ciphertext, same length as plaintext
72 const uint32_t key[], // From the key setup
73 int keysize, // Bit length of the key, 128, 192, or 256
74 const uint8_t iv[]); // IV, must be AES_BLOCK_SIZE bytes long
75
76 void aes_decrypt_ctr(const uint8_t in[], // Ciphertext
77 size_t in_len, // Any byte length
78 uint8_t out[], // Plaintext, same length as ciphertext
79 const uint32_t key[], // From the key setup
80 int keysize, // Bit length of the key, 128, 192, or 256
81 const uint8_t iv[]); // IV, must be AES_BLOCK_SIZE bytes long
82
83 ///////////////////
84 // AES - CCM
85 ///////////////////
86 // Returns True if the input parameters do not violate any constraint.
87 int aes_encrypt_ccm(const uint8_t plaintext[], // IN - Plaintext.
88 uint32_t plaintext_len, // IN - Plaintext length.
89 const uint8_t associated_data[], // IN - Associated Data included in authentication, but not encryption.
90 unsigned short associated_data_len, // IN - Associated Data length in bytes.
91 const uint8_t nonce[], // IN - The Nonce to be used for encryption.
92 unsigned short nonce_len, // IN - Nonce length in bytes.
93 uint8_t ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
94 uint32_t *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
95 uint32_t mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
96 const uint8_t key[], // IN - The AES key for encryption.
97 int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
98
99 // Returns True if the input parameters do not violate any constraint.
100 // Use mac_auth to ensure decryption/validation was preformed correctly.
101 // If authentication does not succeed, the plaintext is zeroed out. To overwride
102 // this, call with mac_auth = NULL. The proper proceedure is to decrypt with
103 // authentication enabled (mac_auth != NULL) and make a second call to that
104 // ignores authentication explicitly if the first call failes.
105 int aes_decrypt_ccm(const uint8_t ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
106 uint32_t ciphertext_len, // IN - Ciphertext length in bytes.
107 const uint8_t assoc[], // IN - The Associated Data, required for authentication.
108 unsigned short assoc_len, // IN - Associated Data length in bytes.
109 const uint8_t nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
110 unsigned short nonce_len, // IN - Nonce length in bytes.
111 uint8_t plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
112 uint32_t *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
113 uint32_t mac_len, // IN - The length of the MAC that was calculated.
114 int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
115 const uint8_t key[], // IN - The AES key for decryption.
116 int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
117
118 ///////////////////
119 // Test functions
120 ///////////////////
121 int aes_test(void);
122 int aes_ecb_test(void);
123 int aes_cbc_test(void);
124 int aes_ctr_test(void);
125 int aes_ccm_test(void);
126
127 #endif // AES_H