summaryrefslogtreecommitdiff
path: root/nacl/crypto_stream/aes128ctr/portable/int128.c
blob: 25894d422cf6a9d253b766680c45fd697ca3186d (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
#include "int128.h"
#include "common.h"

void xor2(int128 *r, const int128 *x)
{
  r->a ^= x->a;
  r->b ^= x->b;
}

void and2(int128 *r, const int128 *x)
{
  r->a &= x->a;
  r->b &= x->b;
}

void or2(int128 *r, const int128 *x)
{
  r->a |= x->a;
  r->b |= x->b;
}

void copy2(int128 *r, const int128 *x)
{
  r->a = x->a;
  r->b = x->b;
}

void shufb(int128 *r, const unsigned char *l)
{
  int128 t;
  copy2(&t,r);
  unsigned char *cr = (unsigned char *)r;
  unsigned char *ct = (unsigned char *)&t;
  cr[0] = ct[l[0]];
  cr[1] = ct[l[1]];
  cr[2] = ct[l[2]];
  cr[3] = ct[l[3]];
  cr[4] = ct[l[4]];
  cr[5] = ct[l[5]];
  cr[6] = ct[l[6]];
  cr[7] = ct[l[7]];
  cr[8] = ct[l[8]];
  cr[9] = ct[l[9]];
  cr[10] = ct[l[10]];
  cr[11] = ct[l[11]];
  cr[12] = ct[l[12]];
  cr[13] = ct[l[13]];
  cr[14] = ct[l[14]];
  cr[15] = ct[l[15]];
}

void shufd(int128 *r, const int128 *x, const unsigned int c)
{
  int128 t;
  uint32 *tp = (uint32 *)&t;
  uint32 *xp = (uint32 *)x;
  tp[0] = xp[c&3];
  tp[1] = xp[(c>>2)&3];
  tp[2] = xp[(c>>4)&3];
  tp[3] = xp[(c>>6)&3];
  copy2(r,&t);
}

void rshift32_littleendian(int128 *r, const unsigned int n)
{
  unsigned char *rp = (unsigned char *)r;
  uint32 t;
  t = load32_littleendian(rp);
  t >>= n;
  store32_littleendian(rp, t);
  t = load32_littleendian(rp+4);
  t >>= n;
  store32_littleendian(rp+4, t);
  t = load32_littleendian(rp+8);
  t >>= n;
  store32_littleendian(rp+8, t);
  t = load32_littleendian(rp+12);
  t >>= n;
  store32_littleendian(rp+12, t);
}

void rshift64_littleendian(int128 *r, const unsigned int n)
{
  unsigned char *rp = (unsigned char *)r;
  uint64 t;
  t = load64_littleendian(rp);
  t >>= n;
  store64_littleendian(rp, t);
  t = load64_littleendian(rp+8);
  t >>= n;
  store64_littleendian(rp+8, t);
}

void lshift64_littleendian(int128 *r, const unsigned int n)
{
  unsigned char *rp = (unsigned char *)r;
  uint64 t;
  t = load64_littleendian(rp);
  t <<= n;
  store64_littleendian(rp, t);
  t = load64_littleendian(rp+8);
  t <<= n;
  store64_littleendian(rp+8, t);
}

void toggle(int128 *r)
{
  r->a ^= 0xffffffffffffffffULL;
  r->b ^= 0xffffffffffffffffULL;
}

void xor_rcon(int128 *r)
{
  unsigned char *rp = (unsigned char *)r;
  uint32 t;
  t = load32_littleendian(rp+12);
  t ^= 0xffffffff;
  store32_littleendian(rp+12, t);
}

void add_uint32_big(int128 *r, uint32 x)
{
  unsigned char *rp = (unsigned char *)r;
  uint32 t;
  t = load32_littleendian(rp+12);
  t += x;
  store32_littleendian(rp+12, t);
}