xref: /titanic_41/usr/src/cmd/ssh/libssh/common/mpaux.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * This file contains various auxiliary functions related to multiple
6  * precision integers.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14 
15 #include "includes.h"
16 RCSID("$OpenBSD: mpaux.c,v 1.16 2001/02/08 19:30:52 itojun Exp $");
17 
18 #pragma ident	"%Z%%M%	%I%	%E% SMI"
19 
20 #include <openssl/bn.h>
21 #include "getput.h"
22 #include "xmalloc.h"
23 
24 #include <openssl/md5.h>
25 
26 #include "mpaux.h"
27 
28 void
29 compute_session_id(u_char session_id[16],
30     u_char cookie[8],
31     BIGNUM* host_key_n,
32     BIGNUM* session_key_n)
33 {
34 	u_int host_key_bytes = BN_num_bytes(host_key_n);
35 	u_int session_key_bytes = BN_num_bytes(session_key_n);
36 	u_int bytes = host_key_bytes + session_key_bytes;
37 	u_char *buf = xmalloc(bytes);
38 	MD5_CTX md;
39 
40 	BN_bn2bin(host_key_n, buf);
41 	BN_bn2bin(session_key_n, buf + host_key_bytes);
42 	MD5_Init(&md);
43 	MD5_Update(&md, buf, bytes);
44 	MD5_Update(&md, cookie, 8);
45 	MD5_Final(session_id, &md);
46 	memset(buf, 0, bytes);
47 	xfree(buf);
48 }
49