rsaref.c 17.6 KB
Newer Older
1 2 3 4 5
/* Demo of how to construct your own engine and using it.  The basis of this
   engine is RSAref, an old reference of the RSA algorithm which can still
   be found a little here and there. */

#include <stdio.h>
6
#include <string.h>
7 8
#include "./source/global.h"
#include "./source/rsaref.h"
9
#include "./source/rsa.h"
R
Richard Levitte 已提交
10
#include "./source/des.h"
11
#include <openssl/err.h>
12 13
#define OPENSSL_NO_MD2
#define OPENSSL_NO_MD5
R
Richard Levitte 已提交
14
#include <openssl/evp.h>
15 16 17
#include <openssl/bn.h>
#include <openssl/engine.h>

18 19 20
#define RSAREF_LIB_NAME "rsaref engine"
#include "rsaref_err.c"

R
Richard Levitte 已提交
21 22 23 24 25 26 27
/*****************************************************************************
 *** Function declarations and global variable definitions                 ***
 *****************************************************************************/

/*****************************************************************************
 * Constants used when creating the ENGINE
 **/
28 29 30
static const char *engine_rsaref_id = "rsaref";
static const char *engine_rsaref_name = "RSAref engine support";

R
Richard Levitte 已提交
31 32 33
/*****************************************************************************
 * Functions to handle the engine
 **/
34 35 36 37 38 39 40
static int rsaref_destroy(ENGINE *e);
static int rsaref_init(ENGINE *e);
static int rsaref_finish(ENGINE *e);
#if 0
static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); 
#endif

R
Richard Levitte 已提交
41 42 43 44 45 46 47 48 49 50
/*****************************************************************************
 * Engine commands
 **/
static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
	{0, NULL, NULL, 0}
	};

/*****************************************************************************
 * RSA functions
 **/
51 52 53 54 55 56 57 58 59 60 61 62
static int rsaref_private_decrypt(int len, const unsigned char *from,
	unsigned char *to, RSA *rsa, int padding);
static int rsaref_private_encrypt(int len, const unsigned char *from,
	unsigned char *to, RSA *rsa, int padding);
static int rsaref_public_encrypt(int len, const unsigned char *from,
	unsigned char *to, RSA *rsa, int padding);
static int rsaref_public_decrypt(int len, const unsigned char *from,
	unsigned char *to, RSA *rsa, int padding);
static int bnref_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
			  BN_CTX *ctx, BN_MONT_CTX *m_ctx);
static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);

R
Richard Levitte 已提交
63 64 65
/*****************************************************************************
 * Our RSA method
 **/
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static RSA_METHOD rsaref_rsa =
{
  "RSAref PKCS#1 RSA",
  rsaref_public_encrypt,
  rsaref_public_decrypt,
  rsaref_private_encrypt,
  rsaref_private_decrypt,
  rsaref_mod_exp,
  bnref_mod_exp,
  NULL,
  NULL,
  0,
  NULL,
  NULL,
  NULL
};

R
Richard Levitte 已提交
83 84 85 86 87 88 89 90 91 92 93
/*****************************************************************************
 * Symetric cipher and digest function registrars
 **/
static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
	const int **nids, int nid);
static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
	const int **nids, int nid);

static int rsaref_cipher_nids[] =
	{ NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
static int rsaref_digest_nids[] =
R
Richard Levitte 已提交
94
	{ NID_md2, NID_md5, 0 };
R
Richard Levitte 已提交
95 96 97 98

/*****************************************************************************
 * DES functions
 **/
R
Richard Levitte 已提交
99
static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
100
	const unsigned char *iv, int enc);
R
Richard Levitte 已提交
101
static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
102
	const unsigned char *in, unsigned int inl);
R
Richard Levitte 已提交
103 104
static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
105
	const unsigned char *iv, int enc);
R
Richard Levitte 已提交
106
static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
107
	const unsigned char *in, unsigned int inl);
