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_SHA512_STREAM_FILTER {
00055 SHA512_CTX sha512_state;
00056 unsigned char result[SHA512_DIGEST_LENGTH];
00057 } RDD_SHA512_STREAM_FILTER;
00058
00059 static int sha512_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte);
00060 static int sha512_close(RDD_FILTER *f);
00061 static int sha512_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte);
00062
00063 static RDD_FILTER_OPS sha512_ops = {
00064 sha512_input,
00065 0,
00066 sha512_close,
00067 sha512_get_result,
00068 0
00069 };
00070
00071 int
00072 rdd_new_sha512_streamfilter(RDD_FILTER **self)
00073 {
00074 RDD_FILTER *f;
00075 RDD_SHA512_STREAM_FILTER *state;
00076 int rc;
00077
00078 if (self == 0) {
00079 return RDD_BADARG;
00080 }
00081
00082 rc = rdd_new_filter(&f, &sha512_ops, sizeof(RDD_SHA512_STREAM_FILTER), 0);
00083 if (rc != RDD_OK) {
00084 return rc;
00085 }
00086 state = (RDD_SHA512_STREAM_FILTER *) f->state;
00087
00088 SHA512_Init(&state->sha512_state);
00089
00090 *self = f;
00091 return RDD_OK;
00092 }
00093
00094 static int
00095 sha512_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00096 {
00097 RDD_SHA512_STREAM_FILTER *state = (RDD_SHA512_STREAM_FILTER *) f->state;
00098
00099 SHA512_Update(&state->sha512_state, (unsigned char *) buf, nbyte);
00100
00101 return RDD_OK;
00102 }
00103
00104 static int
00105 sha512_close(RDD_FILTER *f)
00106 {
00107 if (f == 0) {
00108 return RDD_BADARG;
00109 }
00110
00111 RDD_SHA512_STREAM_FILTER *state = (RDD_SHA512_STREAM_FILTER *) f->state;
00112
00113 SHA512_Final(state->result, &state->sha512_state);
00114
00115 return RDD_OK;
00116 }
00117
00118 static int
00119 sha512_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte)
00120 {
00121 if (f == 0) {
00122 return RDD_BADARG;
00123 }
00124
00125 RDD_SHA512_STREAM_FILTER *state = (RDD_SHA512_STREAM_FILTER *) f->state;
00126
00127 if (nbyte < SHA512_DIGEST_LENGTH) return RDD_ESPACE;
00128
00129 memcpy(buf, state->result, SHA512_DIGEST_LENGTH);
00130
00131 return RDD_OK;
00132 }