blob: b7f7cb122a228faa50c43c7a1bdb0970077a0cf7 [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 Millerd7834352006-08-05 12:39:39 +100023#include <sys/types.h>
24
Damien Millerd7834352006-08-05 12:39:39 +100025#include "buffer.h"
Damien Miller58629fa2006-04-23 12:08:19 +100026#include "log.h"
Damien Miller05e82c32014-05-15 14:33:43 +100027#include "ssherr.h"
Damien Miller58629fa2006-04-23 12:08:19 +100028
Damien Miller58629fa2006-04-23 12:08:19 +100029int
30buffer_put_bignum_ret(Buffer *buffer, const BIGNUM *value)
31{
Damien Miller05e82c32014-05-15 14:33:43 +100032 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100033
Damien Miller05e82c32014-05-15 14:33:43 +100034 if ((ret = sshbuf_put_bignum1(buffer, value)) != 0) {
35 error("%s: %s", __func__, ssh_err(ret));
36 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100037 }
Damien Miller05e82c32014-05-15 14:33:43 +100038 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100039}
40
41void
42buffer_put_bignum(Buffer *buffer, const BIGNUM *value)
43{
44 if (buffer_put_bignum_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100045 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100046}
47
Damien Miller58629fa2006-04-23 12:08:19 +100048int
49buffer_get_bignum_ret(Buffer *buffer, BIGNUM *value)
50{
Damien Miller05e82c32014-05-15 14:33:43 +100051 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100052
Damien Miller05e82c32014-05-15 14:33:43 +100053 if ((ret = sshbuf_get_bignum1(buffer, value)) != 0) {
54 error("%s: %s", __func__, ssh_err(ret));
55 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100056 }
Damien Miller05e82c32014-05-15 14:33:43 +100057 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100058}
59
60void
61buffer_get_bignum(Buffer *buffer, BIGNUM *value)
62{
63 if (buffer_get_bignum_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100064 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100065}
66
Damien Miller58629fa2006-04-23 12:08:19 +100067int
68buffer_put_bignum2_ret(Buffer *buffer, const BIGNUM *value)
69{
Damien Miller05e82c32014-05-15 14:33:43 +100070 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100071
Damien Miller05e82c32014-05-15 14:33:43 +100072 if ((ret = sshbuf_put_bignum2(buffer, value)) != 0) {
73 error("%s: %s", __func__, ssh_err(ret));
74 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100075 }
Damien Miller05e82c32014-05-15 14:33:43 +100076 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100077}
78
79void
80buffer_put_bignum2(Buffer *buffer, const BIGNUM *value)
81{
82 if (buffer_put_bignum2_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100083 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +100084}
85
86int
87buffer_get_bignum2_ret(Buffer *buffer, BIGNUM *value)
88{
Damien Miller05e82c32014-05-15 14:33:43 +100089 int ret;
Damien Miller58629fa2006-04-23 12:08:19 +100090
Damien Miller05e82c32014-05-15 14:33:43 +100091 if ((ret = sshbuf_get_bignum2(buffer, value)) != 0) {
92 error("%s: %s", __func__, ssh_err(ret));
93 return -1;
Damien Miller58629fa2006-04-23 12:08:19 +100094 }
Damien Miller05e82c32014-05-15 14:33:43 +100095 return 0;
Damien Miller58629fa2006-04-23 12:08:19 +100096}
97
98void
99buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
100{
101 if (buffer_get_bignum2_ret(buffer, value) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +1000102 fatal("%s: buffer error", __func__);
Damien Miller58629fa2006-04-23 12:08:19 +1000103}