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.

static int passes_rabin_miller_once(BIGNUM *p) {<br />

BIGNUM a, m, z, tmp;<br />

BN_CTX *ctx;<br />

unsigned int b, i;<br />

/* Initialize a, m, z and tmp properly. */<br />

BN_init(&a);<br />

BN_init(&m);<br />

BN_init(&z);<br />

BN_init(&tmp);<br />

ctx = BN_CTX_new( );<br />

b = calc_b_and_m(p, &m);<br />

/* a is a random number less than p: */<br />

if (!BN_rand_range(&a, p)) abort( );<br />

/* z = a^m mod p. */<br />

if (!BN_mod_exp(&z, &a, &m, p, ctx)) abort( );<br />

/* if z = 1 at the start, pass. */<br />

if (BN_is_one(&z)) return 1;<br />

for (i = 0; i < b; i++) {<br />

if (BN_is_one(&z)) return 0;<br />

/* if z = p-1, pass! */<br />

BN_copy(&tmp, &z);<br />

if (!BN_add_word(&tmp, 1)) abort( );<br />

if (!BN_cmp(&tmp, p)) return 1;<br />

/* z = z^2 mod p */<br />

BN_mod_sqr(&tmp, &z, p, ctx);<br />

BN_copy(&z, &tmp);<br />

}<br />

/* if z = p-1, pass! */<br />

BN_copy(&tmp, &z);<br />

if (!BN_add_word(&tmp, 1)) abort( );<br />

if (!BN_cmp(&tmp, p)) return 1;<br />

/* Fail! */<br />

return 0;<br />

}<br />

/* b = How many times does 2 divide p - 1? This gets returned.<br />

* m is (p-1)/(2^b).<br />

*/<br />

static unsigned int calc_b_and_m(BIGNUM *p, BIGNUM *x) {<br />

unsigned int b;<br />

if (!BN_copy(x, p)) abort( );<br />

if (!BN_sub_word(x, 1)) abort( );<br />

326 | 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!