ccn_digest.c

Go to the documentation of this file.
00001 /**
00002  * @file ccn_digest.c
00003  * @brief Support for creating digests.
00004  * 
00005  * Part of the CCNx C Library.
00006  *
00007  * Copyright (C) 2009 Palo Alto Research Center, Inc.
00008  *
00009  * This library is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License version 2.1
00011  * as published by the Free Software Foundation.
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details. You should have received
00016  * a copy of the GNU Lesser General Public License along with this library;
00017  * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
00018  * Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 #include <stdlib.h>
00021 #include <openssl/sha.h>
00022 #include <ccn/digest.h>
00023 
00024 struct ccn_digest {
00025     enum ccn_digest_id id;
00026     unsigned short sz;
00027     short ready;
00028     SHA256_CTX sha256_ctx;
00029 };
00030 
00031 struct ccn_digest *
00032 ccn_digest_create(enum ccn_digest_id id)
00033 {
00034     unsigned sz = 0;
00035     struct ccn_digest *ans;
00036     switch (id) {
00037         case CCN_DIGEST_DEFAULT:
00038         case CCN_DIGEST_SHA256:
00039             id = CCN_DIGEST_SHA256;
00040             sz = 32;
00041             break;
00042         default:
00043             return(NULL);
00044     }
00045     ans = calloc(1, sizeof(*ans));
00046     if (ans != NULL) {
00047         ans->id = id;
00048         ans->sz = sz;
00049     }
00050     return(ans);
00051 }
00052 
00053 void
00054 ccn_digest_destroy(struct ccn_digest **pd)
00055 {
00056     if (*pd != NULL) {
00057         free(*pd);
00058         *pd = NULL;
00059     }
00060 }
00061 
00062 enum ccn_digest_id
00063 ccn_digest_getid(struct ccn_digest *d)
00064 {
00065     return(d->id);
00066 }
00067 
00068 size_t
00069 ccn_digest_size(struct ccn_digest *d)
00070 {
00071     return(d->sz);
00072 }
00073 
00074 void
00075 ccn_digest_init(struct ccn_digest *d)
00076 {
00077     SHA256_Init(&d->sha256_ctx);
00078     d->ready = 1;
00079 }
00080 
00081 int
00082 ccn_digest_update(struct ccn_digest *d, const void *data, size_t size)
00083 {
00084     int res;
00085     if (d->ready != 1)
00086         return(-1);
00087     res = SHA256_Update(&d->sha256_ctx, data, size);
00088     return((res == 1) ? 0 : -1);
00089 }
00090 
00091 int
00092 ccn_digest_final(struct ccn_digest *d, unsigned char *result, size_t digest_size)
00093 {
00094     int res;
00095     if (digest_size != d->sz) return(-1);
00096     if (d->ready != 1)
00097         return(-1);
00098     res = SHA256_Final(result, &d->sha256_ctx);
00099     d->ready = 0;
00100     return((res == 1) ? 0 : -1);
00101 }

Generated on Thu Feb 16 00:44:00 2012 for Content-Centric Networking in C by  doxygen 1.5.6