Botan  2.4.0
Crypto and TLS for C++11
poly_dbl.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/internal/poly_dbl.h>
8 #include <botan/loadstor.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 template<size_t LIMBS, uint64_t POLY>
16 void poly_double(uint8_t out[], const uint8_t in[])
17  {
18  uint64_t W[LIMBS];
19  load_be(W, in, LIMBS);
20 
21  const uint64_t carry = POLY * (W[0] >> 63);
22  for(size_t i = 0; i != LIMBS - 1; ++i)
23  W[i] = (W[i] << 1) ^ (W[i+1] >> 63);
24  W[LIMBS-1] = (W[LIMBS-1] << 1) ^ carry;
25 
26  copy_out_be(out, LIMBS*8, W);
27  }
28 
29 template<size_t LIMBS, uint64_t POLY>
30 void poly_double_le(uint8_t out[], const uint8_t in[])
31  {
32  uint64_t W[LIMBS];
33  load_le(W, in, LIMBS);
34 
35  const uint64_t carry = POLY * (W[LIMBS-1] >> 63);
36  for(size_t i = 0; i != LIMBS - 1; ++i)
37  W[LIMBS-1-i] = (W[LIMBS-1-i] << 1) ^ (W[LIMBS-2-i] >> 63);
38  W[0] = (W[0] << 1) ^ carry;
39 
40  copy_out_le(out, LIMBS*8, W);
41  }
42 
43 }
44 
45 void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
46  {
47  switch(n)
48  {
49  case 8:
50  return poly_double<1, 0x1B>(out, in);
51  case 16:
52  return poly_double<2, 0x87>(out, in);
53  case 24:
54  return poly_double<3, 0x87>(out, in);
55  case 32:
56  return poly_double<4, 0x425>(out, in);
57  case 64:
58  return poly_double<8, 0x125>(out, in);
59  default:
60  throw Invalid_Argument("Unsupported size for poly_double_n");
61  }
62  }
63 
64 void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n)
65  {
66  switch(n)
67  {
68  case 8:
69  return poly_double_le<1, 0x1B>(out, in);
70  case 16:
71  return poly_double_le<2, 0x87>(out, in);
72  case 24:
73  return poly_double_le<3, 0x87>(out, in);
74  case 32:
75  return poly_double_le<4, 0x425>(out, in);
76  case 64:
77  return poly_double_le<8, 0x125>(out, in);
78  default:
79  throw Invalid_Argument("Unsupported size for poly_double_n_le");
80  }
81  }
82 
83 }
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:675
T load_be(const uint8_t in[], size_t off)
Definition: loadstor.h:105
void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n)
Definition: poly_dbl.cpp:64
T load_le(const uint8_t in[], size_t off)
Definition: loadstor.h:121
Definition: alg_id.cpp:13
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
Definition: poly_dbl.cpp:45
void copy_out_be(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:654