Unnamed Fossil Project

Check-in [718eda106a]
Login

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

Overview
Comment:sha/rsa for xdata
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:718eda106a8e7e2c6200b8c49aba2dee98f930b0
User & Date: monster 2012-07-24 22:17:48
Context
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
17:27
updated check-in: d07351188b user: monster tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to array.hc.

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
    void **iS = S;
    void **middle = iS;
    int half;
    int len = S_len;

    if ( len  )
      {
        if ( len > 1 )
          while (len > 0)
            {
              half = len >> 1;
              middle = iS + half;
              if ( (cmp_r = ((Tcmp)compare)(*middle,val)) < 0 )
                {
                  iS = middle;
                  ++iS;
                  len = len - half - 1;
                }
              else
                len = half;
            }
        else
          middle = 0;
          
        if ( middle != iS && iS < S+S_len )

          cmp_r = ((Tcmp)compare)(*iS,val);

      }
      
    *found = !cmp_r;
    return iS;
  }
#endif
  ;







<
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<


>
|
>







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
    void **iS = S;
    void **middle = iS;
    int half;
    int len = S_len;

    if ( len  )
      {

        while (len > 0)
          {
            half = len >> 1;
            middle = iS + half;
            if ( (cmp_r = ((Tcmp)compare)(*middle,val)) < 0 )
              {
                iS = middle;
                ++iS;
                len = len - half - 1;
              }
            else
              len = half;
          }


          
        if ( middle != iS && iS < S+S_len )
          {
            cmp_r = ((Tcmp)compare)(*iS,val);
          }
      }
      
    *found = !cmp_r;
    return iS;
  }
#endif
  ;

Changes to bigint.hc.

1336
1337
1338
1339
1340
1341
1342
1343
1344
1345












































































1346
1347
1348
      __Raise(YO_ERROR_NO_ENOUGH,"raw bigint outbuffer to small!");
    memcpy(out,bint->value,l);
    return l;
#else
    #error fixme!
#endif
  }
#endif
  ;
  












































































#endif /* C_once_B4220D86_3019_4E13_8682_D7F809F4E829 */











|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



1336
1337
1338
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
      __Raise(YO_ERROR_NO_ENOUGH,"raw bigint outbuffer to small!");
    memcpy(out,bint->value,l);
    return l;
#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 */


Changes to string.hc.

486
487
488
489
490
491
492

493
494
495
496
497
498
499

500
501
502
503
504
505
506
507
...
534
535
536
537
538
539
540

541
542
543
544
545
546
547
548
549

550
551
552
553
554
555
556
557
    *q++ = symbols[val&0x0f];
    *q = 0;
    return out;
  }
#endif
  ;


char *Str_Hex_Encode(void *data, int len)
#ifdef _YO_STRING_BUILTIN
  {
    if ( data && len )
      {
        int i;
        int rq_len = len*2;

        char *out = Yo_Malloc(rq_len+1);
        memset(out,0,rq_len+1);
        for ( i = 0; i < len; ++i )
          Str_Hex_Byte(((byte_t*)data)[i],0,out+i*2);
        return out;
      }
    return 0;
  }
................................................................................
      }
    if ( cnt ) *cnt = c-(byte_t*)S;
    return r;
  }
#endif
  ;


void *Str_Hex_Decode(char *S,int *len)
#ifdef _YO_STRING_BUILTIN
  {
    int S_len = S ? strlen(S): 0;
    int rq_len = S_len ? S_len/2 : 0;
    
    if ( rq_len )
      {
        int i;

        byte_t *out = Yo_Malloc(rq_len+1);
        for ( i = 0; i < rq_len; ++i )
          out[i] = Str_Unhex_Byte(S+i*2,0,0);
        out[rq_len] = 0;
        if ( len ) *len = rq_len;
        return out;
      }
    







>
|






>
|







 







>
|








>
|







486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    *q++ = symbols[val&0x0f];
    *q = 0;
    return out;
  }
#endif
  ;