R
Richard Levitte 已提交
108 109
static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
110
	const unsigned char *iv, int enc);
R
Richard Levitte 已提交
111
static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
112
	const unsigned char *in, unsigned int inl);
R
Richard Levitte 已提交
113
static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
R
Richard Levitte 已提交
114 115 116 117

/*****************************************************************************
 * Our DES ciphers
 **/
R
Richard Levitte 已提交
118
static const EVP_CIPHER cipher_des_cbc =
R
Richard Levitte 已提交
119 120 121
	{
	NID_des_cbc,
	8, 8, 8,
122
	0 | EVP_CIPH_CBC_MODE,
R
Richard Levitte 已提交
123 124 125 126 127 128 129 130 131 132
	cipher_des_cbc_init,
	cipher_des_cbc_code,
	cipher_des_cbc_clean,
	sizeof(DES_CBC_CTX),
	NULL,
	NULL,
	NULL,
	NULL
	};

R
Richard Levitte 已提交
133
static const EVP_CIPHER cipher_des_ede3_cbc =
R
Richard Levitte 已提交
134 135 136
	{
	NID_des_ede3_cbc,
	8, 24, 8,
137
	0 | EVP_CIPH_CBC_MODE,
R
Richard Levitte 已提交
138 139 140 141 142 143 144 145 146 147
	cipher_des_ede3_cbc_init,
	cipher_des_ede3_cbc_code,
	cipher_des_ede3_cbc_clean,
	sizeof(DES3_CBC_CTX),
	NULL,
	NULL,
	NULL,
	NULL
	};

R
Richard Levitte 已提交
148
static const EVP_CIPHER cipher_desx_cbc =
R
Richard Levitte 已提交
149 150 151
	{
	NID_desx_cbc,
	8, 24, 8,
152
	0 | EVP_CIPH_CBC_MODE,
R
Richard Levitte 已提交
153 154 155 156 157 158 159 160 161 162
	cipher_desx_cbc_init,
	cipher_desx_cbc_code,
	cipher_desx_cbc_clean,
	sizeof(DESX_CBC_CTX),
	NULL,
	NULL,
	NULL,
	NULL
	};

R
Richard Levitte 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/*****************************************************************************
 * MD functions
 **/
static int digest_md2_init(EVP_MD_CTX *ctx);
static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
	unsigned long count);
static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md);
static int digest_md5_init(EVP_MD_CTX *ctx);
static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
	unsigned long count);
static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md);

/*****************************************************************************
 * Our MD digests
 **/
static const EVP_MD digest_md2 =
	{
	NID_md2,
	NID_md2WithRSAEncryption,
	16,
	0,
	digest_md2_init,
	digest_md2_update,
	digest_md2_final,
	NULL,
	NULL,
	EVP_PKEY_RSA_method,
	16,
	sizeof(MD2_CTX)
	};

static const EVP_MD digest_md5 =
	{
	NID_md5,
	NID_md5WithRSAEncryption,
	16,
	0,
	digest_md5_init,
	digest_md5_update,
	digest_md5_final,
	NULL,
	NULL,
	EVP_PKEY_RSA_method,
	64,
	sizeof(MD5_CTX)
	};

R
Richard Levitte 已提交
210 211 212 213 214 215 216
/*****************************************************************************
 *** Function definitions                                                  ***
 *****************************************************************************/

/*****************************************************************************
 * Functions to handle the engine
 **/
217 218 219 220 221 222 223

