2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
18 #include <kripto/cast.h>
19 #include <kripto/loadstore.h>
20 #include <kripto/rotate.h>
21 #include <kripto/memwipe.h>
22 #include <kripto/block.h>
23 #include <kripto/desc/block.h>
24 #include <kripto/object/block.h>
26 #include <kripto/block/speck128.h>
30 struct kripto_block_object obj
;
38 A = (ROR64_08(A) + B) ^ (K); \
39 B = ROL64_03(B) ^ A; \
44 B = ROR64_03(B ^ A); \
45 A = ROL64_08((A ^ (K)) - B); \
48 static void speck128_encrypt
50 const kripto_block
*s
,
60 b
= LOAD64B(CU8(pt
) + 8);
62 for(i
= 0; i
< s
->rounds
; i
++)
66 STORE64B(b
, U8(ct
) + 8);
69 static void speck128_decrypt
71 const kripto_block
*s
,
81 b
= LOAD64B(CU8(ct
) + 8);
83 for(i
= s
->rounds
; i
--;)
87 STORE64B(b
, U8(pt
) + 8);
90 static void speck128_setup
99 uint64_t k
[4] = {0, 0, 0, 0};
102 else if(len
> 16) m
= 2;
105 for(i
= 0; i
< len
; i
++)
106 k
[m
- (i
>> 3)] |= (uint64_t)key
[i
] << (56 - ((i
& 7) << 3));
110 for(i
= 0; i
< s
->rounds
- 1;)
112 R(k
[(i
% m
) + 1], k
[0], i
);
116 kripto_memwipe(k
, 32);
119 static kripto_block
*speck128_create
128 if(!r
) r
= 30 + ((key_len
+ 7) >> 3);
130 s
= malloc(sizeof(kripto_block
) + (r
<< 3));
133 s
->obj
.desc
= kripto_block_speck128
;
134 s
->size
= sizeof(kripto_block
) + (r
<< 3);
135 s
->k
= (uint64_t *)(((uint8_t *)s
) + sizeof(kripto_block
));
138 speck128_setup(s
, key
, key_len
);
143 static void speck128_destroy(kripto_block
*s
)
145 kripto_memwipe(s
, s
->size
);
149 static kripto_block
*speck128_recreate
157 if(!r
) r
= 30 + ((key_len
+ 7) >> 3);
159 if(sizeof(kripto_block
) + (r
<< 3) > s
->size
)
162 s
= speck128_create(r
, key
, key_len
);
167 speck128_setup(s
, key
, key_len
);
173 static const kripto_block_desc speck128
=
186 const kripto_block_desc
*const kripto_block_speck128
= &speck128
;