Changeset 700

Show
Ignore:
Timestamp:
08/30/09 13:17:39
Author:
ben
Message:

Layout.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/src/lib/signature.c

    r671 r700  
    9999*/ 
    100100ops_boolean_t encode_hash_buf(const unsigned char *M, size_t mLen, 
    101                            const ops_hash_algorithm_t hash_alg, 
    102                            unsigned char* EM 
    103 
     101                              const ops_hash_algorithm_t hash_alg, 
     102                              unsigned char* EM) 
    104103    { 
    105104    // implementation of EMSA-PKCS1-v1_5, as defined in OpenPGP RFC 
     
    120119    // 1. Apply hash function to M 
    121120 
    122     ops_hash_any(&hash,hash_alg); 
     121    ops_hash_any(&hash, hash_alg); 
    123122    hash.init(&hash); 
    124     hash.add(&hash,M,mLen); 
     123    hash.add(&hash, M, mLen); 
    125124 
    126125    // \todo combine with rsa_sign 
     
    158157    EM[i++]=0x00; 
    159158 
    160     memcpy(&EM[i],prefix,prefix_sz); 
     159    memcpy(&EM[i], prefix, prefix_sz); 
    161160    i+=prefix_sz; 
    162161 
    163162    // finally, write out hashed result 
    164163     
    165     n=hash.finish(&hash,&EM[i]); 
     164    n=hash.finish(&hash, &EM[i]); 
    166165 
    167166    encoded_msg_sz=i+hash_sz-1; 
     
    171170    if (debug) 
    172171        { 
    173         fprintf(stderr,"Encoded Message: \n"); 
     172        fprintf(stderr, "Encoded Message: \n"); 
    174173        for (i=0; i<encoded_msg_sz; i++) 
    175             fprintf(stderr,"%2x ", EM[i]); 
    176         fprintf(stderr,"\n"); 
     174            fprintf(stderr, "%2x ", EM[i]); 
     175        fprintf(stderr, "\n"); 
    177176        } 
    178177 
     
    182181// XXX: both this and verify would be clearer if the signature were 
    183182// treated as an MPI. 
    184 static void rsa_sign(ops_hash_t *hash,const ops_rsa_public_key_t *rsa, 
    185                      const ops_rsa_secret_key_t *srsa, 
    186                      ops_create_info_t *opt) 
     183static void rsa_sign(ops_hash_t *hash, const ops_rsa_public_key_t *rsa, 
     184                     const ops_rsa_secret_key_t *srsa, ops_create_info_t *opt) 
    187185    { 
    188186    unsigned char hashbuf[8192]; 
     
    197195    hashsize=20+sizeof prefix_sha1; 
    198196 
    199     keysize=(BN_num_bits(rsa->n)+7)/8
     197    keysize=BN_num_bytes(rsa->n)
    200198    assert(keysize <= sizeof hashbuf); 
    201199    assert(10+hashsize <= keysize); 
     
    204202    hashbuf[1]=1; 
    205203    if (debug) 
    206         { printf("rsa_sign: PS is %d\n", keysize-hashsize-1-2); } 
     204        printf("rsa_sign: PS is %d\n", keysize-hashsize-1-2); 
    207205    for(n=2 ; n < keysize-hashsize-1 ; ++n) 
    208206        hashbuf[n]=0xff; 
    209207    hashbuf[n++]=0; 
    210208 
    211     memcpy(&hashbuf[n],prefix_sha1,sizeof prefix_sha1); 
     209    memcpy(&hashbuf[n], prefix_sha1, sizeof prefix_sha1); 
    212210    n+=sizeof prefix_sha1; 
    213211 
    214     t=hash->finish(hash,&hashbuf[n]); 
     212    t=hash->finish(hash, &hashbuf[n]); 
    215213    assert(t == 20); 
    216214 
    217     ops_write(&hashbuf[n],2,opt); 
     215    ops_write(&hashbuf[n], 2, opt); 
    218216 
    219217    n+=t; 
    220218    assert(n == keysize); 
    221219 
    222     t=ops_rsa_private_encrypt(sigbuf,hashbuf,keysize,srsa,rsa); 
    223     bn=BN_bin2bn(sigbuf,t,NULL); 
    224     ops_write_mpi(bn,opt); 
     220    t=ops_rsa_private_encrypt(sigbuf, hashbuf, keysize, srsa, rsa); 
     221    bn=BN_bin2bn(sigbuf, t, NULL); 
     222    ops_write_mpi(bn, opt); 
    225223    BN_free(bn); 
    226224    } 
    227225 
    228 static void dsa_sign(ops_hash_t *hash,  
    229                      const ops_dsa_public_key_t *dsa, 
    230                      const ops_dsa_secret_key_t *sdsa, 
    231                      ops_create_info_t *cinfo) 
     226static void dsa_sign(ops_hash_t *hash, const ops_dsa_public_key_t *dsa, 
     227                     const ops_dsa_secret_key_t *sdsa, ops_create_info_t *cinfo) 
    232228    { 
    233229    unsigned char hashbuf[8192]; 
     
    242238 
    243239    // finalise hash 
    244     t=hash->finish(hash,&hashbuf[0]); 
    245     assert(t==20); 
    246  
    247     ops_write(&hashbuf[0],2,cinfo); 
     240    t=hash->finish(hash, &hashbuf[0]); 
     241    assert(t == 20); 
     242 
     243    ops_write(&hashbuf[0], 2, cinfo); 
    248244 
    249245    // write signature to buf 
    250246    DSA_SIG* dsasig; 
    251     dsasig=ops_dsa_sign(hashbuf,hashsize,sdsa,dsa); 
     247    dsasig=ops_dsa_sign(hashbuf, hashsize, sdsa, dsa); 
    252248 
    253249    // convert and write the sig out to memory 
    254     ops_write_mpi(dsasig->r,cinfo); 
    255     ops_write_mpi(dsasig->s,cinfo); 
     250    ops_write_mpi(dsasig->r, cinfo); 
     251    ops_write_mpi(dsasig->s, cinfo); 
    256252    DSA_SIG_free(dsasig); 
    257253    } 
    258254 
    259255static ops_boolean_t rsa_verify(ops_hash_algorithm_t type, 
    260                                 const unsigned char *hash,size_t hash_length, 
     256                                const unsigned char *hash, size_t hash_length, 
    261257                                const ops_rsa_signature_t *sig, 
    262258                                const ops_rsa_public_key_t *rsa) 
     
    273269    assert(keysize <= sizeof hashbuf_from_sig); 
    274270    assert((unsigned)BN_num_bits(sig->sig) <= 8*sizeof sigbuf); 
    275     BN_bn2bin(sig->sig,sigbuf); 
    276  
    277     n=ops_rsa_public_decrypt(hashbuf_from_sig,sigbuf,(BN_num_bits(sig->sig)+7)/8,rsa); 
     271    BN_bn2bin(sig->sig, sigbuf); 
     272 
     273    n=ops_rsa_public_decrypt(hashbuf_from_sig, sigbuf, BN_num_bytes(sig->sig), 
     274                             rsa); 
    278275    int debug_len_decrypted=n; 
    279276 
     
    311308        printf("hashbuf_from_sig\n"); 
    312309        for (zz=0; zz<debug_len_decrypted; zz++) 
    313             { printf("%02x ", hashbuf_from_sig[n+zz]); } 
     310            printf("%02x ", hashbuf_from_sig[n+zz]); 
    314311        printf("\n"); 
    315312        printf("prefix\n"); 
    316313        for (zz=0; zz<plen; zz++) 
    317             { printf("%02x ", prefix[zz]); } 
     314            printf("%02x ", prefix[zz]); 
    318315        printf("\n"); 
    319316 
     
    322319        unsigned uu; 
    323320        for (uu=0; uu<hash_length; uu++) 
    324             { printf("%02x ", hashbuf_from_sig[n+plen+uu]); } 
     321            printf("%02x ", hashbuf_from_sig[n+plen+uu]); 
    325322        printf("\n"); 
    326323        printf("hash passed in (should match hash from sig)\n"); 
    327324        for (uu=0; uu<hash_length; uu++) 
    328             { printf("%02x ", hash[uu]); } 
     325            printf("%02x ", hash[uu]); 
    329326        printf("\n"); 
    330327        } 
    331     if(memcmp(&hashbuf_from_sig[n],prefix,plen) 
    332        || memcmp(&hashbuf_from_sig[n+plen],hash,hash_length)) 
     328    if(memcmp(&hashbuf_from_sig[n], prefix, plen) 
     329       || memcmp(&hashbuf_from_sig[n+plen], hash, hash_length)) 
    333330        return ops_false; 
    334331 
     
    336333    } 
    337334 
    338 static void hash_add_key(ops_hash_t *hash,const ops_public_key_t *key) 
     335static void hash_add_key(ops_hash_t *hash, const ops_public_key_t *key) 
    339336    { 
    340337    ops_memory_t *mem=ops_memory_new(); 
    341338    size_t l; 
    342339 
    343     ops_build_public_key(mem,key,ops_false); 
     340    ops_build_public_key(mem, key, ops_false); 
    344341 
    345342    l=ops_memory_get_length(mem); 
    346     ops_hash_add_int(hash,0x99,1); 
    347     ops_hash_add_int(hash,l,2); 
    348     hash->add(hash,ops_memory_get_data(mem),l); 
     343    ops_hash_add_int(hash, 0x99, 1); 
     344    ops_hash_add_int(hash, l, 2); 
     345    hash->add(hash, ops_memory_get_data(mem), l); 
    349346 
    350347    ops_memory_free(mem); 
    351348    } 
    352349 
    353 static void initialise_hash(ops_hash_t *hash,const ops_signature_t *sig) 
    354     { 
    355     ops_hash_any(hash,sig->info.hash_algorithm); 
     350static void initialise_hash(ops_hash_t *hash, const ops_signature_t *sig) 
     351    { 
     352    ops_hash_any(hash, sig->info.hash_algorithm); 
    356353    hash->init(hash); 
    357354    } 
    358355 
    359 static void init_key_signature(ops_hash_t *hash,const ops_signature_t *sig, 
    360                            const ops_public_key_t *key) 
    361     { 
    362     initialise_hash(hash,sig); 
    363     hash_add_key(hash,key); 
    364     } 
    365  
    366 static void hash_add_trailer(ops_hash_t *hash,const ops_signature_t *sig, 
     356static void init_key_signature(ops_hash_t *hash, const ops_signature_t *sig, 
     357                               const ops_public_key_t *key) 
     358    { 
     359    initialise_hash(hash, sig); 
     360    hash_add_key(hash, key); 
     361    } 
     362 
     363static void hash_add_trailer(ops_hash_t *hash, const ops_signature_t *sig, 
    367364                             const unsigned char *raw_packet) 
    368365    { 
     
    370367        { 
    371368        if(raw_packet) 
    372             hash->add(hash,raw_packet+sig->v4_hashed_data_start, 
     369            hash->add(hash, raw_packet+sig->v4_hashed_data_start, 
    373370                      sig->info.v4_hashed_data_length); 
    374         ops_hash_add_int(hash,sig->info.version,1); 
    375         ops_hash_add_int(hash,0xff,1); 
    376         ops_hash_add_int(hash,sig->info.v4_hashed_data_length,4); 
     371        ops_hash_add_int(hash, sig->info.version, 1); 
     372        ops_hash_add_int(hash, 0xff, 1); 
     373        ops_hash_add_int(hash, sig->info.v4_hashed_data_length, 4); 
    377374        } 
    378375    else 
    379376        { 
    380         ops_hash_add_int(hash,sig->info.type,1); 
    381         ops_hash_add_int(hash,sig->info.creation_time,4); 
     377        ops_hash_add_int(hash, sig->info.type, 1); 
     378        ops_hash_add_int(hash, sig->info.creation_time, 4); 
    382379        } 
    383380    } 
     
    392389   \return ops_true if good; else ops_false 
    393390*/ 
    394 ops_boolean_t ops_check_signature(const unsigned char *hash,unsigned length, 
     391ops_boolean_t ops_check_signature(const unsigned char *hash, unsigned length, 
    395392                                     const ops_signature_t *sig, 
    396393                                     const ops_public_key_t *signer) 
     
    407404        { 
    408405    case OPS_PKA_DSA: 
    409         ret=ops_dsa_verify(hash,length,&sig->info.signature.dsa,&signer->key.dsa); 
     406        ret=ops_dsa_verify(hash, length, &sig->info.signature.dsa, 
     407                           &signer->key.dsa); 
    410408        break; 
    411409 
    412410    case OPS_PKA_RSA: 
    413         ret=rsa_verify(sig->info.hash_algorithm,hash,length,&sig->info.signature.rsa
    414                        &signer->key.rsa); 
     411        ret=rsa_verify(sig->info.hash_algorithm, hash, length
     412                       &sig->info.signature.rsa, &signer->key.rsa); 
    415413        break; 
    416414 
     
    429427    unsigned char hashout[OPS_MAX_HASH_SIZE]; 
    430428 
    431     n=hash->finish(hash,hashout); 
    432  
    433     return ops_check_signature(hashout,n,sig,signer); 
     429    n=hash->finish(hash, hashout); 
     430 
     431    return ops_check_signature(hashout, n, sig, signer); 
    434432    } 
    435433 
     
    439437                                        const unsigned char *raw_packet) 
    440438    { 
    441     hash_add_trailer(hash,sig,raw_packet); 
    442     return hash_and_check_signature(hash,sig,signer); 
     439    hash_add_trailer(hash, sig, raw_packet); 
     440    return hash_and_check_signature(hash, sig, signer); 
    443441    } 
    444442 
     
    465463    size_t user_id_len=strlen((char *)id->user_id); 
    466464 
    467     init_key_signature(&hash,sig,key); 
     465    init_key_signature(&hash, sig, key); 
    468466 
    469467    if(sig->info.version == OPS_V4) 
    470468        { 
    471         ops_hash_add_int(&hash,0xb4,1); 
    472         ops_hash_add_int(&hash,user_id_len,4); 
     469        ops_hash_add_int(&hash, 0xb4, 1); 
     470        ops_hash_add_int(&hash, user_id_len, 4); 
    473471        } 
    474     hash.add(&hash,id->user_id,user_id_len); 
    475  
    476     return finalise_signature(&hash,sig,signer,raw_packet); 
     472    hash.add(&hash, id->user_id, user_id_len); 
     473 
     474    return finalise_signature(&hash, sig, signer, raw_packet); 
    477475    } 
    478476 
     
    491489ops_boolean_t 
    492490ops_check_user_attribute_certification_signature(const ops_public_key_t *key, 
    493                                                 const ops_user_attribute_t *attribute, 
    494                                                 const ops_signature_t *sig, 
    495                                                 const ops_public_key_t *signer, 
    496                                                 const unsigned char *raw_packet) 
     491                                        const ops_user_attribute_t *attribute, 
     492                                        const ops_signature_t *sig, 
     493                                        const ops_public_key_t *signer, 
     494                                        const unsigned char *raw_packet) 
    497495    { 
    498496    ops_hash_t hash; 
    499497 
    500     init_key_signature(&hash,sig,key); 
     498    init_key_signature(&hash, sig, key); 
    501499 
    502500    if(sig->info.version == OPS_V4) 
    503501        { 
    504         ops_hash_add_int(&hash,0xd1,1); 
    505         ops_hash_add_int(&hash,attribute->data.len,4); 
     502        ops_hash_add_int(&hash, 0xd1, 1); 
     503        ops_hash_add_int(&hash, attribute->data.len, 4); 
    506504        } 
    507     hash.add(&hash,attribute->data.contents,attribute->data.len); 
    508  
    509     return finalise_signature(&hash,sig,signer,raw_packet); 
     505    hash.add(&hash, attribute->data.contents, attribute->data.len); 
     506 
     507    return finalise_signature(&hash, sig, signer, raw_packet); 
    510508    } 
    511509 
     
    531529    ops_hash_t hash; 
    532530 
    533     init_key_signature(&hash,sig,key); 
    534     hash_add_key(&hash,subkey); 
    535  
    536     return finalise_signature(&hash,sig,signer,raw_packet); 
     531    init_key_signature(&hash, sig, key); 
     532    hash_add_key(&hash, subkey); 
     533 
     534    return finalise_signature(&hash, sig, signer, raw_packet); 
    537535    } 
    538536 
     
    556554    ops_hash_t hash; 
    557555 
    558     init_key_signature(&hash,sig,key); 
    559     return finalise_signature(&hash,sig,signer,raw_packet); 
     556    init_key_signature(&hash, sig, key); 
     557    return finalise_signature(&hash, sig, signer, raw_packet); 
    560558    } 
    561559 
     
    573571 */ 
    574572ops_boolean_t 
    575 ops_check_hash_signature(ops_hash_t *hash, 
    576                          const ops_signature_t *sig, 
     573ops_check_hash_signature(ops_hash_t *hash, const ops_signature_t *sig, 
    577574                         const ops_public_key_t *signer) 
    578575    { 
     
    580577        return ops_false; 
    581578 
    582     return finalise_signature(hash,sig,signer,NULL); 
     579    return finalise_signature(hash, sig, signer, NULL); 
    583580    } 
    584581 
     
    588585    // thing to get counts before writing. 
    589586    sig->mem=ops_memory_new(); 
    590     ops_memory_init(sig->mem,100); 
    591     ops_writer_set_memory(sig->info,sig->mem); 
     587    ops_memory_init(sig->mem, 100); 
     588    ops_writer_set_memory(sig->info, sig->mem); 
    592589 
    593590    // write nearly up to the first subpacket 
    594     ops_write_scalar(sig->sig.info.version,1,sig->info); 
    595     ops_write_scalar(sig->sig.info.type,1,sig->info); 
    596     ops_write_scalar(sig->sig.info.key_algorithm,1,sig->info); 
    597     ops_write_scalar(sig->sig.info.hash_algorithm,1,sig->info); 
     591    ops_write_scalar(sig->sig.info.version, 1, sig->info); 
     592    ops_write_scalar(sig->sig.info.type, 1, sig->info); 
     593    ops_write_scalar(sig->sig.info.key_algorithm, 1, sig->info); 
     594    ops_write_scalar(sig->sig.info.hash_algorithm, 1, sig->info); 
    598595 
    599596    // dummy hashed subpacket count 
    600597    sig->hashed_count_offset=ops_memory_get_length(sig->mem); 
    601     ops_write_scalar(0,2,sig->info); 
     598    ops_write_scalar(0, 2, sig->info); 
    602599    }     
    603600 
     
    629626    sig->hashed_data_length=-1; 
    630627 
    631     init_key_signature(&sig->hash,&sig->sig,key); 
    632  
    633     ops_hash_add_int(&sig->hash,0xb4,1); 
    634     ops_hash_add_int(&sig->hash,strlen((char *)id->user_id),4); 
    635     sig->hash.add(&sig->hash,id->user_id,strlen((char *)id->user_id)); 
     628    init_key_signature(&sig->hash, &sig->sig, key); 
     629 
     630    ops_hash_add_int(&sig->hash, 0xb4, 1); 
     631    ops_hash_add_int(&sig->hash, strlen((char *)id->user_id), 4); 
     632    sig->hash.add(&sig->hash, id->user_id, strlen((char *)id->user_id)); 
    636633 
    637634    start_signature_in_mem(sig); 
     
    650647 
    651648static void ops_signature_start_signature(ops_create_signature_t *sig, 
    652                                              const ops_secret_key_t *key, 
    653                                              const ops_hash_algorithm_t hash, 
    654                                              const ops_sig_type_t type) 
     649                                          const ops_secret_key_t *key, 
     650                                          const ops_hash_algorithm_t hash, 
     651                                          const ops_sig_type_t type) 
    655652    { 
    656653    sig->info=ops_create_info_new(); 
     
    667664 
    668665    if (debug) 
    669         { fprintf(stderr,"initialising hash for sig in mem\n"); } 
    670     initialise_hash(&sig->hash,&sig->sig); 
     666        { fprintf(stderr, "initialising hash for sig in mem\n"); } 
     667    initialise_hash(&sig->hash, &sig->sig); 
    671668    start_signature_in_mem(sig); 
    672669    } 
     
    677674 */ 
    678675void ops_signature_start_cleartext_signature(ops_create_signature_t *sig, 
    679                                    const ops_secret_key_t *key, 
    680                                    const ops_hash_algorithm_t hash, 
    681                                    const ops_sig_type_t type) 
    682     { 
    683     ops_signature_start_signature(sig,key,hash,type); 
     676                                            const ops_secret_key_t *key, 
     677                                            const ops_hash_algorithm_t hash, 
     678                                            const ops_sig_type_t type) 
     679    { 
     680    ops_signature_start_signature(sig, key, hash, type); 
    684681    } 
    685682 
     
    689686 */ 
    690687void ops_signature_start_message_signature(ops_create_signature_t *sig, 
    691                                    const ops_secret_key_t *key, 
    692                                    const ops_hash_algorithm_t hash, 
    693                                    const ops_sig_type_t type) 
    694     { 
    695     ops_signature_start_signature(sig,key,hash,type); 
     688                                          const ops_secret_key_t *key, 
     689                                          const ops_hash_algorithm_t hash, 
     690                                          const ops_sig_type_t type) 
     691    { 
     692    ops_signature_start_signature(sig, key, hash, type); 
    696693    } 
    697694 
     
    705702 * \param length The amount of plaintext data. 
    706703 */ 
    707 void ops_signature_add_data(ops_create_signature_t *sig,const void *buf, 
     704void ops_signature_add_data(ops_create_signature_t *sig, const void *buf, 
    708705                            size_t length) 
    709706    { 
    710707    if (debug) 
    711         { fprintf(stderr,"ops_signature_add_data adds to hash\n"); } 
    712     sig->hash.add(&sig->hash,buf,length); 
     708        { fprintf(stderr, "ops_signature_add_data adds to hash\n"); } 
     709    sig->hash.add(&sig->hash, buf, length); 
    713710    } 
    714711 
     
    725722    sig->hashed_data_length=ops_memory_get_length(sig->mem) 
    726723        -sig->hashed_count_offset-2; 
    727     ops_memory_place_int(sig->mem,sig->hashed_count_offset, 
    728                          sig->hashed_data_length,2); 
     724    ops_memory_place_int(sig->mem, sig->hashed_count_offset, 
     725                         sig->hashed_data_length, 2); 
    729726    // dummy unhashed subpacket count 
    730727    sig->unhashed_count_offset=ops_memory_get_length(sig->mem); 
    731     return ops_write_scalar(0,2,sig->info); 
     728    return ops_write_scalar(0, 2, sig->info); 
    732729    } 
    733730 
     
    744741 */ 
    745742 
    746 ops_boolean_t ops_write_signature(ops_create_signature_t *sig, const ops_public_key_t *key, 
    747                          const ops_secret_key_t *skey, ops_create_info_t *info) 
     743ops_boolean_t ops_write_signature(ops_create_signature_t *sig, 
     744                                  const ops_public_key_t *key, 
     745                                  const ops_secret_key_t *skey, 
     746                                  ops_create_info_t *info) 
    748747    { 
    749748    ops_boolean_t rtn=ops_false; 
     
    764763 
    765764    default: 
    766         fprintf(stderr,"Unsupported algorithm %d\n", skey->public_key.algorithm); 
     765        fprintf(stderr, "Unsupported algorithm %d\n", 
     766                skey->public_key.algorithm); 
    767767        assert(0); 
    768768        } 
     
    770770    assert(sig->hashed_data_length != (unsigned)-1); 
    771771 
    772     ops_memory_place_int(sig->mem,sig->unhashed_count_offset, 
    773                          l-sig->unhashed_count_offset-2,2); 
     772    ops_memory_place_int(sig->mem, sig->unhashed_count_offset, 
     773                         l-sig->unhashed_count_offset-2, 2); 
    774774 
    775775    // add the packet from version number to end of hashed subpackets 
    776776 
    777777    if (debug) 
    778         { fprintf(stderr, "--- Adding packet to hash from version number to hashed subpkts\n"); } 
    779  
    780     sig->hash.add(&sig->hash,ops_memory_get_data(sig->mem), 
     778        { fprintf(stderr, "--- Adding packet to hash from version number to" 
     779                  " hashed subpkts\n"); } 
     780 
     781    sig->hash.add(&sig->hash, ops_memory_get_data(sig->mem), 
    781782                  sig->unhashed_count_offset); 
    782783 
    783784    // add final trailer 
    784     ops_hash_add_int(&sig->hash,sig->sig.info.version,1); 
    785     ops_hash_add_int(&sig->hash,0xff,1); 
     785    ops_hash_add_int(&sig->hash, sig->sig.info.version, 1); 
     786    ops_hash_add_int(&sig->hash, 0xff, 1); 
    786787    // +6 for version, type, pk alg, hash alg, hashed subpacket length 
    787     ops_hash_add_int(&sig->hash,sig->hashed_data_length+6,4); 
     788    ops_hash_add_int(&sig->hash, sig->hashed_data_length+6, 4); 
    788789 
    789790    if (debug) 
    790         { fprintf(stderr, "--- Finished adding packet to hash from version number to hashed subpkts\n"); } 
     791        { fprintf(stderr, "--- Finished adding packet to hash from version" 
     792                  " number to hashed subpkts\n"); } 
    791793 
    792794    // XXX: technically, we could figure out how big the signature is 
     
    797799    case OPS_PKA_RSA_ENCRYPT_ONLY: 
    798800    case OPS_PKA_RSA_SIGN_ONLY: 
    799         rsa_sign(&sig->hash,&key->key.rsa,&skey->key.rsa,sig->info); 
     801        rsa_sign(&sig->hash, &key->key.rsa, &skey->key.rsa, sig->info); 
    800802        break; 
    801803 
    802804    case OPS_PKA_DSA: 
    803         dsa_sign(&sig->hash,&key->key.dsa,&skey->key.dsa,sig->info); 
     805        dsa_sign(&sig->hash, &key->key.dsa, &skey->key.dsa, sig->info); 
    804806        break; 
    805807 
    806808    default: 
    807         fprintf(stderr,"Unsupported algorithm %d\n", skey->public_key.algorithm); 
     809        fprintf(stderr, "Unsupported algorithm %d\n", 
     810                skey->public_key.algorithm); 
    808811        assert(0); 
    809812        } 
     
    811814 
    812815 
    813     rtn=ops_write_ptag(OPS_PTAG_CT_SIGNATURE,info); 
    814     if (rtn!=ops_false
     816    rtn=ops_write_ptag(OPS_PTAG_CT_SIGNATURE, info); 
     817    if (!rtn
    815818        { 
    816819        l=ops_memory_get_length(sig->mem); 
    817         rtn = ops_write_length(l,info) 
    818             && ops_write(ops_memory_get_data(sig->mem),l,info); 
     820        rtn = ops_write_length(l, info) 
     821            && ops_write(ops_memory_get_data(sig->mem), l, info); 
    819822        } 
    820823 
    821824    ops_memory_free(sig->mem); 
    822825 
    823     if (rtn==ops_false) 
    824         { 
    825         OPS_ERROR(&info->errors,OPS_E_W,"Cannot write signature"); 
    826         } 
     826    if (!rtn) 
     827        OPS_ERROR(&info->errors, OPS_E_W, "Cannot write signature"); 
    827828    return rtn; 
    828829    } 
     
    836837 * \param when 
    837838 */ 
    838 ops_boolean_t ops_signature_add_creation_time(ops_create_signature_t *sig,time_t when) 
    839     { 
    840     return ops_write_ss_header(5,OPS_PTAG_SS_CREATION_TIME,sig->info) 
    841         && ops_write_scalar(when,4,sig->info); 
     839ops_boolean_t ops_signature_add_creation_time(ops_create_signature_t *sig, 
     840                                              time_t when) 
     841    { 
     842    return ops_write_ss_header(5, OPS_PTAG_SS_CREATION_TIME, sig->info) 
     843        && ops_write_scalar(when, 4, sig->info); 
    842844    } 
    843845 
     
    851853 */ 
    852854 
    853 ops_boolean_t ops_signature_add_issuer_key_id(ops_create_signature_t *sig, 
    854                                      const unsigned char keyid[OPS_KEY_ID_SIZE]) 
    855     { 
    856     return ops_write_ss_header(OPS_KEY_ID_SIZE+1,OPS_PTAG_SS_ISSUER_KEY_ID,sig->info) 
    857         && ops_write(keyid,OPS_KEY_ID_SIZE,sig->info); 
     855ops_boolean_t 
     856ops_signature_add_issuer_key_id(ops_create_signature_t *sig, 
     857                                const unsigned char keyid[OPS_KEY_ID_SIZE]) 
     858    { 
     859    return ops_write_ss_header(OPS_KEY_ID_SIZE+1, OPS_PTAG_SS_ISSUER_KEY_ID, 
     860                               sig->info) 
     861        && ops_write(keyid, OPS_KEY_ID_SIZE, sig->info); 
    858862    } 
    859863 
     
    869873                                       ops_boolean_t primary) 
    870874    { 
    871     ops_write_ss_header(2,OPS_PTAG_SS_PRIMARY_USER_ID,sig->info); 
    872     ops_write_scalar(primary,1,sig->info); 
     875    ops_write_ss_header(2, OPS_PTAG_SS_PRIMARY_USER_ID, sig->info); 
     876    ops_write_scalar(primary, 1, sig->info); 
    873877    } 
    874878 
     
    884888    { return &sig->hash; } 
    885889 
    886 static int open_output_file(ops_create_info_t **cinfo, const char* input_filename, const char* output_filename, const ops_boolean_t use_armour, const ops_boolean_t overwrite) 
     890static int open_output_file(ops_create_info_t **cinfo, 
     891                            const char* input_filename, 
     892                            const char* output_filename, 
     893                            const ops_boolean_t use_armour, 
     894                            const ops_boolean_t overwrite) 
    887895    { 
    888896    int fd_out; 
     
    891899 
    892900    if (output_filename) 
    893         { 
    894901        fd_out=ops_setup_file_write(cinfo, output_filename, overwrite); 
    895         } 
    896902    else 
    897903        { 
     
    900906        myfilename=ops_mallocz(filenamelen); 
    901907        if (use_armour) 
    902             snprintf(myfilename,filenamelen,"%s.asc",input_filename); 
     908            snprintf(myfilename, filenamelen, "%s.asc", input_filename); 
    903909        else 
    904             snprintf(myfilename,filenamelen,"%s.gpg",input_filename); 
    905         fd_out=ops_setup_file_write(cinfo, myfilename, overwrite); 
     910            snprintf(myfilename, filenamelen, "%s.gpg", input_filename); 
     911        fd_out=ops_setup_file_write(cinfo, myfilename, overwrite); 
    906912        free(myfilename); 
    907913        }  
     
    930936   \endcode 
    931937*/ 
    932 ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t overwrite) 
     938ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, 
     939                                         const char* output_filename, 
     940                                         const ops_secret_key_t *skey, 
     941                                         const ops_boolean_t overwrite) 
    933942    { 
    934943    // \todo allow choice of hash algorithams 
     
    947956 
    948957    // open file to sign 
    949     fd_in=open(input_filename,O_RDONLY | O_BINARY); 
     958    fd_in=open(input_filename, O_RDONLY | O_BINARY); 
    950959    if(fd_in < 0) 
    951960        { 
     
    955964    // set up output file 
    956965 
    957     fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, overwrite); 
     966    fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, 
     967                            overwrite); 
    958968 
    959969    if (fd_out < 0) 
     
    968978        { 
    969979        close (fd_in); 
    970         ops_teardown_file_write(cinfo,fd_out); 
     980        ops_teardown_file_write(cinfo, fd_out); 
    971981        return ops_false; 
    972982        } 
    973983 
    974984    // \todo could add more error detection here 
    975     ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 
    976     if (ops_writer_push_clearsigned(cinfo,sig)!=ops_true) 
    977         { return ops_false; } 
     985    ops_signature_start_cleartext_signature(sig, skey, 
     986                                            OPS_HASH_SHA1, OPS_SIG_BINARY); 
     987    if (!ops_writer_push_clearsigned(cinfo, sig)) 
     988        return ops_false; 
    978989 
    979990    // Do the signing 
     
    983994        int n=0; 
    984995     
    985         n=read(fd_in,buf,sizeof(buf)); 
     996        n=read(fd_in, buf, sizeof(buf)); 
    986997        if (!n) 
    987998            break; 
    988999        assert(n>=0); 
    989         ops_write(buf,n,cinfo); 
     1000        ops_write(buf, n, cinfo); 
    9901001        } 
    9911002    close(fd_in); 
     
    9951006    // - key id 
    9961007    rtn = ops_writer_switch_to_armoured_signature(cinfo) 
    997         && ops_signature_add_creation_time(sig,time(NULL)); 
    998     if (rtn==ops_false
     1008        && ops_signature_add_creation_time(sig, time(NULL)); 
     1009    if (!rtn
    9991010        { 
    1000         ops_teardown_file_write(cinfo,fd_out); 
     1011        ops_teardown_file_write(cinfo, fd_out); 
    10011012        return ops_false; 
    10021013        } 
    10031014 
    1004     ops_keyid(keyid,&skey->public_key); 
    1005  
    1006     rtn = ops_signature_add_issuer_key_id(sig,keyid) 
     1015    ops_keyid(keyid, &skey->public_key); 
     1016 
     1017    rtn = ops_signature_add_issuer_key_id(sig, keyid) 
    10071018        && ops_signature_hashed_subpackets_end(sig) 
    1008         && ops_write_signature(sig,&skey->public_key,skey,cinfo); 
    1009  
    1010     ops_teardown_file_write(cinfo,fd_out); 
    1011  
    1012     if (rtn==ops_false) 
    1013         { 
    1014         OPS_ERROR(&cinfo->errors,OPS_E_W,"Cannot sign file as cleartext"); 
    1015         } 
     1019        && ops_write_signature(sig, &skey->public_key, skey, cinfo); 
     1020 
     1021    ops_teardown_file_write(cinfo, fd_out); 
     1022 
     1023    if (!rtn) 
     1024        OPS_ERROR(&cinfo->errors, OPS_E_W, "Cannot sign file as cleartext"); 
    10161025    return rtn; 
    10171026    } 
     
    10461055 \endcode 
    10471056 */ 
    1048 ops_boolean_t ops_sign_buf_as_cleartext(const char* cleartext, const size_t len, ops_memory_t** signed_cleartext, const ops_secret_key_t *skey) 
     1057ops_boolean_t ops_sign_buf_as_cleartext(const char* cleartext, const size_t len, 
     1058                                        ops_memory_t** signed_cleartext, 
     1059                                        const ops_secret_key_t *skey) 
    10491060    { 
    10501061    ops_boolean_t rtn=ops_false; 
     
    10581069    ops_create_info_t *cinfo=NULL; 
    10591070     
    1060     assert(*signed_cleartext==NULL); 
     1071    assert(*signed_cleartext == NULL); 
    10611072 
    10621073    // set up signature 
    10631074    sig=ops_create_signature_new(); 
    10641075    if (!sig) 
    1065         {  
    10661076        return ops_false; 
    1067         } 
    10681077 
    10691078    // \todo could add more error detection here 
    1070     ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 
     1079    ops_signature_start_cleartext_signature(sig, skey, OPS_HASH_SHA1, 
     1080                                            OPS_SIG_BINARY); 
    10711081 
    10721082    // set up output file 
     
    10771087    // - creation time 
    10781088    // - key id 
    1079     rtn = ops_writer_push_clearsigned(cinfo,sig) 
    1080         && ops_write(cleartext,len,cinfo) 
     1089    rtn = ops_writer_push_clearsigned(cinfo, sig) 
     1090        && ops_write(cleartext, len, cinfo) 
    10811091        && ops_writer_switch_to_armoured_signature(cinfo) 
    1082         && ops_signature_add_creation_time(sig,time(NULL)); 
    1083  
    1084     if (rtn==ops_false) 
    1085         { 
     1092        && ops_signature_add_creation_time(sig, time(NULL)); 
     1093 
     1094    if (!rtn) 
    10861095        return ops_false; 
    1087         } 
    1088  
    1089     ops_keyid(keyid,&skey->public_key); 
    1090  
    1091     rtn = ops_signature_add_issuer_key_id(sig,keyid) 
     1096 
     1097    ops_keyid(keyid, &skey->public_key); 
     1098 
     1099    rtn = ops_signature_add_issuer_key_id(sig, keyid) 
    10921100        && ops_signature_hashed_subpackets_end(sig) 
    1093         && ops_write_signature(sig,&skey->public_key,skey,cinfo) 
     1101        && ops_write_signature(sig, &skey->public_key, skey, cinfo) 
    10941102        && ops_writer_close(cinfo); 
    10951103 
     
    11241132\endcode 
    11251133*/ 
    1126 ops_boolean_t ops_sign_file(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t use_armour, const ops_boolean_t overwrite) 
     1134ops_boolean_t ops_sign_file(const char* input_filename, 
     1135                            const char* output_filename, 
     1136                            const ops_secret_key_t *skey, 
     1137                            const ops_boolean_t use_armour, 
     1138                            const ops_boolean_t overwrite) 
    11271139    { 
    11281140    // \todo allow choice of hash algorithams 
     
    11441156 
    11451157    int errnum; 
    1146     mem_buf=ops_write_mem_from_file(input_filename,&errnum); 
     1158    mem_buf=ops_write_mem_from_file(input_filename, &errnum); 
    11471159    if (errnum) 
    11481160        return ops_false; 
     
    11501162    // setup output file 
    11511163 
    1152     fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, overwrite); 
     1164    fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, 
     1165                            overwrite); 
    11531166 
    11541167    if (fd_out < 0) 
     
    11741187    // hash file contents 
    11751188    hash=ops_signature_get_hash(sig); 
    1176     hash->add(hash, ops_memory_get_data(mem_buf), ops_memory_get_length(mem_buf)); 
     1189    hash->add(hash, ops_memory_get_data(mem_buf), 
     1190              ops_memory_get_length(mem_buf)); 
    11771191     
    11781192    // output file contents as Literal Data packet 
    11791193 
    11801194    if (debug) 
    1181         { fprintf(stderr,"** Writing out data now\n"); } 
    1182  
    1183     ops_write_literal_data_from_buf(ops_memory_get_data(mem_buf), ops_memory_get_length(mem_buf), OPS_LDT_BINARY, cinfo); 
     1195        fprintf(stderr,"** Writing out data now\n"); 
     1196 
     1197    ops_write_literal_data_from_buf(ops_memory_get_data(mem_buf), 
     1198                                    ops_memory_get_length(mem_buf), 
     1199                                    OPS_LDT_BINARY, cinfo); 
    11841200 
    11851201    if (debug) 
    1186         { fprintf(stderr,"** After Writing out data now\n");} 
     1202        fprintf(stderr, "** After Writing out data now\n"); 
    11871203 
    11881204    // add subpackets to signature 
     
    11901206    // - key id 
    11911207 
    1192     ops_signature_add_creation_time(sig,time(NULL)); 
    1193  
    1194     ops_keyid(keyid,&skey->public_key); 
    1195     ops_signature_add_issuer_key_id(sig,keyid); 
     1208    ops_signature_add_creation_time(sig, time(NULL)); 
     1209 
     1210    ops_keyid(keyid, &skey->public_key); 
     1211    ops_signature_add_issuer_key_id(sig, keyid); 
    11961212 
    11971213    ops_signature_hashed_subpackets_end(sig); 
    11981214 
    11991215    // write out sig 
    1200     ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     1216    ops_write_signature(sig, &skey->public_key, skey, cinfo); 
    12011217 
    12021218    ops_teardown_file_write(cinfo, fd_out); 
     
    12431259\endcode 
    12441260*/ 
    1245 ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, const ops_boolean_t use_armour) 
     1261ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, 
     1262                           const ops_sig_type_t sig_type, 
     1263                           const ops_secret_key_t *skey, 
     1264                           const ops_boolean_t use_armour) 
    12461265    { 
    12471266    // \todo allow choice of hash algorithams 
     
    12591278 
    12601279    // setup literal data packet type 
    1261     if (sig_type==OPS_SIG_BINARY) 
     1280    if (sig_type == OPS_SIG_BINARY) 
    12621281        ld_type=OPS_LDT_BINARY; 
    12631282    else 
     
    12761295 
    12771296    if (debug) 
    1278         { fprintf(stderr, "** Writing out one pass sig\n"); }  
     1297        fprintf(stderr, "** Writing out one pass sig\n"); 
    12791298 
    12801299    // write one_pass_sig 
     
    12881307 
    12891308    if (debug) 
    1290         { fprintf(stderr,"** Writing out data now\n"); } 
     1309        fprintf(stderr,"** Writing out data now\n"); 
    12911310 
    12921311    ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); 
    12931312 
    12941313    if (debug) 
    1295         { fprintf(stderr,"** After Writing out data now\n");} 
     1314        fprintf(stderr,"** After Writing out data now\n"); 
    12961315 
    12971316    // add subpackets to signature 
     
    12991318    // - key id 
    13001319 
    1301     ops_signature_add_creation_time(sig,time(NULL)); 
    1302  
    1303     ops_keyid(keyid,&skey->public_key); 
    1304     ops_signature_add_issuer_key_id(sig,keyid); 
     1320    ops_signature_add_creation_time(sig, time(NULL)); 
     1321 
     1322    ops_keyid(keyid, &skey->public_key); 
     1323    ops_signature_add_issuer_key_id(sig, keyid); 
    13051324 
    13061325    ops_signature_hashed_subpackets_end(sig); 
    13071326 
    13081327    // write out sig 
    1309     ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     1328    ops_write_signature(sig, &skey->public_key, skey, cinfo); 
    13101329 
    13111330    // tidy up 
  • openpgpsdk/trunk/tests/test_rsa_encrypt.c

    r695 r700  
    133133int clean_suite_rsa_encrypt(void) 
    134134    { 
    135          
    136135    ops_finish(); 
    137136