drbg.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * NIST SP800-90A DRBG derivation function
  4. *
  5. * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
  6. */
  7. #ifndef _INTERNAL_DRBG_H
  8. #define _INTERNAL_DRBG_H
  9. /*
  10. * Convert an integer into a byte representation of this integer.
  11. * The byte representation is big-endian
  12. *
  13. * @val value to be converted
  14. * @buf buffer holding the converted integer -- caller must ensure that
  15. * buffer size is at least 32 bit
  16. */
  17. static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
  18. {
  19. struct s {
  20. __be32 conv;
  21. };
  22. struct s *conversion = (struct s *)buf;
  23. conversion->conv = cpu_to_be32(val);
  24. }
  25. /*
  26. * Concatenation Helper and string operation helper
  27. *
  28. * SP800-90A requires the concatenation of different data. To avoid copying
  29. * buffers around or allocate additional memory, the following data structure
  30. * is used to point to the original memory with its size. In addition, it
  31. * is used to build a linked list. The linked list defines the concatenation
  32. * of individual buffers. The order of memory block referenced in that
  33. * linked list determines the order of concatenation.
  34. */
  35. struct drbg_string {
  36. const unsigned char *buf;
  37. size_t len;
  38. struct list_head list;
  39. };
  40. static inline void drbg_string_fill(struct drbg_string *string,
  41. const unsigned char *buf, size_t len)
  42. {
  43. string->buf = buf;
  44. string->len = len;
  45. INIT_LIST_HEAD(&string->list);
  46. }
  47. #endif //_INTERNAL_DRBG_H