You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 line
5.0 KiB

  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. size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; }
  20. size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; }
  21. size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; }
  22. size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; }
  23. void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) {
  24. memset(ctx, 0, sizeof(EVP_AEAD_CTX));
  25. }
  26. int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  27. const uint8_t *key, size_t key_len, size_t tag_len,
  28. ENGINE *impl) {
  29. if (!aead->init) {
  30. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_DIRECTION_SET);
  31. ctx->aead = NULL;
  32. return 0;
  33. }
  34. return EVP_AEAD_CTX_init_with_direction(ctx, aead, key, key_len, tag_len,
  35. evp_aead_open);
  36. }
  37. int EVP_AEAD_CTX_init_with_direction(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
  38. const uint8_t *key, size_t key_len,
  39. size_t tag_len,
  40. enum evp_aead_direction_t dir) {
  41. if (key_len != aead->key_len) {
  42. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_KEY_SIZE);
  43. ctx->aead = NULL;
  44. return 0;
  45. }
  46. ctx->aead = aead;
  47. int ok;
  48. if (aead->init) {
  49. ok = aead->init(ctx, key, key_len, tag_len);
  50. } else {
  51. ok = aead->init_with_direction(ctx, key, key_len, tag_len, dir);
  52. }
  53. if (!ok) {
  54. ctx->aead = NULL;
  55. }
  56. return ok;
  57. }
  58. void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) {
  59. if (ctx->aead == NULL) {
  60. return;
  61. }
  62. ctx->aead->cleanup(ctx);
  63. ctx->aead = NULL;
  64. }
  65. /* check_alias returns 0 if |out| points within the buffer determined by |in|
  66. * and |in_len| and 1 otherwise.
  67. *
  68. * When processing, there's only an issue if |out| points within in[:in_len]
  69. * and isn't equal to |in|. If that's the case then writing the output will
  70. * stomp input that hasn't been read yet.
  71. *
  72. * This function checks for that case. */
  73. static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out) {
  74. if (out <= in) {
  75. return 1;
  76. } else if (in + in_len <= out) {
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  82. size_t max_out_len, const uint8_t *nonce,
  83. size_t nonce_len, const uint8_t *in, size_t in_len,
  84. const uint8_t *ad, size_t ad_len) {
  85. size_t possible_out_len = in_len + ctx->aead->overhead;
  86. if (possible_out_len < in_len /* overflow */) {
  87. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  88. goto error;
  89. }
  90. if (!check_alias(in, in_len, out)) {
  91. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
  92. goto error;
  93. }
  94. if (ctx->aead->seal(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
  95. in_len, ad, ad_len)) {
  96. return 1;
  97. }
  98. error:
  99. /* In the event of an error, clear the output buffer so that a caller
  100. * that doesn't check the return value doesn't send raw data. */
  101. memset(out, 0, max_out_len);
  102. *out_len = 0;
  103. return 0;
  104. }
  105. int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  106. size_t max_out_len, const uint8_t *nonce,
  107. size_t nonce_len, const uint8_t *in, size_t in_len,
  108. const uint8_t *ad, size_t ad_len) {
  109. if (!check_alias(in, in_len, out)) {
  110. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
  111. goto error;
  112. }
  113. if (ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
  114. in_len, ad, ad_len)) {
  115. return 1;
  116. }
  117. error:
  118. /* In the event of an error, clear the output buffer so that a caller
  119. * that doesn't check the return value doesn't try and process bad
  120. * data. */
  121. memset(out, 0, max_out_len);
  122. *out_len = 0;
  123. return 0;
  124. }
  125. int EVP_AEAD_CTX_get_rc4_state(const EVP_AEAD_CTX *ctx, const RC4_KEY **out_key) {
  126. if (ctx->aead->get_rc4_state == NULL) {
  127. return 0;
  128. }
  129. return ctx->aead->get_rc4_state(ctx, out_key);
  130. }
  131. int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
  132. size_t *out_len) {
  133. if (ctx->aead->get_iv == NULL) {
  134. return 0;
  135. }
  136. return ctx->aead->get_iv(ctx, out_iv, out_len);
  137. }