~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/lib/utils/poly_dbl/poly_dbl.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}