null_cipher.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * null_cipher.c
  3. *
  4. * A null cipher implementation. This cipher leaves the plaintext
  5. * unchanged.
  6. *
  7. * David A. McGrew
  8. * Cisco Systems, Inc.
  9. */
  10. /*
  11. *
  12. * Copyright (c) 2001-2017 Cisco Systems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * Neither the name of the Cisco Systems, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42. * OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. */
  45. #ifdef HAVE_CONFIG_H
  46. #include <config.h>
  47. #endif
  48. #include "datatypes.h"
  49. #include "null_cipher.h"
  50. #include "err.h" /* for srtp_debug */
  51. #include "alloc.h"
  52. #include "cipher_types.h"
  53. static srtp_err_status_t srtp_null_cipher_alloc(srtp_cipher_t **c,
  54. int key_len,
  55. int tlen)
  56. {
  57. extern const srtp_cipher_type_t srtp_null_cipher;
  58. debug_print(srtp_mod_cipher, "allocating cipher with key length %d",
  59. key_len);
  60. /* allocate memory a cipher of type null_cipher */
  61. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  62. if (*c == NULL) {
  63. return srtp_err_status_alloc_fail;
  64. }
  65. /* set pointers */
  66. (*c)->algorithm = SRTP_NULL_CIPHER;
  67. (*c)->type = &srtp_null_cipher;
  68. (*c)->state = (void *)0x1; /* The null cipher does not maintain state */
  69. /* set key size */
  70. (*c)->key_len = key_len;
  71. return srtp_err_status_ok;
  72. }
  73. static srtp_err_status_t srtp_null_cipher_dealloc(srtp_cipher_t *c)
  74. {
  75. extern const srtp_cipher_type_t srtp_null_cipher;
  76. /* zeroize entire state*/
  77. octet_string_set_to_zero(c, sizeof(srtp_cipher_t));
  78. /* free memory of type null_cipher */
  79. srtp_crypto_free(c);
  80. return srtp_err_status_ok;
  81. }
  82. static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key)
  83. {
  84. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  85. debug_print0(srtp_mod_cipher, "initializing null cipher");
  86. return srtp_err_status_ok;
  87. }
  88. static srtp_err_status_t srtp_null_cipher_set_iv(void *cv,
  89. uint8_t *iv,
  90. srtp_cipher_direction_t dir)
  91. {
  92. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  93. return srtp_err_status_ok;
  94. }
  95. static srtp_err_status_t srtp_null_cipher_encrypt(void *cv,
  96. unsigned char *buf,
  97. unsigned int *bytes_to_encr)
  98. {
  99. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  100. return srtp_err_status_ok;
  101. }
  102. static const char srtp_null_cipher_description[] = "null cipher";
  103. static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
  104. 0, /* octets in key */
  105. NULL, /* key */
  106. 0, /* packet index */
  107. 0, /* octets in plaintext */
  108. NULL, /* plaintext */
  109. 0, /* octets in plaintext */
  110. NULL, /* ciphertext */
  111. 0, /* */
  112. NULL, /* */
  113. 0, /* */
  114. NULL /* pointer to next testcase */
  115. };
  116. /*
  117. * note: the decrypt function is idential to the encrypt function
  118. */
  119. const srtp_cipher_type_t srtp_null_cipher = {
  120. srtp_null_cipher_alloc, /* */
  121. srtp_null_cipher_dealloc, /* */
  122. srtp_null_cipher_init, /* */
  123. 0, /* set_aad */
  124. srtp_null_cipher_encrypt, /* */
  125. srtp_null_cipher_encrypt, /* */
  126. srtp_null_cipher_set_iv, /* */
  127. 0, /* get_tag */
  128. srtp_null_cipher_description, /* */
  129. &srtp_null_cipher_test_0, /* */
  130. SRTP_NULL_CIPHER /* */
  131. };