| 1 | |
| 2 | * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
|
| 3 | *
|
| 4 | * This file is part of GnuPG.
|
| 5 | *
|
| 6 | * GnuPG is free software; you can redistribute it and/or modify
|
| 7 | * it under the terms of the GNU General Public License as published by
|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
| 9 | * (at your option) any later version.
|
| 10 | *
|
| 11 | * GnuPG is distributed in the hope that it will be useful,
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | * GNU General Public License for more details.
|
| 15 | *
|
| 16 | * You should have received a copy of the GNU General Public License
|
| 17 | * along with this program; if not, write to the Free Software
|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
| 19 |
|
| 20 |
|
| 21 | #include <linux/export.h>
|
| 22 |
|
| 23 | #include "mpi-internal.h"
|
| 24 |
|
| 25 | |
| 26 | * Note: It was a bad idea to use the number of limbs to allocate
|
| 27 | * because on a alpha the limbs are large but we normally need
|
| 28 | * integers of n bits - So we should change this to bits (or bytes).
|
| 29 | *
|
| 30 | * But mpi_alloc is used in a lot of places :-)
|
| 31 |
|
| 32 | MPI mpi_alloc(unsigned nlimbs)
|
| 33 | {
|
| 34 | MPI a;
|
| 35 |
|
| 36 | a = kmalloc_obj(*a);
|
| 37 | if (!a)
|
| 38 | return a;
|
| 39 |
|
| 40 | if (nlimbs) {
|
| 41 | a->d = mpi_alloc_limb_space(nlimbs);
|
| 42 | if (!a->d) {
|
| 43 | kfree(a);
|
| 44 | return NULL;
|
| 45 | }
|
| 46 | } else {
|
| 47 | a->d = NULL;
|
| 48 | }
|
| 49 |
|
| 50 | a->alloced = nlimbs;
|
| 51 | a->nlimbs = 0;
|
| 52 | a->sign = 0;
|
| 53 | a->flags = 0;
|
| 54 | a->nbits = 0;
|
| 55 | return a;
|
| 56 | }
|
| 57 | EXPORT_SYMBOL_GPL(mpi_alloc);
|
| 58 |
|
| 59 | mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
|
| 60 | {
|
| 61 | size_t len = nlimbs * sizeof(mpi_limb_t);
|
| 62 |
|
| 63 | if (!len)
|
| 64 | return NULL;
|
| 65 |
|
| 66 | return kmalloc(len, GFP_KERNEL);
|
| 67 | }
|
| 68 |
|
| 69 | void mpi_free_limb_space(mpi_ptr_t a)
|
| 70 | {
|
| 71 | if (!a)
|
| 72 | return;
|
| 73 |
|
| 74 | kfree_sensitive(a);
|
| 75 | }
|
| 76 |
|
| 77 | void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
|
| 78 | {
|
| 79 | mpi_free_limb_space(a->d);
|
| 80 | a->d = ap;
|
| 81 | a->alloced = nlimbs;
|
| 82 | }
|
| 83 |
|
| 84 | |
| 85 | * Resize the array of A to NLIMBS. the additional space is cleared
|
| 86 | * (set to 0) [done by m_realloc()]
|
| 87 |
|
| 88 | int mpi_resize(MPI a, unsigned nlimbs)
|
| 89 | {
|
| 90 | void *p;
|
| 91 |
|
| 92 | if (nlimbs <= a->alloced)
|
| 93 | return 0;
|
| 94 |
|
| 95 | if (a->d) {
|
| 96 | p = kzalloc_objs(mpi_limb_t, nlimbs);
|
| 97 | if (!p)
|
| 98 | return -ENOMEM;
|
| 99 | memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
|
| 100 | kfree_sensitive(a->d);
|
| 101 | a->d = p;
|
| 102 | } else {
|
| 103 | a->d = kzalloc_objs(mpi_limb_t, nlimbs);
|
| 104 | if (!a->d)
|
| 105 | return -ENOMEM;
|
| 106 | }
|
| 107 | a->alloced = nlimbs;
|
| 108 | return 0;
|
| 109 | }
|
| 110 |
|
| 111 | void mpi_free(MPI a)
|
| 112 | {
|
| 113 | if (!a)
|
| 114 | return;
|
| 115 |
|
| 116 | if (a->flags & 4)
|
| 117 | kfree_sensitive(a->d);
|
| 118 | else
|
| 119 | mpi_free_limb_space(a->d);
|
| 120 |
|
| 121 | if (a->flags & ~7)
|
| 122 | pr_info("invalid flag value in mpi\n");
|
| 123 | kfree(a);
|
| 124 | }
|
| 125 | EXPORT_SYMBOL_GPL(mpi_free);
|
| 126 |
|
| 127 | |
| 128 | * Note: This copy function should not interpret the MPI
|
| 129 | * but copy it transparently.
|
| 130 |
|
| 131 | MPI mpi_copy(MPI a)
|
| 132 | {
|
| 133 | int i;
|
| 134 | MPI b;
|
| 135 |
|
| 136 | if (a) {
|
| 137 | b = mpi_alloc(a->nlimbs);
|
| 138 | if (!b)
|
| 139 | return NULL;
|
| 140 | b->nlimbs = a->nlimbs;
|
| 141 | b->sign = a->sign;
|
| 142 | b->flags = a->flags;
|
| 143 | b->flags &= ~(16|32);
|
| 144 | for (i = 0; i < b->nlimbs; i++)
|
| 145 | b->d[i] = a->d[i];
|
| 146 | } else
|
| 147 | b = NULL;
|
| 148 | return b;
|
| 149 | }
|
| 150 |
|
| 151 | MODULE_DESCRIPTION("Multiprecision maths library");
|
| 152 | MODULE_LICENSE("GPL");
|