e_rc5.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * RC5 low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #ifndef OPENSSL_NO_RC5
  17. # include <openssl/evp.h>
  18. # include "crypto/evp.h"
  19. # include <openssl/objects.h>
  20. # include "evp_local.h"
  21. # include <openssl/rc5.h>
  22. static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  23. const unsigned char *iv, int enc);
  24. static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
  25. typedef struct {
  26. int rounds; /* number of rounds */
  27. RC5_32_KEY ks; /* key schedule */
  28. } EVP_RC5_KEY;
  29. # define data(ctx) EVP_C_DATA(EVP_RC5_KEY,ctx)
  30. IMPLEMENT_BLOCK_CIPHER(rc5_32_12_16, ks, RC5_32, EVP_RC5_KEY, NID_rc5,
  31. 8, RC5_32_KEY_LENGTH, 8, 64,
  32. EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
  33. r_32_12_16_init_key, NULL, NULL, NULL, rc5_ctrl)
  34. static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  35. {
  36. switch (type) {
  37. case EVP_CTRL_INIT:
  38. data(c)->rounds = RC5_12_ROUNDS;
  39. return 1;
  40. case EVP_CTRL_GET_RC5_ROUNDS:
  41. *(int *)ptr = data(c)->rounds;
  42. return 1;
  43. case EVP_CTRL_SET_RC5_ROUNDS:
  44. switch (arg) {
  45. case RC5_8_ROUNDS:
  46. case RC5_12_ROUNDS:
  47. case RC5_16_ROUNDS:
  48. data(c)->rounds = arg;
  49. return 1;
  50. default:
  51. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
  52. return 0;
  53. }
  54. default:
  55. return -1;
  56. }
  57. }
  58. static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  59. const unsigned char *iv, int enc)
  60. {
  61. if (EVP_CIPHER_CTX_key_length(ctx) > 255) {
  62. ERR_raise(ERR_LIB_EVP, EVP_R_BAD_KEY_LENGTH);
  63. return 0;
  64. }
  65. return RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
  66. key, data(ctx)->rounds);
  67. }
  68. #endif