#define Str_Hex_Encode(S,L) Str_Hex_Encode_(S,L,0)
char *Str_Hex_Encode_(void *data, int len, char *out)
#ifdef _YO_STRING_BUILTIN
  {
    if ( data && len )
      {
        int i;
        int rq_len = len*2;
        if ( !out )
          out = Yo_Malloc(rq_len+1);
        memset(out,0,rq_len+1);
        for ( i = 0; i < len; ++i )
          Str_Hex_Byte(((byte_t*)data)[i],0,out+i*2);
        return out;
      }
    return 0;
  }
................................................................................
      }
    if ( cnt ) *cnt = c-(byte_t*)S;
    return r;
  }
#endif
  ;

#define Str_Hex_Decode(S,L) Str_Hex_Decode_(S,L,0)
void *Str_Hex_Decode_(char *S,int *len,byte_t *out)
#ifdef _YO_STRING_BUILTIN
  {
    int S_len = S ? strlen(S): 0;
    int rq_len = S_len ? S_len/2 : 0;
    
    if ( rq_len )
      {
        int i;
        if ( !out )
          out = Yo_Malloc(rq_len+1);
        for ( i = 0; i < rq_len; ++i )
          out[i] = Str_Unhex_Byte(S+i*2,0,0);
        out[rq_len] = 0;
        if ( len ) *len = rq_len;
        return out;
      }
    

Changes to xdata.hc.

49
50
51
52
53
54
55




56
57
58
59
60
61
62
...
818
819
820
821
822
823
824

825
826
827
828
829
830
831
...
847
848
849
850
851
852
853

854
855
856
857
858
859
860
....
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420

































































































































































































1421
1422
#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,
................................................................................

YO_XNODE *Xnode_Down_If(YO_XNODE *node, char *tag_name)
#ifdef _YO_XDATA_BUILTIN
  {
    ushort_t tag;
    YO_XNODE *n;
      

    node = Xnode_Refacc(node);

    STRICT_REQUIRE( node );
    STRICT_REQUIRE( tag_name );
    STRICT_REQUIRE( (node->opt&XVALUE_OPT_IS_VALUE) == 0 );

    tag = (ushort_t)(longptr_t)Xdata_Resolve_Name(node->xdata,tag_name,0);
................................................................................

YO_XNODE *Xnode_Next_If(YO_XNODE *node, char *tag_name)
#ifdef _YO_XDATA_BUILTIN
  {
    ushort_t tag;
    YO_XNODE *n;
      

    n = Xnode_Refacc(node);
    
    STRICT_REQUIRE( n );
    STRICT_REQUIRE( tag_name );
    STRICT_REQUIRE( (n->opt&XVALUE_OPT_IS_VALUE) == 0 );
    
    tag = (ushort_t)(longptr_t)Xdata_Resolve_Name(n->xdata,tag_name,0);
................................................................................
    
    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*/








>
>
>
>







 







>







 







>







 










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
...
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
....
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,
................................................................................

YO_XNODE *Xnode_Down_If(YO_XNODE *node, char *tag_name)
#ifdef _YO_XDATA_BUILTIN
  {
    ushort_t tag;
    YO_XNODE *n;
      
    if ( !node ) return 0;
    node = Xnode_Refacc(node);

    STRICT_REQUIRE( node );
    STRICT_REQUIRE( tag_name );
    STRICT_REQUIRE( (node->opt&XVALUE_OPT_IS_VALUE) == 0 );

    tag = (ushort_t)(longptr_t)Xdata_Resolve_Name(node->xdata,tag_name,0);
................................................................................

YO_XNODE *Xnode_Next_If(YO_XNODE *node, char *tag_name)
#ifdef _YO_XDATA_BUILTIN
  {
    ushort_t tag;
    YO_XNODE *n;
      
    if ( !node ) return 0;
    n = Xnode_Refacc(node);
    
    STRICT_REQUIRE( n );
    STRICT_REQUIRE( tag_name );
    STRICT_REQUIRE( (n->opt&XVALUE_OPT_IS_VALUE) == 0 );
    
    tag = (ushort_t)(longptr_t)Xdata_Resolve_Name(n->xdata,tag_name,0);
................................................................................
    
    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*/