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 fko_set_spa_digest_type(fko_ctx_t ctx, const short digest_type)
42 /* Must be initialized
44 if(!CTX_INITIALIZED(ctx))
45 return(FKO_ERROR_CTX_NOT_INITIALIZED);
47 if(digest_type < 1 || digest_type >= FKO_LAST_DIGEST_TYPE)
48 return(FKO_ERROR_INVALID_DATA);
50 ctx->digest_type = digest_type;
52 ctx->state |= FKO_DIGEST_TYPE_MODIFIED;
57 /* Return the SPA digest type.
60 fko_get_spa_digest_type(fko_ctx_t ctx, short *digest_type)
62 /* Must be initialized
64 if(!CTX_INITIALIZED(ctx))
65 return(FKO_ERROR_CTX_NOT_INITIALIZED);
67 *digest_type = ctx->digest_type;
73 fko_set_spa_digest(fko_ctx_t ctx)
77 /* Must be initialized
79 if(!CTX_INITIALIZED(ctx))
80 return(FKO_ERROR_CTX_NOT_INITIALIZED);
82 /* Must have encoded message data to start with.
84 if(ctx->encoded_msg == NULL)
85 return(FKO_ERROR_MISSING_ENCODED_DATA);
87 switch(ctx->digest_type)
90 md = malloc(MD_HEX_SIZE(MD5_DIGEST_LENGTH)+1);
92 return(FKO_ERROR_MEMORY_ALLOCATION);
95 (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
99 md = malloc(MD_HEX_SIZE(SHA1_DIGEST_LENGTH)+1);
101 return(FKO_ERROR_MEMORY_ALLOCATION);
104 (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
107 case FKO_DIGEST_SHA256:
108 md = malloc(MD_HEX_SIZE(SHA256_DIGEST_LENGTH)+1);
110 return(FKO_ERROR_MEMORY_ALLOCATION);
113 (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
116 case FKO_DIGEST_SHA384:
117 md = malloc(MD_HEX_SIZE(SHA384_DIGEST_LENGTH)+1);
119 return(FKO_ERROR_MEMORY_ALLOCATION);
122 (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
125 case FKO_DIGEST_SHA512:
126 md = malloc(MD_HEX_SIZE(SHA512_DIGEST_LENGTH)+1);
128 return(FKO_ERROR_MEMORY_ALLOCATION);
131 (unsigned char*)ctx->encoded_msg, strlen(ctx->encoded_msg));
135 return(FKO_ERROR_INVALID_DIGEST_TYPE);
138 /* Just in case this is a subsquent call to this function. We
139 * do not want to be leaking memory.
141 if(ctx->digest != NULL)
150 fko_get_spa_digest(fko_ctx_t ctx, char **md)
152 /* Must be initialized
154 if(!CTX_INITIALIZED(ctx))
155 return(FKO_ERROR_CTX_NOT_INITIALIZED);