static int bind_rsaref(ENGINE *e)
	{
	const RSA_METHOD *meth1;
	if(!ENGINE_set_id(e, engine_rsaref_id)
		|| !ENGINE_set_name(e, engine_rsaref_name)
		|| !ENGINE_set_RSA(e, &rsaref_rsa)
R
Richard Levitte 已提交
224 225
		|| !ENGINE_set_ciphers(e, rsaref_ciphers)
		|| !ENGINE_set_digests(e, rsaref_digests)
226 227 228 229 230 231 232 233
		|| !ENGINE_set_destroy_function(e, rsaref_destroy)
		|| !ENGINE_set_init_function(e, rsaref_init)
		|| !ENGINE_set_finish_function(e, rsaref_finish)
		/* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
		/* || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns) */)
		return 0;

	/* Ensure the rsaref error handling is set up */
234
	ERR_load_RSAREF_strings();
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	return 1;
	}

#ifdef ENGINE_DYNAMIC_SUPPORT
static int bind_helper(ENGINE *e, const char *id)
	{
	if(id && (strcmp(id, engine_rsaref_id) != 0))
		return 0;
	if(!bind_rsaref(e))
		return 0;
	return 1;
	}       
IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
#else
static ENGINE *engine_rsaref(void)
	{
	ENGINE *ret = ENGINE_new();
	if(!ret)
		return NULL;
	if(!bind_rsaref(ret))
		{
		ENGINE_free(ret);
		return NULL;
		}
	return ret;
	}

void ENGINE_load_rsaref(void)
	{
	/* Copied from eng_[openssl|dyn].c */
	ENGINE *toadd = engine_rsaref();
	if(!toadd) return;
	ENGINE_add(toadd);
	ENGINE_free(toadd);
	ERR_clear_error();
	}
#endif

/* Initiator which is only present to make sure this engine looks available */
static int rsaref_init(ENGINE *e)
	{
	return 1;
	}

/* Finisher which is only present to make sure this engine looks available */
static int rsaref_finish(ENGINE *e)
	{
	return 1;
	}

/* Destructor (complements the "ENGINE_ncipher()" constructor) */
static int rsaref_destroy(ENGINE *e)
	{
289
	ERR_unload_RSAREF_strings();
290 291 292
	return 1;
	}

R
Richard Levitte 已提交
293 294 295 296
/*****************************************************************************
 * RSA functions
 **/

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
	{
	RSAREFerr(RSAREF_F_RSAREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
	return(0);
	}

static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
			  const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
	{
	RSAREFerr(RSAREF_F_BNREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
	return(0);
	}

/* unsigned char *to:  [max]    */
static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
	{
	int i;

	i=BN_num_bytes(from);
	if (i > max)
		{
		RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
		return(0);
		}

	memset(to,0,(unsigned int)max);
	if (!BN_bn2bin(from,&(to[max-i])))
		return(0);
	return(1);
	}

#ifdef undef
/* unsigned char *from:  [max]    */
static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
	{
	int i;
	BIGNUM *ret;

	for (i=0; i<max; i++)
		if (from[i]) break;

	ret=BN_bin2bn(&(from[i]),max-i,to);
	return(ret);
	}

static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
	{
	to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
	to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
	if ((to->n == NULL) || (to->e == NULL)) return(0);
	return(1);
	}
#endif

351
static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
352 353
	{
	to->bits=BN_num_bits(from->n);
354 355
	if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
	if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	return(1);
	}

#ifdef undef
static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
	{
	if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
		return(0);
	if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
		return(0);
	if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
		return(0);
	if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
		return(0);
	if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
		return(0);
	if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
		== NULL)
		return(0);
	if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
		== NULL)
		return(0);
	if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
		return(0);
	return(1);
	}
#endif

384
static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
385 386
	{
	to->bits=BN_num_bits(from->n);
387 388 389 390 391 392 393 394
	if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
	if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
	if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
	if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
	if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
	if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
	if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
	if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
395 396 397 398 399 400 401
	return(1);
	}

