From 105dbc3c83dc569cc46f5ff76bc154cda9e6e015 Mon Sep 17 00:00:00 2001 From: jbj Date: Tue, 2 Oct 2001 18:29:43 +0000 Subject: [PATCH] Add b32bswabhex/mp32nswabhex. CVS patchset: 5092 CVS date: 2001/10/02 18:29:43 --- beecrypt/mp32barrett.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++--- beecrypt/mp32barrett.h | 6 +++++ beecrypt/mp32number.c | 52 ++++++++++++++++++++++++++++++++++++++ beecrypt/mp32number.h | 6 +++++ 4 files changed, 128 insertions(+), 4 deletions(-) diff --git a/beecrypt/mp32barrett.c b/beecrypt/mp32barrett.c index 86c8982..438d077 100644 --- a/beecrypt/mp32barrett.c +++ b/beecrypt/mp32barrett.c @@ -2,15 +2,14 @@ * \file mp32barrett.c * * Barrett modular reduction, code. - */ - -/* * * For more information on this algorithm, see: * "Handbook of Applied Cryptography", Chapter 14.3.3 * Menezes, van Oorschot, Vanstone * CRC Press - * + */ + +/* * Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V. * * Author: Bob Deblier @@ -227,6 +226,67 @@ void mp32bsethex(mp32barrett* b, const char* hex) } /*@=nullstate =compdef @*/ +/*@-nullstate -compdef @*/ /* b->modl may be null @*/ +void mp32bswabhex(mp32barrett* b, const char* hex) +{ + uint32 length = strlen(hex); + uint32 size = (length+7) >> 3; + uint8 rem = (uint8)(length & 0x7); + + if (b->modl) + { + if (b->size != size) + b->modl = (uint32*) realloc(b->modl, (2*size+1) * sizeof(uint32)); + } + else + b->modl = (uint32*) malloc((2*size+1) * sizeof(uint32)); + + if (b->modl != (uint32*) 0) + { + register uint32 val = 0; + register uint32* dst = b->modl; + register uint32* temp = (uint32*) malloc((6*size+4) * sizeof(uint32)); + register char ch; + + b->size = size; + b->mu = b->modl+size; + + while (length-- > 0) + { + ch = *(hex++); + val <<= 4; + if (ch >= '0' && ch <= '9') + val += (ch - '0'); + else if (ch >= 'A' && ch <= 'F') + val += (ch - 'A') + 10; + else if (ch >= 'a' && ch <= 'f') + val += (ch - 'a') + 10; + else + {}; + + if ((length & 0x7) == 0) + { + *(dst++) = swapu32(val); + val = 0; + } + } + if (rem != 0) + *dst = swapu32(val); + + /*@-nullpass@*/ /* temp may be NULL */ + mp32bmu_w(b, temp); + + free(temp); + /*@=nullpass@*/ + } + else + { + b->size = 0; + b->mu = 0; + } +} +/*@=nullstate =compdef @*/ + /** * Computes the Barrett 'mu' coefficient. * needs workspace of (6*size+4) words diff --git a/beecrypt/mp32barrett.h b/beecrypt/mp32barrett.h index 8b8a566..0b77987 100644 --- a/beecrypt/mp32barrett.h +++ b/beecrypt/mp32barrett.h @@ -82,6 +82,12 @@ void mp32bsethex(mp32barrett* b, const char* hex) /** */ +BEEDLLAPI /*@unused@*/ +void mp32bswabhex(mp32barrett* b, const char* hex) + /*@modifies b->size, b->modl, b->mu @*/; + +/** + */ BEEDLLAPI void mp32bsubone(const mp32barrett* b, uint32* result) /*@modifies result @*/; diff --git a/beecrypt/mp32number.c b/beecrypt/mp32number.c index a4c766d..3cd9755 100644 --- a/beecrypt/mp32number.c +++ b/beecrypt/mp32number.c @@ -229,3 +229,55 @@ void mp32nsethex(mp32number* n, const char* hex) } } /*@=nullstate =compdef @*/ + +/*@-nullstate -compdef @*/ /* n->data may be NULL */ +void mp32nswabhex(mp32number* n, const char* hex) +{ + uint32 length = strlen(hex); + uint32 size = (length+7) >> 3; + uint8 rem = (uint8)(length & 0x7); + + if (n->data) + { + if (n->size != size) + n->data = (uint32*) realloc(n->data, size * sizeof(uint32)); + } + else + n->data = (uint32*) malloc(size * sizeof(uint32)); + + if (n->data) + { + register uint32 val = 0; + register uint32* dst = n->data; + register char ch; + + n->size = size; + + while (length-- > 0) + { + ch = *(hex++); + val <<= 4; + if (ch >= '0' && ch <= '9') + val += (ch - '0'); + else if (ch >= 'A' && ch <= 'F') + val += (ch - 'A') + 10; + else if (ch >= 'a' && ch <= 'f') + val += (ch - 'a') + 10; + else + {}; + + if ((length & 0x7) == 0) + { + *(dst++) = swapu32(val); + val = 0; + } + } + if (rem != 0) + *dst = swapu32(val); + } + else { + n->size = 0; + n->data = (uint32*)0; + } +} +/*@=nullstate =compdef @*/ diff --git a/beecrypt/mp32number.h b/beecrypt/mp32number.h index f2cfc2b..d091fff 100644 --- a/beecrypt/mp32number.h +++ b/beecrypt/mp32number.h @@ -98,6 +98,12 @@ BEEDLLAPI /*@unused@*/ void mp32nsethex(mp32number* n, const char* hex) /*@modifies n->size, n->data @*/; +/** + */ +BEEDLLAPI /*@unused@*/ +void mp32nswabhex(mp32number* n, const char* hex) + /*@modifies n->size, n->data @*/; + #ifdef __cplusplus } #endif -- 2.7.4