e_rc5.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #ifndef OPENSSL_NO_RC5
  12. # include <openssl/evp.h>
  13. # include "crypto/evp.h"
  14. # include <openssl/objects.h>
  15. # include "evp_local.h"
  16. # include <openssl/rc5.h>
  17. static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  18. const unsigned char *iv, int enc);
  19. static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
  20. typedef struct {
  21. int rounds; /* number of rounds */
  22. RC5_32_KEY ks; /* key schedule */
  23. } EVP_RC5_KEY;
  24. # define data(ctx) EVP_C_DATA(EVP_RC5_KEY,ctx)
  25. IMPLEMENT_BLOCK_CIPHER(rc5_32_12_16, ks, RC5_32, EVP_RC5_KEY, NID_rc5,
  26. 8, RC5_32_KEY_LENGTH, 8, 64,
  27. EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
  28. r_32_12_16_init_key, NULL, NULL, NULL, rc5_ctrl)
  29. static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  30. {
  31. switch (type) {
  32. case EVP_CTRL_INIT:
  33. data(c)->rounds = RC5_12_ROUNDS;
  34. return 1;
  35. case EVP_CTRL_GET_RC5_ROUNDS:
  36. *(int *)ptr = data(c)->rounds;
  37. return 1;
  38. case EVP_CTRL_SET_RC5_ROUNDS:
  39. switch (arg) {
  40. case RC5_8_ROUNDS:
  41. case RC5_12_ROUNDS:
  42. case RC5_16_ROUNDS:
  43. data(c)->rounds = arg;
  44. return 1;
  45. default:
  46. EVPerr(EVP_F_RC5_CTRL, EVP_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
  47. return 0;
  48. }
  49. default:
  50. return -1;
  51. }
  52. }
  53. static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  54. const unsigned char *iv, int enc)
  55. {
  56. if (EVP_CIPHER_CTX_key_length(ctx) > 255) {
  57. EVPerr(EVP_F_R_32_12_16_INIT_KEY, EVP_R_BAD_KEY_LENGTH);
  58. return 0;
  59. }
  60. RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
  61. key, data(ctx)->rounds);
  62. return 1;
  63. }
  64. #endif