Botan 3.9.0
Crypto and TLS for C&
serpent_fn.h
Go to the documentation of this file.
1/*
2* (C) 1999-2007,2013 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_SERPENT_FUNCS_H_
8#define BOTAN_SERPENT_FUNCS_H_
9
10#include <botan/compiler.h>
11#include <botan/types.h>
12#include <botan/internal/rotate.h>
13#include <concepts>
14
16
17// Concept for types that support bitwise operations (unsigned integers or SIMD types)
18template <typename T>
19concept BitsliceT = requires(T& a, const T& b) {
20 a ^= b;
21 a &= b;
22 a |= b;
23 ~a;
24};
25
26template <size_t S>
27BOTAN_FORCE_INLINE uint32_t shl(uint32_t v) {
28 return v << S;
29}
30
31/*
32* Serpent's Linear Transform
33*/
34template <BitsliceT T>
35BOTAN_FORCE_INLINE void transform(T& B0, T& B1, T& B2, T& B3) {
36 B0 = rotl<13>(B0);
37 B2 = rotl<3>(B2);
38 B1 ^= B0 ^ B2;
39 B3 ^= B2 ^ shl<3>(B0);
40 B1 = rotl<1>(B1);
41 B3 = rotl<7>(B3);
42 B0 ^= B1 ^ B3;
43 B2 ^= B3 ^ shl<7>(B1);
44 B0 = rotl<5>(B0);
45 B2 = rotl<22>(B2);
46}
47
48/*
49* Serpent's Inverse Linear Transform
50*/
51template <BitsliceT T>
52BOTAN_FORCE_INLINE void i_transform(T& B0, T& B1, T& B2, T& B3) {
53 B2 = rotr<22>(B2);
54 B0 = rotr<5>(B0);
55 B2 ^= B3 ^ shl<7>(B1);
56 B0 ^= B1 ^ B3;
57 B3 = rotr<7>(B3);
58 B1 = rotr<1>(B1);
59 B3 ^= B2 ^ shl<3>(B0);
60 B1 ^= B0 ^ B2;
61 B2 = rotr<3>(B2);
62 B0 = rotr<13>(B0);
63}
64
65class Key_Inserter final {
66 public:
67 explicit Key_Inserter(const uint32_t* RK) : m_RK(RK) {}
68
69 template <BitsliceT T>
70 inline void operator()(size_t R, T& B0, T& B1, T& B2, T& B3) const {
71 B0 ^= m_RK[4 * R];
72 B1 ^= m_RK[4 * R + 1];
73 B2 ^= m_RK[4 * R + 2];
74 B3 ^= m_RK[4 * R + 3];
75 }
76
77 private:
78 const uint32_t* m_RK;
79};
80
81} // namespace Botan::Serpent_F
82
83#endif
Key_Inserter(const uint32_t *RK)
Definition serpent_fn.h:67
void operator()(size_t R, T &B0, T &B1, T &B2, T &B3) const
Definition serpent_fn.h:70
#define BOTAN_FORCE_INLINE
Definition compiler.h:87
BOTAN_FORCE_INLINE void transform(T &B0, T &B1, T &B2, T &B3)
Definition serpent_fn.h:35
BOTAN_FORCE_INLINE uint32_t shl(uint32_t v)
Definition serpent_fn.h:27
BOTAN_FORCE_INLINE void i_transform(T &B0, T &B1, T &B2, T &B3)
Definition serpent_fn.h:52
BOTAN_FORCE_INLINE constexpr T rotr(T input)
Definition rotate.h:35
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23