Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* mpihelp-sub.c  -  MPI helper functions
0003  *  Copyright (C) 1994, 1996 Free Software Foundation, Inc.
0004  *  Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
0005  *
0006  * This file is part of GnuPG.
0007  *
0008  * Note: This code is heavily based on the GNU MP Library.
0009  *   Actually it's the same code with only minor changes in the
0010  *   way the data is stored; this is to support the abstraction
0011  *   of an optional secure memory allocation which may be used
0012  *   to avoid revealing of sensitive data due to paging etc.
0013  *   The GNU MP Library itself is published under the LGPL;
0014  *   however I decided to publish this code under the plain GPL.
0015  */
0016 
0017 #include "mpi-internal.h"
0018 
0019 /****************
0020  * Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
0021  * There are no restrictions on the relative sizes of
0022  * the two arguments.
0023  * Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.
0024  */
0025 int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size)
0026 {
0027     mpi_size_t i;
0028     mpi_limb_t op1_word, op2_word;
0029 
0030     for (i = size - 1; i >= 0; i--) {
0031         op1_word = op1_ptr[i];
0032         op2_word = op2_ptr[i];
0033         if (op1_word != op2_word)
0034             goto diff;
0035     }
0036     return 0;
0037 
0038 diff:
0039     /* This can *not* be simplified to
0040      *   op2_word - op2_word
0041      * since that expression might give signed overflow.  */
0042     return (op1_word > op2_word) ? 1 : -1;
0043 }