diff options
Diffstat (limited to 'src/lib/libcrypto/doc/EVP_EncryptInit.pod')
| -rw-r--r-- | src/lib/libcrypto/doc/EVP_EncryptInit.pod | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/src/lib/libcrypto/doc/EVP_EncryptInit.pod b/src/lib/libcrypto/doc/EVP_EncryptInit.pod index a876ac789c..b2211ea6d3 100644 --- a/src/lib/libcrypto/doc/EVP_EncryptInit.pod +++ b/src/lib/libcrypto/doc/EVP_EncryptInit.pod | |||
| @@ -427,46 +427,49 @@ Set the effective key length used in RC2: | |||
| 427 | 427 | ||
| 428 | Encrypt a string using blowfish: | 428 | Encrypt a string using blowfish: |
| 429 | 429 | ||
| 430 | int do_crypt(char *outfile) | 430 | int |
| 431 | { | 431 | do_crypt(char *outfile) |
| 432 | { | ||
| 432 | unsigned char outbuf[1024]; | 433 | unsigned char outbuf[1024]; |
| 433 | int outlen, tmplen; | 434 | int outlen, tmplen; |
| 434 | /* Bogus key and IV: we'd normally set these from | 435 | /* |
| 436 | * Bogus key and IV: we'd normally set these from | ||
| 435 | * another source. | 437 | * another source. |
| 436 | */ | 438 | */ |
| 437 | unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | 439 | unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
| 438 | unsigned char iv[] = {1,2,3,4,5,6,7,8}; | 440 | unsigned char iv[] = {1,2,3,4,5,6,7,8}; |
| 439 | char intext[] = "Some Crypto Text"; | 441 | const char intext[] = "Some Crypto Text"; |
| 440 | EVP_CIPHER_CTX ctx; | 442 | EVP_CIPHER_CTX ctx; |
| 441 | FILE *out; | 443 | FILE *out; |
| 442 | EVP_CIPHER_CTX_init(&ctx); | 444 | EVP_CIPHER_CTX_init(&ctx); |
| 443 | EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); | 445 | EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); |
| 444 | 446 | ||
| 445 | if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) | 447 | if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, |
| 446 | { | 448 | strlen(intext))) { |
| 447 | /* Error */ | 449 | /* Error */ |
| 448 | return 0; | 450 | return 0; |
| 449 | } | 451 | } |
| 450 | /* Buffer passed to EVP_EncryptFinal() must be after data just | 452 | /* |
| 453 | * Buffer passed to EVP_EncryptFinal() must be after data just | ||
| 451 | * encrypted to avoid overwriting it. | 454 | * encrypted to avoid overwriting it. |
| 452 | */ | 455 | */ |
| 453 | if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) | 456 | if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) { |
| 454 | { | ||
| 455 | /* Error */ | 457 | /* Error */ |
| 456 | return 0; | 458 | return 0; |
| 457 | } | 459 | } |
| 458 | outlen += tmplen; | 460 | outlen += tmplen; |
| 459 | EVP_CIPHER_CTX_cleanup(&ctx); | 461 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 460 | /* Need binary mode for fopen because encrypted data is | 462 | /* |
| 463 | * Need binary mode for fopen because encrypted data is | ||
| 461 | * binary data. Also cannot use strlen() on it because | 464 | * binary data. Also cannot use strlen() on it because |
| 462 | * it wont be null terminated and may contain embedded | 465 | * it won't be NUL terminated and may contain embedded |
| 463 | * nulls. | 466 | * NULs. |
| 464 | */ | 467 | */ |
| 465 | out = fopen(outfile, "wb"); | 468 | out = fopen(outfile, "wb"); |
| 466 | fwrite(outbuf, 1, outlen, out); | 469 | fwrite(outbuf, 1, outlen, out); |
| 467 | fclose(out); | 470 | fclose(out); |
| 468 | return 1; | 471 | return 1; |
| 469 | } | 472 | } |
| 470 | 473 | ||
| 471 | The ciphertext from the above example can be decrypted using the B<openssl> | 474 | The ciphertext from the above example can be decrypted using the B<openssl> |
| 472 | utility with the command line: | 475 | utility with the command line: |
| @@ -476,16 +479,19 @@ utility with the command line: | |||
| 476 | General encryption, decryption function example using FILE I/O and RC2 with an | 479 | General encryption, decryption function example using FILE I/O and RC2 with an |
| 477 | 80 bit key: | 480 | 80 bit key: |
| 478 | 481 | ||
| 479 | int do_crypt(FILE *in, FILE *out, int do_encrypt) | 482 | int |
| 480 | { | 483 | do_crypt(FILE *in, FILE *out, int do_encrypt) |
| 484 | { | ||
| 481 | /* Allow enough space in output buffer for additional block */ | 485 | /* Allow enough space in output buffer for additional block */ |
| 482 | inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; | 486 | inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; |
| 483 | int inlen, outlen; | 487 | int inlen, outlen; |
| 484 | /* Bogus key and IV: we'd normally set these from | 488 | /* |
| 489 | * Bogus key and IV: we'd normally set these from | ||
| 485 | * another source. | 490 | * another source. |
| 486 | */ | 491 | */ |
| 487 | unsigned char key[] = "0123456789"; | 492 | unsigned char key[] = "0123456789"; |
| 488 | unsigned char iv[] = "12345678"; | 493 | unsigned char iv[] = "12345678"; |
| 494 | |||
| 489 | /* Don't set key or IV because we will modify the parameters */ | 495 | /* Don't set key or IV because we will modify the parameters */ |
| 490 | EVP_CIPHER_CTX_init(&ctx); | 496 | EVP_CIPHER_CTX_init(&ctx); |
| 491 | EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); | 497 | EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); |
| @@ -493,30 +499,28 @@ General encryption, decryption function example using FILE I/O and RC2 with an | |||
| 493 | /* We finished modifying parameters so now we can set key and IV */ | 499 | /* We finished modifying parameters so now we can set key and IV */ |
| 494 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); | 500 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); |
| 495 | 501 | ||
| 496 | for(;;) | 502 | for(;;) { |
| 497 | { | ||
| 498 | inlen = fread(inbuf, 1, 1024, in); | 503 | inlen = fread(inbuf, 1, 1024, in); |
| 499 | if(inlen <= 0) break; | 504 | if (inlen <= 0) |
| 500 | if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) | 505 | break; |
| 501 | { | 506 | if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, |
| 507 | inlen)) { | ||
| 502 | /* Error */ | 508 | /* Error */ |
| 503 | EVP_CIPHER_CTX_cleanup(&ctx); | 509 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 504 | return 0; | 510 | return 0; |
| 505 | } | ||
| 506 | fwrite(outbuf, 1, outlen, out); | ||
| 507 | } | 511 | } |
| 508 | if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) | 512 | fwrite(outbuf, 1, outlen, out); |
| 509 | { | 513 | } |
| 514 | if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) { | ||
| 510 | /* Error */ | 515 | /* Error */ |
| 511 | EVP_CIPHER_CTX_cleanup(&ctx); | 516 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 512 | return 0; | 517 | return 0; |
| 513 | } | 518 | } |
| 514 | fwrite(outbuf, 1, outlen, out); | 519 | fwrite(outbuf, 1, outlen, out); |
| 515 | 520 | ||
| 516 | EVP_CIPHER_CTX_cleanup(&ctx); | 521 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 517 | return 1; | 522 | return 1; |
| 518 | } | 523 | } |
| 519 | |||
| 520 | 524 | ||
| 521 | =head1 SEE ALSO | 525 | =head1 SEE ALSO |
| 522 | 526 | ||
