00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef lint
00032 static char copyright[] =
00033 "@(#) Copyright (c) 2002-2007\n\
00034 Netherlands Forensic Institute. All rights reserved.\n";
00035 #endif
00036
00037 #ifdef HAVE_CONFIG_H
00038 #include <config.h>
00039 #endif
00040
00041 #include <stdio.h>
00042 #include <string.h>
00043
00044 #include "rdd.h"
00045 #include "rdd_internals.h"
00046
00047 #ifdef HAVE_OPENSSL
00048 #include <openssl/sha.h>
00049 #endif
00050
00051 #include "writer.h"
00052 #include "filter.h"
00053
00054 typedef struct _RDD_SHA256_STREAM_FILTER {
00055 SHA256_CTX sha256_state;
00056 unsigned char result[SHA256_DIGEST_LENGTH];
00057 } RDD_SHA256_STREAM_FILTER;
00058
00059 static int sha256_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte);
00060 static int sha256_close(RDD_FILTER *f);
00061 static int sha256_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte);
00062
00063 static RDD_FILTER_OPS sha256_ops = {
00064 sha256_input,
00065 0,
00066 sha256_close,
00067 sha256_get_result,
00068 0
00069 };
00070
00071 int
00072 rdd_new_sha256_streamfilter(RDD_FILTER **self)
00073 {
00074 RDD_FILTER *f;
00075 RDD_SHA256_STREAM_FILTER *state;
00076 int rc;
00077
00078 if (self == 0) {
00079 return RDD_BADARG;
00080 }
00081
00082 rc = rdd_new_filter(&f, &sha256_ops, sizeof(RDD_SHA256_STREAM_FILTER), 0);
00083 if (rc != RDD_OK) {
00084 return rc;
00085 }
00086 state = (RDD_SHA256_STREAM_FILTER *) f->state;
00087
00088 SHA256_Init(&state->sha256_state);
00089
00090 *self = f;
00091 return RDD_OK;
00092 }
00093
00094 static int
00095 sha256_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00096 {
00097
00098
00099 RDD_SHA256_STREAM_FILTER *state = (RDD_SHA256_STREAM_FILTER *) f->state;
00100
00101 SHA256_Update(&state->sha256_state, (unsigned char *) buf, nbyte);
00102
00103 return RDD_OK;
00104 }
00105
00106 static int
00107 sha256_close(RDD_FILTER *f)
00108 {
00109 if (f == 0) {
00110 return RDD_BADARG;
00111 }
00112
00113 RDD_SHA256_STREAM_FILTER *state = (RDD_SHA256_STREAM_FILTER *) f->state;
00114
00115 SHA256_Final(state->result, &state->sha256_state);
00116
00117 return RDD_OK;
00118 }
00119
00120 static int
00121 sha256_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte)
00122 {
00123 if (f == 0) {
00124 return RDD_BADARG;
00125 }
00126
00127 RDD_SHA256_STREAM_FILTER *state = (RDD_SHA256_STREAM_FILTER *) f->state;
00128
00129 if (nbyte < SHA256_DIGEST_LENGTH) return RDD_ESPACE;
00130
00131 memcpy(buf, state->result, SHA256_DIGEST_LENGTH);
00132
00133 return RDD_OK;
00134 }