21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

siglen<br />

The number of bytes written into the signature buffer will placed in the integer<br />

pointed to by this argument. The number of bytes will always be the same size as<br />

the prime parameter q, which can be determined by calling DSA_size( ) with the<br />

DSA object that will be used to generate the signature.<br />

dsa<br />

DSA object to be used to generate the signature. The DSA object must contain the<br />

parameters and the private key for signing.<br />

Here’s a slightly higher-level function that wraps the DSA_sign( ) function, signing an<br />

arbitrary message:<br />

#include <br />

#include <br />

#include <br />

int spc_DSA_sign(unsigned char *msg, int msglen, unsigned char *sig, DSA *dsa) {<br />

unsigned int ignored;<br />

unsigned char hash[20];<br />

if (!SHA1(msg, msglen, hash)) return 0;<br />

return DSA_sign(NID_sha1, hash, 20, sig, &ignored, dsa);<br />

}<br />

Verification of a signature is done with the function DSA_verify( ):<br />

int DSA_verify(int type, unsigned char *md, int mdlen, unsigned char *sig,<br />

int siglen, DSA *dsa);<br />

The arguments for DSA_verify( ) are essentially the same as the arguments for DSA_<br />

sign( ). The DSA object must contain the public key of the signer, and the fourth<br />

argument, sig, must contain the signature that is to be verified. Unlike with DSA_<br />

sign( ), it actually makes sense to pass in the length of the signature because it saves<br />

the caller from having to check to see if the signature is of the proper length. Nonetheless,<br />

DSA_verify( ) could do without the first argument, and it could hash the<br />

message for you. Here’s our wrapper for it:<br />

#include <br />

#include <br />

#include <br />

int spc_DSA_verify(unsigned char *msg, int msglen, unsigned char *sig, int siglen,<br />

DSA *dsa) {<br />

unsigned char hash[20];<br />

if (!SHA1(msg, msglen, hash)) return 0;<br />

return DSA_verify(NID_sha1, hash, 20, sig, siglen, dsa);<br />

}<br />

Using the Digital Signature Algorithm (DSA) | 351<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!