| 1 |
/* |
| 2 |
* Copyright (c) 2006 - 2016 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifdef HAVE_CONFIG_H |
| 35 |
#include <config.h> |
| 36 |
#endif |
| 37 |
#include <roken.h> |
| 38 |
|
| 39 |
#define HC_DEPRECATED |
| 40 |
#define HC_DEPRECATED_CRYPTO |
| 41 |
|
| 42 |
#include <assert.h> |
| 43 |
|
| 44 |
#include <evp.h> |
| 45 |
#include <evp-hcrypto.h> |
| 46 |
#include <evp-cc.h> |
| 47 |
#if defined(_WIN32) |
| 48 |
#include <evp-w32.h> |
| 49 |
#endif |
| 50 |
#include <evp-pkcs11.h> |
| 51 |
#include <evp-openssl.h> |
| 52 |
|
| 53 |
#include <krb5-types.h> |
| 54 |
|
| 55 |
#ifndef HCRYPTO_DEF_PROVIDER |
| 56 |
# ifdef __APPLE__ |
| 57 |
# define HCRYPTO_DEF_PROVIDER cc |
| 58 |
# elif __sun |
| 59 |
# define HCRYPTO_DEF_PROVIDER pkcs11_hcrypto |
| 60 |
# elif HAVE_HCRYPTO_W_OPENSSL |
| 61 |
# define HCRYPTO_DEF_PROVIDER ossl |
| 62 |
# else |
| 63 |
# define HCRYPTO_DEF_PROVIDER hcrypto |
| 64 |
# endif |
| 65 |
#endif |
| 66 |
|
| 67 |
#define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa |
| 68 |
|
| 69 |
|
| 70 |
#define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)() |
| 71 |
|
| 72 |
/** |
| 73 |
* @page page_evp EVP - generic crypto interface |
| 74 |
* |
| 75 |
* See the library functions here: @ref hcrypto_evp |
| 76 |
* |
| 77 |
* @section evp_cipher EVP Cipher |
| 78 |
* |
| 79 |
* The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to |
| 80 |
* understand forward, then EVP_CipherUpdate() and |
| 81 |
* EVP_CipherFinal_ex() really needs an example to explain @ref |
| 82 |
* example_evp_cipher.c . |
| 83 |
* |
| 84 |
* @example example_evp_cipher.c |
| 85 |
* |
| 86 |
* This is an example how to use EVP_CipherInit_ex(), |
| 87 |
* EVP_CipherUpdate() and EVP_CipherFinal_ex(). |
| 88 |
*/ |
| 89 |
|
| 90 |
struct hc_EVP_MD_CTX { |
| 91 |
const EVP_MD *md; |
| 92 |
ENGINE *engine; |
| 93 |
void *ptr; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Return the output size of the message digest function. |
| 99 |
* |
| 100 |
* @param md the evp message |
| 101 |
* |
| 102 |
* @return size output size of the message digest function. |
| 103 |
* |
| 104 |
* @ingroup hcrypto_evp |
| 105 |
*/ |
| 106 |
|
| 107 |
size_t |
| 108 |
EVP_MD_size(const EVP_MD *md) |
| 109 |
{ |
| 110 |
return md->hash_size; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Return the blocksize of the message digest function. |
| 115 |
* |
| 116 |
* @param md the evp message |
| 117 |
* |
| 118 |
* @return size size of the message digest block size |
| 119 |
* |
| 120 |
* @ingroup hcrypto_evp |
| 121 |
*/ |
| 122 |
|
| 123 |
size_t |
| 124 |
EVP_MD_block_size(const EVP_MD *md) |
| 125 |
{ |
| 126 |
return md->block_size; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Allocate a messsage digest context object. Free with |
| 131 |
* EVP_MD_CTX_destroy(). |
| 132 |
* |
| 133 |
* @return a newly allocated message digest context object. |
| 134 |
* |
| 135 |
* @ingroup hcrypto_evp |
| 136 |
*/ |
| 137 |
|
| 138 |
EVP_MD_CTX * |
| 139 |
EVP_MD_CTX_create(void) |
| 140 |
{ |
| 141 |
return calloc(1, sizeof(EVP_MD_CTX)); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Initiate a messsage digest context object. Deallocate with |
| 146 |
* EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead. |
| 147 |
* |
| 148 |
* @param ctx variable to initiate. |
| 149 |
* |
| 150 |
* @ingroup hcrypto_evp |
| 151 |
*/ |
| 152 |
|
| 153 |
void |
| 154 |
EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED |
| 155 |
{ |
| 156 |
memset(ctx, 0, sizeof(*ctx)); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Free a messsage digest context object. |
| 161 |
* |
| 162 |
* @param ctx context to free. |
| 163 |
* |
| 164 |
* @ingroup hcrypto_evp |
| 165 |
*/ |
| 166 |
|
| 167 |
void |
| 168 |
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) |
| 169 |
{ |
| 170 |
EVP_MD_CTX_cleanup(ctx); |
| 171 |
free(ctx); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Free the resources used by the EVP_MD context. |
| 176 |
* |
| 177 |
* @param ctx the context to free the resources from. |
| 178 |
* |
| 179 |
* @return 1 on success. |
| 180 |
* |
| 181 |
* @ingroup hcrypto_evp |
| 182 |
*/ |
| 183 |
|
| 184 |
int |
| 185 |
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED |
| 186 |
{ |
| 187 |
if (ctx->md && ctx->md->cleanup) { |
| 188 |
int ret = (ctx->md->cleanup)(ctx->ptr); |
| 189 |
if (!ret) |
| 190 |
return ret; |
| 191 |
} else if (ctx->md) { |
| 192 |
memset(ctx->ptr, 0, ctx->md->ctx_size); |
| 193 |
} |
| 194 |
ctx->md = NULL; |
| 195 |
ctx->engine = NULL; |
| 196 |
free(ctx->ptr); |
| 197 |
memset(ctx, 0, sizeof(*ctx)); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the EVP_MD use for a specified context. |
| 203 |
* |
| 204 |
* @param ctx the EVP_MD context to get the EVP_MD for. |
| 205 |
* |
| 206 |
* @return the EVP_MD used for the context. |
| 207 |
* |
| 208 |
* @ingroup hcrypto_evp |
| 209 |
*/ |
| 210 |
|
| 211 |
const EVP_MD * |
| 212 |
EVP_MD_CTX_md(EVP_MD_CTX *ctx) |
| 213 |
{ |
| 214 |
return ctx->md; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Return the output size of the message digest function. |
| 219 |
* |
| 220 |
* @param ctx the evp message digest context |
| 221 |
* |
| 222 |
* @return size output size of the message digest function. |
| 223 |
* |
| 224 |
* @ingroup hcrypto_evp |
| 225 |
*/ |
| 226 |
|
| 227 |
size_t |
| 228 |
EVP_MD_CTX_size(EVP_MD_CTX *ctx) |
| 229 |
{ |
| 230 |
return EVP_MD_size(ctx->md); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Return the blocksize of the message digest function. |
| 235 |
* |
| 236 |
* @param ctx the evp message digest context |
| 237 |
* |
| 238 |
* @return size size of the message digest block size |
| 239 |
* |
| 240 |
* @ingroup hcrypto_evp |
| 241 |
*/ |
| 242 |
|
| 243 |
size_t |
| 244 |
EVP_MD_CTX_block_size(EVP_MD_CTX *ctx) |
| 245 |
{ |
| 246 |
return EVP_MD_block_size(ctx->md); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Init a EVP_MD_CTX for use a specific message digest and engine. |
| 251 |
* |
| 252 |
* @param ctx the message digest context to init. |
| 253 |
* @param md the message digest to use. |
| 254 |
* @param engine the engine to use, NULL to use the default engine. |
| 255 |
* |
| 256 |
* @return 1 on success. |
| 257 |
* |
| 258 |
* @ingroup hcrypto_evp |
| 259 |
*/ |
| 260 |
|
| 261 |
int |
| 262 |
EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine) |
| 263 |
{ |
| 264 |
if (ctx->md != md || ctx->engine != engine) { |
| 265 |
EVP_MD_CTX_cleanup(ctx); |
| 266 |
ctx->md = md; |
| 267 |
ctx->engine = engine; |
| 268 |
if (md == NULL) |
| 269 |
return 0; |
| 270 |
|
| 271 |
ctx->ptr = calloc(1, md->ctx_size); |
| 272 |
if (ctx->ptr == NULL) |
| 273 |
return 0; |
| 274 |
} |
| 275 |
if (ctx->md == 0) |
| 276 |
return 0; |
| 277 |
return (ctx->md->init)(ctx->ptr); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Update the digest with some data. |
| 282 |
* |
| 283 |
* @param ctx the context to update |
| 284 |
* @param data the data to update the context with |
| 285 |
* @param size length of data |
| 286 |
* |
| 287 |
* @return 1 on success. |
| 288 |
* |
| 289 |
* @ingroup hcrypto_evp |
| 290 |
*/ |
| 291 |
|
| 292 |
int |
| 293 |
EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size) |
| 294 |
{ |
| 295 |
(ctx->md->update)(ctx->ptr, data, size); |
| 296 |
return 1; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Complete the message digest. |
| 301 |
* |
| 302 |
* @param ctx the context to complete. |
| 303 |
* @param hash the output of the message digest function. At least |
| 304 |
* EVP_MD_size(). |
| 305 |
* @param size the output size of hash. |
| 306 |
* |
| 307 |
* @return 1 on success. |
| 308 |
* |
| 309 |
* @ingroup hcrypto_evp |
| 310 |
*/ |
| 311 |
|
| 312 |
int |
| 313 |
EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size) |
| 314 |
{ |
| 315 |
(ctx->md->final)(hash, ctx->ptr); |
| 316 |
if (size) |
| 317 |
*size = ctx->md->hash_size; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(), |
| 323 |
* EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy() |
| 324 |
* dance in one call. |
| 325 |
* |
| 326 |
* @param data the data to update the context with |
| 327 |
* @param dsize length of data |
| 328 |
* @param hash output data of at least EVP_MD_size() length. |
| 329 |
* @param hsize output length of hash. |
| 330 |
* @param md message digest to use |
| 331 |
* @param engine engine to use, NULL for default engine. |
| 332 |
* |
| 333 |
* @return 1 on success. |
| 334 |
* |
| 335 |
* @ingroup hcrypto_evp |
| 336 |
*/ |
| 337 |
|
| 338 |
int |
| 339 |
EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize, |
| 340 |
const EVP_MD *md, ENGINE *engine) |
| 341 |
{ |
| 342 |
EVP_MD_CTX *ctx; |
| 343 |
int ret; |
| 344 |
|
| 345 |
ctx = EVP_MD_CTX_create(); |
| 346 |
if (ctx == NULL) |
| 347 |
return 0; |
| 348 |
ret = EVP_DigestInit_ex(ctx, md, engine); |
| 349 |
if (ret != 1) { |
| 350 |
EVP_MD_CTX_destroy(ctx); |
| 351 |
return ret; |
| 352 |
} |
| 353 |
ret = EVP_DigestUpdate(ctx, data, dsize); |
| 354 |
if (ret != 1) { |
| 355 |
EVP_MD_CTX_destroy(ctx); |
| 356 |
return ret; |
| 357 |
} |
| 358 |
ret = EVP_DigestFinal_ex(ctx, hash, hsize); |
| 359 |
EVP_MD_CTX_destroy(ctx); |
| 360 |
return ret; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* The message digest SHA256 |
| 365 |
* |
| 366 |
* @return the message digest type. |
| 367 |
* |
| 368 |
* @ingroup hcrypto_evp |
| 369 |
*/ |
| 370 |
|
| 371 |
const EVP_MD * |
| 372 |
EVP_sha256(void) |
| 373 |
{ |
| 374 |
hcrypto_validate(); |
| 375 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* The message digest SHA384 |
| 380 |
* |
| 381 |
* @return the message digest type. |
| 382 |
* |
| 383 |
* @ingroup hcrypto_evp |
| 384 |
*/ |
| 385 |
|
| 386 |
const EVP_MD * |
| 387 |
EVP_sha384(void) |
| 388 |
{ |
| 389 |
hcrypto_validate(); |
| 390 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* The message digest SHA512 |
| 395 |
* |
| 396 |
* @return the message digest type. |
| 397 |
* |
| 398 |
* @ingroup hcrypto_evp |
| 399 |
*/ |
| 400 |
|
| 401 |
const EVP_MD * |
| 402 |
EVP_sha512(void) |
| 403 |
{ |
| 404 |
hcrypto_validate(); |
| 405 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512); |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* The message digest SHA1 |
| 410 |
* |
| 411 |
* @return the message digest type. |
| 412 |
* |
| 413 |
* @ingroup hcrypto_evp |
| 414 |
*/ |
| 415 |
|
| 416 |
const EVP_MD * |
| 417 |
EVP_sha1(void) |
| 418 |
{ |
| 419 |
hcrypto_validate(); |
| 420 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* The message digest SHA1 |
| 425 |
* |
| 426 |
* @return the message digest type. |
| 427 |
* |
| 428 |
* @ingroup hcrypto_evp |
| 429 |
*/ |
| 430 |
|
| 431 |
const EVP_MD * |
| 432 |
EVP_sha(void) HC_DEPRECATED |
| 433 |
|
| 434 |
{ |
| 435 |
hcrypto_validate(); |
| 436 |
return EVP_sha1(); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* The message digest MD5 |
| 441 |
* |
| 442 |
* @return the message digest type. |
| 443 |
* |
| 444 |
* @ingroup hcrypto_evp |
| 445 |
*/ |
| 446 |
|
| 447 |
const EVP_MD * |
| 448 |
EVP_md5(void) HC_DEPRECATED_CRYPTO |
| 449 |
{ |
| 450 |
hcrypto_validate(); |
| 451 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5); |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* The message digest MD4 |
| 456 |
* |
| 457 |
* @return the message digest type. |
| 458 |
* |
| 459 |
* @ingroup hcrypto_evp |
| 460 |
*/ |
| 461 |
|
| 462 |
const EVP_MD * |
| 463 |
EVP_md4(void) HC_DEPRECATED_CRYPTO |
| 464 |
{ |
| 465 |
hcrypto_validate(); |
| 466 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4); |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
* |
| 471 |
*/ |
| 472 |
|
| 473 |
static int |
| 474 |
null_Init (void *m) |
| 475 |
{ |
| 476 |
return -1; |
| 477 |
} |
| 478 |
static int |
| 479 |
null_Update (void *m, const void * data, size_t size) |
| 480 |
{ |
| 481 |
return -1; |
| 482 |
} |
| 483 |
static int |
| 484 |
null_Final(void *res, void *m) |
| 485 |
{ |
| 486 |
return -1; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* The null message digest |
| 491 |
* |
| 492 |
* @return the message digest type. |
| 493 |
* |
| 494 |
* @ingroup hcrypto_evp |
| 495 |
*/ |
| 496 |
|
| 497 |
const EVP_MD * |
| 498 |
EVP_md_null(void) |
| 499 |
{ |
| 500 |
static const struct hc_evp_md null = { |
| 501 |
0, |
| 502 |
0, |
| 503 |
0, |
| 504 |
(hc_evp_md_init)null_Init, |
| 505 |
(hc_evp_md_update)null_Update, |
| 506 |
(hc_evp_md_final)null_Final, |
| 507 |
NULL |
| 508 |
}; |
| 509 |
return &null; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Return the block size of the cipher. |
| 514 |
* |
| 515 |
* @param c cipher to get the block size from. |
| 516 |
* |
| 517 |
* @return the block size of the cipher. |
| 518 |
* |
| 519 |
* @ingroup hcrypto_evp |
| 520 |
*/ |
| 521 |
|
| 522 |
size_t |
| 523 |
EVP_CIPHER_block_size(const EVP_CIPHER *c) |
| 524 |
{ |
| 525 |
return c->block_size; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Return the key size of the cipher. |
| 530 |
* |
| 531 |
* @param c cipher to get the key size from. |
| 532 |
* |
| 533 |
* @return the key size of the cipher. |
| 534 |
* |
| 535 |
* @ingroup hcrypto_evp |
| 536 |
*/ |
| 537 |
|
| 538 |
size_t |
| 539 |
EVP_CIPHER_key_length(const EVP_CIPHER *c) |
| 540 |
{ |
| 541 |
return c->key_len; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Return the IV size of the cipher. |
| 546 |
* |
| 547 |
* @param c cipher to get the IV size from. |
| 548 |
* |
| 549 |
* @return the IV size of the cipher. |
| 550 |
* |
| 551 |
* @ingroup hcrypto_evp |
| 552 |
*/ |
| 553 |
|
| 554 |
size_t |
| 555 |
EVP_CIPHER_iv_length(const EVP_CIPHER *c) |
| 556 |
{ |
| 557 |
return c->iv_len; |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Initiate a EVP_CIPHER_CTX context. Clean up with |
| 562 |
* EVP_CIPHER_CTX_cleanup(). |
| 563 |
* |
| 564 |
* @param c the cipher initiate. |
| 565 |
* |
| 566 |
* @ingroup hcrypto_evp |
| 567 |
*/ |
| 568 |
|
| 569 |
void |
| 570 |
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c) |
| 571 |
{ |
| 572 |
memset(c, 0, sizeof(*c)); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Clean up the EVP_CIPHER_CTX context. |
| 577 |
* |
| 578 |
* @param c the cipher to clean up. |
| 579 |
* |
| 580 |
* @return 1 on success. |
| 581 |
* |
| 582 |
* @ingroup hcrypto_evp |
| 583 |
*/ |
| 584 |
|
| 585 |
int |
| 586 |
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) |
| 587 |
{ |
| 588 |
if (c->cipher && c->cipher->cleanup) { |
| 589 |
int ret = c->cipher->cleanup(c); |
| 590 |
if (!ret) |
| 591 |
return ret; |
| 592 |
} |
| 593 |
if (c->cipher_data) { |
| 594 |
if (c->cipher) |
| 595 |
memset(c->cipher_data, 0, c->cipher->ctx_size); |
| 596 |
free(c->cipher_data); |
| 597 |
c->cipher_data = NULL; |
| 598 |
} |
| 599 |
return 1; |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* If the cipher type supports it, change the key length |
| 604 |
* |
| 605 |
* @param c the cipher context to change the key length for |
| 606 |
* @param length new key length |
| 607 |
* |
| 608 |
* @return 1 on success. |
| 609 |
* |
| 610 |
* @ingroup hcrypto_evp |
| 611 |
*/ |
| 612 |
|
| 613 |
int |
| 614 |
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length) |
| 615 |
{ |
| 616 |
if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) { |
| 617 |
c->key_len = length; |
| 618 |
return 1; |
| 619 |
} |
| 620 |
return 0; |
| 621 |
} |
| 622 |
|
| 623 |
#if 0 |
| 624 |
int |
| 625 |
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad) |
| 626 |
{ |
| 627 |
return 0; |
| 628 |
} |
| 629 |
#endif |
| 630 |
|
| 631 |
/** |
| 632 |
* Return the EVP_CIPHER for a EVP_CIPHER_CTX context. |
| 633 |
* |
| 634 |
* @param ctx the context to get the cipher type from. |
| 635 |
* |
| 636 |
* @return the EVP_CIPHER pointer. |
| 637 |
* |
| 638 |
* @ingroup hcrypto_evp |
| 639 |
*/ |
| 640 |
|
| 641 |
const EVP_CIPHER * |
| 642 |
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx) |
| 643 |
{ |
| 644 |
return ctx->cipher; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Return the block size of the cipher context. |
| 649 |
* |
| 650 |
* @param ctx cipher context to get the block size from. |
| 651 |
* |
| 652 |
* @return the block size of the cipher context. |
| 653 |
* |
| 654 |
* @ingroup hcrypto_evp |
| 655 |
*/ |
| 656 |
|
| 657 |
size_t |
| 658 |
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) |
| 659 |
{ |
| 660 |
return EVP_CIPHER_block_size(ctx->cipher); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Return the key size of the cipher context. |
| 665 |
* |
| 666 |
* @param ctx cipher context to get the key size from. |
| 667 |
* |
| 668 |
* @return the key size of the cipher context. |
| 669 |
* |
| 670 |
* @ingroup hcrypto_evp |
| 671 |
*/ |
| 672 |
|
| 673 |
size_t |
| 674 |
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) |
| 675 |
{ |
| 676 |
return EVP_CIPHER_key_length(ctx->cipher); |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Return the IV size of the cipher context. |
| 681 |
* |
| 682 |
* @param ctx cipher context to get the IV size from. |
| 683 |
* |
| 684 |
* @return the IV size of the cipher context. |
| 685 |
* |
| 686 |
* @ingroup hcrypto_evp |
| 687 |
*/ |
| 688 |
|
| 689 |
size_t |
| 690 |
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) |
| 691 |
{ |
| 692 |
return EVP_CIPHER_iv_length(ctx->cipher); |
| 693 |
} |
| 694 |
|
| 695 |
/** |
| 696 |
* Get the flags for an EVP_CIPHER_CTX context. |
| 697 |
* |
| 698 |
* @param ctx the EVP_CIPHER_CTX to get the flags from |
| 699 |
* |
| 700 |
* @return the flags for an EVP_CIPHER_CTX. |
| 701 |
* |
| 702 |
* @ingroup hcrypto_evp |
| 703 |
*/ |
| 704 |
|
| 705 |
unsigned long |
| 706 |
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) |
| 707 |
{ |
| 708 |
return ctx->cipher->flags; |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* Get the mode for an EVP_CIPHER_CTX context. |
| 713 |
* |
| 714 |
* @param ctx the EVP_CIPHER_CTX to get the mode from |
| 715 |
* |
| 716 |
* @return the mode for an EVP_CIPHER_CTX. |
| 717 |
* |
| 718 |
* @ingroup hcrypto_evp |
| 719 |
*/ |
| 720 |
|
| 721 |
int |
| 722 |
EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) |
| 723 |
{ |
| 724 |
return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE; |
| 725 |
} |
| 726 |
|
| 727 |
/** |
| 728 |
* Get the app data for an EVP_CIPHER_CTX context. |
| 729 |
* |
| 730 |
* @param ctx the EVP_CIPHER_CTX to get the app data from |
| 731 |
* |
| 732 |
* @return the app data for an EVP_CIPHER_CTX. |
| 733 |
* |
| 734 |
* @ingroup hcrypto_evp |
| 735 |
*/ |
| 736 |
|
| 737 |
void * |
| 738 |
EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx) |
| 739 |
{ |
| 740 |
return ctx->app_data; |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* Set the app data for an EVP_CIPHER_CTX context. |
| 745 |
* |
| 746 |
* @param ctx the EVP_CIPHER_CTX to set the app data for |
| 747 |
* @param data the app data to set for an EVP_CIPHER_CTX. |
| 748 |
* |
| 749 |
* @ingroup hcrypto_evp |
| 750 |
*/ |
| 751 |
|
| 752 |
void |
| 753 |
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) |
| 754 |
{ |
| 755 |
ctx->app_data = data; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data. |
| 760 |
* Clean up with EVP_CIPHER_CTX_cleanup(). |
| 761 |
* |
| 762 |
* @param ctx context to initiate |
| 763 |
* @param c cipher to use. |
| 764 |
* @param engine crypto engine to use, NULL to select default. |
| 765 |
* @param key the crypto key to use, NULL will use the previous value. |
| 766 |
* @param iv the IV to use, NULL will use the previous value. |
| 767 |
* @param encp non zero will encrypt, -1 use the previous value. |
| 768 |
* |
| 769 |
* @return 1 on success. |
| 770 |
* |
| 771 |
* @ingroup hcrypto_evp |
| 772 |
*/ |
| 773 |
|
| 774 |
int |
| 775 |
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine, |
| 776 |
const void *key, const void *iv, int encp) |
| 777 |
{ |
| 778 |
ctx->buf_len = 0; |
| 779 |
|
| 780 |
if (encp == -1) |
| 781 |
encp = ctx->encrypt; |
| 782 |
else |
| 783 |
ctx->encrypt = (encp ? 1 : 0); |
| 784 |
|
| 785 |
if (c && (c != ctx->cipher)) { |
| 786 |
EVP_CIPHER_CTX_cleanup(ctx); |
| 787 |
ctx->cipher = c; |
| 788 |
ctx->key_len = c->key_len; |
| 789 |
|
| 790 |
ctx->cipher_data = calloc(1, c->ctx_size); |
| 791 |
if (ctx->cipher_data == NULL && c->ctx_size != 0) |
| 792 |
return 0; |
| 793 |
|
| 794 |
/* assume block size is a multiple of 2 */ |
| 795 |
ctx->block_mask = EVP_CIPHER_block_size(c) - 1; |
| 796 |
|
| 797 |
if ((ctx->cipher->flags & EVP_CIPH_CTRL_INIT) && |
| 798 |
!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) |
| 799 |
return 0; |
| 800 |
|
| 801 |
} else if (ctx->cipher == NULL) { |
| 802 |
/* reuse of cipher, but not any cipher ever set! */ |
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
switch (EVP_CIPHER_CTX_mode(ctx)) { |
| 807 |
case EVP_CIPH_CBC_MODE: |
| 808 |
|
| 809 |
assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv)); |
| 810 |
|
| 811 |
if (iv) |
| 812 |
memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 813 |
memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 814 |
break; |
| 815 |
|
| 816 |
case EVP_CIPH_STREAM_CIPHER: |
| 817 |
break; |
| 818 |
case EVP_CIPH_CFB8_MODE: |
| 819 |
if (iv) |
| 820 |
memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx)); |
| 821 |
break; |
| 822 |
|
| 823 |
default: |
| 824 |
return 0; |
| 825 |
} |
| 826 |
|
| 827 |
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) |
| 828 |
return ctx->cipher->init(ctx, key, iv, encp); |
| 829 |
|
| 830 |
return 1; |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
* Encipher/decipher partial data |
| 835 |
* |
| 836 |
* @param ctx the cipher context. |
| 837 |
* @param out output data from the operation. |
| 838 |
* @param outlen output length |
| 839 |
* @param in input data to the operation. |
| 840 |
* @param inlen length of data. |
| 841 |
* |
| 842 |
* The output buffer length should at least be EVP_CIPHER_block_size() |
| 843 |
* byte longer then the input length. |
| 844 |
* |
| 845 |
* See @ref evp_cipher for an example how to use this function. |
| 846 |
* |
| 847 |
* @return 1 on success. |
| 848 |
* |
| 849 |
* @ingroup hcrypto_evp |
| 850 |
*/ |
| 851 |
|
| 852 |
int |
| 853 |
EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen, |
| 854 |
void *in, size_t inlen) |
| 855 |
{ |
| 856 |
int ret, left, blocksize; |
| 857 |
|
| 858 |
*outlen = 0; |
| 859 |
|
| 860 |
/** |
| 861 |
* If there in no spare bytes in the left from last Update and the |
| 862 |
* input length is on the block boundery, the EVP_CipherUpdate() |
| 863 |
* function can take a shortcut (and preformance gain) and |
| 864 |
* directly encrypt the data, otherwise we hav to fix it up and |
| 865 |
* store extra it the EVP_CIPHER_CTX. |
| 866 |
*/ |
| 867 |
if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) { |
| 868 |
ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen); |
| 869 |
if (ret == 1) |
| 870 |
*outlen = inlen; |
| 871 |
else |
| 872 |
*outlen = 0; |
| 873 |
return ret; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
blocksize = EVP_CIPHER_CTX_block_size(ctx); |
| 878 |
left = blocksize - ctx->buf_len; |
| 879 |
assert(left > 0); |
| 880 |
|
| 881 |
if (ctx->buf_len) { |
| 882 |
|
| 883 |
/* if total buffer is smaller then input, store locally */ |
| 884 |
if (inlen < left) { |
| 885 |
memcpy(ctx->buf + ctx->buf_len, in, inlen); |
| 886 |
ctx->buf_len += inlen; |
| 887 |
return 1; |
| 888 |
} |
| 889 |
|
| 890 |
/* fill in local buffer and encrypt */ |
| 891 |
memcpy(ctx->buf + ctx->buf_len, in, left); |
| 892 |
ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize); |
| 893 |
memset(ctx->buf, 0, blocksize); |
| 894 |
if (ret != 1) |
| 895 |
return ret; |
| 896 |
|
| 897 |
*outlen += blocksize; |
| 898 |
inlen -= left; |
| 899 |
in = ((unsigned char *)in) + left; |
| 900 |
out = ((unsigned char *)out) + blocksize; |
| 901 |
ctx->buf_len = 0; |
| 902 |
} |
| 903 |
|
| 904 |
if (inlen) { |
| 905 |
ctx->buf_len = (inlen & ctx->block_mask); |
| 906 |
inlen &= ~ctx->block_mask; |
| 907 |
|
| 908 |
ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen); |
| 909 |
if (ret != 1) |
| 910 |
return ret; |
| 911 |
|
| 912 |
*outlen += inlen; |
| 913 |
|
| 914 |
in = ((unsigned char *)in) + inlen; |
| 915 |
memcpy(ctx->buf, in, ctx->buf_len); |
| 916 |
} |
| 917 |
|
| 918 |
return 1; |
| 919 |
} |
| 920 |
|
| 921 |
/** |
| 922 |
* Encipher/decipher final data |
| 923 |
* |
| 924 |
* @param ctx the cipher context. |
| 925 |
* @param out output data from the operation. |
| 926 |
* @param outlen output length |
| 927 |
* |
| 928 |
* The input length needs to be at least EVP_CIPHER_block_size() bytes |
| 929 |
* long. |
| 930 |
* |
| 931 |
* See @ref evp_cipher for an example how to use this function. |
| 932 |
* |
| 933 |
* @return 1 on success. |
| 934 |
* |
| 935 |
* @ingroup hcrypto_evp |
| 936 |
*/ |
| 937 |
|
| 938 |
int |
| 939 |
EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen) |
| 940 |
{ |
| 941 |
*outlen = 0; |
| 942 |
|
| 943 |
if (ctx->buf_len) { |
| 944 |
int ret, left, blocksize; |
| 945 |
|
| 946 |
blocksize = EVP_CIPHER_CTX_block_size(ctx); |
| 947 |
|
| 948 |
left = blocksize - ctx->buf_len; |
| 949 |
assert(left > 0); |
| 950 |
|
| 951 |
/* zero fill local buffer */ |
| 952 |
memset(ctx->buf + ctx->buf_len, 0, left); |
| 953 |
ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize); |
| 954 |
memset(ctx->buf, 0, blocksize); |
| 955 |
if (ret != 1) |
| 956 |
return ret; |
| 957 |
|
| 958 |
*outlen += blocksize; |
| 959 |
} |
| 960 |
|
| 961 |
return 1; |
| 962 |
} |
| 963 |
|
| 964 |
/** |
| 965 |
* Encipher/decipher data |
| 966 |
* |
| 967 |
* @param ctx the cipher context. |
| 968 |
* @param out out data from the operation. |
| 969 |
* @param in in data to the operation. |
| 970 |
* @param size length of data. |
| 971 |
* |
| 972 |
* @return 1 on success. |
| 973 |
*/ |
| 974 |
|
| 975 |
int |
| 976 |
EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size) |
| 977 |
{ |
| 978 |
return ctx->cipher->do_cipher(ctx, out, in, size); |
| 979 |
} |
| 980 |
|
| 981 |
/* |
| 982 |
* |
| 983 |
*/ |
| 984 |
|
| 985 |
static int |
| 986 |
enc_null_init(EVP_CIPHER_CTX *ctx, |
| 987 |
const unsigned char * key, |
| 988 |
const unsigned char * iv, |
| 989 |
int encp) |
| 990 |
{ |
| 991 |
return 1; |
| 992 |
} |
| 993 |
|
| 994 |
static int |
| 995 |
enc_null_do_cipher(EVP_CIPHER_CTX *ctx, |
| 996 |
unsigned char *out, |
| 997 |
const unsigned char *in, |
| 998 |
unsigned int size) |
| 999 |
{ |
| 1000 |
memmove(out, in, size); |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
static int |
| 1005 |
enc_null_cleanup(EVP_CIPHER_CTX *ctx) |
| 1006 |
{ |
| 1007 |
return 1; |
| 1008 |
} |
| 1009 |
|
| 1010 |
/** |
| 1011 |
* The NULL cipher type, does no encryption/decryption. |
| 1012 |
* |
| 1013 |
* @return the null EVP_CIPHER pointer. |
| 1014 |
* |
| 1015 |
* @ingroup hcrypto_evp |
| 1016 |
*/ |
| 1017 |
|
| 1018 |
const EVP_CIPHER * |
| 1019 |
EVP_enc_null(void) |
| 1020 |
{ |
| 1021 |
static const EVP_CIPHER enc_null = { |
| 1022 |
0, |
| 1023 |
0, |
| 1024 |
0, |
| 1025 |
0, |
| 1026 |
EVP_CIPH_CBC_MODE, |
| 1027 |
enc_null_init, |
| 1028 |
enc_null_do_cipher, |
| 1029 |
enc_null_cleanup, |
| 1030 |
0, |
| 1031 |
NULL, |
| 1032 |
NULL, |
| 1033 |
NULL, |
| 1034 |
NULL |
| 1035 |
}; |
| 1036 |
return &enc_null; |
| 1037 |
} |
| 1038 |
|
| 1039 |
/** |
| 1040 |
* The RC2 cipher type |
| 1041 |
* |
| 1042 |
* @return the RC2 EVP_CIPHER pointer. |
| 1043 |
* |
| 1044 |
* @ingroup hcrypto_evp |
| 1045 |
*/ |
| 1046 |
|
| 1047 |
const EVP_CIPHER * |
| 1048 |
EVP_rc2_cbc(void) |
| 1049 |
{ |
| 1050 |
hcrypto_validate(); |
| 1051 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/** |
| 1055 |
* The RC2 cipher type |
| 1056 |
* |
| 1057 |
* @return the RC2 EVP_CIPHER pointer. |
| 1058 |
* |
| 1059 |
* @ingroup hcrypto_evp |
| 1060 |
*/ |
| 1061 |
|
| 1062 |
const EVP_CIPHER * |
| 1063 |
EVP_rc2_40_cbc(void) |
| 1064 |
{ |
| 1065 |
hcrypto_validate(); |
| 1066 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/** |
| 1070 |
* The RC2 cipher type |
| 1071 |
* |
| 1072 |
* @return the RC2 EVP_CIPHER pointer. |
| 1073 |
* |
| 1074 |
* @ingroup hcrypto_evp |
| 1075 |
*/ |
| 1076 |
|
| 1077 |
const EVP_CIPHER * |
| 1078 |
EVP_rc2_64_cbc(void) |
| 1079 |
{ |
| 1080 |
hcrypto_validate(); |
| 1081 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/** |
| 1085 |
* The RC4 cipher type |
| 1086 |
* |
| 1087 |
* @return the RC4 EVP_CIPHER pointer. |
| 1088 |
* |
| 1089 |
* @ingroup hcrypto_evp |
| 1090 |
*/ |
| 1091 |
|
| 1092 |
const EVP_CIPHER * |
| 1093 |
EVP_rc4(void) |
| 1094 |
{ |
| 1095 |
hcrypto_validate(); |
| 1096 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4); |
| 1097 |
} |
| 1098 |
|
| 1099 |
/** |
| 1100 |
* The RC4-40 cipher type |
| 1101 |
* |
| 1102 |
* @return the RC4-40 EVP_CIPHER pointer. |
| 1103 |
* |
| 1104 |
* @ingroup hcrypto_evp |
| 1105 |
*/ |
| 1106 |
|
| 1107 |
const EVP_CIPHER * |
| 1108 |
EVP_rc4_40(void) |
| 1109 |
{ |
| 1110 |
hcrypto_validate(); |
| 1111 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40); |
| 1112 |
} |
| 1113 |
|
| 1114 |
/** |
| 1115 |
* The DES cipher type |
| 1116 |
* |
| 1117 |
* @return the DES-CBC EVP_CIPHER pointer. |
| 1118 |
* |
| 1119 |
* @ingroup hcrypto_evp |
| 1120 |
*/ |
| 1121 |
|
| 1122 |
const EVP_CIPHER * |
| 1123 |
EVP_des_cbc(void) |
| 1124 |
{ |
| 1125 |
hcrypto_validate(); |
| 1126 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc); |
| 1127 |
} |
| 1128 |
|
| 1129 |
/** |
| 1130 |
* The triple DES cipher type |
| 1131 |
* |
| 1132 |
* @return the DES-EDE3-CBC EVP_CIPHER pointer. |
| 1133 |
* |
| 1134 |
* @ingroup hcrypto_evp |
| 1135 |
*/ |
| 1136 |
|
| 1137 |
const EVP_CIPHER * |
| 1138 |
EVP_des_ede3_cbc(void) |
| 1139 |
{ |
| 1140 |
hcrypto_validate(); |
| 1141 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc); |
| 1142 |
} |
| 1143 |
|
| 1144 |
/** |
| 1145 |
* The AES-128 cipher type |
| 1146 |
* |
| 1147 |
* @return the AES-128 EVP_CIPHER pointer. |
| 1148 |
* |
| 1149 |
* @ingroup hcrypto_evp |
| 1150 |
*/ |
| 1151 |
|
| 1152 |
const EVP_CIPHER * |
| 1153 |
EVP_aes_128_cbc(void) |
| 1154 |
{ |
| 1155 |
hcrypto_validate(); |
| 1156 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc); |
| 1157 |
} |
| 1158 |
|
| 1159 |
/** |
| 1160 |
* The AES-192 cipher type |
| 1161 |
* |
| 1162 |
* @return the AES-192 EVP_CIPHER pointer. |
| 1163 |
* |
| 1164 |
* @ingroup hcrypto_evp |
| 1165 |
*/ |
| 1166 |
|
| 1167 |
const EVP_CIPHER * |
| 1168 |
EVP_aes_192_cbc(void) |
| 1169 |
{ |
| 1170 |
hcrypto_validate(); |
| 1171 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/** |
| 1175 |
* The AES-256 cipher type |
| 1176 |
* |
| 1177 |
* @return the AES-256 EVP_CIPHER pointer. |
| 1178 |
* |
| 1179 |
* @ingroup hcrypto_evp |
| 1180 |
*/ |
| 1181 |
|
| 1182 |
const EVP_CIPHER * |
| 1183 |
EVP_aes_256_cbc(void) |
| 1184 |
{ |
| 1185 |
hcrypto_validate(); |
| 1186 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* The AES-128 cipher type |
| 1191 |
* |
| 1192 |
* @return the AES-128 EVP_CIPHER pointer. |
| 1193 |
* |
| 1194 |
* @ingroup hcrypto_evp |
| 1195 |
*/ |
| 1196 |
|
| 1197 |
const EVP_CIPHER * |
| 1198 |
EVP_aes_128_cfb8(void) |
| 1199 |
{ |
| 1200 |
hcrypto_validate(); |
| 1201 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8); |
| 1202 |
} |
| 1203 |
|
| 1204 |
/** |
| 1205 |
* The AES-192 cipher type |
| 1206 |
* |
| 1207 |
* @return the AES-192 EVP_CIPHER pointer. |
| 1208 |
* |
| 1209 |
* @ingroup hcrypto_evp |
| 1210 |
*/ |
| 1211 |
|
| 1212 |
const EVP_CIPHER * |
| 1213 |
EVP_aes_192_cfb8(void) |
| 1214 |
{ |
| 1215 |
hcrypto_validate(); |
| 1216 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8); |
| 1217 |
} |
| 1218 |
|
| 1219 |
/** |
| 1220 |
* The AES-256 cipher type |
| 1221 |
* |
| 1222 |
* @return the AES-256 EVP_CIPHER pointer. |
| 1223 |
* |
| 1224 |
* @ingroup hcrypto_evp |
| 1225 |
*/ |
| 1226 |
|
| 1227 |
const EVP_CIPHER * |
| 1228 |
EVP_aes_256_cfb8(void) |
| 1229 |
{ |
| 1230 |
hcrypto_validate(); |
| 1231 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8); |
| 1232 |
} |
| 1233 |
|
| 1234 |
/** |
| 1235 |
* The Camellia-128 cipher type |
| 1236 |
* |
| 1237 |
* @return the Camellia-128 EVP_CIPHER pointer. |
| 1238 |
* |
| 1239 |
* @ingroup hcrypto_evp |
| 1240 |
*/ |
| 1241 |
|
| 1242 |
const EVP_CIPHER * |
| 1243 |
EVP_camellia_128_cbc(void) |
| 1244 |
{ |
| 1245 |
hcrypto_validate(); |
| 1246 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc); |
| 1247 |
} |
| 1248 |
|
| 1249 |
/** |
| 1250 |
* The Camellia-198 cipher type |
| 1251 |
* |
| 1252 |
* @return the Camellia-198 EVP_CIPHER pointer. |
| 1253 |
* |
| 1254 |
* @ingroup hcrypto_evp |
| 1255 |
*/ |
| 1256 |
|
| 1257 |
const EVP_CIPHER * |
| 1258 |
EVP_camellia_192_cbc(void) |
| 1259 |
{ |
| 1260 |
hcrypto_validate(); |
| 1261 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/** |
| 1265 |
* The Camellia-256 cipher type |
| 1266 |
* |
| 1267 |
* @return the Camellia-256 EVP_CIPHER pointer. |
| 1268 |
* |
| 1269 |
* @ingroup hcrypto_evp |
| 1270 |
*/ |
| 1271 |
|
| 1272 |
const EVP_CIPHER * |
| 1273 |
EVP_camellia_256_cbc(void) |
| 1274 |
{ |
| 1275 |
hcrypto_validate(); |
| 1276 |
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc); |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* |
| 1280 |
* |
| 1281 |
*/ |
| 1282 |
|
| 1283 |
static const struct cipher_name { |
| 1284 |
const char *name; |
| 1285 |
const EVP_CIPHER *(*func)(void); |
| 1286 |
} cipher_name[] = { |
| 1287 |
{ "des-ede3-cbc", EVP_des_ede3_cbc }, |
| 1288 |
{ "aes-128-cbc", EVP_aes_128_cbc }, |
| 1289 |
{ "aes-192-cbc", EVP_aes_192_cbc }, |
| 1290 |
{ "aes-256-cbc", EVP_aes_256_cbc }, |
| 1291 |
{ "aes-128-cfb8", EVP_aes_128_cfb8 }, |
| 1292 |
{ "aes-192-cfb8", EVP_aes_192_cfb8 }, |
| 1293 |
{ "aes-256-cfb8", EVP_aes_256_cfb8 }, |
| 1294 |
{ "camellia-128-cbc", EVP_camellia_128_cbc }, |
| 1295 |
{ "camellia-192-cbc", EVP_camellia_192_cbc }, |
| 1296 |
{ "camellia-256-cbc", EVP_camellia_256_cbc } |
| 1297 |
}; |
| 1298 |
|
| 1299 |
/** |
| 1300 |
* Get the cipher type using their name. |
| 1301 |
* |
| 1302 |
* @param name the name of the cipher. |
| 1303 |
* |
| 1304 |
* @return the selected EVP_CIPHER pointer or NULL if not found. |
| 1305 |
* |
| 1306 |
* @ingroup hcrypto_evp |
| 1307 |
*/ |
| 1308 |
|
| 1309 |
const EVP_CIPHER * |
| 1310 |
EVP_get_cipherbyname(const char *name) |
| 1311 |
{ |
| 1312 |
int i; |
| 1313 |
for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) { |
| 1314 |
if (strcasecmp(cipher_name[i].name, name) == 0) |
| 1315 |
return (*cipher_name[i].func)(); |
| 1316 |
} |
| 1317 |
return NULL; |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
/* |
| 1322 |
* |
| 1323 |
*/ |
| 1324 |
|
| 1325 |
#ifndef min |
| 1326 |
#define min(a,b) (((a)>(b))?(b):(a)) |
| 1327 |
#endif |
| 1328 |
|
| 1329 |
/** |
| 1330 |
* Provides a legancy string to key function, used in PEM files. |
| 1331 |
* |
| 1332 |
* New protocols should use new string to key functions like NIST |
| 1333 |
* SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()). |
| 1334 |
* |
| 1335 |
* @param type type of cipher to use |
| 1336 |
* @param md message digest to use |
| 1337 |
* @param salt salt salt string, should be an binary 8 byte buffer. |
| 1338 |
* @param data the password/input key string. |
| 1339 |
* @param datalen length of data parameter. |
| 1340 |
* @param count iteration counter. |
| 1341 |
* @param keydata output keydata, needs to of the size EVP_CIPHER_key_length(). |
| 1342 |
* @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size(). |
| 1343 |
* |
| 1344 |
* @return the size of derived key. |
| 1345 |
* |
| 1346 |
* @ingroup hcrypto_evp |
| 1347 |
*/ |
| 1348 |
|
| 1349 |
int |
| 1350 |
EVP_BytesToKey(const EVP_CIPHER *type, |
| 1351 |
const EVP_MD *md, |
| 1352 |
const void *salt, |
| 1353 |
const void *data, size_t datalen, |
| 1354 |
unsigned int count, |
| 1355 |
void *keydata, |
| 1356 |
void *ivdata) |
| 1357 |
{ |
| 1358 |
unsigned int ivlen, keylen; |
| 1359 |
int first = 0; |
| 1360 |
unsigned int mds = 0, i; |
| 1361 |
unsigned char *key = keydata; |
| 1362 |
unsigned char *iv = ivdata; |
| 1363 |
unsigned char *buf; |
| 1364 |
EVP_MD_CTX c; |
| 1365 |
|
| 1366 |
keylen = EVP_CIPHER_key_length(type); |
| 1367 |
ivlen = EVP_CIPHER_iv_length(type); |
| 1368 |
|
| 1369 |
if (data == NULL) |
| 1370 |
return keylen; |
| 1371 |
|
| 1372 |
buf = malloc(EVP_MD_size(md)); |
| 1373 |
if (buf == NULL) |
| 1374 |
return -1; |
| 1375 |
|
| 1376 |
EVP_MD_CTX_init(&c); |
| 1377 |
|
| 1378 |
first = 1; |
| 1379 |
while (1) { |
| 1380 |
EVP_DigestInit_ex(&c, md, NULL); |
| 1381 |
if (!first) |
| 1382 |
EVP_DigestUpdate(&c, buf, mds); |
| 1383 |
first = 0; |
| 1384 |
EVP_DigestUpdate(&c,data,datalen); |
| 1385 |
|
| 1386 |
#define PKCS5_SALT_LEN 8 |
| 1387 |
|
| 1388 |
if (salt) |
| 1389 |
EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN); |
| 1390 |
|
| 1391 |
EVP_DigestFinal_ex(&c, buf, &mds); |
| 1392 |
assert(mds == EVP_MD_size(md)); |
| 1393 |
|
| 1394 |
for (i = 1; i < count; i++) { |
| 1395 |
EVP_DigestInit_ex(&c, md, NULL); |
| 1396 |
EVP_DigestUpdate(&c, buf, mds); |
| 1397 |
EVP_DigestFinal_ex(&c, buf, &mds); |
| 1398 |
assert(mds == EVP_MD_size(md)); |
| 1399 |
} |
| 1400 |
|
| 1401 |
i = 0; |
| 1402 |
if (keylen) { |
| 1403 |
size_t sz = min(keylen, mds); |
| 1404 |
if (key) { |
| 1405 |
memcpy(key, buf, sz); |
| 1406 |
key += sz; |
| 1407 |
} |
| 1408 |
keylen -= sz; |
| 1409 |
i += sz; |
| 1410 |
} |
| 1411 |
if (ivlen && mds > i) { |
| 1412 |
size_t sz = min(ivlen, (mds - i)); |
| 1413 |
if (iv) { |
| 1414 |
memcpy(iv, &buf[i], sz); |
| 1415 |
iv += sz; |
| 1416 |
} |
| 1417 |
ivlen -= sz; |
| 1418 |
} |
| 1419 |
if (keylen == 0 && ivlen == 0) |
| 1420 |
break; |
| 1421 |
} |
| 1422 |
|
| 1423 |
EVP_MD_CTX_cleanup(&c); |
| 1424 |
free(buf); |
| 1425 |
|
| 1426 |
return EVP_CIPHER_key_length(type); |
| 1427 |
} |
| 1428 |
|
| 1429 |
/** |
| 1430 |
* Generate a random key for the specificed EVP_CIPHER. |
| 1431 |
* |
| 1432 |
* @param ctx EVP_CIPHER_CTX type to build the key for. |
| 1433 |
* @param key return key, must be at least EVP_CIPHER_key_length() byte long. |
| 1434 |
* |
| 1435 |
* @return 1 for success, 0 for failure. |
| 1436 |
* |
| 1437 |
* @ingroup hcrypto_core |
| 1438 |
*/ |
| 1439 |
|
| 1440 |
int |
| 1441 |
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key) |
| 1442 |
{ |
| 1443 |
if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) |
| 1444 |
return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); |
| 1445 |
if (RAND_bytes(key, ctx->key_len) != 1) |
| 1446 |
return 0; |
| 1447 |
return 1; |
| 1448 |
} |
| 1449 |
|
| 1450 |
/** |
| 1451 |
* Perform a operation on a ctx |
| 1452 |
* |
| 1453 |
* @param ctx context to perform operation on. |
| 1454 |
* @param type type of operation. |
| 1455 |
* @param arg argument to operation. |
| 1456 |
* @param data addition data to operation. |
| 1457 |
|
| 1458 |
* @return 1 for success, 0 for failure. |
| 1459 |
* |
| 1460 |
* @ingroup hcrypto_core |
| 1461 |
*/ |
| 1462 |
|
| 1463 |
int |
| 1464 |
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data) |
| 1465 |
{ |
| 1466 |
if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL) |
| 1467 |
return 0; |
| 1468 |
return (*ctx->cipher->ctrl)(ctx, type, arg, data); |
| 1469 |
} |
| 1470 |
|
| 1471 |
/** |
| 1472 |
* Add all algorithms to the crypto core. |
| 1473 |
* |
| 1474 |
* @ingroup hcrypto_core |
| 1475 |
*/ |
| 1476 |
|
| 1477 |
void |
| 1478 |
OpenSSL_add_all_algorithms(void) |
| 1479 |
{ |
| 1480 |
return; |
| 1481 |
} |
| 1482 |
|
| 1483 |
/** |
| 1484 |
* Add all algorithms to the crypto core using configuration file. |
| 1485 |
* |
| 1486 |
* @ingroup hcrypto_core |
| 1487 |
*/ |
| 1488 |
|
| 1489 |
void |
| 1490 |
OpenSSL_add_all_algorithms_conf(void) |
| 1491 |
{ |
| 1492 |
return; |
| 1493 |
} |
| 1494 |
|
| 1495 |
/** |
| 1496 |
* Add all algorithms to the crypto core, but don't use the |
| 1497 |
* configuration file. |
| 1498 |
* |
| 1499 |
* @ingroup hcrypto_core |
| 1500 |
*/ |
| 1501 |
|
| 1502 |
void |
| 1503 |
OpenSSL_add_all_algorithms_noconf(void) |
| 1504 |
{ |
| 1505 |
return; |
| 1506 |
} |