xref: /titanic_44/usr/src/common/crypto/ecc/secitem.c (revision f9fbec18f5b458b560ecf45d3db8e8bd56bf6942)
1*f9fbec18Smcpowers /* ***** BEGIN LICENSE BLOCK *****
2*f9fbec18Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3*f9fbec18Smcpowers  *
4*f9fbec18Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
5*f9fbec18Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
6*f9fbec18Smcpowers  * the License. You may obtain a copy of the License at
7*f9fbec18Smcpowers  * http://www.mozilla.org/MPL/
8*f9fbec18Smcpowers  *
9*f9fbec18Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
10*f9fbec18Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11*f9fbec18Smcpowers  * for the specific language governing rights and limitations under the
12*f9fbec18Smcpowers  * License.
13*f9fbec18Smcpowers  *
14*f9fbec18Smcpowers  * The Original Code is the Netscape security libraries.
15*f9fbec18Smcpowers  *
16*f9fbec18Smcpowers  * The Initial Developer of the Original Code is
17*f9fbec18Smcpowers  * Netscape Communications Corporation.
18*f9fbec18Smcpowers  * Portions created by the Initial Developer are Copyright (C) 1994-2000
19*f9fbec18Smcpowers  * the Initial Developer. All Rights Reserved.
20*f9fbec18Smcpowers  *
21*f9fbec18Smcpowers  * Contributor(s):
22*f9fbec18Smcpowers  *
23*f9fbec18Smcpowers  * Alternatively, the contents of this file may be used under the terms of
24*f9fbec18Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
25*f9fbec18Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26*f9fbec18Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
27*f9fbec18Smcpowers  * of those above. If you wish to allow use of your version of this file only
28*f9fbec18Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
29*f9fbec18Smcpowers  * use your version of this file under the terms of the MPL, indicate your
30*f9fbec18Smcpowers  * decision by deleting the provisions above and replace them with the notice
31*f9fbec18Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
32*f9fbec18Smcpowers  * the provisions above, a recipient may use your version of this file under
33*f9fbec18Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
34*f9fbec18Smcpowers  *
35*f9fbec18Smcpowers  * ***** END LICENSE BLOCK ***** */
36*f9fbec18Smcpowers /*
37*f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
38*f9fbec18Smcpowers  * Use is subject to license terms.
39*f9fbec18Smcpowers  *
40*f9fbec18Smcpowers  * Sun elects to use this software under the MPL license.
41*f9fbec18Smcpowers  */
42*f9fbec18Smcpowers 
43*f9fbec18Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
44*f9fbec18Smcpowers 
45*f9fbec18Smcpowers /*
46*f9fbec18Smcpowers  * Support routines for SECItem data structure.
47*f9fbec18Smcpowers  *
48*f9fbec18Smcpowers  * $Id: secitem.c,v 1.14 2006/05/22 22:24:34 wtchang%redhat.com Exp $
49*f9fbec18Smcpowers  */
50*f9fbec18Smcpowers 
51*f9fbec18Smcpowers #include <sys/types.h>
52*f9fbec18Smcpowers #include <sys/systm.h>
53*f9fbec18Smcpowers #include <sys/param.h>
54*f9fbec18Smcpowers #ifdef _KERNEL
55*f9fbec18Smcpowers #include <sys/kmem.h>
56*f9fbec18Smcpowers #else
57*f9fbec18Smcpowers #include <string.h>
58*f9fbec18Smcpowers #include <strings.h>
59*f9fbec18Smcpowers #include <assert.h>
60*f9fbec18Smcpowers #endif
61*f9fbec18Smcpowers #include "ec.h"
62*f9fbec18Smcpowers #include "ecl-curve.h"
63*f9fbec18Smcpowers #include "ecc_impl.h"
64*f9fbec18Smcpowers 
65*f9fbec18Smcpowers void SECITEM_FreeItem(SECItem *, PRBool);
66*f9fbec18Smcpowers 
67*f9fbec18Smcpowers SECItem *
SECITEM_AllocItem(PRArenaPool * arena,SECItem * item,unsigned int len,int kmflag)68*f9fbec18Smcpowers SECITEM_AllocItem(PRArenaPool *arena, SECItem *item, unsigned int len,
69*f9fbec18Smcpowers     int kmflag)
70*f9fbec18Smcpowers {
71*f9fbec18Smcpowers     SECItem *result = NULL;
72*f9fbec18Smcpowers     void *mark = NULL;
73*f9fbec18Smcpowers 
74*f9fbec18Smcpowers     if (arena != NULL) {
75*f9fbec18Smcpowers 	mark = PORT_ArenaMark(arena);
76*f9fbec18Smcpowers     }
77*f9fbec18Smcpowers 
78*f9fbec18Smcpowers     if (item == NULL) {
79*f9fbec18Smcpowers 	if (arena != NULL) {
80*f9fbec18Smcpowers 	    result = PORT_ArenaZAlloc(arena, sizeof(SECItem), kmflag);
81*f9fbec18Smcpowers 	} else {
82*f9fbec18Smcpowers 	    result = PORT_ZAlloc(sizeof(SECItem), kmflag);
83*f9fbec18Smcpowers 	}
84*f9fbec18Smcpowers 	if (result == NULL) {
85*f9fbec18Smcpowers 	    goto loser;
86*f9fbec18Smcpowers 	}
87*f9fbec18Smcpowers     } else {
88*f9fbec18Smcpowers 	PORT_Assert(item->data == NULL);
89*f9fbec18Smcpowers 	result = item;
90*f9fbec18Smcpowers     }
91*f9fbec18Smcpowers 
92*f9fbec18Smcpowers     result->len = len;
93*f9fbec18Smcpowers     if (len) {
94*f9fbec18Smcpowers 	if (arena != NULL) {
95*f9fbec18Smcpowers 	    result->data = PORT_ArenaAlloc(arena, len, kmflag);
96*f9fbec18Smcpowers 	} else {
97*f9fbec18Smcpowers 	    result->data = PORT_Alloc(len, kmflag);
98*f9fbec18Smcpowers 	}
99*f9fbec18Smcpowers 	if (result->data == NULL) {
100*f9fbec18Smcpowers 	    goto loser;
101*f9fbec18Smcpowers 	}
102*f9fbec18Smcpowers     } else {
103*f9fbec18Smcpowers 	result->data = NULL;
104*f9fbec18Smcpowers     }
105*f9fbec18Smcpowers 
106*f9fbec18Smcpowers     if (mark) {
107*f9fbec18Smcpowers 	PORT_ArenaUnmark(arena, mark);
108*f9fbec18Smcpowers     }
109*f9fbec18Smcpowers     return(result);
110*f9fbec18Smcpowers 
111*f9fbec18Smcpowers loser:
112*f9fbec18Smcpowers     if ( arena != NULL ) {
113*f9fbec18Smcpowers 	if (mark) {
114*f9fbec18Smcpowers 	    PORT_ArenaRelease(arena, mark);
115*f9fbec18Smcpowers 	}
116*f9fbec18Smcpowers 	if (item != NULL) {
117*f9fbec18Smcpowers 	    item->data = NULL;
118*f9fbec18Smcpowers 	    item->len = 0;
119*f9fbec18Smcpowers 	}
120*f9fbec18Smcpowers     } else {
121*f9fbec18Smcpowers 	if (result != NULL) {
122*f9fbec18Smcpowers 	    SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);
123*f9fbec18Smcpowers 	}
124*f9fbec18Smcpowers 	/*
125*f9fbec18Smcpowers 	 * If item is not NULL, the above has set item->data and
126*f9fbec18Smcpowers 	 * item->len to 0.
127*f9fbec18Smcpowers 	 */
128*f9fbec18Smcpowers     }
129*f9fbec18Smcpowers     return(NULL);
130*f9fbec18Smcpowers }
131*f9fbec18Smcpowers 
132*f9fbec18Smcpowers SECStatus
SECITEM_CopyItem(PRArenaPool * arena,SECItem * to,const SECItem * from,int kmflag)133*f9fbec18Smcpowers SECITEM_CopyItem(PRArenaPool *arena, SECItem *to, const SECItem *from,
134*f9fbec18Smcpowers    int kmflag)
135*f9fbec18Smcpowers {
136*f9fbec18Smcpowers     to->type = from->type;
137*f9fbec18Smcpowers     if (from->data && from->len) {
138*f9fbec18Smcpowers 	if ( arena ) {
139*f9fbec18Smcpowers 	    to->data = (unsigned char*) PORT_ArenaAlloc(arena, from->len,
140*f9fbec18Smcpowers 		kmflag);
141*f9fbec18Smcpowers 	} else {
142*f9fbec18Smcpowers 	    to->data = (unsigned char*) PORT_Alloc(from->len, kmflag);
143*f9fbec18Smcpowers 	}
144*f9fbec18Smcpowers 
145*f9fbec18Smcpowers 	if (!to->data) {
146*f9fbec18Smcpowers 	    return SECFailure;
147*f9fbec18Smcpowers 	}
148*f9fbec18Smcpowers 	PORT_Memcpy(to->data, from->data, from->len);
149*f9fbec18Smcpowers 	to->len = from->len;
150*f9fbec18Smcpowers     } else {
151*f9fbec18Smcpowers 	to->data = 0;
152*f9fbec18Smcpowers 	to->len = 0;
153*f9fbec18Smcpowers     }
154*f9fbec18Smcpowers     return SECSuccess;
155*f9fbec18Smcpowers }
156*f9fbec18Smcpowers 
157*f9fbec18Smcpowers void
SECITEM_FreeItem(SECItem * zap,PRBool freeit)158*f9fbec18Smcpowers SECITEM_FreeItem(SECItem *zap, PRBool freeit)
159*f9fbec18Smcpowers {
160*f9fbec18Smcpowers     if (zap) {
161*f9fbec18Smcpowers #ifdef _KERNEL
162*f9fbec18Smcpowers 	kmem_free(zap->data, zap->len);
163*f9fbec18Smcpowers #else
164*f9fbec18Smcpowers 	free(zap->data);
165*f9fbec18Smcpowers #endif
166*f9fbec18Smcpowers 	zap->data = 0;
167*f9fbec18Smcpowers 	zap->len = 0;
168*f9fbec18Smcpowers 	if (freeit) {
169*f9fbec18Smcpowers #ifdef _KERNEL
170*f9fbec18Smcpowers 	    kmem_free(zap, sizeof (SECItem));
171*f9fbec18Smcpowers #else
172*f9fbec18Smcpowers 	    free(zap);
173*f9fbec18Smcpowers #endif
174*f9fbec18Smcpowers 	}
175*f9fbec18Smcpowers     }
176*f9fbec18Smcpowers }
177