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.
 
 
 
 
 
 

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