diff options
Diffstat (limited to 'nacl/crypto_stream/aes128ctr/portable/int128.c')
-rw-r--r-- | nacl/crypto_stream/aes128ctr/portable/int128.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/nacl/crypto_stream/aes128ctr/portable/int128.c b/nacl/crypto_stream/aes128ctr/portable/int128.c new file mode 100644 index 00000000..25894d42 --- /dev/null +++ b/nacl/crypto_stream/aes128ctr/portable/int128.c | |||
@@ -0,0 +1,128 @@ | |||
1 | #include "int128.h" | ||
2 | #include "common.h" | ||
3 | |||
4 | void xor2(int128 *r, const int128 *x) | ||
5 | { | ||
6 | r->a ^= x->a; | ||
7 | r->b ^= x->b; | ||
8 | } | ||
9 | |||
10 | void and2(int128 *r, const int128 *x) | ||
11 | { | ||
12 | r->a &= x->a; | ||
13 | r->b &= x->b; | ||
14 | } | ||
15 | |||
16 | void or2(int128 *r, const int128 *x) | ||
17 | { | ||
18 | r->a |= x->a; | ||
19 | r->b |= x->b; | ||
20 | } | ||
21 | |||
22 | void copy2(int128 *r, const int128 *x) | ||
23 | { | ||
24 | r->a = x->a; | ||
25 | r->b = x->b; | ||
26 | } | ||
27 | |||
28 | void shufb(int128 *r, const unsigned char *l) | ||
29 | { | ||
30 | int128 t; | ||
31 | copy2(&t,r); | ||
32 | unsigned char *cr = (unsigned char *)r; | ||
33 | unsigned char *ct = (unsigned char *)&t; | ||
34 | cr[0] = ct[l[0]]; | ||
35 | cr[1] = ct[l[1]]; | ||
36 | cr[2] = ct[l[2]]; | ||
37 | cr[3] = ct[l[3]]; | ||
38 | cr[4] = ct[l[4]]; | ||
39 | cr[5] = ct[l[5]]; | ||
40 | cr[6] = ct[l[6]]; | ||
41 | cr[7] = ct[l[7]]; | ||
42 | cr[8] = ct[l[8]]; | ||
43 | cr[9] = ct[l[9]]; | ||
44 | cr[10] = ct[l[10]]; | ||
45 | cr[11] = ct[l[11]]; | ||
46 | cr[12] = ct[l[12]]; | ||
47 | cr[13] = ct[l[13]]; | ||
48 | cr[14] = ct[l[14]]; | ||
49 | cr[15] = ct[l[15]]; | ||
50 | } | ||
51 | |||
52 | void shufd(int128 *r, const int128 *x, const unsigned int c) | ||
53 | { | ||
54 | int128 t; | ||
55 | uint32 *tp = (uint32 *)&t; | ||
56 | uint32 *xp = (uint32 *)x; | ||
57 | tp[0] = xp[c&3]; | ||
58 | tp[1] = xp[(c>>2)&3]; | ||
59 | tp[2] = xp[(c>>4)&3]; | ||
60 | tp[3] = xp[(c>>6)&3]; | ||
61 | copy2(r,&t); | ||
62 | } | ||
63 | |||
64 | void rshift32_littleendian(int128 *r, const unsigned int n) | ||
65 | { | ||
66 | unsigned char *rp = (unsigned char *)r; | ||
67 | uint32 t; | ||
68 | t = load32_littleendian(rp); | ||
69 | t >>= n; | ||
70 | store32_littleendian(rp, t); | ||
71 | t = load32_littleendian(rp+4); | ||
72 | t >>= n; | ||
73 | store32_littleendian(rp+4, t); | ||
74 | t = load32_littleendian(rp+8); | ||
75 | t >>= n; | ||
76 | store32_littleendian(rp+8, t); | ||
77 | t = load32_littleendian(rp+12); | ||
78 | t >>= n; | ||
79 | store32_littleendian(rp+12, t); | ||
80 | } | ||
81 | |||
82 | void rshift64_littleendian(int128 *r, const unsigned int n) | ||
83 | { | ||
84 | unsigned char *rp = (unsigned char *)r; | ||
85 | uint64 t; | ||
86 | t = load64_littleendian(rp); | ||
87 | t >>= n; | ||
88 | store64_littleendian(rp, t); | ||
89 | t = load64_littleendian(rp+8); | ||
90 | t >>= n; | ||
91 | store64_littleendian(rp+8, t); | ||
92 | } | ||
93 | |||
94 | void lshift64_littleendian(int128 *r, const unsigned int n) | ||
95 | { | ||
96 | unsigned char *rp = (unsigned char *)r; | ||
97 | uint64 t; | ||
98 | t = load64_littleendian(rp); | ||
99 | t <<= n; | ||
100 | store64_littleendian(rp, t); | ||
101 | t = load64_littleendian(rp+8); | ||
102 | t <<= n; | ||
103 | store64_littleendian(rp+8, t); | ||
104 | } | ||
105 | |||
106 | void toggle(int128 *r) | ||
107 | { | ||
108 | r->a ^= 0xffffffffffffffffULL; | ||
109 | r->b ^= 0xffffffffffffffffULL; | ||
110 | } | ||
111 | |||
112 | void xor_rcon(int128 *r) | ||
113 | { | ||
114 | unsigned char *rp = (unsigned char *)r; | ||
115 | uint32 t; | ||
116 | t = load32_littleendian(rp+12); | ||
117 | t ^= 0xffffffff; | ||
118 | store32_littleendian(rp+12, t); | ||
119 | } | ||
120 | |||
121 | void add_uint32_big(int128 *r, uint32 x) | ||
122 | { | ||
123 | unsigned char *rp = (unsigned char *)r; | ||
124 | uint32 t; | ||
125 | t = load32_littleendian(rp+12); | ||
126 | t += x; | ||
127 | store32_littleendian(rp+12, t); | ||
128 | } | ||