2
0

null_cipher.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-2006,2013 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. #include "datatypes.h"
  46. #include "null_cipher.h"
  47. #include "alloc.h"
  48. /* the null_cipher uses the cipher debug module */
  49. extern debug_module_t mod_cipher;
  50. err_status_t
  51. null_cipher_alloc(cipher_t **c, int key_len, int tlen) {
  52. extern cipher_type_t null_cipher;
  53. uint8_t *pointer;
  54. debug_print(mod_cipher,
  55. "allocating cipher with key length %d", key_len);
  56. /* allocate memory a cipher of type null_cipher */
  57. pointer = (uint8_t*)crypto_alloc(sizeof(null_cipher_ctx_t) + sizeof(cipher_t));
  58. if (pointer == NULL)
  59. return err_status_alloc_fail;
  60. /* set pointers */
  61. *c = (cipher_t *)pointer;
  62. (*c)->algorithm = NULL_CIPHER;
  63. (*c)->type = &null_cipher;
  64. (*c)->state = pointer + sizeof(cipher_t);
  65. /* set key size */
  66. (*c)->key_len = key_len;
  67. /* increment ref_count */
  68. null_cipher.ref_count++;
  69. return err_status_ok;
  70. }
  71. err_status_t
  72. null_cipher_dealloc(cipher_t *c) {
  73. extern cipher_type_t null_cipher;
  74. /* zeroize entire state*/
  75. octet_string_set_to_zero((uint8_t *)c,
  76. sizeof(null_cipher_ctx_t) + sizeof(cipher_t));
  77. /* free memory of type null_cipher */
  78. crypto_free(c);
  79. /* decrement reference count */
  80. null_cipher.ref_count--;
  81. return err_status_ok;
  82. }
  83. err_status_t
  84. null_cipher_init(null_cipher_ctx_t *ctx, const uint8_t *key, int key_len) {
  85. debug_print(mod_cipher, "initializing null cipher", NULL);
  86. return err_status_ok;
  87. }
  88. err_status_t
  89. null_cipher_set_iv(null_cipher_ctx_t *c, void *iv) {
  90. return err_status_ok;
  91. }
  92. err_status_t
  93. null_cipher_encrypt(null_cipher_ctx_t *c,
  94. unsigned char *buf, unsigned int *bytes_to_encr) {
  95. return err_status_ok;
  96. }
  97. char
  98. null_cipher_description[] = "null cipher";
  99. cipher_test_case_t
  100. null_cipher_test_0 = {
  101. 0, /* octets in key */
  102. NULL, /* key */
  103. 0, /* packet index */
  104. 0, /* octets in plaintext */
  105. NULL, /* plaintext */
  106. 0, /* octets in plaintext */
  107. NULL, /* ciphertext */
  108. 0,
  109. NULL,
  110. 0,
  111. NULL /* pointer to next testcase */
  112. };
  113. /*
  114. * note: the decrypt function is idential to the encrypt function
  115. */
  116. cipher_type_t null_cipher = {
  117. (cipher_alloc_func_t) null_cipher_alloc,
  118. (cipher_dealloc_func_t) null_cipher_dealloc,
  119. (cipher_init_func_t) null_cipher_init,
  120. (cipher_set_aad_func_t) 0,
  121. (cipher_encrypt_func_t) null_cipher_encrypt,
  122. (cipher_decrypt_func_t) null_cipher_encrypt,
  123. (cipher_set_iv_func_t) null_cipher_set_iv,
  124. (cipher_get_tag_func_t) 0,
  125. (char *) null_cipher_description,
  126. (int) 0,
  127. (cipher_test_case_t *) &null_cipher_test_0,
  128. (debug_module_t *) NULL,
  129. (cipher_type_id_t) NULL_CIPHER
  130. };