]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - ext/olm/lib/crypto-algorithms/aes.h
changes to olm and esp
[matrix_esp_thesis] / ext / olm / lib / crypto-algorithms / aes.h
index 84e53550fe1e9afcd3c54a7b332022db16f2d431..6d570f45e64c786d2a6354d003268101a5bb96b5 100644 (file)
-/*********************************************************************
-* Filename:   aes.h
-* Author:     Brad Conte (brad AT bradconte.com)
-* Copyright:
-* Disclaimer: This code is presented "as is" without any guarantees.
-* Details:    Defines the API for the corresponding AES implementation.
-*********************************************************************/
-
-#ifndef AES_H
-#define AES_H
-
-/*************************** HEADER FILES ***************************/
-#include <stddef.h>
-
-/****************************** MACROS ******************************/
-#define AES_BLOCK_SIZE 16               // AES operates on 16 bytes at a time
-
-/**************************** DATA TYPES ****************************/
-typedef unsigned char BYTE;            // 8-bit byte
-typedef unsigned int WORD;             // 32-bit word, change to "long" for 16-bit machines
-
-/*********************** FUNCTION DECLARATIONS **********************/
-///////////////////
-// AES
-///////////////////
-// Key setup must be done before any AES en/de-cryption functions can be used.
-void _olm_aes_key_setup(const BYTE key[],          // The key, must be 128, 192, or 256 bits
-                   WORD w[],                  // Output key schedule to be used later
-                   int keysize);              // Bit length of the key, 128, 192, or 256
-
-void _olm_aes_encrypt(const BYTE in[],             // 16 bytes of plaintext
-                 BYTE out[],                  // 16 bytes of ciphertext
-                 const WORD key[],            // From the key setup
-                 int keysize);                // Bit length of the key, 128, 192, or 256
-
-void _olm_aes_decrypt(const BYTE in[],             // 16 bytes of ciphertext
-                 BYTE out[],                  // 16 bytes of plaintext
-                 const WORD key[],            // From the key setup
-                 int keysize);                // Bit length of the key, 128, 192, or 256
-
-///////////////////
-// AES - CBC
-///////////////////
-int _olm_aes_encrypt_cbc(const BYTE in[],          // Plaintext
-                    size_t in_len,            // Must be a multiple of AES_BLOCK_SIZE
-                    BYTE out[],               // Ciphertext, same length as plaintext
-                    const WORD key[],         // From the key setup
-                    int keysize,              // Bit length of the key, 128, 192, or 256
-                    const BYTE iv[]);         // IV, must be AES_BLOCK_SIZE bytes long
-
-// Only output the CBC-MAC of the input.
-int _olm_aes_encrypt_cbc_mac(const BYTE in[],      // plaintext
-                        size_t in_len,        // Must be a multiple of AES_BLOCK_SIZE
-                        BYTE out[],           // Output MAC
-                        const WORD key[],     // From the key setup
-                        int keysize,          // Bit length of the key, 128, 192, or 256
-                        const BYTE iv[]);     // IV, must be AES_BLOCK_SIZE bytes long
-
-///////////////////
-// AES - CTR
-///////////////////
-void increment_iv(BYTE iv[],                  // Must be a multiple of AES_BLOCK_SIZE
-                  int counter_size);          // Bytes of the IV used for counting (low end)
-
-void _olm_aes_encrypt_ctr(const BYTE in[],         // Plaintext
-                     size_t in_len,           // Any byte length
-                     BYTE out[],              // Ciphertext, same length as plaintext
-                     const WORD key[],        // From the key setup
-                     int keysize,             // Bit length of the key, 128, 192, or 256
-                     const BYTE iv[]);        // IV, must be AES_BLOCK_SIZE bytes long
-
-void _olm_aes_decrypt_ctr(const BYTE in[],         // Ciphertext
-                     size_t in_len,           // Any byte length
-                     BYTE out[],              // Plaintext, same length as ciphertext
-                     const WORD key[],        // From the key setup
-                     int keysize,             // Bit length of the key, 128, 192, or 256
-                     const BYTE iv[]);        // IV, must be AES_BLOCK_SIZE bytes long
-
-///////////////////
-// AES - CCM
-///////////////////
-// Returns True if the input parameters do not violate any constraint.
-int _olm_aes_encrypt_ccm(const BYTE plaintext[],              // IN  - Plaintext.
-                    WORD plaintext_len,                  // IN  - Plaintext length.
-                    const BYTE associated_data[],        // IN  - Associated Data included in authentication, but not encryption.
-                    unsigned short associated_data_len,  // IN  - Associated Data length in bytes.
-                    const BYTE nonce[],                  // IN  - The Nonce to be used for encryption.
-                    unsigned short nonce_len,            // IN  - Nonce length in bytes.
-                    BYTE ciphertext[],                   // OUT - Ciphertext, a concatination of the plaintext and the MAC.
-                    WORD *ciphertext_len,                // OUT - The length of the ciphertext, always plaintext_len + mac_len.
-                    WORD mac_len,                        // IN  - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
-                    const BYTE key[],                    // IN  - The AES key for encryption.
-                    int keysize);                        // IN  - The length of the key in bits. Valid values are 128, 192, 256.
-
-// Returns True if the input parameters do not violate any constraint.
-// Use mac_auth to ensure decryption/validation was preformed correctly.
-// If authentication does not succeed, the plaintext is zeroed out. To overwride
-// this, call with mac_auth = NULL. The proper proceedure is to decrypt with
-// authentication enabled (mac_auth != NULL) and make a second call to that
-// ignores authentication explicitly if the first call failes.
-int _olm_aes_decrypt_ccm(const BYTE ciphertext[],             // IN  - Ciphertext, the concatination of encrypted plaintext and MAC.
-                    WORD ciphertext_len,                 // IN  - Ciphertext length in bytes.
-                    const BYTE assoc[],                  // IN  - The Associated Data, required for authentication.
-                    unsigned short assoc_len,            // IN  - Associated Data length in bytes.
-                    const BYTE nonce[],                  // IN  - The Nonce to use for decryption, same one as for encryption.
-                    unsigned short nonce_len,            // IN  - Nonce length in bytes.
-                    BYTE plaintext[],                    // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
-                    WORD *plaintext_len,                 // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
-                    WORD mac_len,                        // IN  - The length of the MAC that was calculated.
-                    int *mac_auth,                       // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
-                    const BYTE key[],                    // IN  - The AES key for decryption.
-                    int keysize);                        // IN  - The length of the key in BITS. Valid values are 128, 192, 256.
-
-///////////////////
-// Test functions
-///////////////////
-int aes_test();
-int aes_ecb_test();
-int aes_cbc_test();
-int aes_ctr_test();
-int aes_ccm_test();
-
-#endif   // AES_H
+/*********************************************************************\r
+* Filename:   aes.h\r
+* Author:     Brad Conte (brad AT bradconte.com)\r
+* Copyright:\r
+* Disclaimer: This code is presented "as is" without any guarantees.\r
+* Details:    Defines the API for the corresponding AES implementation.\r
+*********************************************************************/\r
+\r
+#ifndef AES_H\r
+#define AES_H\r
+\r
+/*************************** HEADER FILES ***************************/\r
+#include <stddef.h>\r
+\r
+/****************************** MACROS ******************************/\r
+#define AES_BLOCK_SIZE 16               // AES operates on 16 bytes at a time\r
+\r
+/**************************** DATA TYPES ****************************/\r
+typedef unsigned char BYTE;            // 8-bit byte\r
+typedef unsigned int WORD;             // 32-bit word, change to "long" for 16-bit machines\r
+\r
+/*********************** FUNCTION DECLARATIONS **********************/\r
+///////////////////\r
+// AES\r
+///////////////////\r
+// Key setup must be done before any AES en/de-cryption functions can be used.\r
+void _olm_aes_key_setup(const BYTE key[],          // The key, must be 128, 192, or 256 bits\r
+                   WORD w[],                  // Output key schedule to be used later\r
+                   int keysize);              // Bit length of the key, 128, 192, or 256\r
+\r
+void _olm_aes_encrypt(const BYTE in[],             // 16 bytes of plaintext\r
+                 BYTE out[],                  // 16 bytes of ciphertext\r
+                 const WORD key[],            // From the key setup\r
+                 int keysize);                // Bit length of the key, 128, 192, or 256\r
+\r
+void _olm_aes_decrypt(const BYTE in[],             // 16 bytes of ciphertext\r
+                 BYTE out[],                  // 16 bytes of plaintext\r
+                 const WORD key[],            // From the key setup\r
+                 int keysize);                // Bit length of the key, 128, 192, or 256\r
+\r
+///////////////////\r
+// AES - CBC\r
+///////////////////\r
+int _olm_aes_encrypt_cbc(const BYTE in[],          // Plaintext\r
+                    size_t in_len,            // Must be a multiple of AES_BLOCK_SIZE\r
+                    BYTE out[],               // Ciphertext, same length as plaintext\r
+                    const WORD key[],         // From the key setup\r
+                    int keysize,              // Bit length of the key, 128, 192, or 256\r
+                    const BYTE iv[]);         // IV, must be AES_BLOCK_SIZE bytes long\r
+\r
+// Only output the CBC-MAC of the input.\r
+int _olm_aes_encrypt_cbc_mac(const BYTE in[],      // plaintext\r
+                        size_t in_len,        // Must be a multiple of AES_BLOCK_SIZE\r
+                        BYTE out[],           // Output MAC\r
+                        const WORD key[],     // From the key setup\r
+                        int keysize,          // Bit length of the key, 128, 192, or 256\r
+                        const BYTE iv[]);     // IV, must be AES_BLOCK_SIZE bytes long\r
+\r
+///////////////////\r
+// AES - CTR\r
+///////////////////\r
+void increment_iv(BYTE iv[],                  // Must be a multiple of AES_BLOCK_SIZE\r
+                  int counter_size);          // Bytes of the IV used for counting (low end)\r
+\r
+void _olm_aes_encrypt_ctr(const BYTE in[],         // Plaintext\r
+                     size_t in_len,           // Any byte length\r
+                     BYTE out[],              // Ciphertext, same length as plaintext\r
+                     const WORD key[],        // From the key setup\r
+                     int keysize,             // Bit length of the key, 128, 192, or 256\r
+                     const BYTE iv[]);        // IV, must be AES_BLOCK_SIZE bytes long\r
+\r
+void _olm_aes_decrypt_ctr(const BYTE in[],         // Ciphertext\r
+                     size_t in_len,           // Any byte length\r
+                     BYTE out[],              // Plaintext, same length as ciphertext\r
+                     const WORD key[],        // From the key setup\r
+                     int keysize,             // Bit length of the key, 128, 192, or 256\r
+                     const BYTE iv[]);        // IV, must be AES_BLOCK_SIZE bytes long\r
+\r
+///////////////////\r
+// AES - CCM\r
+///////////////////\r
+// Returns True if the input parameters do not violate any constraint.\r
+int _olm_aes_encrypt_ccm(const BYTE plaintext[],              // IN  - Plaintext.\r
+                    WORD plaintext_len,                  // IN  - Plaintext length.\r
+                    const BYTE associated_data[],        // IN  - Associated Data included in authentication, but not encryption.\r
+                    unsigned short associated_data_len,  // IN  - Associated Data length in bytes.\r
+                    const BYTE nonce[],                  // IN  - The Nonce to be used for encryption.\r
+                    unsigned short nonce_len,            // IN  - Nonce length in bytes.\r
+                    BYTE ciphertext[],                   // OUT - Ciphertext, a concatination of the plaintext and the MAC.\r
+                    WORD *ciphertext_len,                // OUT - The length of the ciphertext, always plaintext_len + mac_len.\r
+                    WORD mac_len,                        // IN  - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.\r
+                    const BYTE key[],                    // IN  - The AES key for encryption.\r
+                    int keysize);                        // IN  - The length of the key in bits. Valid values are 128, 192, 256.\r
+\r
+// Returns True if the input parameters do not violate any constraint.\r
+// Use mac_auth to ensure decryption/validation was preformed correctly.\r
+// If authentication does not succeed, the plaintext is zeroed out. To overwride\r
+// this, call with mac_auth = NULL. The proper proceedure is to decrypt with\r
+// authentication enabled (mac_auth != NULL) and make a second call to that\r
+// ignores authentication explicitly if the first call failes.\r
+int _olm_aes_decrypt_ccm(const BYTE ciphertext[],             // IN  - Ciphertext, the concatination of encrypted plaintext and MAC.\r
+                    WORD ciphertext_len,                 // IN  - Ciphertext length in bytes.\r
+                    const BYTE assoc[],                  // IN  - The Associated Data, required for authentication.\r
+                    unsigned short assoc_len,            // IN  - Associated Data length in bytes.\r
+                    const BYTE nonce[],                  // IN  - The Nonce to use for decryption, same one as for encryption.\r
+                    unsigned short nonce_len,            // IN  - Nonce length in bytes.\r
+                    BYTE plaintext[],                    // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.\r
+                    WORD *plaintext_len,                 // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .\r
+                    WORD mac_len,                        // IN  - The length of the MAC that was calculated.\r
+                    int *mac_auth,                       // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.\r
+                    const BYTE key[],                    // IN  - The AES key for decryption.\r
+                    int keysize);                        // IN  - The length of the key in BITS. Valid values are 128, 192, 256.\r
+\r
+///////////////////\r
+// Test functions\r
+///////////////////\r
+int aes_test();\r
+int aes_ecb_test();\r
+int aes_cbc_test();\r
+int aes_ctr_test();\r
+int aes_ccm_test();\r
+\r
+#endif   // AES_H\r