blob: 33ae7f73fd9c1db53bd8a3ed4deaf261fcd0f487 [file] [log] [blame]
Damien Miller05e82c32014-05-15 14:33:43 +10001/* $OpenBSD: bufbn.c,v 1.12 2014/04/30 05:29:56 djm Exp $ */
2
Damien Miller58629fa2006-04-23 12:08:19 +10003/*
Damien Miller05e82c32014-05-15 14:33:43 +10004 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
Damien Miller58629fa2006-04-23 12:08:19 +10005 *
Damien Miller05e82c32014-05-15 14:33:43 +10006 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
Damien Miller58629fa2006-04-23 12:08:19 +10009 *
Damien Miller05e82c32014-05-15 14:33:43 +100010 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller58629fa2006-04-23 12:08:19 +100017 */
18
Damien Miller05e82c32014-05-15 14:33:43 +100019/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
Damien Miller58629fa2006-04-23 12:08:19 +100020
Darren Tucker58538d72014-06-11 13:39:24 +100021#include "includes.h"
22
Damien Miller72ef7c12015-01-15 02:21:31 +110023#ifdef WITH_OPENSSL
24
Damien Millerd7834352006-08-05 12:39:39 +100025#include <sys/types.h>
26
Damien Millerd7834352006-08-05 12:39:39 +100027#include "buffer.h"
Damien Miller58629fa2006-04-23 12:08:19 +100028#include "log.h"
Damien Miller05e82c32014-05-15 14:33:43 +100029#include "ssherr.h"
Damien Miller58629fa2006-04-23 12:08:19 +100030
Damien Miller72ef7c12015-01-15 02:21:31 +110031#ifdef WITH_SSH1
Damien Miller58629fa2006-04-23 12:08:19 +100032int
33buffer_put_bignum_ret(Buffer *buffer, const BIGNUM *value)
34{
Damien Miller05e82c32014-05-15 14:33:43 +100035 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100036
Damien Miller05e82c32014-05-15 14:33:43 +100037 if ((ret = sshbuf_put_bignum1(buffer, value)) != 0) {
38 error("%s: %s", __func__, ssh_err(ret));
39 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100040 }
Damien Miller05e82c32014-05-15 14:33:43 +100041 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100042}
43
44void
45buffer_put_bignum(Buffer *buffer, const BIGNUM *value)
46{
47 if (buffer_put_bignum_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100048 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100049}
50
Damien Miller58629fa2006-04-23 12:08:19 +100051int
52buffer_get_bignum_ret(Buffer *buffer, BIGNUM *value)
53{
Damien Miller05e82c32014-05-15 14:33:43 +100054 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100055
Damien Miller05e82c32014-05-15 14:33:43 +100056 if ((ret = sshbuf_get_bignum1(buffer, value)) != 0) {
57 error("%s: %s", __func__, ssh_err(ret));
58 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100059 }
Damien Miller05e82c32014-05-15 14:33:43 +100060 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100061}
62
63void
64buffer_get_bignum(Buffer *buffer, BIGNUM *value)
65{
66 if (buffer_get_bignum_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100067 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100068}
Damien Miller72ef7c12015-01-15 02:21:31 +110069#endif /* WITH_SSH1 */
Damien Miller58629fa2006-04-23 12:08:19 +100070
Damien Miller58629fa2006-04-23 12:08:19 +100071int
72buffer_put_bignum2_ret(Buffer *buffer, const BIGNUM *value)
73{
Damien Miller05e82c32014-05-15 14:33:43 +100074 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100075
Damien Miller05e82c32014-05-15 14:33:43 +100076 if ((ret = sshbuf_put_bignum2(buffer, value)) != 0) {
77 error("%s: %s", __func__, ssh_err(ret));
78 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100079 }
Damien Miller05e82c32014-05-15 14:33:43 +100080 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100081}
82
83void
84buffer_put_bignum2(Buffer *buffer, const BIGNUM *value)
85{
86 if (buffer_put_bignum2_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100087 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100088}
89
90int
91buffer_get_bignum2_ret(Buffer *buffer, BIGNUM *value)
92{
Damien Miller05e82c32014-05-15 14:33:43 +100093 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100094
Damien Miller05e82c32014-05-15 14:33:43 +100095 if ((ret = sshbuf_get_bignum2(buffer, value)) != 0) {
96 error("%s: %s", __func__, ssh_err(ret));
97 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100098 }
Damien Miller05e82c32014-05-15 14:33:43 +100099 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +1000100}
101
102void
103buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
104{
105 if (buffer_get_bignum2_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +1000106 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +1000107}
Damien Miller72ef7c12015-01-15 02:21:31 +1100108
109#endif /* WITH_OPENSSL */