8#include <botan/internal/idea.h>
10#include <botan/internal/cpuid.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/loadstor.h>
21inline uint16_t mul(uint16_t x, uint16_t y) {
22 const uint32_t P =
static_cast<uint32_t
>(x) * y;
25 const uint32_t P_hi = P >> 16;
26 const uint32_t P_lo = P & 0xFFFF;
28 const uint16_t
carry = (P_lo < P_hi);
29 const uint16_t r_1 =
static_cast<uint16_t
>((P_lo - P_hi) +
carry);
30 const uint16_t r_2 = 1 - x - y;
32 return P_mask.select(r_2, r_1);
46uint16_t mul_inv(uint16_t x) {
49 for(
size_t i = 0; i != 15; ++i) {
60void idea_op(
const uint8_t in[], uint8_t out[],
size_t blocks,
const uint16_t K[52]) {
61 const size_t BLOCK_SIZE = 8;
67 for(
size_t i = 0; i < blocks; ++i) {
68 uint16_t X1, X2, X3, X4;
69 load_be(in + BLOCK_SIZE * i, X1, X2, X3, X4);
71 for(
size_t j = 0; j != 8; ++j) {
72 X1 = mul(X1, K[6 * j + 0]);
75 X4 = mul(X4, K[6 * j + 3]);
77 const uint16_t T0 = X3;
78 X3 = mul(X3 ^ X1, K[6 * j + 4]);
80 const uint16_t T1 = X2;
81 X2 = mul((X2 ^ X4) + X3, K[6 * j + 5]);
95 store_be(out + BLOCK_SIZE * i, X1, X3, X2, X4);
106#if defined(BOTAN_HAS_IDEA_SSE2)
107 if(CPUID::has_sse2()) {
116#if defined(BOTAN_HAS_IDEA_SSE2)
117 if(CPUID::has_sse2()) {
131#if defined(BOTAN_HAS_IDEA_SSE2)
132 if(CPUID::has_sse2()) {
134 sse2_idea_op_8(in, out, m_EK.data());
142 idea_op(in, out, blocks, m_EK.data());
151#if defined(BOTAN_HAS_IDEA_SSE2)
152 if(CPUID::has_sse2()) {
154 sse2_idea_op_8(in, out, m_DK.data());
162 idea_op(in, out, blocks, m_DK.data());
166 return !m_EK.empty();
172void IDEA::key_schedule(std::span<const uint8_t> key) {
185 for(
size_t off = 0; off != 48; off += 8) {
186 for(
size_t i = 0; i != 8; ++i) {
187 m_EK[off + i] =
static_cast<uint16_t
>(K[i / 4] >> (48 - 16 * (i % 4)));
190 const uint64_t Kx = (K[0] >> 39);
191 const uint64_t Ky = (K[1] >> 39);
193 K[0] = (K[0] << 25) | Ky;
194 K[1] = (K[1] << 25) | Kx;
197 for(
size_t i = 0; i != 4; ++i) {
198 m_EK[48 + i] =
static_cast<uint16_t
>(
K[i / 4] >> (48 - 16 * (i % 4)));
201 m_DK[0] = mul_inv(m_EK[48]);
204 m_DK[3] = mul_inv(m_EK[51]);
206 for(
size_t i = 0; i != 8 * 6; i += 6) {
207 m_DK[i + 4] = m_EK[46 - i];
208 m_DK[i + 5] = m_EK[47 - i];
209 m_DK[i + 6] = mul_inv(m_EK[42 - i]);
210 m_DK[i + 7] = -m_EK[44 - i];
211 m_DK[i + 8] = -m_EK[43 - i];
212 m_DK[i + 9] = mul_inv(m_EK[45 - i]);
215 std::swap(m_DK[49], m_DK[50]);
static constexpr Mask< T > is_zero(T x)
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
std::string provider() const override
bool has_keying_material() const override
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
size_t parallelism() const override
void assert_key_material_set() const
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
void zap(std::vector< T, Alloc > &vec)
void carry(int64_t &h0, int64_t &h1)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)