bf_skey.c 1.4 KB

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