5 #include "multi2_error_code.h"
7 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
10 static __inline
uint8_t *load_be_uint32(uint32_t *dst
, uint8_t *src
)
12 *dst
= ((src
[0]<<24)|(src
[1]<<16)|(src
[2]<<8)|src
[3]);
16 static __inline
uint8_t *save_be_uint32(uint8_t *dst
, uint32_t src
)
18 dst
[0] = (uint8_t)((src
>>24) & 0xff);
19 dst
[1] = (uint8_t)((src
>>16) & 0xff);
20 dst
[2] = (uint8_t)((src
>> 8) & 0xff);
21 dst
[3] = (uint8_t)( src
& 0xff);
25 static __inline
uint32_t left_rotate_uint32(uint32_t val
, uint32_t count
)
27 return ((val
<< count
) | (val
>> (32-count
)));
30 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
49 CORE_DATA scr
[2]; /* 0: odd, 1: even */
50 CORE_PARAM wrk
[2]; /* 0: odd, 1: even */
55 } MULTI2_PRIVATE_DATA
;
57 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
60 #define MULTI2_STATE_CBC_INIT_SET (0x0001)
61 #define MULTI2_STATE_SYSTEM_KEY_SET (0x0002)
62 #define MULTI2_STATE_SCRAMBLE_KEY_SET (0x0004)
64 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
65 function prottypes (interface method)
66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
67 static void release_multi2(void *m2
);
68 static int add_ref_multi2(void *m2
);
69 static int set_round_multi2(void *m2
, int32_t val
);
70 static int set_system_key_multi2(void *m2
, uint8_t *val
);
71 static int set_init_cbc_multi2(void *m2
, uint8_t *val
);
72 static int set_scramble_key_multi2(void *m2
, uint8_t *val
);
73 static int clear_scramble_key_multi2(void *m2
);
74 static int encrypt_multi2(void *m2
, int32_t type
, uint8_t *buf
, int32_t size
);
75 static int decrypt_multi2(void *m2
, int32_t type
, uint8_t *buf
, int32_t size
);
77 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78 global function implementation
79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
80 MULTI2
*create_multi2()
85 MULTI2_PRIVATE_DATA
*prv
;
87 n
= sizeof(MULTI2_PRIVATE_DATA
);
90 prv
= (MULTI2_PRIVATE_DATA
*)calloc(1, n
);
95 r
= (MULTI2
*)(prv
+1);
96 r
->private_data
= prv
;
101 r
->release
= release_multi2
;
102 r
->add_ref
= add_ref_multi2
;
103 r
->set_round
= set_round_multi2
;
104 r
->set_system_key
= set_system_key_multi2
;
105 r
->set_init_cbc
= set_init_cbc_multi2
;
106 r
->set_scramble_key
= set_scramble_key_multi2
;
107 r
->clear_scramble_key
= clear_scramble_key_multi2
;
108 r
->encrypt
= encrypt_multi2
;
109 r
->decrypt
= decrypt_multi2
;
114 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115 function prottypes (private method)
116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
117 static MULTI2_PRIVATE_DATA
*private_data(void *m2
);
119 static void core_schedule(CORE_PARAM
*work
, CORE_PARAM
*skey
, CORE_DATA
*dkey
);
121 static void core_encrypt(CORE_DATA
*dst
, CORE_DATA
*src
, CORE_PARAM
*w
, int32_t round
);
122 static void core_decrypt(CORE_DATA
*dst
, CORE_DATA
*src
, CORE_PARAM
*w
, int32_t round
);
124 static void core_pi1(CORE_DATA
*dst
, CORE_DATA
*src
);
125 static void core_pi2(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
);
126 static void core_pi3(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
, uint32_t b
);
127 static void core_pi4(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
);
129 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130 interface method implementation
131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
132 static void release_multi2(void *m2
)
134 MULTI2_PRIVATE_DATA
*prv
;
136 prv
= private_data(m2
);
143 if(prv
->ref_count
== 0){
148 static int add_ref_multi2(void *m2
)
150 MULTI2_PRIVATE_DATA
*prv
;
152 prv
= private_data(m2
);
154 return MULTI2_ERROR_INVALID_PARAMETER
;
162 static int set_round_multi2(void *m2
, int32_t val
)
164 MULTI2_PRIVATE_DATA
*prv
;
166 prv
= private_data(m2
);
169 return MULTI2_ERROR_INVALID_PARAMETER
;
177 static int set_system_key_multi2(void *m2
, uint8_t *val
)
182 MULTI2_PRIVATE_DATA
*prv
;
184 prv
= private_data(m2
);
185 if( (prv
== NULL
) || (val
== NULL
) ){
186 return MULTI2_ERROR_INVALID_PARAMETER
;
191 p
= load_be_uint32(prv
->sys
.key
+i
, p
);
194 prv
->state
|= MULTI2_STATE_SYSTEM_KEY_SET
;
199 static int set_init_cbc_multi2(void *m2
, uint8_t *val
)
203 MULTI2_PRIVATE_DATA
*prv
;
205 prv
= private_data(m2
);
206 if( (prv
== NULL
) || (val
== NULL
) ){
207 return MULTI2_ERROR_INVALID_PARAMETER
;
212 p
= load_be_uint32(&(prv
->cbc_init
.l
), p
);
213 p
= load_be_uint32(&(prv
->cbc_init
.r
), p
);
215 prv
->state
|= MULTI2_STATE_CBC_INIT_SET
;
220 static int set_scramble_key_multi2(void *m2
, uint8_t *val
)
224 MULTI2_PRIVATE_DATA
*prv
;
226 prv
= private_data(m2
);
227 if( (prv
== NULL
) || (val
== NULL
) ){
228 return MULTI2_ERROR_INVALID_PARAMETER
;
233 p
= load_be_uint32(&(prv
->scr
[0].l
), p
);
234 p
= load_be_uint32(&(prv
->scr
[0].r
), p
);
235 p
= load_be_uint32(&(prv
->scr
[1].l
), p
);
236 p
= load_be_uint32(&(prv
->scr
[1].r
), p
);
238 core_schedule(prv
->wrk
+0, &(prv
->sys
), prv
->scr
+0);
239 core_schedule(prv
->wrk
+1, &(prv
->sys
), prv
->scr
+1);
241 prv
->state
|= MULTI2_STATE_SCRAMBLE_KEY_SET
;
246 static int clear_scramble_key_multi2(void *m2
)
248 MULTI2_PRIVATE_DATA
*prv
;
250 prv
= private_data(m2
);
252 return MULTI2_ERROR_INVALID_PARAMETER
;
255 memset(prv
->scr
, 0, sizeof(prv
->scr
));
256 memset(prv
->wrk
, 0, sizeof(prv
->wrk
));
258 prv
->state
&= (~MULTI2_STATE_SCRAMBLE_KEY_SET
);
263 static int encrypt_multi2(void *m2
, int32_t type
, uint8_t *buf
, int32_t size
)
270 MULTI2_PRIVATE_DATA
*prv
;
272 prv
= private_data(m2
);
273 if( (prv
== NULL
) || (buf
== NULL
) || (size
< 1) ){
274 return MULTI2_ERROR_INVALID_PARAMETER
;
277 if(prv
->state
!= (MULTI2_STATE_CBC_INIT_SET
|MULTI2_STATE_SYSTEM_KEY_SET
|MULTI2_STATE_SCRAMBLE_KEY_SET
)){
278 if( (prv
->state
& MULTI2_STATE_CBC_INIT_SET
) == 0 ){
279 return MULTI2_ERROR_UNSET_CBC_INIT
;
281 if( (prv
->state
& MULTI2_STATE_SYSTEM_KEY_SET
) == 0 ){
282 return MULTI2_ERROR_UNSET_SYSTEM_KEY
;
284 if( (prv
->state
& MULTI2_STATE_SCRAMBLE_KEY_SET
) == 0 ){
285 return MULTI2_ERROR_UNSET_SCRAMBLE_KEY
;
295 dst
.l
= prv
->cbc_init
.l
;
296 dst
.r
= prv
->cbc_init
.r
;
300 load_be_uint32(&(src
.l
), p
+0);
301 load_be_uint32(&(src
.r
), p
+4);
302 src
.l
= src
.l
^ dst
.l
;
303 src
.r
= src
.r
^ dst
.r
;
304 core_encrypt(&dst
, &src
, prm
, prv
->round
);
305 p
= save_be_uint32(p
, dst
.l
);
306 p
= save_be_uint32(p
, dst
.r
);
316 core_encrypt(&dst
, &src
, prm
, prv
->round
);
317 save_be_uint32(tmp
+0, dst
.l
);
318 save_be_uint32(tmp
+4, dst
.r
);
321 p
[i
] = (uint8_t)(p
[i
] ^ tmp
[i
]);
328 static int decrypt_multi2(void *m2
, int32_t type
, uint8_t *buf
, int32_t size
)
330 CORE_DATA src
,dst
,cbc
;
335 MULTI2_PRIVATE_DATA
*prv
;
337 prv
= private_data(m2
);
338 if( (prv
== NULL
) || (buf
== NULL
) || (size
< 1) ){
339 return MULTI2_ERROR_INVALID_PARAMETER
;
342 if(prv
->state
!= (MULTI2_STATE_CBC_INIT_SET
|MULTI2_STATE_SYSTEM_KEY_SET
|MULTI2_STATE_SCRAMBLE_KEY_SET
)){
343 if( (prv
->state
& MULTI2_STATE_CBC_INIT_SET
) == 0 ){
344 return MULTI2_ERROR_UNSET_CBC_INIT
;
346 if( (prv
->state
& MULTI2_STATE_SYSTEM_KEY_SET
) == 0 ){
347 return MULTI2_ERROR_UNSET_SYSTEM_KEY
;
349 if( (prv
->state
& MULTI2_STATE_SCRAMBLE_KEY_SET
) == 0 ){
350 return MULTI2_ERROR_UNSET_SCRAMBLE_KEY
;
360 cbc
.l
= prv
->cbc_init
.l
;
361 cbc
.r
= prv
->cbc_init
.r
;
365 load_be_uint32(&(src
.l
), p
+0);
366 load_be_uint32(&(src
.r
), p
+4);
367 core_decrypt(&dst
, &src
, prm
, prv
->round
);
368 dst
.l
= dst
.l
^ cbc
.l
;
369 dst
.r
= dst
.r
^ cbc
.r
;
372 p
= save_be_uint32(p
, dst
.l
);
373 p
= save_be_uint32(p
, dst
.r
);
381 core_encrypt(&dst
, &cbc
, prm
, prv
->round
);
382 save_be_uint32(tmp
+0, dst
.l
);
383 save_be_uint32(tmp
+4, dst
.r
);
386 p
[i
] = (uint8_t)(p
[i
] ^ tmp
[i
]);
393 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
394 private method implementation
395 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
396 static MULTI2_PRIVATE_DATA
*private_data(void *m2
)
398 MULTI2_PRIVATE_DATA
*r
;
406 r
= (MULTI2_PRIVATE_DATA
*)(p
->private_data
);
407 if( ((void *)(r
+1)) != ((void *)p
) ){
414 static void core_schedule(CORE_PARAM
*work
, CORE_PARAM
*skey
, CORE_DATA
*dkey
)
416 CORE_DATA b1
,b2
,b3
,b4
,b5
,b6
,b7
,b8
,b9
;
420 core_pi2(&b2
, &b1
, skey
->key
[0]);
423 core_pi3(&b3
, &b2
, skey
->key
[1], skey
->key
[2]);
426 core_pi4(&b4
, &b3
, skey
->key
[3]);
432 core_pi2(&b6
, &b5
, skey
->key
[4]);
435 core_pi3(&b7
, &b6
, skey
->key
[5], skey
->key
[6]);
438 core_pi4(&b8
, &b7
, skey
->key
[7]);
445 static void core_encrypt(CORE_DATA
*dst
, CORE_DATA
*src
, CORE_PARAM
*w
, int32_t round
)
453 for(i
=0;i
<round
;i
++){
455 core_pi2( dst
, &tmp
, w
->key
[0]);
456 core_pi3(&tmp
, dst
, w
->key
[1], w
->key
[2]);
457 core_pi4( dst
, &tmp
, w
->key
[3]);
459 core_pi2( dst
, &tmp
, w
->key
[4]);
460 core_pi3(&tmp
, dst
, w
->key
[5], w
->key
[6]);
461 core_pi4( dst
, &tmp
, w
->key
[7]);
465 static void core_decrypt(CORE_DATA
*dst
, CORE_DATA
*src
, CORE_PARAM
*w
, int32_t round
)
473 for(i
=0;i
<round
;i
++){
474 core_pi4(&tmp
, dst
, w
->key
[7]);
475 core_pi3( dst
, &tmp
, w
->key
[5], w
->key
[6]);
476 core_pi2(&tmp
, dst
, w
->key
[4]);
477 core_pi1( dst
, &tmp
);
478 core_pi4(&tmp
, dst
, w
->key
[3]);
479 core_pi3( dst
, &tmp
, w
->key
[1], w
->key
[2]);
480 core_pi2(&tmp
, dst
, w
->key
[0]);
481 core_pi1( dst
, &tmp
);
485 static void core_pi1(CORE_DATA
*dst
, CORE_DATA
*src
)
488 dst
->r
= src
->r
^ src
->l
;
491 static void core_pi2(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
)
496 t1
= left_rotate_uint32(t0
, 1) + t0
- 1;
497 t2
= left_rotate_uint32(t1
, 4) ^ t1
;
499 dst
->l
= src
->l
^ t2
;
503 static void core_pi3(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
, uint32_t b
)
505 uint32_t t0
,t1
,t2
,t3
,t4
,t5
;
508 t1
= left_rotate_uint32(t0
, 2) + t0
+ 1;
509 t2
= left_rotate_uint32(t1
, 8) ^ t1
;
511 t4
= left_rotate_uint32(t3
, 1) - t3
;
512 t5
= left_rotate_uint32(t4
, 16) ^ (t4
| src
->l
);
515 dst
->r
= src
->r
^ t5
;
518 static void core_pi4(CORE_DATA
*dst
, CORE_DATA
*src
, uint32_t a
)
523 t1
= left_rotate_uint32(t0
, 2) + t0
+ 1;
525 dst
->l
= src
->l
^ t1
;