crypto.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Author: Cecylia Bocovich <cbocovic@uwaterloo.ca>
  3. *
  4. * This file contains cryptographic helper functions to
  5. * tag flows for use with the Slitheen decoy routing system
  6. * Some code in this document is based on the OpenSSL source files:
  7. * crypto/ec/ec_key.c
  8. * crypto/dh/dh_key.c
  9. *
  10. */
  11. /*
  12. * Written by Nils Larsch for the OpenSSL project.
  13. */
  14. /* ====================================================================
  15. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. *
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in
  26. * the documentation and/or other materials provided with the
  27. * distribution.
  28. *
  29. * 3. All advertising materials mentioning features or use of this
  30. * software must display the following acknowledgment:
  31. * "This product includes software developed by the OpenSSL Project
  32. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  33. *
  34. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  35. * endorse or promote products derived from this software without
  36. * prior written permission. For written permission, please contact
  37. * openssl-core@openssl.org.
  38. *
  39. * 5. Products derived from this software may not be called "OpenSSL"
  40. * nor may "OpenSSL" appear in their names without prior written
  41. * permission of the OpenSSL Project.
  42. *
  43. * 6. Redistributions of any form whatsoever must retain the following
  44. * acknowledgment:
  45. * "This product includes software developed by the OpenSSL Project
  46. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  49. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  52. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  54. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  55. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  57. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  58. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  59. * OF THE POSSIBILITY OF SUCH DAMAGE.
  60. * ====================================================================
  61. *
  62. * This product includes cryptographic software written by Eric Young
  63. * (eay@cryptsoft.com). This product includes software written by Tim
  64. * Hudson (tjh@cryptsoft.com).
  65. *
  66. */
  67. /* ====================================================================
  68. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  69. * Portions originally developed by SUN MICROSYSTEMS, INC., and
  70. * contributed to the OpenSSL project.
  71. */
  72. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  73. * All rights reserved.
  74. *
  75. * This package is an SSL implementation written
  76. * by Eric Young (eay@cryptsoft.com).
  77. * The implementation was written so as to conform with Netscapes SSL.
  78. *
  79. * This library is free for commercial and non-commercial use as long as
  80. * the following conditions are aheared to. The following conditions
  81. * apply to all code found in this distribution, be it the RC4, RSA,
  82. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  83. * included with this distribution is covered by the same copyright terms
  84. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  85. *
  86. * Copyright remains Eric Young's, and as such any Copyright notices in
  87. * the code are not to be removed.
  88. * If this package is used in a product, Eric Young should be given attribution
  89. * as the author of the parts of the library used.
  90. * This can be in the form of a textual message at program startup or
  91. * in documentation (online or textual) provided with the package.
  92. *
  93. * Redistribution and use in source and binary forms, with or without
  94. * modification, are permitted provided that the following conditions
  95. * are met:
  96. * 1. Redistributions of source code must retain the copyright
  97. * notice, this list of conditions and the following disclaimer.
  98. * 2. Redistributions in binary form must reproduce the above copyright
  99. * notice, this list of conditions and the following disclaimer in the
  100. * documentation and/or other materials provided with the distribution.
  101. * 3. All advertising materials mentioning features or use of this software
  102. * must display the following acknowledgement:
  103. * "This product includes cryptographic software written by
  104. * Eric Young (eay@cryptsoft.com)"
  105. * The word 'cryptographic' can be left out if the rouines from the library
  106. * being used are not cryptographic related :-).
  107. * 4. If you include any Windows specific code (or a derivative thereof) from
  108. * the apps directory (application code) you must include an acknowledgement:
  109. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  110. *
  111. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  112. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  113. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  114. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  115. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  116. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  117. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  118. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  119. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  120. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  121. * SUCH DAMAGE.
  122. *
  123. * The licence and distribution terms for any publically available version or
  124. * derivative of this code cannot be changed. i.e. this code cannot simply be
  125. * copied and put under another distribution licence
  126. * [including the GNU Public Licence.]
  127. */
  128. #include <openssl/evp.h>
  129. #include <openssl/dh.h>
  130. #include <openssl/bn.h>
  131. #include <openssl/err.h>
  132. #include <openssl/rand.h>
  133. #include <openssl/ssl.h>
  134. #include "crypto.h"
  135. /* PRF using sha384, as defined in RFC 5246 */
  136. int PRF(uint8_t *secret, int32_t secret_len,
  137. uint8_t *seed1, int32_t seed1_len,
  138. uint8_t *seed2, int32_t seed2_len,
  139. uint8_t *seed3, int32_t seed3_len,
  140. uint8_t *seed4, int32_t seed4_len,
  141. uint8_t *output, int32_t output_len){
  142. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  143. EVP_PKEY *mac_key;
  144. const EVP_MD *md = EVP_sha384();
  145. uint8_t A[EVP_MAX_MD_SIZE];
  146. size_t len, A_len;
  147. int chunk = EVP_MD_size(md);
  148. int remaining = output_len;
  149. uint8_t *out = output;
  150. EVP_MD_CTX_init(&ctx);
  151. EVP_MD_CTX_init(&ctx_tmp);
  152. EVP_MD_CTX_init(&ctx_init);
  153. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  154. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  155. /* Calculate first A value */
  156. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  157. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  158. if(seed1 != NULL && seed1_len > 0){
  159. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  160. }
  161. if(seed2 != NULL && seed2_len > 0){
  162. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  163. }
  164. if(seed3 != NULL && seed3_len > 0){
  165. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  166. }
  167. if(seed4 != NULL && seed4_len > 0){
  168. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  169. }
  170. EVP_DigestSignFinal(&ctx, A, &A_len);
  171. //iterate until desired length is achieved
  172. while(remaining > 0){
  173. /* Now compute SHA384(secret, A+seed) */
  174. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  175. EVP_DigestSignUpdate(&ctx, A, A_len);
  176. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  177. if(seed1 != NULL && seed1_len > 0){
  178. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  179. }
  180. if(seed2 != NULL && seed2_len > 0){
  181. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  182. }
  183. if(seed3 != NULL && seed3_len > 0){
  184. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  185. }
  186. if(seed4 != NULL && seed4_len > 0){
  187. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  188. }
  189. if(remaining > chunk){
  190. EVP_DigestSignFinal(&ctx, out, &len);
  191. out += len;
  192. remaining -= len;
  193. /* Next A value */
  194. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  195. } else {
  196. EVP_DigestSignFinal(&ctx, A, &A_len);
  197. memcpy(out, A, remaining);
  198. remaining -= remaining;
  199. }
  200. }
  201. return 1;
  202. }