|
| 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 | + |
| 9 | +#ifndef AES_H |
| 10 | +#define AES_H |
| 11 | + |
| 12 | +/*************************** HEADER FILES ***************************/ |
| 13 | +#include <stddef.h> |
| 14 | + |
| 15 | +/****************************** MACROS ******************************/ |
| 16 | +#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time |
| 17 | + |
| 18 | +/**************************** DATA TYPES ****************************/ |
| 19 | +typedef unsigned char BYTE; // 8-bit byte |
| 20 | +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines |
| 21 | + |
| 22 | +/*********************** FUNCTION DECLARATIONS **********************/ |
| 23 | +/////////////////// |
| 24 | +// AES |
| 25 | +/////////////////// |
| 26 | +// Key setup must be done before any AES en/de-cryption functions can be used. |
| 27 | +// 128 bits = 16 字节 |
| 28 | +// 192 bits = 24 字节 |
| 29 | +// 256 bits = 32 字节 |
| 30 | +void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits |
| 31 | + WORD w[], // Output key schedule to be used later |
| 32 | + int keysize); // Bit length of the key, 128, 192, or 256 |
| 33 | + |
| 34 | +void aes_encrypt(const BYTE in[], // 16 bytes of plaintext |
| 35 | + BYTE out[], // 16 bytes of ciphertext |
| 36 | + const WORD key[], // From the key setup |
| 37 | + int keysize); // Bit length of the key, 128, 192, or 256 |
| 38 | + |
| 39 | +void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext |
| 40 | + BYTE out[], // 16 bytes of plaintext |
| 41 | + const WORD key[], // From the key setup |
| 42 | + int keysize); // Bit length of the key, 128, 192, or 256 |
| 43 | + |
| 44 | +/////////////////// |
| 45 | +// AES - CBC |
| 46 | +/////////////////// |
| 47 | +void aes_encrypt_cbc(const BYTE in[], // Plaintext |
| 48 | + size_t in_len, const BYTE * lastBlock, // Must be a multiple of AES_BLOCK_SIZE |
| 49 | + BYTE out[], // Ciphertext, same length as plaintext |
| 50 | + const WORD key[], // From the key setup |
| 51 | + int keysize, // Bit length of the key, 128, 192, or 256 |
| 52 | + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long |
| 53 | + |
| 54 | +void aes_decrypt_cbc(const BYTE in[], |
| 55 | + size_t in_len, |
| 56 | + BYTE out[], |
| 57 | + const WORD key[], |
| 58 | + int keysize, |
| 59 | + const BYTE iv[]); |
| 60 | + |
| 61 | +//// Only output the CBC-MAC of the input. |
| 62 | +//int aes_encrypt_cbc_mac(const BYTE in[], // plaintext |
| 63 | +// size_t in_len, // Must be a multiple of AES_BLOCK_SIZE |
| 64 | +// BYTE out[], // Output MAC |
| 65 | +// const WORD key[], // From the key setup |
| 66 | +// int keysize, // Bit length of the key, 128, 192, or 256 |
| 67 | +// const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long |
| 68 | + |
| 69 | +/////////////////// |
| 70 | +// AES - CTR |
| 71 | +/////////////////// |
| 72 | +//void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE |
| 73 | +// int counter_size); // Bytes of the IV used for counting (low end) |
| 74 | + |
| 75 | +void aes_encrypt_ctr(const BYTE in[], // Plaintext |
| 76 | + size_t in_len, // Any byte length |
| 77 | + BYTE out[], // Ciphertext, same length as plaintext |
| 78 | + const WORD key[], // From the key setup |
| 79 | + int keysize, // Bit length of the key, 128, 192, or 256 |
| 80 | + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long |
| 81 | + |
| 82 | +void aes_decrypt_ctr(const BYTE in[], // Ciphertext |
| 83 | + size_t in_len, // Any byte length |
| 84 | + BYTE out[], // Plaintext, same length as ciphertext |
| 85 | + const WORD key[], // From the key setup |
| 86 | + int keysize, // Bit length of the key, 128, 192, or 256 |
| 87 | + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long |
| 88 | + |
| 89 | +/////////////////// |
| 90 | +// AES - CCM |
| 91 | +/////////////////// |
| 92 | +// Returns True if the input parameters do not violate any constraint. |
| 93 | +int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext. |
| 94 | + WORD plaintext_len, // IN - Plaintext length. |
| 95 | + const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption. |
| 96 | + unsigned short associated_data_len, // IN - Associated Data length in bytes. |
| 97 | + const BYTE nonce[], // IN - The Nonce to be used for encryption. |
| 98 | + unsigned short nonce_len, // IN - Nonce length in bytes. |
| 99 | + BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC. |
| 100 | + WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len. |
| 101 | + WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16. |
| 102 | + const BYTE key[], // IN - The AES key for encryption. |
| 103 | + int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256. |
| 104 | + |
| 105 | +// Returns True if the input parameters do not violate any constraint. |
| 106 | +// Use mac_auth to ensure decryption/validation was preformed correctly. |
| 107 | +// If authentication does not succeed, the plaintext is zeroed out. To overwride |
| 108 | +// this, call with mac_auth = NULL. The proper proceedure is to decrypt with |
| 109 | +// authentication enabled (mac_auth != NULL) and make a second call to that |
| 110 | +// ignores authentication explicitly if the first call failes. |
| 111 | +int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC. |
| 112 | + WORD ciphertext_len, // IN - Ciphertext length in bytes. |
| 113 | + const BYTE assoc[], // IN - The Associated Data, required for authentication. |
| 114 | + unsigned short assoc_len, // IN - Associated Data length in bytes. |
| 115 | + const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption. |
| 116 | + unsigned short nonce_len, // IN - Nonce length in bytes. |
| 117 | + BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len. |
| 118 | + WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len . |
| 119 | + WORD mac_len, // IN - The length of the MAC that was calculated. |
| 120 | + int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication. |
| 121 | + const BYTE key[], // IN - The AES key for decryption. |
| 122 | + int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256. |
| 123 | + |
| 124 | +#endif // AES_H |
0 commit comments