axle OS
x86_32 UNIX-like hobby OS
aes.h
1 #ifndef AES_H
2 #define AES_H
3 
4 #include <std/std.h>
5 
6 #define AES_BLOCK_SIZE 16
7 
8 typedef unsigned char BYTE; // 8-bit byte
9 typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
10 
11 
12 void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
13  WORD w[], // Output key schedule to be used later
14  int keysize); // Bit length of the key, 128, 192, or 256
15 
16 void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
17  BYTE out[], // 16 bytes of ciphertext
18  const WORD key[], // From the key setup
19  int keysize); // Bit length of the key, 128, 192, or 256
20 
21 void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
22  BYTE out[], // 16 bytes of plaintext
23  const WORD key[], // From the key setup
24  int keysize); // Bit length of the key, 128, 192, or 256
25 
26 int aes_encrypt_cbc(const BYTE in[], // Plaintext
27  size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
28  BYTE out[], // Ciphertext, same length as plaintext
29  const WORD key[], // From the key setup
30  int keysize, // Bit length of the key, 128, 192, or 256
31  const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
32 
33 // Only output the CBC-MAC of the input.
34 int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
35  size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
36  BYTE out[], // Output MAC
37  const WORD key[], // From the key setup
38  int keysize, // Bit length of the key, 128, 192, or 256
39  const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
40 
41 void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE
42  int counter_size); // Bytes of the IV used for counting (low end)
43 
44 void aes_encrypt_ctr(const BYTE in[], // Plaintext
45  size_t in_len, // Any byte length
46  BYTE out[], // Ciphertext, same length as plaintext
47  const WORD key[], // From the key setup
48  int keysize, // Bit length of the key, 128, 192, or 256
49  const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
50 
51 void aes_decrypt_ctr(const BYTE in[], // Ciphertext
52  size_t in_len, // Any byte length
53  BYTE out[], // Plaintext, same length as ciphertext
54  const WORD key[], // From the key setup
55  int keysize, // Bit length of the key, 128, 192, or 256
56  const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
57 
58 int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
59  WORD plaintext_len, // IN - Plaintext length.
60  const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
61  unsigned short associated_data_len, // IN - Associated Data length in bytes.
62  const BYTE nonce[], // IN - The Nonce to be used for encryption.
63  unsigned short nonce_len, // IN - Nonce length in bytes.
64  BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
65  WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
66  WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
67  const BYTE key[], // IN - The AES key for encryption.
68  int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
69 
70 int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
71  WORD ciphertext_len, // IN - Ciphertext length in bytes.
72  const BYTE assoc[], // IN - The Associated Data, required for authentication.
73  unsigned short assoc_len, // IN - Associated Data length in bytes.
74  const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
75  unsigned short nonce_len, // IN - Nonce length in bytes.
76  BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
77  WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
78  WORD mac_len, // IN - The length of the MAC that was calculated.
79  int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
80  const BYTE key[], // IN - The AES key for decryption.
81  int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
82 
83 
84 int aes_test();
85 int aes_ecb_test();
86 int aes_cbc_test();
87 int aes_ctr_test();
88 int aes_ccm_test();
89 
90 #endif // AES_H