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.

chap.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <gpxe/crypto.h>
  24. #include <gpxe/chap.h>
  25. /** @file
  26. *
  27. * CHAP protocol
  28. *
  29. */
  30. /**
  31. * Initialise CHAP challenge/response
  32. *
  33. * @v chap CHAP challenge/response
  34. * @v digest Digest algorithm to use
  35. * @ret rc Return status code
  36. *
  37. * Initialises a CHAP challenge/response structure. This routine
  38. * allocates memory, and so may fail. The allocated memory must
  39. * eventually be freed by a call to chap_finish().
  40. */
  41. int chap_init ( struct chap_response *chap,
  42. struct crypto_algorithm *digest ) {
  43. size_t state_len;
  44. void *state;
  45. assert ( chap->digest == NULL );
  46. assert ( chap->digest_context == NULL );
  47. assert ( chap->response == NULL );
  48. DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
  49. state_len = ( digest->ctxsize + digest->digestsize );
  50. state = malloc ( state_len );
  51. if ( ! state ) {
  52. DBG ( "CHAP %p could not allocate %zd bytes for state\n",
  53. chap, state_len );
  54. return -ENOMEM;
  55. }
  56. chap->digest = digest;
  57. chap->digest_context = state;
  58. chap->response = ( state + digest->ctxsize );
  59. chap->response_len = digest->digestsize;
  60. digest_init ( chap->digest, chap->digest_context );
  61. return 0;
  62. }
  63. /**
  64. * Add data to the CHAP challenge
  65. *
  66. * @v chap CHAP response
  67. * @v data Data to add
  68. * @v len Length of data to add
  69. */
  70. void chap_update ( struct chap_response *chap, const void *data,
  71. size_t len ) {
  72. assert ( chap->digest != NULL );
  73. assert ( chap->digest_context != NULL );
  74. if ( ! chap->digest )
  75. return;
  76. digest_update ( chap->digest, chap->digest_context, data, len );
  77. }
  78. /**
  79. * Respond to the CHAP challenge
  80. *
  81. * @v chap CHAP response
  82. *
  83. * Calculates the final CHAP response value, and places it in @c
  84. * chap->response, with a length of @c chap->response_len.
  85. */
  86. void chap_respond ( struct chap_response *chap ) {
  87. assert ( chap->digest != NULL );
  88. assert ( chap->digest_context != NULL );
  89. assert ( chap->response != NULL );
  90. DBG ( "CHAP %p responding to challenge\n", chap );
  91. if ( ! chap->digest )
  92. return;
  93. digest_final ( chap->digest, chap->digest_context, chap->response );
  94. }
  95. /**
  96. * Free resources used by a CHAP response
  97. *
  98. * @v chap CHAP response
  99. */
  100. void chap_finish ( struct chap_response *chap ) {
  101. void *state = chap->digest_context;
  102. DBG ( "CHAP %p finished\n", chap );
  103. free ( state );
  104. memset ( chap, 0, sizeof ( *chap ) );
  105. }