Skip to content

Commit

Permalink
Update pgp_fingerprint_t with vector constructor and size_valid() met…
Browse files Browse the repository at this point in the history
…hod.
  • Loading branch information
ni4 authored and ronaldtse committed Mar 18, 2024
1 parent ebf0400 commit a4fb479
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
2 changes: 2 additions & 0 deletions include/repgp/repgp_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#define PGP_KEY_ID_SIZE 8

/* Size of the fingerprint */
#define PGP_FINGERPRINT_V3_SIZE 16
#define PGP_FINGERPRINT_V4_SIZE 20
#define PGP_FINGERPRINT_V5_SIZE 32
#define PGP_MAX_FINGERPRINT_SIZE 32
Expand All @@ -105,6 +106,7 @@ static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V5_SIZE, "FP size mism
#if defined(ENABLE_CRYPTO_REFRESH)
#define PGP_FINGERPRINT_V6_SIZE 32
static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
static_assert(PGP_FINGERPRINT_V5_SIZE == PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
#endif

/* SEIPDv2 salt length */
Expand Down
79 changes: 36 additions & 43 deletions src/lib/types.h
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
/*
* Copyright (c) 2017-2021, [Ribose Inc](https://www.ribose.com).
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* Copyright (c) 2017-2023, [Ribose Inc](https://www.ribose.com).
* All rights reserved.
*
* This code is originally derived from software contributed to
* The NetBSD Foundation by Alistair Crooks ([email protected]), and
* carried further by Ribose Inc (https://www.ribose.com).
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
* All rights reserved.
* Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
* their moral rights under the UK Copyright Design and Patents Act 1988 to
* be recorded as the authors of this copyright work.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* See the License for the specific language governing permissions and
* limitations under the License.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TYPES_H_
#define TYPES_H_
Expand All @@ -56,6 +31,7 @@
#include <vector>
#include <array>
#include <cstring>
#include <stdexcept>
#include <type_traits>

#include <rnp/rnp_def.h>
Expand Down Expand Up @@ -106,6 +82,23 @@ typedef struct pgp_fingerprint_t {
unsigned length;
bool operator==(const pgp_fingerprint_t &src) const;
bool operator!=(const pgp_fingerprint_t &src) const;

pgp_fingerprint_t() = default;
pgp_fingerprint_t(const std::vector<uint8_t> &src)
{
if (!size_valid(src.size())) {
throw std::invalid_argument("src");
}
memcpy(fingerprint, src.data(), src.size());
length = src.size();
}

static bool
size_valid(size_t size)
{
return (size == PGP_FINGERPRINT_V4_SIZE) || (size == PGP_FINGERPRINT_V3_SIZE) ||
(size == PGP_FINGERPRINT_V5_SIZE);
}
} pgp_fingerprint_t;

typedef std::array<uint8_t, PGP_KEY_GRIP_SIZE> pgp_sig_id_t;
Expand Down

0 comments on commit a4fb479

Please sign in to comment.