/** * Digital Voice Modem - Host Software * GPLv2 Open Source. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * @package DVM / Host Software * */ // // Based on code from the C++ AES project. (https://github.com/SergeyBel/AES) // Licensed under the MIT License (https://opensource.org/license/MIT) // /* * Copyright (C) 2019 SergeyBel * Copyright (C) 2023 Bryan Biedenkapp N2PLL * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "Defines.h" #include "AESCrypto.h" using namespace crypto; #include #include #include #include #include // --------------------------------------------------------------------------- // Constants // --------------------------------------------------------------------------- const uint8_t SBOX[16][16] = { { 0x63U, 0x7CU, 0x77U, 0x7BU, 0xF2U, 0x6BU, 0x6FU, 0xC5U, 0x30U, 0x01U, 0x67U, 0x2BU, 0xFEU, 0xD7U, 0xABU, 0x76U }, { 0xCAU, 0x82U, 0xC9U, 0x7DU, 0xFAU, 0x59U, 0x47U, 0xF0U, 0xADU, 0xD4U, 0xA2U, 0xAFU, 0x9CU, 0xA4U, 0x72U, 0xC0U }, { 0xB7U, 0xFDU, 0x93U, 0x26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EBU, 0xBBU, 0x3CU, 0x83U, 0x53U, 0x99U, 0x61U }, { 0x17U, 0x2BU, 0x04U, 0x7EU, 0xBAU, 0x77U, 0xD6U, 0x26U, 0xE1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0CU, 0x7DU } }; // Galois Multiplication lookup tables static const uint8_t GF_MUL_TABLE[15][256] = { {}, {}, // mul 2 { 0x00U, 0x02U, 0x04U, 0x06U, 0x08U, 0x0AU, 0x0CU, 0x0EU, 0x10U, 0x12U, 0x14U, 0x16U, 0x18U, 0x1AU, 0x1CU, 0x1EU, 0x20U, 0x22U, 0x24U, 0x26U, 0x28U, 0x2AU, 0x2CU, 0x2EU, 0x30U, 0x32U, 0x34U, 0x36U, 0x38U, 0x3AU, 0x3CU, 0x3EU, 0x40U, 0x42U, 0x44U, 0x46U, 0x48U, 0x4AU, 0x4CU, 0x4EU, 0x50U, 0x52U, 0x54U, 0x56U, 0x58U, 0x5AU, 0x5CU, 0x5EU, 0x60U, 0x62U, 0x64U, 0x66U, 0x68U, 0x6AU, 0x6CU, 0x6EU, 0x70U, 0x72U, 0x74U, 0x76U, 0x78U, 0x7AU, 0x7CU, 0x7EU, 0x80U, 0x82U, 0x84U, 0x86U, 0x88U, 0x8AU, 0x8CU, 0x8EU, 0x90U, 0x92U, 0x94U, 0x96U, 0x98U, 0x9AU, 0x9CU, 0x9EU, 0xA0U, 0xA2U, 0xA4U, 0xA6U, 0xA8U, 0xAAU, 0xACU, 0xAEU, 0xB0U, 0xB2U, 0xB4U, 0xB6U, 0xB8U, 0xBAU, 0xBCU, 0xBEU, 0xC0U, 0xC2U, 0xC4U, 0xC6U, 0xC8U, 0xCAU, 0xCCU, 0xCEU, 0xD0U, 0xD2U, 0xD4U, 0xD6U, 0xD8U, 0xDAU, 0xDCU, 0xDEU, 0xE0U, 0xE2U, 0xE4U, 0xE6U, 0xE8U, 0xEAU, 0xECU, 0xEEU, 0xF0U, 0xF2U, 0xF4U, 0xF6U, 0xF8U,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, 0x5BU, 0x55U, 0x7FU, 0x71U, 0x63U, 0x6DU, 0xD7U, 0xD9U, 0xCBU, 0xC5U, 0xEFU, 0xE1U, 0xF3U, 0xFDU, 0xA7U, 0xA9U, 0xBBU, 0xB5U, 0x9FU, 0x91U, 0x83U, 0x8DU } }; // Circulant MDS matrix static const uint8_t CMDS[4][4] = { {2, 3, 1, 1}, {1, 2, 3, 1}, {1, 1, 2, 3}, {3, 1, 1, 2} }; // Inverse circulant MDS matrix static const uint8_t INV_CMDS[4][4] = { {14, 11, 13, 9}, {9, 14, 11, 13}, {13, 9, 14, 11}, {11, 13, 9, 14} }; // --------------------------------------------------------------------------- // Public Class Members // --------------------------------------------------------------------------- /// /// Initializes a new instance of the AES class. /// AES::AES(const AESKeyLength keyLength) { switch (keyLength) { case AESKeyLength::AES_128: this->m_Nk = 4; this->m_Nr = 10; break; case AESKeyLength::AES_192: this->m_Nk = 6; this->m_Nr = 12; break; case AESKeyLength::AES_256: this->m_Nk = 8; this->m_Nr = 14; break; } } /// /// /// /// /// /// /// uint8_t* AES::encryptECB(const uint8_t in[], uint32_t inLen, const uint8_t key[]) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { encryptBlock(in + i, out + i, roundKeys); } delete[] roundKeys; return out; } /// /// /// /// /// /// /// uint8_t* AES::decryptECB(const uint8_t in[], uint32_t inLen, const uint8_t key[]) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { decryptBlock(in + i, out + i, roundKeys); } delete[] roundKeys; return out; } /// /// /// /// /// /// /// /// uint8_t* AES::encryptCBC(const uint8_t in[], uint32_t inLen, const uint8_t key[], const uint8_t* iv) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t block[m_blockBytesLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); memcpy(block, iv, m_blockBytesLen); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { xorBlocks(block, in + i, block, m_blockBytesLen); encryptBlock(block, out + i, roundKeys); memcpy(block, out + i, m_blockBytesLen); } delete[] roundKeys; return out; } /// /// /// /// /// /// /// /// uint8_t* AES::decryptCBC(const uint8_t in[], uint32_t inLen, const uint8_t key[], const uint8_t *iv) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t block[m_blockBytesLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); memcpy(block, iv, m_blockBytesLen); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { decryptBlock(in + i, out + i, roundKeys); xorBlocks(block, out + i, out + i, m_blockBytesLen); memcpy(block, in + i, m_blockBytesLen); } delete[] roundKeys; return out; } /// /// /// /// /// /// /// /// uint8_t* AES::encryptCFB(const uint8_t in[], uint32_t inLen, const uint8_t key[], const uint8_t *iv) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t block[m_blockBytesLen]; uint8_t encryptedBlock[m_blockBytesLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); memcpy(block, iv, m_blockBytesLen); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { encryptBlock(block, encryptedBlock, roundKeys); xorBlocks(in + i, encryptedBlock, out + i, m_blockBytesLen); memcpy(block, out + i, m_blockBytesLen); } delete[] roundKeys; return out; } /// /// /// /// /// /// /// /// uint8_t* AES::decryptCFB(const uint8_t in[], uint32_t inLen, const uint8_t key[], const uint8_t *iv) { if (inLen % m_blockBytesLen != 0) { throw std::length_error("Plaintext length must be divisible by " + std::to_string(m_blockBytesLen)); } uint8_t* out = new uint8_t[inLen]; uint8_t block[m_blockBytesLen]; uint8_t encryptedBlock[m_blockBytesLen]; uint8_t* roundKeys = new uint8_t[4 * AES_NB * (m_Nr + 1)]; keyExpansion(key, roundKeys); memcpy(block, iv, m_blockBytesLen); for (uint32_t i = 0; i < inLen; i += m_blockBytesLen) { encryptBlock(block, encryptedBlock, roundKeys); xorBlocks(in + i, encryptedBlock, out + i, m_blockBytesLen); memcpy(block, in + i, m_blockBytesLen); } delete[] roundKeys; return out; } // --------------------------------------------------------------------------- // Private Class Members // --------------------------------------------------------------------------- /// /// /// /// void AES::subBytes(uint8_t state[4][AES_NB]) { for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { uint8_t t = state[i][j]; state[i][j] = SBOX[t / 16][t % 16]; } } } /// /// /// /// void AES::invSubBytes(uint8_t state[4][AES_NB]) { for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { uint8_t t = state[i][j]; state[i][j] = INV_SBOX[t / 16][t % 16]; } } } /// /// Shift row i on n positions. /// /// /// /// void AES::shiftRow(uint8_t state[4][AES_NB], uint32_t i, uint32_t n) { uint8_t tmp[AES_NB]; for (uint32_t j = 0; j < AES_NB; j++) { tmp[j] = state[i][(j + n) % AES_NB]; } memcpy(state[i], tmp, AES_NB * sizeof(uint8_t)); } /// /// /// /// void AES::shiftRows(uint8_t state[4][AES_NB]) { shiftRow(state, 1, 1); shiftRow(state, 2, 2); shiftRow(state, 3, 3); } /// /// /// /// void AES::invShiftRows(uint8_t state[4][AES_NB]) { shiftRow(state, 1, AES_NB - 1); shiftRow(state, 2, AES_NB - 2); shiftRow(state, 3, AES_NB - 3); } /// /// /// /// void AES::mixColumns(uint8_t state[4][AES_NB]) { uint8_t tempState[4][AES_NB]; for (size_t i = 0; i < 4; ++i) { memset(tempState[i], 0, 4); } for (size_t i = 0; i < 4; ++i) { for (size_t k = 0; k < 4; ++k) { for (size_t j = 0; j < 4; ++j) { if (CMDS[i][k] == 1) tempState[i][j] ^= state[k][j]; else tempState[i][j] ^= GF_MUL_TABLE[CMDS[i][k]][state[k][j]]; } } } for (size_t i = 0; i < 4; ++i) { memcpy(state[i], tempState[i], 4); } } /// /// /// /// void AES::invMixColumns(uint8_t state[4][AES_NB]) { uint8_t tempState[4][AES_NB]; for (size_t i = 0; i < 4; ++i) { memset(tempState[i], 0, 4); } for (size_t i = 0; i < 4; ++i) { for (size_t k = 0; k < 4; ++k) { for (size_t j = 0; j < 4; ++j) { tempState[i][j] ^= GF_MUL_TABLE[INV_CMDS[i][k]][state[k][j]]; } } } for (size_t i = 0; i < 4; ++i) { memcpy(state[i], tempState[i], 4); } } /// /// /// /// /// void AES::addRoundKey(uint8_t state[4][AES_NB], uint8_t* key) { for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { state[i][j] = state[i][j] ^ key[i + 4 * j]; } } } /// /// /// /// void AES::subWord(uint8_t* a) { for (uint32_t i = 0; i < 4; i++) { a[i] = SBOX[a[i] / 16][a[i] % 16]; } } /// /// /// /// void AES::rotWord(uint8_t* a) { uint8_t c = a[0]; a[0] = a[1]; a[1] = a[2]; a[2] = a[3]; a[3] = c; } /// /// /// /// /// /// void AES::xorWords(uint8_t* a, uint8_t* b, uint8_t* c) { for (uint32_t i = 0; i < 4; i++) { c[i] = a[i] ^ b[i]; } } /// /// /// /// /// void AES::rcon(uint8_t *a, uint32_t n) { uint8_t c = 1; for (uint32_t i = 0; i < n - 1; i++) { c = xtime(c); } a[0] = c; a[1] = a[2] = a[3] = 0; } /// /// /// /// /// void AES::keyExpansion(const uint8_t key[], uint8_t w[]) { uint8_t temp[4], rcon[4]; uint32_t i = 0; while (i < 4 * m_Nk) { w[i] = key[i]; i++; } i = 4 * m_Nk; while (i < 4 * AES_NB * (m_Nr + 1)) { temp[0] = w[i - 4 + 0]; temp[1] = w[i - 4 + 1]; temp[2] = w[i - 4 + 2]; temp[3] = w[i - 4 + 3]; if (i / 4 % m_Nk == 0) { rotWord(temp); subWord(temp); this->rcon(rcon, i / (m_Nk * 4)); xorWords(temp, rcon, temp); } else if (m_Nk > 6 && i / 4 % m_Nk == 4) { subWord(temp); } w[i + 0] = w[i - 4 * m_Nk] ^ temp[0]; w[i + 1] = w[i + 1 - 4 * m_Nk] ^ temp[1]; w[i + 2] = w[i + 2 - 4 * m_Nk] ^ temp[2]; w[i + 3] = w[i + 3 - 4 * m_Nk] ^ temp[3]; i += 4; } } /// /// /// /// /// /// void AES::encryptBlock(const uint8_t in[], uint8_t out[], uint8_t* roundKeys) { uint8_t state[4][AES_NB]; for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { state[i][j] = in[i + 4 * j]; } } addRoundKey(state, roundKeys); for (uint32_t round = 1; round <= m_Nr - 1; round++) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, roundKeys + round * 4 * AES_NB); } subBytes(state); shiftRows(state); addRoundKey(state, roundKeys + m_Nr * 4 * AES_NB); for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { out[i + 4 * j] = state[i][j]; } } } /// /// /// /// /// /// void AES::decryptBlock(const uint8_t in[], uint8_t out[], uint8_t* roundKeys) { uint8_t state[4][AES_NB]; for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { state[i][j] = in[i + 4 * j]; } } addRoundKey(state, roundKeys + m_Nr * 4 * AES_NB); for (uint32_t round = m_Nr - 1; round >= 1; round--) { invSubBytes(state); invShiftRows(state); addRoundKey(state, roundKeys + round * 4 * AES_NB); invMixColumns(state); } invSubBytes(state); invShiftRows(state); addRoundKey(state, roundKeys); for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < AES_NB; j++) { out[i + 4 * j] = state[i][j]; } } } /// /// /// /// /// /// /// void AES::xorBlocks(const uint8_t *a, const uint8_t *b, uint8_t *c, uint32_t len) { for (uint32_t i = 0; i < len; i++) { c[i] = a[i] ^ b[i]; } }