2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Create the base64-encoded digest for the current spa data. The
9 * digest used is determined by the digest_type setting in the
12 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.org)
14 * License (GNU Public License):
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 *****************************************************************************
33 #include "fko_common.h"
37 /* Set the SPA digest type.
40 set_spa_digest_type(fko_ctx_t ctx,
41 short *digest_type_field, const short digest_type)
43 /* Must be initialized
45 if(!CTX_INITIALIZED(ctx))
46 return(FKO_ERROR_CTX_NOT_INITIALIZED);
48 if(digest_type < 1 || digest_type >= FKO_LAST_DIGEST_TYPE)
49 return(FKO_ERROR_INVALID_DATA);
51 *digest_type_field = digest_type;
53 ctx->state |= FKO_DIGEST_TYPE_MODIFIED;
59 fko_set_spa_digest_type(fko_ctx_t ctx, const short digest_type)
61 return set_spa_digest_type(ctx, &ctx->digest_type, digest_type);
65 fko_set_raw_spa_digest_type(fko_ctx_t ctx, const short raw_digest_type)
67 return set_spa_digest_type(ctx, &ctx->raw_digest_type, raw_digest_type);
70 /* Return the SPA digest type.
73 fko_get_spa_digest_type(fko_ctx_t ctx, short *digest_type)
75 /* Must be initialized
77 if(!CTX_INITIALIZED(ctx))
78 return(FKO_ERROR_CTX_NOT_INITIALIZED);
80 *digest_type = ctx->digest_type;
85 /* Return the SPA digest type.
88 fko_get_raw_spa_digest_type(fko_ctx_t ctx, short *raw_digest_type)
90 /* Must be initialized
92 if(!CTX_INITIALIZED(ctx))
93 return(FKO_ERROR_CTX_NOT_INITIALIZED);
95 *raw_digest_type = ctx->raw_digest_type;
101 set_digest(char *data, char **digest, short digest_type)
108 md = malloc(MD_HEX_SIZE(MD5_DIGEST_LENGTH)+1);
110 return(FKO_ERROR_MEMORY_ALLOCATION);
113 (unsigned char*)data, strlen(data));
116 case FKO_DIGEST_SHA1:
117 md = malloc(MD_HEX_SIZE(SHA1_DIGEST_LENGTH)+1);
119 return(FKO_ERROR_MEMORY_ALLOCATION);
122 (unsigned char*)data, strlen(data));
125 case FKO_DIGEST_SHA256:
126 md = malloc(MD_HEX_SIZE(SHA256_DIGEST_LENGTH)+1);
128 return(FKO_ERROR_MEMORY_ALLOCATION);
131 (unsigned char*)data, strlen(data));
134 case FKO_DIGEST_SHA384:
135 md = malloc(MD_HEX_SIZE(SHA384_DIGEST_LENGTH)+1);
137 return(FKO_ERROR_MEMORY_ALLOCATION);
140 (unsigned char*)data, strlen(data));
143 case FKO_DIGEST_SHA512:
144 md = malloc(MD_HEX_SIZE(SHA512_DIGEST_LENGTH)+1);
146 return(FKO_ERROR_MEMORY_ALLOCATION);
149 (unsigned char*)data, strlen(data));
153 return(FKO_ERROR_INVALID_DIGEST_TYPE);
156 /* Just in case this is a subsquent call to this function. We
157 * do not want to be leaking memory.
168 fko_set_spa_digest(fko_ctx_t ctx)
170 /* Must be initialized
172 if(!CTX_INITIALIZED(ctx))
173 return(FKO_ERROR_CTX_NOT_INITIALIZED);
175 /* Must have encoded message data to start with.
177 if(ctx->encoded_msg == NULL)
178 return(FKO_ERROR_MISSING_ENCODED_DATA);
180 return set_digest(ctx->encoded_msg,
181 &ctx->digest, ctx->digest_type);
185 fko_set_raw_spa_digest(fko_ctx_t ctx)
187 /* Must be initialized
189 if(!CTX_INITIALIZED(ctx))
190 return(FKO_ERROR_CTX_NOT_INITIALIZED);
192 /* Must have encoded message data to start with.
194 if(ctx->encrypted_msg == NULL)
195 return(FKO_ERROR_MISSING_ENCODED_DATA);
197 return set_digest(ctx->encrypted_msg,
198 &ctx->raw_digest, ctx->raw_digest_type);
202 fko_get_spa_digest(fko_ctx_t ctx, char **md)
204 /* Must be initialized
206 if(!CTX_INITIALIZED(ctx))
207 return(FKO_ERROR_CTX_NOT_INITIALIZED);
215 fko_get_raw_spa_digest(fko_ctx_t ctx, char **md)
217 /* Must be initialized
219 if(!CTX_INITIALIZED(ctx))
220 return(FKO_ERROR_CTX_NOT_INITIALIZED);
222 *md = ctx->raw_digest;