initial commit
[arib_std_b25.git] / src / multi2.c
blobdcff36170ae5f3f2946249dc763f8a0bf4fdf2f3
1 #include <stdlib.h>
2 #include <string.h>
4 #include "multi2.h"
5 #include "multi2_error_code.h"
7 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 inline functions
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]);
13 return src+4;
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);
22 return dst+4;
25 static __inline uint32_t left_rotate_uint32(uint32_t val, uint32_t count)
27 return ((val << count) | (val >> (32-count)));
30 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31 inner structures
32 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
33 typedef struct {
34 uint32_t key[8];
35 } CORE_PARAM;
37 typedef struct {
38 uint32_t l;
39 uint32_t r;
40 } CORE_DATA;
42 typedef struct {
44 int32_t ref_count;
46 CORE_DATA cbc_init;
48 CORE_PARAM sys;
49 CORE_DATA scr[2]; /* 0: odd, 1: even */
50 CORE_PARAM wrk[2]; /* 0: odd, 1: even */
52 uint32_t round;
53 uint32_t state;
55 } MULTI2_PRIVATE_DATA;
57 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58 constant values
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()
82 int n;
84 MULTI2 *r;
85 MULTI2_PRIVATE_DATA *prv;
87 n = sizeof(MULTI2_PRIVATE_DATA);
88 n += sizeof(MULTI2);
90 prv = (MULTI2_PRIVATE_DATA *)calloc(1, n);
91 if(prv == NULL){
92 return NULL;
95 r = (MULTI2 *)(prv+1);
96 r->private_data = prv;
98 prv->ref_count = 1;
99 prv->round = 4;
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;
111 return r;
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);
137 if(prv == NULL){
138 /* do nothing */
139 return;
142 prv->ref_count -= 1;
143 if(prv->ref_count == 0){
144 free(prv);
148 static int add_ref_multi2(void *m2)
150 MULTI2_PRIVATE_DATA *prv;
152 prv = private_data(m2);
153 if(prv == NULL){
154 return MULTI2_ERROR_INVALID_PARAMETER;
157 prv->ref_count += 1;
159 return 0;
162 static int set_round_multi2(void *m2, int32_t val)
164 MULTI2_PRIVATE_DATA *prv;
166 prv = private_data(m2);
167 if(prv == NULL){
168 /* do nothing */
169 return MULTI2_ERROR_INVALID_PARAMETER;
172 prv->round = val;
174 return 0;
177 static int set_system_key_multi2(void *m2, uint8_t *val)
179 int i;
180 uint8_t *p;
182 MULTI2_PRIVATE_DATA *prv;
184 prv = private_data(m2);
185 if( (prv == NULL) || (val == NULL) ){
186 return MULTI2_ERROR_INVALID_PARAMETER;
189 p = val;
190 for(i=0;i<8;i++){
191 p = load_be_uint32(prv->sys.key+i, p);
194 prv->state |= MULTI2_STATE_SYSTEM_KEY_SET;
196 return 0;
199 static int set_init_cbc_multi2(void *m2, uint8_t *val)
201 uint8_t *p;
203 MULTI2_PRIVATE_DATA *prv;
205 prv = private_data(m2);
206 if( (prv == NULL) || (val == NULL) ){
207 return MULTI2_ERROR_INVALID_PARAMETER;
210 p = val;
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;
217 return 0;
220 static int set_scramble_key_multi2(void *m2, uint8_t *val)
222 uint8_t *p;
224 MULTI2_PRIVATE_DATA *prv;
226 prv = private_data(m2);
227 if( (prv == NULL) || (val == NULL) ){
228 return MULTI2_ERROR_INVALID_PARAMETER;
231 p = val;
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;
243 return 0;
246 static int clear_scramble_key_multi2(void *m2)
248 MULTI2_PRIVATE_DATA *prv;
250 prv = private_data(m2);
251 if(prv == NULL){
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);
260 return 0;
263 static int encrypt_multi2(void *m2, int32_t type, uint8_t *buf, int32_t size)
265 CORE_DATA src,dst;
266 CORE_PARAM *prm;
268 uint8_t *p;
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;
289 if(type == 0x02){
290 prm = prv->wrk+1;
291 }else{
292 prm = prv->wrk+0;
295 dst.l = prv->cbc_init.l;
296 dst.r = prv->cbc_init.r;
298 p = buf;
299 while(size >= 8){
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);
307 size -= 8;
310 if(size > 0){
311 int i;
312 uint8_t tmp[8];
314 src.l = dst.l;
315 src.r = 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);
320 for(i=0;i<size;i++){
321 p[i] = (uint8_t)(p[i] ^ tmp[i]);
325 return 0;
328 static int decrypt_multi2(void *m2, int32_t type, uint8_t *buf, int32_t size)
330 CORE_DATA src,dst,cbc;
331 CORE_PARAM *prm;
333 uint8_t *p;
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;
354 if(type == 0x02){
355 prm = prv->wrk+1;
356 }else{
357 prm = prv->wrk+0;
360 cbc.l = prv->cbc_init.l;
361 cbc.r = prv->cbc_init.r;
363 p = buf;
364 while(size >= 8){
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;
370 cbc.l = src.l;
371 cbc.r = src.r;
372 p = save_be_uint32(p, dst.l);
373 p = save_be_uint32(p, dst.r);
374 size -= 8;
377 if(size > 0){
378 int i;
379 uint8_t tmp[8];
381 core_encrypt(&dst, &cbc, prm, prv->round);
382 save_be_uint32(tmp+0, dst.l);
383 save_be_uint32(tmp+4, dst.r);
385 for(i=0;i<size;i++){
386 p[i] = (uint8_t)(p[i] ^ tmp[i]);
390 return 0;
393 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
394 private method implementation
395 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
396 static MULTI2_PRIVATE_DATA *private_data(void *m2)
398 MULTI2_PRIVATE_DATA *r;
399 MULTI2 *p;
401 p = (MULTI2 *)m2;
402 if(p == NULL){
403 return NULL;
406 r = (MULTI2_PRIVATE_DATA *)(p->private_data);
407 if( ((void *)(r+1)) != ((void *)p) ){
408 return NULL;
411 return r;
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;
418 core_pi1(&b1, dkey);
420 core_pi2(&b2, &b1, skey->key[0]);
421 work->key[0] = b2.l;
423 core_pi3(&b3, &b2, skey->key[1], skey->key[2]);
424 work->key[1] = b3.r;
426 core_pi4(&b4, &b3, skey->key[3]);
427 work->key[2] = b4.l;
429 core_pi1(&b5, &b4);
430 work->key[3] = b5.r;
432 core_pi2(&b6, &b5, skey->key[4]);
433 work->key[4] = b6.l;
435 core_pi3(&b7, &b6, skey->key[5], skey->key[6]);
436 work->key[5] = b7.r;
438 core_pi4(&b8, &b7, skey->key[7]);
439 work->key[6] = b8.l;
441 core_pi1(&b9, &b8);
442 work->key[7] = b9.r;
445 static void core_encrypt(CORE_DATA *dst, CORE_DATA *src, CORE_PARAM *w, int32_t round)
447 int32_t i;
449 CORE_DATA tmp;
451 dst->l = src->l;
452 dst->r = src->r;
453 for(i=0;i<round;i++){
454 core_pi1(&tmp, dst);
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]);
458 core_pi1(&tmp, dst);
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)
467 int32_t i;
469 CORE_DATA tmp;
471 dst->l = src->l;
472 dst->r = src->r;
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)
487 dst->l = src->l;
488 dst->r = src->r ^ src->l;
491 static void core_pi2(CORE_DATA *dst, CORE_DATA *src, uint32_t a)
493 uint32_t t0,t1,t2;
495 t0 = src->r + a;
496 t1 = left_rotate_uint32(t0, 1) + t0 - 1;
497 t2 = left_rotate_uint32(t1, 4) ^ t1;
499 dst->l = src->l ^ t2;
500 dst->r = src->r;
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;
507 t0 = src->l + a;
508 t1 = left_rotate_uint32(t0, 2) + t0 + 1;
509 t2 = left_rotate_uint32(t1, 8) ^ t1;
510 t3 = t2 + b;
511 t4 = left_rotate_uint32(t3, 1) - t3;
512 t5 = left_rotate_uint32(t4, 16) ^ (t4 | src->l);
514 dst->l = src->l;
515 dst->r = src->r ^ t5;
518 static void core_pi4(CORE_DATA *dst, CORE_DATA *src, uint32_t a)
520 uint32_t t0,t1;
522 t0 = src->r + a;
523 t1 = left_rotate_uint32(t0, 2) + t0 + 1;
525 dst->l = src->l ^ t1;
526 dst->r = src->r;