Annotation of src/usr.bin/ssh/mpaux.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2:
! 3: mpaux.c
! 4:
! 5: Author: Tatu Ylonen <ylo@cs.hut.fi>
! 6:
! 7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
! 8: All rights reserved
! 9:
! 10: Created: Sun Jul 16 04:29:30 1995 ylo
! 11:
! 12: This file contains various auxiliary functions related to multiple
! 13: precision integers.
! 14:
! 15: */
! 16:
! 17: #include "includes.h"
! 18: RCSID("$Id: mpaux.c,v 1.3 1999/05/04 11:58:51 bg Exp $");
! 19:
! 20: #include <gmp.h>
! 21: #include "getput.h"
! 22: #include "xmalloc.h"
! 23: #include "ssh_md5.h"
! 24:
! 25: /* Converts a multiple-precision integer into bytes to be stored in the buffer.
! 26: The buffer will contain the value of the integer, msb first. */
! 27:
! 28: void mp_linearize_msb_first(unsigned char *buf, unsigned int len,
! 29: MP_INT *value)
! 30: {
! 31: unsigned int i;
! 32: MP_INT aux;
! 33: mpz_init_set(&aux, value);
! 34: for (i = len; i >= 4; i -= 4)
! 35: {
! 36: unsigned int limb = mpz_get_ui(&aux);
! 37: PUT_32BIT(buf + i - 4, limb);
! 38: mpz_div_2exp(&aux, &aux, 32);
! 39: }
! 40: for (; i > 0; i--)
! 41: {
! 42: buf[i - 1] = mpz_get_ui(&aux);
! 43: mpz_div_2exp(&aux, &aux, 8);
! 44: }
! 45: mpz_clear(&aux);
! 46: }
! 47:
! 48: /* Extract a multiple-precision integer from buffer. The value is stored
! 49: in the buffer msb first. */
! 50:
! 51: void mp_unlinearize_msb_first(MP_INT *value, const unsigned char *buf,
! 52: unsigned int len)
! 53: {
! 54: unsigned int i;
! 55: mpz_set_ui(value, 0);
! 56: for (i = 0; i + 4 <= len; i += 4)
! 57: {
! 58: unsigned int limb = GET_32BIT(buf + i);
! 59: mpz_mul_2exp(value, value, 32);
! 60: mpz_add_ui(value, value, limb);
! 61: }
! 62: for (; i < len; i++)
! 63: {
! 64: mpz_mul_2exp(value, value, 8);
! 65: mpz_add_ui(value, value, buf[i]);
! 66: }
! 67: }
! 68:
! 69: /* Computes a 16-byte session id in the global variable session_id.
! 70: The session id is computed by concatenating the linearized, msb
! 71: first representations of host_key_n, session_key_n, and the cookie. */
! 72:
! 73: void compute_session_id(unsigned char session_id[16],
! 74: unsigned char cookie[8],
! 75: unsigned int host_key_bits,
! 76: MP_INT *host_key_n,
! 77: unsigned int session_key_bits,
! 78: MP_INT *session_key_n)
! 79: {
! 80: unsigned int bytes = (host_key_bits + 7) / 8 + (session_key_bits + 7) / 8 + 8;
! 81: unsigned char *buf = xmalloc(bytes);
! 82: struct MD5Context md;
! 83:
! 84: mp_linearize_msb_first(buf, (host_key_bits + 7 ) / 8, host_key_n);
! 85: mp_linearize_msb_first(buf + (host_key_bits + 7 ) / 8,
! 86: (session_key_bits + 7) / 8, session_key_n);
! 87: memcpy(buf + (host_key_bits + 7) / 8 + (session_key_bits + 7) / 8,
! 88: cookie, 8);
! 89: MD5Init(&md);
! 90: MD5Update(&md, buf, bytes);
! 91: MD5Final(session_id, &md);
! 92: xfree(buf);
! 93: }