ctr_drbg.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #ifndef __MSM_CTR_DRBG_H__
  15. #define __MSM_CTR_DRBG_H__
  16. /* This is the module that is actually follows the details of NIST SP
  17. * 800-90 so it can claim to use a FIPS-approved algorithm.
  18. */
  19. /* Added ctr_drbg_generate_w_data which supplies
  20. * additional input to the generate operation.
  21. */
  22. #define CTR_DRBG_MAX_REQ_LEN_BITS (1 << 19)
  23. #define CTR_DRBG_SEED_LEN_BITS 256
  24. #define CTR_DRBG_BLOCK_LEN_BITS 128
  25. #define CTR_DRBG_BLOCK_LEN_BYTES (CTR_DRBG_BLOCK_LEN_BITS/8)
  26. #define CTR_DRBG_MAX_RESEED_INTERVAL (1ULL << 48)
  27. #define MSM_AES128_BLOCK_SIZE (16)
  28. #define MSM_ENTROPY_BUFFER_SIZE (16)
  29. #define MSM_NONCE_BUFFER_SIZE (8)
  30. enum ctr_drbg_status_t {
  31. CTR_DRBG_SUCCESS = 0,
  32. CTR_DRBG_NEEDS_RESEED,
  33. CTR_DRBG_INVALID_ARG,
  34. CTR_DRBG_GENERAL_ERROR = 0xFF,
  35. };
  36. union ctr_drbg_seed_t {
  37. uint8_t as_bytes[32];
  38. uint32_t as_words[8];
  39. uint64_t as_64[4];
  40. struct {
  41. uint8_t key[16];
  42. uint8_t V[16];
  43. } key_V;
  44. };
  45. struct msm_ctr_tcrypt_result_s {
  46. struct completion completion;
  47. int err;
  48. };
  49. struct msm_ctr_buffer_s {
  50. unsigned char *virt_addr;
  51. };
  52. struct aes_struct_s {
  53. struct crypto_ablkcipher *tfm;
  54. struct ablkcipher_request *req;
  55. struct msm_ctr_buffer_s input;
  56. struct msm_ctr_buffer_s output;
  57. struct msm_ctr_tcrypt_result_s result;
  58. };
  59. struct ctr_drbg_ctx_s {
  60. unsigned long long reseed_counter; /* starts at 1 as per SP
  61. * 800-90
  62. */
  63. unsigned long long reseed_interval;
  64. union ctr_drbg_seed_t seed;
  65. struct aes_struct_s aes_ctx;
  66. struct aes_struct_s df_aes_ctx;
  67. uint8_t prev_drn[MSM_AES128_BLOCK_SIZE];
  68. uint8_t continuous_test_started;
  69. };
  70. enum ctr_drbg_status_t ctr_drbg_instantiate(struct ctr_drbg_ctx_s *ctx,
  71. const uint8_t *entropy,
  72. size_t entropy_len_bits,
  73. const uint8_t *nonce,
  74. size_t nonce_len_bits,
  75. unsigned long long reseed_interval);
  76. enum ctr_drbg_status_t ctr_drbg_reseed(struct ctr_drbg_ctx_s *ctx,
  77. const void *entropy,
  78. size_t entropy_len);
  79. enum ctr_drbg_status_t ctr_drbg_generate_w_data(struct ctr_drbg_ctx_s *ctx,
  80. void *additional_input,
  81. size_t additional_input_len_bits,
  82. void *buffer,
  83. size_t len_bits);
  84. enum ctr_drbg_status_t ctr_drbg_generate(struct ctr_drbg_ctx_s *ctx,
  85. void *buffer,
  86. size_t len);
  87. void ctr_drbg_uninstantiate(struct ctr_drbg_ctx_s *ctx);
  88. enum ctr_drbg_status_t block_cipher_df(struct ctr_drbg_ctx_s *ctx,
  89. const uint8_t *input,
  90. uint32_t input_size,
  91. uint8_t *output,
  92. uint32_t output_size
  93. );
  94. void ctr_aes_deinit(struct ctr_drbg_ctx_s *ctx);
  95. #endif /* __MSM_CTR_DRBG_H__ */