aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/poly_dbl.cpp
blob: 0cea4957b4e3ef7f4cd5bc9a814fd5255687aba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* (C) 2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/poly_dbl.h>
#include <botan/loadstor.h>
#include <botan/exceptn.h>

namespace Botan {

void poly_double_n(uint8_t b[], size_t n)
   {
   if(n == 8)
      return poly_double_8(b);
   else if(n == 16)
      return poly_double_16(b);
   else if(n == 24)
      return poly_double_24(b);
   else if(n == 32)
      return poly_double_32(b);
   else if(n == 64)
      return poly_double_64(b);
   else
      throw Invalid_Argument("Unsupported size for poly_double_n");
   }

void poly_double_8(uint8_t b[8])
   {
   const uint64_t poly = 0x1B;
   uint64_t b0 = load_be<uint64_t>(b, 0);
   const uint64_t carry0 = (b0 >> 63);
   b0 = (b0 << 1) ^ (carry0 * poly);
   store_be(b0, b);
   }

void poly_double_16(uint8_t b[16])
   {
   const uint64_t poly = 0x87;

   uint64_t b0 = load_be<uint64_t>(b, 0);
   uint64_t b1 = load_be<uint64_t>(b, 1);

   const uint64_t carry0 = (b0 >> 63);

   b0 = (b0 << 1) ^ (b1 >> 63);
   b1 = (b1 << 1) ^ (carry0 * poly);

   store_be(b0, b);
   store_be(b1, b+8);
   }

void poly_double_24(uint8_t b[24])
   {
   const uint64_t poly = 0x87;

   uint64_t b0 = load_be<uint64_t>(b, 0);
   uint64_t b1 = load_be<uint64_t>(b, 1);
   uint64_t b2 = load_be<uint64_t>(b, 2);

   const uint64_t carry0 = (b0 >> 63);

   b0 = (b0 << 1) ^ (b1 >> 63);
   b1 = (b1 << 1) ^ (b2 >> 63);
   b2 = (b2 << 1) ^ (carry0 * poly);

   store_be(b0, b);
   store_be(b1, b+8);
   store_be(b2, b+16);
   }

void poly_double_32(uint8_t b[32])
   {
   const uint64_t poly = 0x425;

   uint64_t b0 = load_be<uint64_t>(b, 0);
   uint64_t b1 = load_be<uint64_t>(b, 1);
   uint64_t b2 = load_be<uint64_t>(b, 2);
   uint64_t b3 = load_be<uint64_t>(b, 3);

   const uint64_t carry0 = (b0 >> 63);

   b0 = (b0 << 1) ^ (b1 >> 63);
   b1 = (b1 << 1) ^ (b2 >> 63);
   b2 = (b2 << 1) ^ (b3 >> 63);
   b3 = (b3 << 1) ^ (carry0 * poly);

   store_be(b0, b);
   store_be(b1, b+8);
   store_be(b2, b+16);
   store_be(b3, b+24);
   }

void poly_double_64(uint8_t b[64])
   {
   const uint64_t poly = 0x125;

   uint64_t b0 = load_be<uint64_t>(b, 0);
   uint64_t b1 = load_be<uint64_t>(b, 1);
   uint64_t b2 = load_be<uint64_t>(b, 2);
   uint64_t b3 = load_be<uint64_t>(b, 3);
   uint64_t b4 = load_be<uint64_t>(b, 4);
   uint64_t b5 = load_be<uint64_t>(b, 5);
   uint64_t b6 = load_be<uint64_t>(b, 6);
   uint64_t b7 = load_be<uint64_t>(b, 7);

   const uint64_t carry0 = (b0 >> 63);

   b0 = (b0 << 1) ^ (b1 >> 63);
   b1 = (b1 << 1) ^ (b2 >> 63);
   b2 = (b2 << 1) ^ (b3 >> 63);
   b3 = (b3 << 1) ^ (b4 >> 63);
   b4 = (b4 << 1) ^ (b5 >> 63);
   b5 = (b5 << 1) ^ (b6 >> 63);
   b6 = (b6 << 1) ^ (b7 >> 63);
   b7 = (b7 << 1) ^ (carry0 * poly);

   store_be(b0, b);
   store_be(b1, b+8);
   store_be(b2, b+16);
   store_be(b3, b+24);
   store_be(b4, b+32);
   store_be(b5, b+40);
   store_be(b6, b+48);
   store_be(b7, b+56);
   }

}