cipher-aes.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2003 Markus Friedl. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "includes.h"
  25. /* compatibility with old or broken OpenSSL versions */
  26. #include "openbsd-compat/openssl-compat.h"
  27. #ifdef USE_BUILTIN_RIJNDAEL
  28. #include <sys/types.h>
  29. #include <openssl/evp.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include "rijndael.h"
  33. #include "xmalloc.h"
  34. #include "log.h"
  35. #define RIJNDAEL_BLOCKSIZE 16
  36. struct ssh_rijndael_ctx
  37. {
  38. rijndael_ctx r_ctx;
  39. u_char r_iv[RIJNDAEL_BLOCKSIZE];
  40. };
  41. static int
  42. ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
  43. int enc)
  44. {
  45. struct ssh_rijndael_ctx *c;
  46. if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
  47. c = xmalloc(sizeof(*c));
  48. EVP_CIPHER_CTX_set_app_data(ctx, c);
  49. }
  50. if (key != NULL) {
  51. if (enc == -1)
  52. enc = ctx->encrypt;
  53. rijndael_set_key(&c->r_ctx, (u_char *)key,
  54. 8*EVP_CIPHER_CTX_key_length(ctx), enc);
  55. }
  56. if (iv != NULL)
  57. memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
  58. return (1);
  59. }
  60. static int
  61. ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
  62. LIBCRYPTO_EVP_INL_TYPE len)
  63. {
  64. struct ssh_rijndael_ctx *c;
  65. u_char buf[RIJNDAEL_BLOCKSIZE];
  66. u_char *cprev, *cnow, *plain, *ivp;
  67. int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
  68. if (len == 0)
  69. return (1);
  70. if (len % RIJNDAEL_BLOCKSIZE)
  71. fatal("ssh_rijndael_cbc: bad len %d", len);
  72. if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
  73. error("ssh_rijndael_cbc: no context");
  74. return (0);
  75. }
  76. if (ctx->encrypt) {
  77. cnow = dest;
  78. plain = (u_char *)src;
  79. cprev = c->r_iv;
  80. for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
  81. cnow+=RIJNDAEL_BLOCKSIZE) {
  82. for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
  83. buf[j] = plain[j] ^ cprev[j];
  84. rijndael_encrypt(&c->r_ctx, buf, cnow);
  85. cprev = cnow;
  86. }
  87. memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
  88. } else {
  89. cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
  90. plain = dest+len-RIJNDAEL_BLOCKSIZE;
  91. memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
  92. for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
  93. plain-=RIJNDAEL_BLOCKSIZE) {
  94. rijndael_decrypt(&c->r_ctx, cnow, plain);
  95. ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
  96. for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
  97. plain[j] ^= ivp[j];
  98. }
  99. memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
  100. }
  101. return (1);
  102. }
  103. static int
  104. ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
  105. {
  106. struct ssh_rijndael_ctx *c;
  107. if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
  108. memset(c, 0, sizeof(*c));
  109. free(c);
  110. EVP_CIPHER_CTX_set_app_data(ctx, NULL);
  111. }
  112. return (1);
  113. }
  114. void
  115. ssh_rijndael_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
  116. {
  117. struct ssh_rijndael_ctx *c;
  118. if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
  119. fatal("ssh_rijndael_iv: no context");
  120. if (doset)
  121. memcpy(c->r_iv, iv, len);
  122. else
  123. memcpy(iv, c->r_iv, len);
  124. }
  125. const EVP_CIPHER *
  126. evp_rijndael(void)
  127. {
  128. static EVP_CIPHER rijndal_cbc;
  129. memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
  130. rijndal_cbc.nid = NID_undef;
  131. rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
  132. rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
  133. rijndal_cbc.key_len = 16;
  134. rijndal_cbc.init = ssh_rijndael_init;
  135. rijndal_cbc.cleanup = ssh_rijndael_cleanup;
  136. rijndal_cbc.do_cipher = ssh_rijndael_cbc;
  137. #ifndef SSH_OLD_EVP
  138. rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
  139. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
  140. #endif
  141. return (&rijndal_cbc);
  142. }
  143. #endif /* USE_BUILTIN_RIJNDAEL */