| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
#include <openpgpsdk/configure.h> |
|---|
| 5 |
#include <openpgpsdk/crypto.h> |
|---|
| 6 |
#include <openssl/md5.h> |
|---|
| 7 |
#include <openssl/sha.h> |
|---|
| 8 |
#include <openssl/dsa.h> |
|---|
| 9 |
#include <openssl/rsa.h> |
|---|
| 10 |
#include <openssl/err.h> |
|---|
| 11 |
#include <assert.h> |
|---|
| 12 |
#include <stdlib.h> |
|---|
| 13 |
|
|---|
| 14 |
#include <openpgpsdk/final.h> |
|---|
| 15 |
|
|---|
| 16 |
static void md5_init(ops_hash_t *hash) |
|---|
| 17 |
{ |
|---|
| 18 |
assert(!hash->data); |
|---|
| 19 |
hash->data=malloc(sizeof(MD5_CTX)); |
|---|
| 20 |
MD5_Init(hash->data); |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
static void md5_add(ops_hash_t *hash,const unsigned char *data,unsigned length) |
|---|
| 24 |
{ |
|---|
| 25 |
MD5_Update(hash->data,data,length); |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
static unsigned md5_finish(ops_hash_t *hash,unsigned char *out) |
|---|
| 29 |
{ |
|---|
| 30 |
MD5_Final(out,hash->data); |
|---|
| 31 |
free(hash->data); |
|---|
| 32 |
hash->data=NULL; |
|---|
| 33 |
return 16; |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
static ops_hash_t md5={OPS_HASH_MD5,MD5_DIGEST_LENGTH,"MD5",md5_init,md5_add, |
|---|
| 37 |
md5_finish,NULL}; |
|---|
| 38 |
|
|---|
| 39 |
void ops_hash_md5(ops_hash_t *hash) |
|---|
| 40 |
{ |
|---|
| 41 |
*hash=md5; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
static void sha1_init(ops_hash_t *hash) |
|---|
| 45 |
{ |
|---|
| 46 |
assert(!hash->data); |
|---|
| 47 |
hash->data=malloc(sizeof(SHA_CTX)); |
|---|
| 48 |
SHA1_Init(hash->data); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
static void sha1_add(ops_hash_t *hash,const unsigned char *data, |
|---|
| 52 |
unsigned length) |
|---|
| 53 |
{ |
|---|
| 54 |
SHA1_Update(hash->data,data,length); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
static unsigned sha1_finish(ops_hash_t *hash,unsigned char *out) |
|---|
| 58 |
{ |
|---|
| 59 |
SHA1_Final(out,hash->data); |
|---|
| 60 |
free(hash->data); |
|---|
| 61 |
hash->data=NULL; |
|---|
| 62 |
return 20; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
static ops_hash_t sha1={OPS_HASH_SHA1,SHA_DIGEST_LENGTH,"SHA1",sha1_init, |
|---|
| 66 |
sha1_add,sha1_finish,NULL}; |
|---|
| 67 |
|
|---|
| 68 |
void ops_hash_sha1(ops_hash_t *hash) |
|---|
| 69 |
{ |
|---|
| 70 |
*hash=sha1; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
ops_boolean_t ops_dsa_verify(const unsigned char *hash,size_t hash_length, |
|---|
| 74 |
const ops_dsa_signature_t *sig, |
|---|
| 75 |
const ops_dsa_public_key_t *dsa) |
|---|
| 76 |
{ |
|---|
| 77 |
DSA_SIG *osig; |
|---|
| 78 |
DSA *odsa; |
|---|
| 79 |
int ret; |
|---|
| 80 |
|
|---|
| 81 |
osig=DSA_SIG_new(); |
|---|
| 82 |
osig->r=sig->r; |
|---|
| 83 |
osig->s=sig->s; |
|---|
| 84 |
|
|---|
| 85 |
odsa=DSA_new(); |
|---|
| 86 |
odsa->p=dsa->p; |
|---|
| 87 |
odsa->q=dsa->q; |
|---|
| 88 |
odsa->g=dsa->g; |
|---|
| 89 |
odsa->pub_key=dsa->y; |
|---|
| 90 |
|
|---|
| 91 |
ret=DSA_do_verify(hash,hash_length,osig,odsa); |
|---|
| 92 |
assert(ret >= 0); |
|---|
| 93 |
|
|---|
| 94 |
odsa->p=odsa->q=odsa->g=odsa->pub_key=NULL; |
|---|
| 95 |
DSA_free(odsa); |
|---|
| 96 |
|
|---|
| 97 |
osig->r=osig->s=NULL; |
|---|
| 98 |
DSA_SIG_free(osig); |
|---|
| 99 |
|
|---|
| 100 |
return ret != 0; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
int ops_rsa_public_decrypt(unsigned char *out,const unsigned char *in, |
|---|
| 104 |
size_t length,const ops_rsa_public_key_t *rsa) |
|---|
| 105 |
{ |
|---|
| 106 |
RSA *orsa; |
|---|
| 107 |
int n; |
|---|
| 108 |
|
|---|
| 109 |
orsa=RSA_new(); |
|---|
| 110 |
orsa->n=rsa->n; |
|---|
| 111 |
orsa->e=rsa->e; |
|---|
| 112 |
|
|---|
| 113 |
n=RSA_public_decrypt(length,in,out,orsa,RSA_NO_PADDING); |
|---|
| 114 |
|
|---|
| 115 |
orsa->n=orsa->e=NULL; |
|---|
| 116 |
RSA_free(orsa); |
|---|
| 117 |
|
|---|
| 118 |
return n; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in, |
|---|
| 122 |
size_t length,const ops_rsa_secret_key_t *srsa, |
|---|
| 123 |
const ops_rsa_public_key_t *rsa) |
|---|
| 124 |
{ |
|---|
| 125 |
RSA *orsa; |
|---|
| 126 |
int n; |
|---|
| 127 |
|
|---|
| 128 |
orsa=RSA_new(); |
|---|
| 129 |
orsa->n=rsa->n; |
|---|
| 130 |
orsa->d=srsa->d; |
|---|
| 131 |
orsa->p=srsa->q; |
|---|
| 132 |
orsa->q=srsa->p; |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
orsa->e=rsa->e; |
|---|
| 136 |
assert(RSA_check_key(orsa) == 1); |
|---|
| 137 |
orsa->e=NULL; |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
n=RSA_private_encrypt(length,in,out,orsa,RSA_NO_PADDING); |
|---|
| 141 |
|
|---|
| 142 |
orsa->n=orsa->d=orsa->p=orsa->q=NULL; |
|---|
| 143 |
RSA_free(orsa); |
|---|
| 144 |
|
|---|
| 145 |
return n; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
int ops_rsa_private_decrypt(unsigned char *out,const unsigned char *in, |
|---|
| 149 |
size_t length,const ops_rsa_secret_key_t *srsa, |
|---|
| 150 |
const ops_rsa_public_key_t *rsa) |
|---|
| 151 |
{ |
|---|
| 152 |
RSA *orsa; |
|---|
| 153 |
int n; |
|---|
| 154 |
|
|---|
| 155 |
orsa=RSA_new(); |
|---|
| 156 |
orsa->n=rsa->n; |
|---|
| 157 |
orsa->d=srsa->d; |
|---|
| 158 |
orsa->p=srsa->q; |
|---|
| 159 |
orsa->q=srsa->p; |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
orsa->e=rsa->e; |
|---|
| 163 |
assert(RSA_check_key(orsa) == 1); |
|---|
| 164 |
orsa->e=NULL; |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
n=RSA_private_decrypt(length,in,out,orsa,RSA_NO_PADDING); |
|---|
| 168 |
|
|---|
| 169 |
orsa->n=orsa->d=orsa->p=orsa->q=NULL; |
|---|
| 170 |
RSA_free(orsa); |
|---|
| 171 |
|
|---|
| 172 |
return n; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
int ops_rsa_public_encrypt(unsigned char *out,const unsigned char *in, |
|---|
| 176 |
size_t length,const ops_rsa_public_key_t *rsa) |
|---|
| 177 |
{ |
|---|
| 178 |
RSA *orsa; |
|---|
| 179 |
int n; |
|---|
| 180 |
|
|---|
| 181 |
orsa=RSA_new(); |
|---|
| 182 |
orsa->n=rsa->n; |
|---|
| 183 |
orsa->e=rsa->e; |
|---|
| 184 |
|
|---|
| 185 |
n=RSA_public_encrypt(length,in,out,orsa,RSA_NO_PADDING); |
|---|
| 186 |
|
|---|
| 187 |
orsa->n=orsa->e=NULL; |
|---|
| 188 |
RSA_free(orsa); |
|---|
| 189 |
|
|---|
| 190 |
return n; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
void ops_crypto_init() |
|---|
| 194 |
{ |
|---|
| 195 |
#ifdef DMALLOC |
|---|
| 196 |
CRYPTO_malloc_debug_init(); |
|---|
| 197 |
CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); |
|---|
| 198 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); |
|---|
| 199 |
#endif |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
void ops_crypto_finish() |
|---|
| 203 |
{ |
|---|
| 204 |
CRYPTO_cleanup_all_ex_data(); |
|---|
| 205 |
ERR_remove_state(0); |
|---|
| 206 |
#ifdef DMALLOC |
|---|
| 207 |
CRYPTO_mem_leaks_fp(stderr); |
|---|
| 208 |
#endif |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
const char *ops_text_from_hash(ops_hash_t *hash) |
|---|
| 212 |
{ return hash->name; } |
|---|