random.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * \file random.h
  3. *
  4. * \brief This file contains the prototypes of helper functions to generate
  5. * random numbers for the purpose of testing.
  6. */
  7. /*
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #ifndef TEST_RANDOM_H
  24. #define TEST_RANDOM_H
  25. #include "mbedtls/build_info.h"
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. typedef struct {
  29. unsigned char *buf; /* Pointer to a buffer of length bytes. */
  30. size_t length;
  31. /* If fallback_f_rng is NULL, fail after delivering length bytes. */
  32. int (*fallback_f_rng)(void *, unsigned char *, size_t);
  33. void *fallback_p_rng;
  34. } mbedtls_test_rnd_buf_info;
  35. /**
  36. * Info structure for the pseudo random function
  37. *
  38. * Key should be set at the start to a test-unique value.
  39. * Do not forget endianness!
  40. * State( v0, v1 ) should be set to zero.
  41. */
  42. typedef struct {
  43. uint32_t key[16];
  44. uint32_t v0, v1;
  45. } mbedtls_test_rnd_pseudo_info;
  46. /**
  47. * This function just returns data from rand().
  48. * Although predictable and often similar on multiple
  49. * runs, this does not result in identical random on
  50. * each run. So do not use this if the results of a
  51. * test depend on the random data that is generated.
  52. *
  53. * rng_state shall be NULL.
  54. */
  55. int mbedtls_test_rnd_std_rand(void *rng_state,
  56. unsigned char *output,
  57. size_t len);
  58. /**
  59. * This function only returns zeros.
  60. *
  61. * \p rng_state shall be \c NULL.
  62. */
  63. int mbedtls_test_rnd_zero_rand(void *rng_state,
  64. unsigned char *output,
  65. size_t len);
  66. /**
  67. * This function returns random data based on a buffer it receives.
  68. *
  69. * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure.
  70. *
  71. * The number of bytes released from the buffer on each call to
  72. * the random function is specified by \p len.
  73. *
  74. * After the buffer is empty, this function will call the fallback RNG in the
  75. * #mbedtls_test_rnd_buf_info structure if there is one, and
  76. * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
  77. */
  78. int mbedtls_test_rnd_buffer_rand(void *rng_state,
  79. unsigned char *output,
  80. size_t len);
  81. /**
  82. * This function returns random based on a pseudo random function.
  83. * This means the results should be identical on all systems.
  84. * Pseudo random is based on the XTEA encryption algorithm to
  85. * generate pseudorandom.
  86. *
  87. * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure.
  88. */
  89. int mbedtls_test_rnd_pseudo_rand(void *rng_state,
  90. unsigned char *output,
  91. size_t len);
  92. #endif /* TEST_RANDOM_H */