static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned char *to,
	     RSA *rsa, int padding)
	{
	int i,outlen= -1;
402
	R_RSA_PRIVATE_KEY RSAkey;
403 404 405

	if (!RSAref_Private_eay2ref(rsa,&RSAkey))
		goto err;
R
Richard Levitte 已提交
406
	if ((i=RSAPrivateDecrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
407
		{
408
		RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
409 410 411 412 413 414 415 416 417 418 419
		outlen= -1;
		}
err:
	memset(&RSAkey,0,sizeof(RSAkey));
	return(outlen);
	}

static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned char *to,
	     RSA *rsa, int padding)
	{
	int i,outlen= -1;
420
	R_RSA_PRIVATE_KEY RSAkey;
421 422 423

	if (padding != RSA_PKCS1_PADDING)
		{
424
		RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
425 426 427 428
		goto err;
	}
	if (!RSAref_Private_eay2ref(rsa,&RSAkey))
		goto err;
R
Richard Levitte 已提交
429
	if ((i=RSAPrivateEncrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
430
		{
431
		RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
432 433 434 435 436 437 438 439 440 441 442
		outlen= -1;
		}
err:
	memset(&RSAkey,0,sizeof(RSAkey));
	return(outlen);
	}

static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned char *to,
	     RSA *rsa, int padding)
	{
	int i,outlen= -1;
443
	R_RSA_PUBLIC_KEY RSAkey;
444 445 446

	if (!RSAref_Public_eay2ref(rsa,&RSAkey))
		goto err;
R
Richard Levitte 已提交
447
	if ((i=RSAPublicDecrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
448
		{
449
		RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
450 451 452 453 454 455 456 457 458 459 460 461
		outlen= -1;
		}
err:
	memset(&RSAkey,0,sizeof(RSAkey));
	return(outlen);
	}

static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to,
	     RSA *rsa, int padding)
	{
	int outlen= -1;
	int i;
462 463
	R_RSA_PUBLIC_KEY RSAkey;
	R_RANDOM_STRUCT rnd;
464 465 466 467
	unsigned char buf[16];

	if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
		{
468
		RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
		goto err;
		}
	
	R_RandomInit(&rnd);
	R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
	while (i > 0)
		{
		if (RAND_bytes(buf,16) <= 0)
			goto err;
		R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
		i-=16;
		}

	if (!RSAref_Public_eay2ref(rsa,&RSAkey))
		goto err;
R
Richard Levitte 已提交
484
	if ((i=RSAPublicEncrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
485
		{
486
		RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
487 488 489 490 491 492 493 494 495
		outlen= -1;
		goto err;
		}
err:
	memset(&RSAkey,0,sizeof(RSAkey));
	R_RandomFinal(&rnd);
	memset(&rnd,0,sizeof(rnd));
	return(outlen);
	}
R
Richard Levitte 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

/*****************************************************************************
 * Symetric cipher and digest function registrars
 **/
static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
	const int **nids, int nid)
	{
	int ok = 1;
	if(!cipher)
		{
		/* We are returning a list of supported nids */
		*nids = rsaref_cipher_nids;
		return (sizeof(rsaref_cipher_nids)-1)/sizeof(rsaref_cipher_nids[0]);
		}
	/* We are being asked for a specific cipher */
	switch (nid)
		{
	case NID_des_cbc:
		*cipher = &cipher_des_cbc; break;
	case NID_des_ede3_cbc:
		*cipher = &cipher_des_ede3_cbc; break;
	case NID_desx_cbc:
		*cipher = &cipher_desx_cbc; break;
	default:
		ok = 0;
		*cipher = NULL;
		break;
		}
	return ok;
	}
static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
	const int **nids, int nid)
	{
	int ok = 1;
	if(!digest)
		{
		/* We are returning a list of supported nids */
		*nids = rsaref_digest_nids;
		return (sizeof(rsaref_digest_nids)-1)/sizeof(rsaref_digest_nids[0]);
		}
	/* We are being asked for a specific digest */
	switch (nid)
		{
R
Richard Levitte 已提交
539 540 541 542
	case NID_md2:
		*digest = &digest_md2; break;
	case NID_md5:
		*digest = &digest_md5; break;
R
Richard Levitte 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555
	default:
		ok = 0;
		*digest = NULL;
		break;
		}
	return ok;
	}

/*****************************************************************************
 * DES functions
 **/
#undef data
#define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
R
Richard Levitte 已提交
556
static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
557 558 559
	const unsigned char *iv, int enc)
	{
	DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
R
Richard Levitte 已提交
560
	return 1;
R
Richard Levitte 已提交
561
	}
R
Richard Levitte 已提交
562
static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
563 564
	const unsigned char *in, unsigned int inl)
	{
R
Richard Levitte 已提交
565 566 567 568 569 570 571 572 573 574 575 576
	int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
	switch (ret)
		{
	case RE_LEN:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
		break;
	case 0:
		break;
	default:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
		}
	return !ret;
R
Richard Levitte 已提交
577
	}
R
Richard Levitte 已提交
578
static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
R
Richard Levitte 已提交
579 580
	{
	memset(data(ctx), 0, ctx->cipher->ctx_size);
R
Richard Levitte 已提交
581
	return 1;
R
Richard Levitte 已提交
582 583 584 585
	}

#undef data
#define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
R
Richard Levitte 已提交
586
static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
587 588 589 590
	const unsigned char *iv, int enc)
	{
	DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
		enc);
