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/simon32.h>
30 struct kripto_block_object obj
;
36 #define F(X) ((ROL16_01(X) & ROL16_08(X)) ^ ROL16_02(X))
38 static void simon32_encrypt
40 const kripto_block
*s
,
50 b
= LOAD16B(CU8(pt
) + 2);
54 b
^= F(a
) ^ s
->k
[i
++];
58 STORE16B(a
, U8(ct
) + 2);
63 a
^= F(b
) ^ s
->k
[i
++];
67 STORE16B(b
, U8(ct
) + 2);
70 static void simon32_decrypt
72 const kripto_block
*s
,
79 unsigned int i
= s
->rounds
;
82 b
= LOAD16B(CU8(ct
) + 2);
86 a
^= F(b
) ^ s
->k
[--i
];
90 STORE16B(a
, U8(pt
) + 2);
95 b
^= F(a
) ^ s
->k
[--i
];
99 STORE16B(b
, U8(pt
) + 2);
102 static void simon32_setup
112 for(i
= 0; i
< 4; i
++)
115 for(i
= 0; i
< len
; i
++)
116 s
->k
[3 - (i
>> 1)] |=
117 (uint16_t)key
[i
] << (8 - ((i
& 1) << 3));
119 for(i
= 4; i
< s
->rounds
; i
++)
121 t
= ROR16_03(s
->k
[i
- 1]) ^ s
->k
[i
- 3];
122 t
^= ROR16_01(t
) ^ ~s
->k
[i
- 4] ^ 3;
123 s
->k
[i
] = t
^ ((0x19C3522FB386A45F >> ((i
- 4) % 62)) & 1);
126 kripto_memwipe(&t
, sizeof(uint16_t));
129 static kripto_block
*simon32_create
140 s
= malloc(sizeof(kripto_block
) + (r
<< 1));
143 s
->obj
.desc
= kripto_block_simon32
;
144 s
->size
= sizeof(kripto_block
) + (r
<< 1);
145 s
->k
= (uint16_t *)(((uint8_t *)s
) + sizeof(kripto_block
));
148 simon32_setup(s
, key
, key_len
);
153 static void simon32_destroy(kripto_block
*s
)
155 kripto_memwipe(s
, s
->size
);
159 static kripto_block
*simon32_recreate
169 if(sizeof(kripto_block
) + (r
<< 1) > s
->size
)
172 s
= simon32_create(r
, key
, key_len
);
177 simon32_setup(s
, key
, key_len
);
183 static const kripto_block_desc simon32
=
196 const kripto_block_desc
*const kripto_block_simon32
= &simon32
;