You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <ipxe/crypto.h>
  26. #include <ipxe/chap.h>
  27. /** @file
  28. *
  29. * CHAP protocol
  30. *
  31. */
  32. /**
  33. * Initialise CHAP challenge/response
  34. *
  35. * @v chap CHAP challenge/response
  36. * @v digest Digest algorithm to use
  37. * @ret rc Return status code
  38. *
  39. * Initialises a CHAP challenge/response structure. This routine
  40. * allocates memory, and so may fail. The allocated memory must
  41. * eventually be freed by a call to chap_finish().
  42. */
  43. int chap_init ( struct chap_response *chap,
  44. struct digest_algorithm *digest ) {
  45. size_t state_len;
  46. void *state;
  47. assert ( chap->digest == NULL );
  48. assert ( chap->digest_context == NULL );
  49. assert ( chap->response == NULL );
  50. DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
  51. state_len = ( digest->ctxsize + digest->digestsize );
  52. state = malloc ( state_len );
  53. if ( ! state ) {
  54. DBG ( "CHAP %p could not allocate %zd bytes for state\n",
  55. chap, state_len );
  56. return -ENOMEM;
  57. }
  58. chap->digest = digest;
  59. chap->digest_context = state;
  60. chap->response = ( state + digest->ctxsize );
  61. chap->response_len = digest->digestsize;
  62. digest_init ( chap->digest, chap->digest_context );
  63. return 0;
  64. }
  65. /**
  66. * Add data to the CHAP challenge
  67. *
  68. * @v chap CHAP response
  69. * @v data Data to add
  70. * @v len Length of data to add
  71. */
  72. void chap_update ( struct chap_response *chap, const void *data,
  73. size_t len ) {
  74. assert ( chap->digest != NULL );
  75. assert ( chap->digest_context != NULL );
  76. if ( ! chap->digest )
  77. return;
  78. digest_update ( chap->digest, chap->digest_context, data, len );
  79. }
  80. /**
  81. * Respond to the CHAP challenge
  82. *
  83. * @v chap CHAP response
  84. *
  85. * Calculates the final CHAP response value, and places it in @c
  86. * chap->response, with a length of @c chap->response_len.
  87. */
  88. void chap_respond ( struct chap_response *chap ) {
  89. assert ( chap->digest != NULL );
  90. assert ( chap->digest_context != NULL );
  91. assert ( chap->response != NULL );
  92. DBG ( "CHAP %p responding to challenge\n", chap );
  93. if ( ! chap->digest )
  94. return;
  95. digest_final ( chap->digest, chap->digest_context, chap->response );
  96. }
  97. /**
  98. * Free resources used by a CHAP response
  99. *
  100. * @v chap CHAP response
  101. */
  102. void chap_finish ( struct chap_response *chap ) {
  103. void *state = chap->digest_context;
  104. DBG ( "CHAP %p finished\n", chap );
  105. free ( state );
  106. memset ( chap, 0, sizeof ( *chap ) );
  107. }