Skip to content

Commit fb3e2de

Browse files
ni4ronaldtse
authored andcommitted
Refactor bn.cpp with more clear code/less lines.
1 parent 821569c commit fb3e2de

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/lib/crypto/bn.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2018 Ribose Inc.
2+
* Copyright (c) 2017-2021 Ribose Inc.
33
* Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
44
* All rights reserved.
55
*
@@ -27,10 +27,9 @@
2727
#include "bn.h"
2828
#include <botan/ffi.h>
2929
#include <stdlib.h>
30+
#include <assert.h>
3031
#include "utils.h"
3132

32-
/**************************************************************************/
33-
3433
/* essentiually, these are just wrappers around the botan functions */
3534
/* usually the order of args changes */
3635
/* the bignum_t API tends to have more const poisoning */
@@ -39,38 +38,35 @@
3938
bignum_t *
4039
bn_bin2bn(const uint8_t *data, int len, bignum_t *ret)
4140
{
42-
if (data == NULL) {
43-
return bn_new();
41+
assert(data);
42+
if (!data) {
43+
RNP_LOG("NULL data.");
44+
return NULL;
4445
}
45-
if (ret == NULL) {
46+
if (!ret) {
4647
ret = bn_new();
4748
}
48-
49-
if (ret == NULL) {
49+
if (!ret) {
5050
return NULL;
5151
}
52-
53-
return (botan_mp_from_bin(ret->mp, data, len) == 0) ? ret : NULL;
52+
return !botan_mp_from_bin(ret->mp, data, len) ? ret : NULL;
5453
}
5554

5655
/* store in unsigned [big endian] format */
5756
int
5857
bn_bn2bin(const bignum_t *a, unsigned char *b)
5958
{
60-
if (a == NULL || b == NULL) {
59+
if (!a || !b) {
6160
return -1;
6261
}
63-
6462
return botan_mp_to_bin(a->mp, b);
6563
}
6664

6765
bignum_t *
6866
bn_new(void)
6967
{
70-
bignum_t *a;
71-
72-
a = (bignum_t *) calloc(1, sizeof(*a));
73-
if (a == NULL) {
68+
bignum_t *a = (bignum_t *) calloc(1, sizeof(*a));
69+
if (!a) {
7470
return NULL;
7571
}
7672
botan_mp_init(&a->mp);
@@ -80,7 +76,7 @@ bn_new(void)
8076
void
8177
bn_free(bignum_t *a)
8278
{
83-
if (a != NULL) {
79+
if (a) {
8480
botan_mp_destroy(a->mp);
8581
free(a);
8682
}
@@ -89,10 +85,7 @@ bn_free(bignum_t *a)
8985
bool
9086
bn_num_bits(const bignum_t *a, size_t *bits)
9187
{
92-
if (!a || botan_mp_num_bits(a->mp, bits)) {
93-
return false;
94-
}
95-
return true;
88+
return a && !botan_mp_num_bits(a->mp, bits);
9689
}
9790

9891
bool

0 commit comments

Comments
 (0)