Botan 3.14.1
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
15
16// Concept for types that support bitwise operations (unsigned integers or SIMD types)
17template <typename T>
18concept BitsliceT = requires(T& a, const T& b) {
19 a ^= b;
20 a &= b;
21 a |= b;
22 ~a;
23};
24
25template <size_t S>
26BOTAN_FORCE_INLINE uint32_t shl(uint32_t v) {
27 return v << S;
28}
29
30/*
31* Serpent's Linear Transform
32*/
33template <BitsliceT T>
34BOTAN_FORCE_INLINE void transform(T& B0, T& B1, T& B2, T& B3) {
35 B0 = rotl<13>(B0);
36 B2 = rotl<3>(B2);
37 B1 ^= B0 ^ B2;
38 B3 ^= B2 ^ shl<3>(B0);
39 B1 = rotl<1>(B1);
40 B3 = rotl<7>(B3);
41 B0 ^= B1 ^ B3;
42 B2 ^= B3 ^ shl<7>(B1);
43 B0 = rotl<5>(B0);
44 B2 = rotl<22>(B2);
45}
46
47/*
48* Serpent's Inverse Linear Transform
49*/
50template <BitsliceT T>
51BOTAN_FORCE_INLINE void i_transform(T& B0, T& B1, T& B2, T& B3) {
52 B2 = rotr<22>(B2);
53 B0 = rotr<5>(B0);
54 B2 ^= B3 ^ shl<7>(B1);
55 B0 ^= B1 ^ B3;
56 B3 = rotr<7>(B3);
57 B1 = rotr<1>(B1);
58 B3 ^= B2 ^ shl<3>(B0);
59 B1 ^= B0 ^ B2;
60 B2 = rotr<3>(B2);
61 B0 = rotr<13>(B0);
62}
63
64class Key_Inserter final {
65 public:
66 explicit Key_Inserter(const uint32_t* RK) : m_RK(RK) {}
67
68 template <BitsliceT T>
69 BOTAN_FORCE_INLINE void operator()(size_t R, T& B0, T& B1, T& B2, T& B3) const {
70 B0 ^= m_RK[4 * R];
71 B1 ^= m_RK[4 * R + 1];
72 B2 ^= m_RK[4 * R + 2];
73 B3 ^= m_RK[4 * R + 3];
74 }
75
76 private:
77 const uint32_t* m_RK;
78};
79
80} // namespace Botan::Serpent_F
81
82#endif
BOTAN_FORCE_INLINE void operator()(size_t R, T &B0, T &B1, T &B2, T &B3) const
Definition serpent_fn.h:69
Key_Inserter(const uint32_t *RK)
Definition serpent_fn.h:66
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
BOTAN_FORCE_INLINE void transform(T &B0, T &B1, T &B2, T &B3)
Definition serpent_fn.h:34
BOTAN_FORCE_INLINE uint32_t shl(uint32_t v)
Definition serpent_fn.h:26
BOTAN_FORCE_INLINE void i_transform(T &B0, T &B1, T &B2, T &B3)
Definition serpent_fn.h:51
BOTAN_FORCE_INLINE constexpr T rotr(T input)
Definition rotate.h:35
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23