Back to index

lightning-sunbird  0.9+nobinonly
pqgutil.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 #include "pqgutil.h"
00037 #include "prerror.h"
00038 #include "secitem.h"
00039 
00040 #define PQG_DEFAULT_CHUNKSIZE 2048 /* bytes */
00041 
00042 /**************************************************************************
00043  *  Return a pointer to a new PQGParams struct that is a duplicate of     *
00044  *  the one passed as an argument.                                        *
00045  *  Return NULL on failure, or if NULL was passed in.                     *
00046  *                                                                        *
00047  **************************************************************************/
00048 
00049 PQGParams *
00050 PQG_DupParams(const PQGParams *src)
00051 {
00052     PRArenaPool *arena;
00053     PQGParams *dest;
00054     SECStatus status;
00055 
00056     if (src == NULL) {
00057        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
00058        return NULL;
00059     }
00060 
00061     arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE);
00062     if (arena == NULL)
00063        goto loser;
00064 
00065     dest = (PQGParams*)PORT_ArenaZAlloc(arena, sizeof(PQGParams));
00066     if (dest == NULL)
00067        goto loser;
00068 
00069     dest->arena = arena;
00070 
00071     status = SECITEM_CopyItem(arena, &dest->prime, &src->prime);
00072     if (status != SECSuccess)
00073        goto loser;
00074 
00075     status = SECITEM_CopyItem(arena, &dest->subPrime, &src->subPrime);
00076     if (status != SECSuccess)
00077        goto loser;
00078 
00079     status = SECITEM_CopyItem(arena, &dest->base, &src->base);
00080     if (status != SECSuccess)
00081        goto loser;
00082 
00083     return dest;
00084 
00085 loser:
00086     if (arena != NULL)
00087        PORT_FreeArena(arena, PR_FALSE);
00088     return NULL;
00089 }
00090 
00091 /**************************************************************************
00092  *  Return a pointer to a new PQGParams struct that is constructed from   *
00093  *  copies of the arguments passed in.                                    *
00094  *  Return NULL on failure.                                               *
00095  **************************************************************************/
00096 
00097 PQGParams *
00098 PQG_NewParams(const SECItem * prime, const SECItem * subPrime, 
00099               const SECItem * base)
00100 {
00101     PQGParams *  dest;
00102     PQGParams    src;
00103 
00104     src.arena    = NULL;
00105     src.prime    = *prime;
00106     src.subPrime = *subPrime;
00107     src.base     = *base;
00108     dest         = PQG_DupParams(&src);
00109     return dest;
00110 }
00111 
00112 /**************************************************************************
00113  * Fills in caller's "prime" SECItem with the prime value in params.
00114  * Contents can be freed by calling SECITEM_FreeItem(prime, PR_FALSE);       
00115  **************************************************************************/
00116 SECStatus
00117 PQG_GetPrimeFromParams(const PQGParams *params, SECItem * prime)
00118 {
00119     return SECITEM_CopyItem(NULL, prime, &params->prime);
00120 }
00121 
00122 /**************************************************************************
00123  * Fills in caller's "subPrime" SECItem with the prime value in params.
00124  * Contents can be freed by calling SECITEM_FreeItem(subPrime, PR_FALSE);    
00125  **************************************************************************/
00126 SECStatus
00127 PQG_GetSubPrimeFromParams(const PQGParams *params, SECItem * subPrime)
00128 {
00129     return SECITEM_CopyItem(NULL, subPrime, &params->subPrime);
00130 }
00131 
00132 /**************************************************************************
00133  * Fills in caller's "base" SECItem with the base value in params.
00134  * Contents can be freed by calling SECITEM_FreeItem(base, PR_FALSE); 
00135  **************************************************************************/
00136 SECStatus
00137 PQG_GetBaseFromParams(const PQGParams *params, SECItem * base)
00138 {
00139     return SECITEM_CopyItem(NULL, base, &params->base);
00140 }
00141 
00142 /**************************************************************************
00143  *  Free the PQGParams struct and the things it points to.                *
00144  **************************************************************************/
00145 void
00146 PQG_DestroyParams(PQGParams *params)
00147 {
00148     if (params == NULL) 
00149        return;
00150     if (params->arena != NULL) {
00151        PORT_FreeArena(params->arena, PR_FALSE);  /* don't zero it */
00152     } else {
00153        SECITEM_FreeItem(&params->prime,    PR_FALSE); /* don't free prime */
00154        SECITEM_FreeItem(&params->subPrime, PR_FALSE); /* don't free subPrime */
00155        SECITEM_FreeItem(&params->base,     PR_FALSE); /* don't free base */
00156        PORT_Free(params);
00157     }
00158 }
00159 
00160 /**************************************************************************
00161  *  Return a pointer to a new PQGVerify struct that is a duplicate of     *
00162  *  the one passed as an argument.                                        *
00163  *  Return NULL on failure, or if NULL was passed in.                     *
00164  **************************************************************************/
00165 
00166 PQGVerify *
00167 PQG_DupVerify(const PQGVerify *src)
00168 {
00169     PRArenaPool *arena;
00170     PQGVerify *  dest;
00171     SECStatus    status;
00172 
00173     if (src == NULL) {
00174        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
00175        return NULL;
00176     }
00177 
00178     arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE);
00179     if (arena == NULL)
00180        goto loser;
00181 
00182     dest = (PQGVerify*)PORT_ArenaZAlloc(arena, sizeof(PQGVerify));
00183     if (dest == NULL)
00184        goto loser;
00185 
00186     dest->arena   = arena;
00187     dest->counter = src->counter;
00188 
00189     status = SECITEM_CopyItem(arena, &dest->seed, &src->seed);
00190     if (status != SECSuccess)
00191        goto loser;
00192 
00193     status = SECITEM_CopyItem(arena, &dest->h, &src->h);
00194     if (status != SECSuccess)
00195        goto loser;
00196 
00197     return dest;
00198 
00199 loser:
00200     if (arena != NULL)
00201        PORT_FreeArena(arena, PR_FALSE);
00202     return NULL;
00203 }
00204 
00205 /**************************************************************************
00206  *  Return a pointer to a new PQGVerify struct that is constructed from   *
00207  *  copies of the arguments passed in.                                    *
00208  *  Return NULL on failure.                                               *
00209  **************************************************************************/
00210 
00211 PQGVerify *
00212 PQG_NewVerify(unsigned int counter, const SECItem * seed, const SECItem * h)
00213 {
00214     PQGVerify *  dest;
00215     PQGVerify    src;
00216 
00217     src.arena    = NULL;
00218     src.counter  = counter;
00219     src.seed     = *seed;
00220     src.h        = *h;
00221     dest         = PQG_DupVerify(&src);
00222     return dest;
00223 }
00224 
00225 /**************************************************************************
00226  * Returns the "counter" value from the PQGVerify.
00227  **************************************************************************/
00228 unsigned int
00229 PQG_GetCounterFromVerify(const PQGVerify *verify)
00230 {
00231     return verify->counter;
00232 }
00233 
00234 /**************************************************************************
00235  * Fills in caller's "seed" SECItem with the seed value in verify.
00236  * Contents can be freed by calling SECITEM_FreeItem(seed, PR_FALSE); 
00237  **************************************************************************/
00238 SECStatus
00239 PQG_GetSeedFromVerify(const PQGVerify *verify, SECItem * seed)
00240 {
00241     return SECITEM_CopyItem(NULL, seed, &verify->seed);
00242 }
00243 
00244 /**************************************************************************
00245  * Fills in caller's "h" SECItem with the h value in verify.
00246  * Contents can be freed by calling SECITEM_FreeItem(h, PR_FALSE);    
00247  **************************************************************************/
00248 SECStatus
00249 PQG_GetHFromVerify(const PQGVerify *verify, SECItem * h)
00250 {
00251     return SECITEM_CopyItem(NULL, h, &verify->h);
00252 }
00253 
00254 /**************************************************************************
00255  *  Free the PQGVerify struct and the things it points to.                *
00256  **************************************************************************/
00257 
00258 void
00259 PQG_DestroyVerify(PQGVerify *vfy)
00260 {
00261     if (vfy == NULL) 
00262        return;
00263     if (vfy->arena != NULL) {
00264        PORT_FreeArena(vfy->arena, PR_FALSE);     /* don't zero it */
00265     } else {
00266        SECITEM_FreeItem(&vfy->seed,   PR_FALSE); /* don't free seed */
00267        SECITEM_FreeItem(&vfy->h,      PR_FALSE); /* don't free h */
00268        PORT_Free(vfy);
00269     }
00270 }