aead.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/aead.h>
  15. #include <string.h>
  16. #include <openssl/cipher.h>
  17. #include <openssl/err.h>
  18. #include "internal.h"
  19. #include "../internal.h"
  20. size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; }
  21. size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; }
  22. size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; }
  23. size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; }
  24. void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) {
  25. OPENSSL_memset(ctx, 0, sizeof(EVP_AEAD_CTX));
  26. }
  27. int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  28. const uint8_t *key, size_t key_len, size_t tag_len,
  29. ENGINE *impl) {
  30. if (!aead->init) {
  31. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_DIRECTION_SET);
  32. ctx->aead = NULL;
  33. return 0;
  34. }
  35. return EVP_AEAD_CTX_init_with_direction(ctx, aead, key, key_len, tag_len,
  36. evp_aead_open);
  37. }
  38. int EVP_AEAD_CTX_init_with_direction(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  39. const uint8_t *key, size_t key_len,
  40. size_t tag_len,
  41. enum evp_aead_direction_t dir) {
  42. if (key_len != aead->key_len) {
  43. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_KEY_SIZE);
  44. ctx->aead = NULL;
  45. return 0;
  46. }
  47. ctx->aead = aead;
  48. int ok;
  49. if (aead->init) {
  50. ok = aead->init(ctx, key, key_len, tag_len);
  51. } else {
  52. ok = aead->init_with_direction(ctx, key, key_len, tag_len, dir);
  53. }
  54. if (!ok) {
  55. ctx->aead = NULL;
  56. }
  57. return ok;
  58. }
  59. void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) {
  60. if (ctx->aead == NULL) {
  61. return;
  62. }
  63. ctx->aead->cleanup(ctx);
  64. ctx->aead = NULL;
  65. }
  66. /* check_alias returns 1 if |out| is compatible with |in| and 0 otherwise. If
  67. * |in| and |out| alias, we require that |in| == |out|. */
  68. static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out,
  69. size_t out_len) {
  70. if (!buffers_alias(in, in_len, out, out_len)) {
  71. return 1;
  72. }
  73. return in == out;
  74. }
  75. int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  76. size_t max_out_len, const uint8_t *nonce,
  77. size_t nonce_len, const uint8_t *in, size_t in_len,
  78. const uint8_t *ad, size_t ad_len) {
  79. size_t possible_out_len = in_len + ctx->aead->overhead;
  80. if (possible_out_len < in_len /* overflow */) {
  81. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  82. goto error;
  83. }
  84. if (!check_alias(in, in_len, out, max_out_len)) {
  85. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
  86. goto error;
  87. }
  88. if (ctx->aead->seal(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
  89. in_len, ad, ad_len)) {
  90. return 1;
  91. }
  92. error:
  93. /* In the event of an error, clear the output buffer so that a caller
  94. * that doesn't check the return value doesn't send raw data. */
  95. OPENSSL_memset(out, 0, max_out_len);
  96. *out_len = 0;
  97. return 0;
  98. }
  99. int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  100. size_t max_out_len, const uint8_t *nonce,
  101. size_t nonce_len, const uint8_t *in, size_t in_len,
  102. const uint8_t *ad, size_t ad_len) {
  103. if (!check_alias(in, in_len, out, max_out_len)) {
  104. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
  105. goto error;
  106. }
  107. if (ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
  108. in_len, ad, ad_len)) {
  109. return 1;
  110. }
  111. error:
  112. /* In the event of an error, clear the output buffer so that a caller
  113. * that doesn't check the return value doesn't try and process bad
  114. * data. */
  115. OPENSSL_memset(out, 0, max_out_len);
  116. *out_len = 0;
  117. return 0;
  118. }
  119. const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx) { return ctx->aead; }
  120. int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
  121. size_t *out_len) {
  122. if (ctx->aead->get_iv == NULL) {
  123. return 0;
  124. }
  125. return ctx->aead->get_iv(ctx, out_iv, out_len);
  126. }