at_aes_crypt.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2015-2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <assert.h>
  18. #include <stdint.h>
  19. //#include <lk/compiler.h>
  20. #include <limits.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <openssl/evp.h>
  26. #include <openssl/hmac.h>
  27. #include <openssl/rand.h>
  28. #define DEBUG_MAC_VALUES 0
  29. #define AT_HMAC_SIZE 16
  30. #ifndef STATIC_ASSERT
  31. #define STATIC_ASSERT(e) _Static_assert(e, #e)
  32. #endif
  33. struct key {
  34. uint8_t byte[16];
  35. };
  36. struct mac {
  37. uint8_t byte[16];
  38. };
  39. struct iv {
  40. uint8_t byte[16];
  41. };
  42. typedef struct at_crypt_data_type {
  43. void* data;
  44. int size;
  45. } at_crypt_data;
  46. #define IV_INITIAL_ZERO_VALUE(iv) \
  47. { \
  48. { 0 } \
  49. }
  50. #if DEBUG_MAC_VALUES
  51. #define UINT8_16_PRINTF_STR \
  52. "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
  53. #define UINT8_16_PRINTF_ARGS(var) \
  54. var[0], var[1], var[2], var[3], var[4], var[5], var[6], var[7], var[8], \
  55. var[9], var[10], var[11], var[12], var[13], var[14], var[15]
  56. #else
  57. #define UINT8_16_PRINTF_STR "%c"
  58. #define UINT8_16_PRINTF_ARGS(var) '*'
  59. #endif
  60. #define MAC_PRINTF_STR UINT8_16_PRINTF_STR
  61. #define MAC_PRINTF_ARGS(var) UINT8_16_PRINTF_ARGS((var)->byte)
  62. #define IV_PRINTF_STR UINT8_16_PRINTF_STR
  63. #define IV_PRINTF_ARGS(var) UINT8_16_PRINTF_ARGS((var)->byte)
  64. int at_aes_hmac_encrypt(const struct key* key,
  65. at_crypt_data* data_in,
  66. at_crypt_data* data_out,
  67. const struct iv* iv_in);
  68. int at_aes_hmac_decrypt(const struct key* key,
  69. at_crypt_data* data_in,
  70. at_crypt_data* data_out,
  71. const struct iv* iv_in);
  72. int at_aes_encrypt(const struct key* key,
  73. at_crypt_data* data_in,
  74. at_crypt_data* data_out,
  75. const struct iv* iv_in);
  76. int at_aes_decrypt(const struct key* key,
  77. at_crypt_data* data_in,
  78. at_crypt_data* data_out,
  79. const struct iv* iv_in);