bf_skey.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * BF 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 <string.h>
  16. #include <openssl/blowfish.h>
  17. #include "bf_local.h"
  18. #include "bf_pi.h"
  19. void BF_set_key(BF_KEY *key, int len, const unsigned char *data)
  20. {
  21. int i;
  22. BF_LONG *p, ri, in[2];
  23. const unsigned char *d, *end;
  24. memcpy(key, &bf_init, sizeof(BF_KEY));
  25. p = key->P;
  26. if (len > ((BF_ROUNDS + 2) * 4))
  27. len = (BF_ROUNDS + 2) * 4;
  28. d = data;
  29. end = &(data[len]);
  30. for (i = 0; i < (BF_ROUNDS + 2); i++) {
  31. ri = *(d++);
  32. if (d >= end)
  33. d = data;
  34. ri <<= 8;
  35. ri |= *(d++);
  36. if (d >= end)
  37. d = data;
  38. ri <<= 8;
  39. ri |= *(d++);
  40. if (d >= end)
  41. d = data;
  42. ri <<= 8;
  43. ri |= *(d++);
  44. if (d >= end)
  45. d = data;
  46. p[i] ^= ri;
  47. }
  48. in[0] = 0L;
  49. in[1] = 0L;
  50. for (i = 0; i < (BF_ROUNDS + 2); i += 2) {
  51. BF_encrypt(in, key);
  52. p[i] = in[0];
  53. p[i + 1] = in[1];
  54. }
  55. p = key->S;
  56. for (i = 0; i < 4 * 256; i += 2) {
  57. BF_encrypt(in, key);
  58. p[i] = in[0];
  59. p[i + 1] = in[1];
  60. }
  61. }