No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

61 líneas
1.9 KiB

  1. // Copyright (c) 2017, Google Inc.
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/cipher.h>
  15. #include <string.h>
  16. #include <openssl/aes.h>
  17. #include <openssl/obj.h>
  18. #include "../../crypto/internal.h"
  19. typedef struct {
  20. AES_KEY ks;
  21. } EVP_CFB_CTX;
  22. static int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  23. const uint8_t *iv, int enc) {
  24. if (key) {
  25. EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
  26. AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks);
  27. }
  28. return 1;
  29. }
  30. static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
  31. const uint8_t *in, size_t len) {
  32. if (!out || !in) {
  33. return 0;
  34. }
  35. EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
  36. int num = ctx->num;
  37. AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
  38. ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
  39. ctx->num = num;
  40. return 1;
  41. }
  42. static const EVP_CIPHER aes_128_cfb128 = {
  43. NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */,
  44. 16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
  45. NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
  46. NULL /* cleanup */, NULL /* ctrl */,
  47. };
  48. const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; }