R
Richard Levitte 已提交
591
	return 1;
R
Richard Levitte 已提交
592
	}
R
Richard Levitte 已提交
593
static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
594 595
	const unsigned char *in, unsigned int inl)
	{
R
Richard Levitte 已提交
596 597 598 599 600 601 602 603 604 605 606 607
	int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
	switch (ret)
		{
	case RE_LEN:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
		break;
	case 0:
		break;
	default:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
		}
	return !ret;
R
Richard Levitte 已提交
608
	}
R
Richard Levitte 已提交
609
static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
R
Richard Levitte 已提交
610 611
	{
	memset(data(ctx), 0, ctx->cipher->ctx_size);
R
Richard Levitte 已提交
612
	return 1;
R
Richard Levitte 已提交
613 614 615 616
	}

#undef data
#define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
R
Richard Levitte 已提交
617
static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
R
Richard Levitte 已提交
618 619 620 621
	const unsigned char *iv, int enc)
	{
	DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
		enc);
R
Richard Levitte 已提交
622
	return 1;
R
Richard Levitte 已提交
623
	}
R
Richard Levitte 已提交
624
static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
R
Richard Levitte 已提交
625 626
	const unsigned char *in, unsigned int inl)
	{
R
Richard Levitte 已提交
627 628 629 630 631 632 633 634 635 636 637 638
	int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
	switch (ret)
		{
	case RE_LEN:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
		break;
	case 0:
		break;
	default:
		RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
		}
	return !ret;
R
Richard Levitte 已提交
639
	}
R
Richard Levitte 已提交
640
static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
R
Richard Levitte 已提交
641 642
	{
	memset(data(ctx), 0, ctx->cipher->ctx_size);
R
Richard Levitte 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	return 1;
	}

/*****************************************************************************
 * MD functions
 **/
#undef data
#define data(ctx) ((MD2_CTX *)(ctx)->md_data)
static int digest_md2_init(EVP_MD_CTX *ctx)
	{
	MD2Init(data(ctx));
	return 1;
	}
static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
	unsigned long count)
	{
	MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
	return 1;
	}
static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md)
	{
	MD2Final(md, data(ctx));
	return 1;
	}

#undef data
#define data(ctx) ((MD5_CTX *)(ctx)->md_data)
static int digest_md5_init(EVP_MD_CTX *ctx)
	{
	MD5Init(data(ctx));
	return 1;
	}
static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
	unsigned long count)
	{
	MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
	return 1;
	}
static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
	{
	MD5Final(md, data(ctx));
	return 1;
R
Richard Levitte 已提交
685
	}