21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

free(out);<br />

out = 0;<br />

goto err;<br />

}<br />

bytes_remaining -= b_per_ct;<br />

ptr += b_per_ct;<br />

/* Remember, output is larger than the input. */<br />

p += RSA_size(recip_pub_key);<br />

}<br />

err:<br />

if (sig) free(sig);<br />

if (tmp) free(tmp);<br />

if (to_encrypt) free(to_encrypt);<br />

return out;<br />

}<br />

Once the message generated by this function is received on the server side, the following<br />

code will validate the signature on the message and retrieve the secret:<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#define MIN(x,y) ((x) > (y) ? (y) : (x))<br />

/* recip_key must contain both the public and private key. */<br />

int validate_and_retreive_secret(RSA *recip_key, RSA *signers_pub_key,<br />

unsigned char *encr, unsigned int inlen,<br />

unsigned char *secret) {<br />

int result = 0;<br />

BN_CTX *tctx;<br />

unsigned int ctlen, stlen, i, l;<br />

unsigned char *decrypt, *signedtext, *p, hash[20];<br />

if (inlen % RSA_size(recip_key)) return 0;<br />

if (!(p = decrypt = (unsigned char *)malloc(inlen))) return 0;<br />

if (!(tctx = BN_CTX_new( ))) {<br />

free(decrypt);<br />

return 0;<br />

}<br />

RSA_blinding_on(recip_key, tctx);<br />

for (ctlen = i = 0; i < inlen / RSA_size(recip_key); i++) {<br />

if (!(l = RSA_private_decrypt(RSA_size(recip_key), encr, p, recip_key,<br />

RSA_PKCS1_OAEP_PADDING))) goto err;<br />

encr += RSA_size(recip_key);<br />

p += l;<br />

ctlen += l;<br />

}<br />

if (ctlen != 16 + RSA_size(signers_pub_key)) goto err;<br />

stlen = 16 + BN_num_bytes(recip_key->n) + BN_num_bytes(recip_key->e);<br />

if (!(signedtext = (unsigned char *)malloc(stlen))) goto err;<br />

346 | Chapter 7: Public Key Cryptography<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!