Changeset 667
- Timestamp:
- 08/01/09 14:26:33
- Files:
-
- openpgpsdk/trunk/src/lib/crypto.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/src/lib/crypto.c
r625 r667 42 42 \note only RSA at present 43 43 */ 44 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 45 const ops_secret_key_t *skey) 44 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen, 45 const BIGNUM *encmpi, 46 const ops_secret_key_t *skey) 46 47 { 47 48 unsigned char encmpibuf[8192]; … … 54 55 /* MPI can't be more than 65,536 */ 55 56 assert(mpisize <= sizeof encmpibuf); 56 BN_bn2bin(encmpi, encmpibuf);57 BN_bn2bin(encmpi, encmpibuf); 57 58 58 59 assert(skey->public_key.algorithm == OPS_PKA_RSA); … … 66 67 */ 67 68 68 n=ops_rsa_private_decrypt(mpibuf, encmpibuf,(BN_num_bits(encmpi)+7)/8,69 &skey->key.rsa, &skey->public_key.key.rsa);70 assert(n !=-1);69 n=ops_rsa_private_decrypt(mpibuf, encmpibuf, (BN_num_bits(encmpi)+7)/8, 70 &skey->key.rsa, &skey->public_key.key.rsa); 71 assert(n != -1); 71 72 72 73 /* … … 103 104 // this is the unencoded m buf 104 105 if((unsigned)(n-i) <= buflen) 105 memcpy(buf, mpibuf+i,n-i);106 memcpy(buf, mpibuf+i, n-i); 106 107 107 108 /* … … 121 122 */ 122 123 ops_boolean_t ops_rsa_encrypt_mpi(const unsigned char *encoded_m_buf, 123 const size_t sz_encoded_m_buf,124 const ops_public_key_t *pkey,125 ops_pk_session_key_parameters_t *skp)124 const size_t sz_encoded_m_buf, 125 const ops_public_key_t *pkey, 126 ops_pk_session_key_parameters_t *skp) 126 127 { 127 128 assert(sz_encoded_m_buf==(size_t) BN_num_bytes(pkey->key.rsa.n)); … … 130 131 int n=0; 131 132 132 n=ops_rsa_public_encrypt(encmpibuf, encoded_m_buf, sz_encoded_m_buf, &pkey->key.rsa); 133 n=ops_rsa_public_encrypt(encmpibuf, encoded_m_buf, sz_encoded_m_buf, 134 &pkey->key.rsa); 133 135 assert(n!=-1); 134 136 … … 152 154 153 155 static ops_parse_cb_return_t 154 callback_write_parsed(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 156 callback_write_parsed(const ops_parser_content_t *content_, 157 ops_parse_cb_info_t *cbinfo); 155 158 156 159 /** … … 164 167 \return ops_true if OK; else ops_false 165 168 */ 166 ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite) 169 ops_boolean_t ops_encrypt_file(const char* input_filename, 170 const char* output_filename, 171 const ops_keydata_t *pub_key, 172 const ops_boolean_t use_armour, 173 const ops_boolean_t allow_overwrite) 167 174 { 168 175 int fd_in=0; … … 191 198 192 199 // Push the encrypted writer 193 ops_writer_push_encrypt_se_ip(cinfo, pub_key);200 ops_writer_push_encrypt_se_ip(cinfo, pub_key); 194 201 195 202 // Do the writing … … 200 207 for (;;) 201 208 { 202 buf=realloc(buf, done+bufsz);209 buf=realloc(buf, done+bufsz); 203 210 204 int n=0; 205 206 n=read(fd_in,buf+done,bufsz); 207 if (!n) 208 break; 209 assert(n>=0); 211 int n=0; 212 213 n=read(fd_in,buf+done,bufsz); 214 if (!n) 215 break; 216 assert(n>=0); 217 210 218 done+=n; 211 219 } 212 220 213 221 // This does the writing 214 ops_write(buf, done,cinfo);222 ops_write(buf, done, cinfo); 215 223 216 224 // tidy up 217 225 close(fd_in); 218 226 free(buf); 219 ops_teardown_file_write(cinfo, fd_out);227 ops_teardown_file_write(cinfo, fd_out); 220 228 221 229 return ops_true; … … 233 241 */ 234 242 235 ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t* keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite, ops_parse_cb_t* cb_get_passphrase) 243 ops_boolean_t ops_decrypt_file(const char* input_filename, 244 const char* output_filename, 245 ops_keyring_t* keyring, 246 const ops_boolean_t use_armour, 247 const ops_boolean_t allow_overwrite, 248 ops_parse_cb_t* cb_get_passphrase) 236 249 { 237 250 int fd_in=0; … … 244 257 // setup for reading from given input file 245 258 fd_in=ops_setup_file_read(&pinfo, input_filename, 246 NULL,247 callback_write_parsed,248 ops_false);259 NULL, 260 callback_write_parsed, 261 ops_false); 249 262 if (fd_in < 0) 250 263 { … … 257 270 if (output_filename) 258 271 { 259 fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, output_filename, allow_overwrite); 272 fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, output_filename, 273 allow_overwrite); 260 274 261 275 if (fd_out < 0) … … 270 284 int suffixlen=4; 271 285 char *defaultsuffix=".decrypted"; 272 const char *suffix=input_filename+strlen((char *)input_filename)-suffixlen; 286 const char *suffix=input_filename+strlen((char *)input_filename) 287 -suffixlen; 273 288 if (!strcmp(suffix,".gpg") || !strcmp(suffix,".asc")) 274 289 { 275 290 myfilename=ops_mallocz(strlen(input_filename)-suffixlen+1); 276 strncpy(myfilename,input_filename,strlen(input_filename)-suffixlen); 291 strncpy(myfilename, input_filename, 292 strlen(input_filename)-suffixlen); 277 293 } 278 294 else … … 280 296 unsigned filenamelen=strlen(input_filename)+strlen(defaultsuffix)+1; 281 297 myfilename=ops_mallocz(filenamelen); 282 snprintf(myfilename,filenamelen,"%s%s",input_filename,defaultsuffix); 298 snprintf(myfilename, filenamelen, "%s%s", input_filename, 299 defaultsuffix); 283 300 } 284 301 285 fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, myfilename, allow_overwrite); 302 fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, myfilename, 303 allow_overwrite); 286 304 287 305 if (fd_out < 0) … … 289 307 perror(myfilename); 290 308 free(myfilename); 291 ops_teardown_file_read(pinfo, fd_in);309 ops_teardown_file_read(pinfo, fd_in); 292 310 return ops_false; 293 311 } … … 326 344 327 345 static ops_parse_cb_return_t 328 callback_write_parsed(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 346 callback_write_parsed(const ops_parser_content_t *content_, 347 ops_parse_cb_info_t *cbinfo) 329 348 { 330 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 349 ops_parser_content_union_t* content 350 =(ops_parser_content_union_t *)&content_->content; 331 351 static ops_boolean_t skipping; 332 352 // ops_boolean_t write=ops_true; … … 351 371 skipping=ops_true; 352 372 } 353 fwrite(content->unarmoured_text.data, 1,354 content->unarmoured_text.length, stdout);373 fwrite(content->unarmoured_text.data, 1, 374 content->unarmoured_text.length, stdout); 355 375 break; 356 376 357 377 case OPS_PTAG_CT_PK_SESSION_KEY: 358 return callback_pk_session_key(content_, cbinfo);378 return callback_pk_session_key(content_, cbinfo); 359 379 break; 360 380 361 381 case OPS_PARSER_CMD_GET_SECRET_KEY: 362 return callback_cmd_get_secret_key(content_, cbinfo);382 return callback_cmd_get_secret_key(content_, cbinfo); 363 383 break; 364 384 365 385 case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 366 386 // return callback_cmd_get_secret_key_passphrase(content_,cbinfo); 367 return cbinfo->cryptinfo.cb_get_passphrase(content_, cbinfo);387 return cbinfo->cryptinfo.cb_get_passphrase(content_, cbinfo); 368 388 break; 369 389 370 390 case OPS_PTAG_CT_LITERAL_DATA_BODY: 371 return callback_literal_data(content_, cbinfo);372 break;391 return callback_literal_data(content_, cbinfo); 392 break; 373 393 374 394 case OPS_PTAG_CT_ARMOUR_HEADER:
