null_cipher.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. (void)tlen;
  59. debug_print(srtp_mod_cipher, "allocating cipher with key length %d",
  60. key_len);
  61. /* allocate memory a cipher of type null_cipher */
  62. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  63. if (*c == NULL) {
  64. return srtp_err_status_alloc_fail;
  65. }
  66. /* set pointers */
  67. (*c)->algorithm = SRTP_NULL_CIPHER;
  68. (*c)->type = &srtp_null_cipher;
  69. (*c)->state = (void *)0x1; /* The null cipher does not maintain state */
  70. /* set key size */
  71. (*c)->key_len = key_len;
  72. return srtp_err_status_ok;
  73. }
  74. static srtp_err_status_t srtp_null_cipher_dealloc(srtp_cipher_t *c)
  75. {
  76. extern const srtp_cipher_type_t srtp_null_cipher;
  77. /* zeroize entire state*/
  78. octet_string_set_to_zero(c, sizeof(srtp_cipher_t));
  79. /* free memory of type null_cipher */
  80. srtp_crypto_free(c);
  81. return srtp_err_status_ok;
  82. }
  83. static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key)
  84. {
  85. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  86. (void)cv;
  87. (void)key;
  88. debug_print0(srtp_mod_cipher, "initializing null cipher");
  89. return srtp_err_status_ok;
  90. }
  91. static srtp_err_status_t srtp_null_cipher_set_iv(void *cv,
  92. uint8_t *iv,
  93. srtp_cipher_direction_t dir)
  94. {
  95. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  96. (void)cv;
  97. (void)iv;
  98. (void)dir;
  99. return srtp_err_status_ok;
  100. }
  101. static srtp_err_status_t srtp_null_cipher_encrypt(void *cv,
  102. unsigned char *buf,
  103. unsigned int *bytes_to_encr)
  104. {
  105. /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
  106. (void)cv;
  107. (void)buf;
  108. (void)bytes_to_encr;
  109. return srtp_err_status_ok;
  110. }
  111. static const char srtp_null_cipher_description[] = "null cipher";
  112. static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
  113. 0, /* octets in key */
  114. NULL, /* key */
  115. 0, /* packet index */
  116. 0, /* octets in plaintext */
  117. NULL, /* plaintext */
  118. 0, /* octets in plaintext */
  119. NULL, /* ciphertext */
  120. 0, /* */
  121. NULL, /* */
  122. 0, /* */
  123. NULL /* pointer to next testcase */
  124. };
  125. /*
  126. * note: the decrypt function is idential to the encrypt function
  127. */
  128. const srtp_cipher_type_t srtp_null_cipher = {
  129. srtp_null_cipher_alloc, /* */
  130. srtp_null_cipher_dealloc, /* */
  131. srtp_null_cipher_init, /* */
  132. 0, /* set_aad */
  133. srtp_null_cipher_encrypt, /* */
  134. srtp_null_cipher_encrypt, /* */
  135. srtp_null_cipher_set_iv, /* */
  136. 0, /* get_tag */
  137. srtp_null_cipher_description, /* */
  138. &srtp_null_cipher_test_0, /* */
  139. SRTP_NULL_CIPHER /* */
  140. };