| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-23 00:43:01 +04:30
										 |  |  |  * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-26 10:32:08 -07:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <LibCrypto/Cipher/Cipher.h>
 | 
					
						
							|  |  |  | #include <LibCrypto/Cipher/Mode/CBC.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-23 14:05:26 -06:00
										 |  |  | #include <LibCrypto/Cipher/Mode/CTR.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-11 13:17:23 +03:30
										 |  |  | #include <LibCrypto/Cipher/Mode/GCM.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | #ifndef KERNEL
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | #    include <AK/DeprecatedString.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 13:49:08 -04:00
										 |  |  | namespace Crypto::Cipher { | 
					
						
							| 
									
										
										
										
											2021-12-17 14:11:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | struct AESCipherBlock : public CipherBlock { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     static constexpr size_t BlockSizeInBits = 128; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     explicit AESCipherBlock(PaddingMode mode = PaddingMode::CMS) | 
					
						
							|  |  |  |         : CipherBlock(mode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     AESCipherBlock(u8 const* data, size_t length, PaddingMode mode = PaddingMode::CMS) | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |         : AESCipherBlock(mode) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-11 23:30:49 +04:30
										 |  |  |         CipherBlock::overwrite(data, length); | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     constexpr static size_t block_size() { return BlockSizeInBits / 8; } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 09:25:55 +01:00
										 |  |  |     virtual ReadonlyBytes bytes() const override { return ReadonlyBytes { m_data, sizeof(m_data) }; } | 
					
						
							|  |  |  |     virtual Bytes bytes() override { return Bytes { m_data, sizeof(m_data) }; } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-15 18:47:25 +02:00
										 |  |  |     virtual void overwrite(ReadonlyBytes) override; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     virtual void overwrite(u8 const* data, size_t size) override { overwrite({ data, size }); } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 09:32:24 +04:30
										 |  |  |     virtual void apply_initialization_vector(ReadonlyBytes ivec) override | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-14 09:32:24 +04:30
										 |  |  |         for (size_t i = 0; i < min(block_size(), ivec.size()); ++i) | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |             m_data[i] ^= ivec[i]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | #ifndef KERNEL
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     DeprecatedString to_deprecated_string() const; | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-05-13 12:13:11 +04:30
										 |  |  |     constexpr static size_t data_size() { return sizeof(m_data); } | 
					
						
							| 
									
										
										
										
											2021-01-12 09:25:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     u8 m_data[BlockSizeInBits / 8] {}; | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct AESCipherKey : public CipherKey { | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     virtual ReadonlyBytes bytes() const override { return ReadonlyBytes { m_rd_keys, sizeof(m_rd_keys) }; } | 
					
						
							| 
									
										
										
										
											2020-12-19 15:07:09 +01:00
										 |  |  |     virtual void expand_encrypt_key(ReadonlyBytes user_key, size_t bits) override; | 
					
						
							|  |  |  |     virtual void expand_decrypt_key(ReadonlyBytes user_key, size_t bits) override; | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     static bool is_valid_key_size(size_t bits) { return bits == 128 || bits == 192 || bits == 256; } | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef KERNEL
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     DeprecatedString to_deprecated_string() const; | 
					
						
							| 
									
										
										
										
											2022-02-15 21:22:11 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     u32 const* round_keys() const | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         return (u32 const*)m_rd_keys; | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 15:07:09 +01:00
										 |  |  |     AESCipherKey(ReadonlyBytes user_key, size_t key_bits, Intent intent) | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |         : m_bits(key_bits) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (intent == Intent::Encryption) | 
					
						
							|  |  |  |             expand_encrypt_key(user_key, key_bits); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             expand_decrypt_key(user_key, key_bits); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-26 10:32:08 -07:00
										 |  |  |     virtual ~AESCipherKey() override = default; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     size_t rounds() const { return m_rounds; } | 
					
						
							|  |  |  |     size_t length() const { return m_bits / 8; } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | protected: | 
					
						
							|  |  |  |     u32* round_keys() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return (u32*)m_rd_keys; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | private: | 
					
						
							|  |  |  |     static constexpr size_t MAX_ROUND_COUNT = 14; | 
					
						
							|  |  |  |     u32 m_rd_keys[(MAX_ROUND_COUNT + 1) * 4] { 0 }; | 
					
						
							|  |  |  |     size_t m_rounds; | 
					
						
							|  |  |  |     size_t m_bits; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | class AESCipher final : public Cipher<AESCipherKey, AESCipherBlock> { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     using CBCMode = CBC<AESCipher>; | 
					
						
							| 
									
										
										
										
											2020-06-23 14:05:26 -06:00
										 |  |  |     using CTRMode = CTR<AESCipher>; | 
					
						
							| 
									
										
										
										
											2020-11-11 13:17:23 +03:30
										 |  |  |     using GCMMode = GCM<AESCipher>; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |     constexpr static size_t BlockSizeInBits = BlockType::BlockSizeInBits; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 15:07:09 +01:00
										 |  |  |     AESCipher(ReadonlyBytes user_key, size_t key_bits, Intent intent = Intent::Encryption, PaddingMode mode = PaddingMode::CMS) | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  |         : Cipher<AESCipherKey, AESCipherBlock>(mode) | 
					
						
							|  |  |  |         , m_key(user_key, key_bits, intent) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     virtual AESCipherKey const& key() const override { return m_key; } | 
					
						
							|  |  |  |     virtual AESCipherKey& key() override { return m_key; } | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     virtual void encrypt_block(BlockType const& in, BlockType& out) override; | 
					
						
							|  |  |  |     virtual void decrypt_block(BlockType const& in, BlockType& out) override; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:36:46 +02:00
										 |  |  | #ifndef KERNEL
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     virtual DeprecatedString class_name() const override | 
					
						
							| 
									
										
										
										
											2022-02-15 21:36:46 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return "AES"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-04-08 01:54:50 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 02:53:11 +04:30
										 |  |  | protected: | 
					
						
							|  |  |  |     AESCipherKey m_key; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:58:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | } |