Unnamed Fossil Project

Check-in [5059020756]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Rsa_Sign/Rsa_Verify on Xdata
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:50590207568ea78418d33c56efe49d8b5325f388
User & Date: monster 2012-07-25 01:29:43
Context
2012-07-30
05:24
phreads on winows and linux check-in: 1c0b0643a5 user: monster tags: trunk
2012-07-25
01:29
Rsa_Sign/Rsa_Verify on Xdata check-in: 5059020756 user: monster tags: trunk
2012-07-24
22:17
sha/rsa for xdata check-in: 718eda106a user: monster tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to bigint.hc.

869
870
871
872
873
874
875
876
877
878






879
880
881
882
883
884
885
....
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424

char *Bigint_Encode_16(YO_BIGINT *bint)
#ifdef _YO_BINGINT_BUILTIN
  {
    char *ret = 0;
    __Auto_Ptr(ret)
      {
        int bc = (Bigint_Bitcount(bint)+7)/8;
        char *S = Str_Hex_Encode(bint->value,bc);
        ret = Str_Reverse(S,bc*2);






      }
    return ret;
  }
#endif
  ;

YO_BIGINT *Bigint_Mul(YO_BIGINT *bint, YO_BIGINT *d)
................................................................................
#else
    #error fixme!
#endif
  }
#endif
  ;

YO_BIGINT *Bigint_Sign(YO_BIGINT *rsa_priv,YO_BIGINT *rsa_mod,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    YO_BIGINT *tmp = 0;
    int mbits = Bigint_Bitcount(rsa_mod)-1;
    if ( len*8 > mbits )
      __Raise_Format(YO_ERROR_OUT_OF_RANGE,
                      (__yoTa("data length is %d bytes, maximum %bytes alowed to sign with %d bits keys",0),
                       len,mbits/8,mbits))
    
    __Auto_Ptr(tmp)
      {
        void *Q = __Malloc(mbits/8);
        Soft_Random(Q,mbits/8);
        memcpy(Q,data,len);

        tmp = Bigint_From_Bytes(0,Q,mbits/8);
        tmp = Bigint_Expmod(tmp,rsa_priv,rsa_mod);
      }
      
    return tmp;
  }
#endif
  ;
  
char *Bigint_Hex_Sign(YO_BIGINT *rsa_priv,YO_BIGINT *rsa_mod,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    char *hex = 0;
    __Auto_Ptr(hex)
      {
        YO_BIGINT *bint = Bigint_Sign(rsa_priv,rsa_mod,data,len);
        hex = Bigint_Encode_16(bint);
      }
    return hex;
  }
#endif
  ;
    
int Bigint_Verify(YO_BIGINT *rsa_pub,YO_BIGINT *rsa_mod,YO_BIGINT *digest,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    int correct = 0;
    int mbits = Bigint_Bitcount(rsa_mod)-1;
    if ( len*8 > mbits )
      __Raise_Format(YO_ERROR_OUT_OF_RANGE,
                      (__yoTa("data length is %d bytes, maximum %bytes alowed to sign with %d bits keys",0),
                       len,mbits/8,mbits))

    __Auto_Release
      {
        YO_BIGINT *tmp = Bigint_Copy(digest);
        tmp = Bigint_Expmod(tmp,rsa_pub,rsa_mod);
        if ( Bigint_Bitcount(tmp) <= len*8 && !memcmp(tmp->digits,data,len) )
          correct = 1;
      }
      
    return correct;
  }
#endif
  ;
  
int Bigint_Hex_Verify(YO_BIGINT *rsa_pub,YO_BIGINT *rsa_mod,char *S,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    int correct = 0;
    __Auto_Release
      {
        correct = Bigint_Verify(rsa_pub,rsa_mod,
                                Bigint_Decode_16(S),
                                data,len);
      }
    return correct;
  }
#endif
  ;
  
#endif /* C_once_B4220D86_3019_4E13_8682_D7F809F4E829 */









|
|
|
>
>
>
>
>
>







 







|



|



|



|
|

>

|







|





|







|



|



|




|
|








|





|











869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
....
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431

char *Bigint_Encode_16(YO_BIGINT *bint)
#ifdef _YO_BINGINT_BUILTIN
  {
    char *ret = 0;
    __Auto_Ptr(ret)
      {
        int i,L,bc = (Bigint_Bitcount(bint)+7)/8;
        ret = Str_Hex_Encode(bint->value,bc);
        L = ret?strlen(ret):0;
        for ( i = 0; i < L/2; i+=2 )
          { 
            char T;
            T = ret[i]; ret[i] = ret[(L-i)-2]; ret[(L-i)-2] = T;
            T = ret[i+1]; ret[i+1] = ret[(L-i)-1]; ret[(L-i)-1] = T;
          }
      }
    return ret;
  }
#endif
  ;

YO_BIGINT *Bigint_Mul(YO_BIGINT *bint, YO_BIGINT *d)
................................................................................
#else
    #error fixme!
#endif
  }
#endif
  ;

YO_BIGINT *Bigint_Sign(YO_BIGINT *K,YO_BIGINT *N,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    YO_BIGINT *tmp = 0;
    int mbits = Bigint_Bitcount(N)-1;
    if ( len*8 > mbits )
      __Raise_Format(YO_ERROR_OUT_OF_RANGE,
                      (__yoTa("data length is %d bytes, maximum %bytes alowed to sign with %d bits keys",0),
                       len,mbits/8,mbits));
    
    __Auto_Ptr(tmp)
      {
        void *Q = __Zero_Malloc((mbits+1)/8);
        //Soft_Random(Q,(mbits+1)/8);
        memcpy(Q,data,len);
        ((byte_t*)Q)[mbits/8] &= ~(1<<(mbits%8)); 
        tmp = Bigint_From_Bytes(0,Q,mbits/8);
        tmp = Bigint_Expmod(tmp,K,N);
      }
      
    return tmp;
  }
#endif
  ;
  
char *Bigint_Hex_Sign(YO_BIGINT *K,YO_BIGINT *N,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    char *hex = 0;
    __Auto_Ptr(hex)
      {
        YO_BIGINT *bint = Bigint_Sign(K,N,data,len);
        hex = Bigint_Encode_16(bint);
      }
    return hex;
  }
#endif
  ;
    
int Bigint_Verify(YO_BIGINT *K,YO_BIGINT *N,YO_BIGINT *digest,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    int correct = 0;
    int mbits = Bigint_Bitcount(N)-1;
    if ( len*8 > mbits )
      __Raise_Format(YO_ERROR_OUT_OF_RANGE,
                      (__yoTa("data length is %d bytes, maximum %bytes alowed to sign with %d bits keys",0),
                       len,mbits/8,mbits));

    __Auto_Release
      {
        YO_BIGINT *tmp = Bigint_Copy(digest);
        tmp = Bigint_Expmod(tmp,K,N);
        if ( Bigint_Bitcount(tmp) <= len*8 && !memcmp(tmp->value,data,len) )
          correct = 1;
      }
      
    return correct;
  }
#endif
  ;
  
int Bigint_Hex_Verify(YO_BIGINT *K,YO_BIGINT *N,char *S,void *data,int len)
#ifdef _YO_BINGINT_BUILTIN
  {
    int correct = 0;
    __Auto_Release
      {
        correct = Bigint_Verify(K,N,
                                Bigint_Decode_16(S),
                                data,len);
      }
    return correct;
  }
#endif
  ;
  
#endif /* C_once_B4220D86_3019_4E13_8682_D7F809F4E829 */


Changes to libyoyo.hc.

53
54
55
56
57
58
59
60
61
62
63
64
#include "array.hc"
#include "buffer.hc"
#include "string.hc"
#include "prog.hc"
#include "file.hc"
#include "random.hc"
#include "xdata.hc"
#include "defpars.hc"
#include "datetime.hc"

#endif /* C_once_9C09BC1E_F80E_4C2D_8EF5_8B6E388D7975 */








|




53
54
55
56
57
58
59
60
61
62
63
64
#include "array.hc"
#include "buffer.hc"
#include "string.hc"
#include "prog.hc"
#include "file.hc"
#include "random.hc"
#include "xdata.hc"
#include "xdef.hc"
#include "datetime.hc"

#endif /* C_once_9C09BC1E_F80E_4C2D_8EF5_8B6E388D7975 */

Changes to tests/rsa.c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#define _LIBYOYO
#include "../libyoyo.hc"
#include "../bigint.hc"

enum { MAX_ITER_COUNT = 3 };

int main(int argc, char **argv)
  {
    int bits, i;
    YOYO_BIGINT *data;
    
    Prog_Init(argc,argv,"?|h",PROG_EXIT_ON_ERROR);
    bits = Str_To_Int(Prog_Argument_Dflt(0,"128"));
    data = Bigint_Expand(0,(bits+sizeof(halflong_t)*8-1)/(sizeof(halflong_t)*8));
    System_Random(data->value,data->digits*sizeof(halflong_t));
    Bigint_Rshift_1(data);
    //data = Bigint_Random(bits-1);
    puts(Bigint_Encode_16(data));
    
    //__Raise_User_Error("test");
    
    for ( i = 0; i < MAX_ITER_COUNT; ++i ) __Auto_Release
      {
        YOYO_BIGINT *data_e, *data_x;
        YOYO_BIGINT *p, *e, *m;
        Bigint_Generate_Rsa_Key_Pair(&e,&p,&m,bits);
        data_e = Bigint_Expmod(data,p,m);
        data_x = Bigint_Expmod(data_e,e,m);
        puts("--------------------------");
        printf("P:%s\n",Bigint_Encode_10(p));
        printf("E:%s\n",Bigint_Encode_10(e));
        printf("M:%s\n",Bigint_Encode_10(m));
        puts(Bigint_Encode_16(data_x));
        REQUIRE( Bigint_Equal(data_x,data) );
      }
    
    return 0;
  }
  





|




|
|







|

|


|
|










|


<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


#define _LIBYOYO
#include "../libyoyo.hc"
#include "../bigint.hc"

enum { MAX_ITER_COUNT = 1 };

int main(int argc, char **argv)
  {
    int bits, i;
    YO_BIGINT *data;

    Prog_Init(argc,argv,"?|h",PROG_EXIT_ON_ERROR);
    bits = Str_To_Int(Prog_Argument_Dflt(0,"128"));
    data = Bigint_Expand(0,(bits+sizeof(halflong_t)*8-1)/(sizeof(halflong_t)*8));
    System_Random(data->value,data->digits*sizeof(halflong_t));
    Bigint_Rshift_1(data);
    //data = Bigint_Random(bits-1);
    puts(Bigint_Encode_16(data));

    //__Raise_User_Error("test");

    for ( i = 0; i < MAX_ITER_COUNT; ++i ) __Auto_Release
      {
        YO_BIGINT *data_e, *data_x;
        YO_BIGINT *p, *e, *m;
        Bigint_Generate_Rsa_Key_Pair(&e,&p,&m,bits);
        data_e = Bigint_Expmod(data,p,m);
        data_x = Bigint_Expmod(data_e,e,m);
        puts("--------------------------");
        printf("P:%s\n",Bigint_Encode_10(p));
        printf("E:%s\n",Bigint_Encode_10(e));
        printf("M:%s\n",Bigint_Encode_10(m));
        puts(Bigint_Encode_16(data_x));
        REQUIRE( Bigint_Equal(data_x,data) );
      }

    return 0;
  }

Changes to xdata.hc.

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
....
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
#ifdef _LIBYOYO
#define _YO_XDATA_BUILTIN
#endif

#include "yoyo.hc"
#include "string.hc"
#include "dicto.hc"
#include "sha1.hc"
#include "sha2.hc"
#include "md5.hc"
#include "bigint.hc"

#define XNODE_MAX_NAME_INDEX_PTR  ((char*)0x07fff)
#define XNODE_NUMBER_OF_NODE_LISTS 9

enum XVALUE_OPT_VALTYPE
  {
    XVALUE_OPT_VALTYPE_NONE     = 0x08000,
................................................................................
    
    value = Xnode_Query_Value(n,query);
    if ( value )
      return Xvalue_Get_Int(value,dflt);
      
    return dflt;
  }
#endif
  ;

YO_ARRAY *Xnode_Sorted_Names(YO_XNODE *n)
#ifdef _YO_XDATA_BUILTIN
  {
    char      *tag; 
    ushort_t  next;
    YO_XDATA  *doc;
    YO_XNODE  *nod;
    YO_XVALUE *val;
    YO_ARRAY  *a = Array_Void();
    __Object_Extend(a,Oj_Compare_Elements_OjMID,strcmp);
    
    n = Xnode_Refacc(n);
    STRICT_REQUIRE( (n->opt&XVALUE_OPT_IS_VALUE) == 0 );
    
    doc = n->xdata;
    next = n->opt;
    
    while ( next )
      {
        val = (YO_XVALUE *)Xdata_Idxref(doc,next);
        tag = doc->tags[val->tag-1];
        if ( !Str_Starts_With(tag,"$$") )
          Array_Sorted_Insert(a,tag);
        next = val->next;
      }
      
    next = n->down;
    while ( next )
      {
        nod = (YO_XNODE *)Xdata_Idxref(doc,next);
        tag = doc->tags[nod->tag-1];
        if ( !Str_Starts_With(tag,"$$") )
          Array_Sorted_Insert(a,tag);
        next = nod->next;
      }

    return a;    
  }      
#endif
  ;

void Xvalue_Normal_Print(YO_XVALUE *val, YO_BUFFER *bf)
#ifdef _YO_XDATA_BUILTIN
  {__Auto_Release {
    int i;
    byte_t dgst[16];
    char   hexdgst[33];
    
    switch ( val->opt&XVALUE_OPT_VALTYPE_MASK )
      {
      case XVALUE_OPT_VALTYPE_NONE:
        Buffer_Append(bf,"\0\0",2);
        break;
      case XVALUE_OPT_VALTYPE_BOOL:
        Buffer_Append(bf,(val->bval?"1":"0"),1);
        break;
      case XVALUE_OPT_VALTYPE_INT:
        Buffer_Printf(bf,"%ld",val->dec);
        break;
      case XVALUE_OPT_VALTYPE_FLT:
        if ( val->flt - (double)((long)val->flt) > 0.0009999999 )
          Buffer_Printf(bf,"%.3f",val->flt);
        else
          Buffer_Printf(bf,"%.f",val->flt);
        break;
      case XVALUE_OPT_VALTYPE_STR:
        Buffer_Append(bf,val->txt,-1);
        break;
      case XVALUE_OPT_VALTYPE_LIT:
        Buffer_Append(bf,(char*)&val->down,-1);
        break;
      case XVALUE_OPT_VALTYPE_BIN:
        Md5_Digest(val->binary->at,val->binary->count,dgst);
        Str_Hex_Encode_(dgst,16,hexdgst);
        Buffer_Fill_Append(bf,'\3',1);
        Buffer_Append(bf,hexdgst,32);
      case XVALUE_OPT_VALTYPE_STR_ARR:
        Buffer_Fill_Append(bf,'[',1);
        for ( i = 0; i < val->strarr->count; ++i )
          {
            Buffer_Fill_Append(bf,'\1',1);
            Buffer_Append(bf,val->strarr->at[i],-1);
          }
        Buffer_Fill_Append(bf,']',1);
        break;
      case XVALUE_OPT_VALTYPE_FLT_ARR:
        for ( i = 0; i+iszof_double <= val->binary->count; i+=iszof_double )
          {
            double d = *(double*)(val->binary->at+i*iszof_double);
            Buffer_Fill_Append(bf,'\1',1);
            if ( (d - (double)((long)d)) > 0.000999999 )
              Buffer_Printf(bf,"%.3f",d);
            else
              Buffer_Printf(bf,"%.f",d);
          }
        break;
      }   
  }}
#endif
  ;

void Xnode_Normal_Print(YO_XNODE *n, YO_BUFFER *bf)
#ifdef _YO_XDATA_BUILTIN
  {__Auto_Release {
    int i;
    YO_ARRAY *a = Xnode_Sorted_Names(n);
    
    Buffer_Fill_Append(bf,'{',1);
    for( i = 0; i < a->count; ++i )
      {
        YO_XNODE  *nod, *next;
        YO_XVALUE *val = Xnode_Value(n,a->at[i],0);
  
        Buffer_Fill_Append(bf,'\1',1);
  
        if ( val )
          {
            Buffer_Append(bf,a->at[i],-1);
            Buffer_Fill_Append(bf,'\2',1);
            Xvalue_Normal_Print(val,bf);
          }
  
        nod = Xnode_Down_If(n,a->at[i]);
        next = Xnode_Next_If(nod,a->at[i]);
  
        if ( val && nod )
          Buffer_Fill_Append(bf,'\1',1);

        if ( nod )
          {    
            Buffer_Append(bf,a->at[i],-1);
            Buffer_Fill_Append(bf,'\2',1);
        
            if ( next )
              {
                Buffer_Fill_Append(bf,'[',1);
                Buffer_Fill_Append(bf,'\1',1);
              }
              
            Xnode_Normal_Print(nod,bf);

            while ( next )
              {
                Buffer_Fill_Append(bf,'\1',1);
                Xnode_Normal_Print(next,bf);
                next = Xnode_Next_If(next,a->at[i]);
                if ( !next )
                  Buffer_Fill_Append(bf,']',1);
              }
          }
      }
    Buffer_Fill_Append(bf,'}',1);
  }}
#endif
  ;

void Xdata_Sha1_Update(YO_XDATA *xdata,byte_t dgst[20])
#ifdef _YO_XDATA_BUILTIN
  {__Auto_Release {
    YO_BUFFER *bf = Buffer_Init(0);
    Xnode_Normal_Print(&xdata->root,bf);
    Sha1_Digest(bf->at,bf->count,dgst);      
  }}
#endif
  ;

void Xdata_Sha2_Update(YO_XDATA *xdata,byte_t dgst[32])
#ifdef _YO_XDATA_BUILTIN
  {__Auto_Release {
    YO_BUFFER *bf = Buffer_Init(0);
    Xnode_Normal_Print(&xdata->root,bf);
    Sha2_Digest(bf->at,bf->count,dgst);      
  }}
#endif
  ;

int Xdata_Sha1_Verify(YO_XDATA *xdata)
#ifdef _YO_XDATA_BUILTIN
  {
    byte_t dgst1[20] = {0};
    byte_t dgst2[20] = {0};
    char *sha1 = Xnode_Value_Get_Str(&xdata->root,"$$sha1",0);
    if ( sha1 && strlen(sha1) == 40 )
      {
        Str_Hex_Decode_(sha1,0,dgst1);
        Xdata_Sha1_Digest_Update(xdata,dgst2);
        return !memcmp(dgst2,dgst1,20);
      }
    return 0;
  }
#endif
  ;

#endif /*C_once_E46D6A8A_889E_4AE9_9F89_5B0AB5263C95*/








<
<
<
<







 










<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


49
50
51
52
53
54
55




56
57
58
59
60
61
62
....
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422

































































































































































































1423
1424
#ifdef _LIBYOYO
#define _YO_XDATA_BUILTIN
#endif

#include "yoyo.hc"
#include "string.hc"
#include "dicto.hc"





#define XNODE_MAX_NAME_INDEX_PTR  ((char*)0x07fff)
#define XNODE_NUMBER_OF_NODE_LISTS 9

enum XVALUE_OPT_VALTYPE
  {
    XVALUE_OPT_VALTYPE_NONE     = 0x08000,
................................................................................
    
    value = Xnode_Query_Value(n,query);
    if ( value )
      return Xvalue_Get_Int(value,dflt);
      
    return dflt;
  }
#endif
  ;


































































































































































































#endif /*C_once_E46D6A8A_889E_4AE9_9F89_5B0AB5263C95*/

Added xdatasec.hc.











































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261


#ifndef C_once_9CC5C44A_A9F9_40B3_85DA_10A2EE2AD4FC
#define C_once_9CC5C44A_A9F9_40B3_85DA_10A2EE2AD4FC

#include "xdata.hc"
#include "sha1.hc"
#include "sha2.hc"
#include "md5.hc"
#include "bigint.hc"

#ifdef _LIBYOYO
  #define _YO_XDATASEC_BUILTIN
#endif

YO_ARRAY *Xnode_Sorted_Names(YO_XNODE *n)
#ifdef _YO_XDATASEC_BUILTIN
  {
    char      *tag; 
    ushort_t  next;
    YO_XDATA  *doc;
    YO_XNODE  *nod;
    YO_XVALUE *val;
    YO_ARRAY  *a = Array_Void();
    __Object_Extend(a,Oj_Compare_Elements_OjMID,strcmp);
    
    n = Xnode_Refacc(n);
    STRICT_REQUIRE( (n->opt&XVALUE_OPT_IS_VALUE) == 0 );
    
    doc = n->xdata;
    next = n->opt;
    
    while ( next )
      {
        val = (YO_XVALUE *)Xdata_Idxref(doc,next);
        tag = doc->tags[val->tag-1];
        if ( !Str_Starts_With(tag,"$$") )
          Array_Sorted_Insert(a,tag);
        next = val->next;
      }
      
    next = n->down;
    while ( next )
      {
        nod = (YO_XNODE *)Xdata_Idxref(doc,next);
        tag = doc->tags[nod->tag-1];
        if ( !Str_Starts_With(tag,"$$") )
          Array_Sorted_Insert(a,tag);
        next = nod->next;
      }

    return a;    
  }      
#endif
  ;

void Xvalue_Normal_Print(YO_XVALUE *val, YO_BUFFER *bf)
#ifdef _YO_XDATASEC_BUILTIN
  {__Auto_Release {
    int i;
    byte_t dgst[16];
    char   hexdgst[33];
    
    switch ( val->opt&XVALUE_OPT_VALTYPE_MASK )
      {
      case XVALUE_OPT_VALTYPE_NONE:
        Buffer_Append(bf,"\0\0",2);
        break;
      case XVALUE_OPT_VALTYPE_BOOL:
        Buffer_Append(bf,(val->bval?"1":"0"),1);
        break;
      case XVALUE_OPT_VALTYPE_INT:
        Buffer_Printf(bf,"%ld",val->dec);
        break;
      case XVALUE_OPT_VALTYPE_FLT:
        if ( val->flt - (double)((long)val->flt) > 0.0009999999 )
          Buffer_Printf(bf,"%.3f",val->flt);
        else
          Buffer_Printf(bf,"%.f",val->flt);
        break;
      case XVALUE_OPT_VALTYPE_STR:
        Buffer_Append(bf,val->txt,-1);
        break;
      case XVALUE_OPT_VALTYPE_LIT:
        Buffer_Append(bf,(char*)&val->down,-1);
        break;
      case XVALUE_OPT_VALTYPE_BIN:
        Md5_Digest(val->binary->at,val->binary->count,dgst);
        Str_Hex_Encode_(dgst,16,hexdgst);
        Buffer_Fill_Append(bf,'\3',1);
        Buffer_Append(bf,hexdgst,32);
      case XVALUE_OPT_VALTYPE_STR_ARR:
        Buffer_Fill_Append(bf,'[',1);
        for ( i = 0; i < val->strarr->count; ++i )
          {
            Buffer_Fill_Append(bf,'\1',1);
            Buffer_Append(bf,val->strarr->at[i],-1);
          }
        Buffer_Fill_Append(bf,']',1);
        break;
      case XVALUE_OPT_VALTYPE_FLT_ARR:
        for ( i = 0; i+iszof_double <= val->binary->count; i+=iszof_double )
          {
            double d = *(double*)(val->binary->at+i*iszof_double);
            Buffer_Fill_Append(bf,'\1',1);
            if ( (d - (double)((long)d)) > 0.000999999 )
              Buffer_Printf(bf,"%.3f",d);
            else
              Buffer_Printf(bf,"%.f",d);
          }
        break;
      }   
  }}
#endif
  ;

void Xnode_Normal_Print(YO_XNODE *n, YO_BUFFER *bf)
#ifdef _YO_XDATASEC_BUILTIN
  {__Auto_Release {
    int i;
    YO_ARRAY *a = Xnode_Sorted_Names(n);
    
    Buffer_Fill_Append(bf,'{',1);
    for( i = 0; i < a->count; ++i )
      {
        YO_XNODE  *nod, *next;
        YO_XVALUE *val = Xnode_Value(n,a->at[i],0);
  
        Buffer_Fill_Append(bf,'\1',1);
  
        if ( val )
          {
            Buffer_Append(bf,a->at[i],-1);
            Buffer_Fill_Append(bf,'\2',1);
            Xvalue_Normal_Print(val,bf);
          }
  
        nod = Xnode_Down_If(n,a->at[i]);
        next = Xnode_Next_If(nod,a->at[i]);
  
        if ( val && nod )
          Buffer_Fill_Append(bf,'\1',1);

        if ( nod )
          {    
            Buffer_Append(bf,a->at[i],-1);
            Buffer_Fill_Append(bf,'\2',1);
        
            if ( next )
              {
                Buffer_Fill_Append(bf,'[',1);
                Buffer_Fill_Append(bf,'\1',1);
              }
              
            Xnode_Normal_Print(nod,bf);

            while ( next )
              {
                Buffer_Fill_Append(bf,'\1',1);
                Xnode_Normal_Print(next,bf);
                next = Xnode_Next_If(next,a->at[i]);
                if ( !next )
                  Buffer_Fill_Append(bf,']',1);
              }
          }
      }
    Buffer_Fill_Append(bf,'}',1);
  }}
#endif
  ;

void Xdata_Sha1_Digest_Update(YO_XDATA *xdata,byte_t dgst[20])
#ifdef _YO_XDATASEC_BUILTIN
  {__Auto_Release {
    YO_BUFFER *bf = Buffer_Init(0);
    Xnode_Normal_Print(&xdata->root,bf);
    Sha1_Digest(bf->at,bf->count,dgst);      
  }}
#endif
  ;

void Xdata_Sha1_Update(YO_XDATA *xdata)
#ifdef _YO_XDATASEC_BUILTIN
  {
    byte_t dgst[20];
    byte_t hex[41];
    Xdata_Sha1_Digest_Update(xdata,dgst);
    Xnode_Value_Set_Str(&xdata->root,"$$sha1",Str_Hex_Encode_(dgst,20,hex));
  }
#endif
  ;
  
void Xdata_Sha2_Digest_Update(YO_XDATA *xdata,byte_t dgst[32])
#ifdef _YO_XDATASEC_BUILTIN
  {__Auto_Release {
    YO_BUFFER *bf = Buffer_Init(0);
    Xnode_Normal_Print(&xdata->root,bf);
    Sha2_Digest(bf->at,bf->count,dgst);      
  }}
#endif
  ;

void Xdata_Sha2_Update(YO_XDATA *xdata)
#ifdef _YO_XDATASEC_BUILTIN
  {
    byte_t dgst[32];
    byte_t hex[65];
    Xdata_Sha2_Digest_Update(xdata,dgst);
    Xnode_Value_Set_Str(&xdata->root,"$$sha2",Str_Hex_Encode_(dgst,32,hex));
  }
#endif
  ;

int Xdata_Sha1_Verify(YO_XDATA *xdata)
#ifdef _YO_XDATASEC_BUILTIN
  {
    byte_t dgst1[20] = {0};
    byte_t dgst2[20] = {0};
    char *sha1 = Xnode_Value_Get_Str(&xdata->root,"$$sha1",0);
    if ( sha1 && strlen(sha1) == 40 )
      {
        Str_Hex_Decode_(sha1,0,dgst1);
        Xdata_Sha1_Digest_Update(xdata,dgst2);
        return !memcmp(dgst2,dgst1,20);
      }
    return 0;
  }
#endif
  ;

void Xdata_Rsa_Update(YO_XDATA *xdata,YO_BIGINT *K,YO_BIGINT *N)
#ifdef _YO_XDATASEC_BUILTIN
  {__Auto_Release {
    byte_t dgst[32];
    YO_BUFFER *bf = Buffer_Init(0);
    Xnode_Normal_Print(&xdata->root,bf);
    Sha2_Digest(bf->at,bf->count,dgst);
    Xnode_Value_Set_Str(&xdata->root,"$$rsa",Bigint_Hex_Sign(K,N,dgst,32));      
  }}
#endif
  ;

int Xdata_Rsa_Verify(YO_XDATA *xdata,YO_BIGINT *K,YO_BIGINT *N)
#ifdef _YO_XDATASEC_BUILTIN
  {
    int ok = 0;
    char *hex = Xnode_Value_Get_Str(&xdata->root,"$$rsa",0);
    if ( hex ) __Auto_Release 
      {
        byte_t dgst[32];
        YO_BUFFER *bf = Buffer_Init(0);
        Xnode_Normal_Print(&xdata->root,bf);
        Sha2_Digest(bf->at,bf->count,dgst);
        ok = Bigint_Hex_Verify(K,N,hex,dgst,32);
      }
    return ok;
  }
#endif
  ;

#endif /* C_once_9CC5C44A_A9F9_40B3_85DA_10A2EE2AD4FC */