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
00032
00033
00034
00035
00036
00037
00038 #ifndef lint
00039 static char copyright[] =
00040 "@(#) Copyright (c) 2002\n\
00041 Netherlands Forensic Institute. All rights reserved.\n";
00042 #endif
00043
00044 #ifdef HAVE_CONFIG_H
00045 #include <config.h>
00046 #endif
00047
00048 #include <stdio.h>
00049 #include <string.h>
00050
00051 #include "rdd.h"
00052 #include "rdd_internals.h"
00053
00054 #ifdef HAVE_OPENSSL
00055 #include <openssl/sha.h>
00056 #endif
00057
00058 #include "writer.h"
00059 #include "filter.h"
00060
00061 typedef struct _RDD_SHA1_STREAM_FILTER {
00062 SHA_CTX sha1_state;
00063 unsigned char result[SHA_DIGEST_LENGTH];
00064 } RDD_SHA1_STREAM_FILTER;
00065
00066 static int sha1_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte);
00067 static int sha1_close(RDD_FILTER *f);
00068 static int sha1_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte);
00069
00070 static RDD_FILTER_OPS sha1_ops = {
00071 sha1_input,
00072 0,
00073 sha1_close,
00074 sha1_get_result,
00075 0
00076 };
00077
00078 int
00079 rdd_new_sha1_streamfilter(RDD_FILTER **self)
00080 {
00081 RDD_FILTER *f;
00082 RDD_SHA1_STREAM_FILTER *state;
00083 int rc;
00084
00085 if (self == 0) {
00086 return RDD_BADARG;
00087 }
00088
00089 rc = rdd_new_filter(&f, &sha1_ops, sizeof(RDD_SHA1_STREAM_FILTER), 0);
00090 if (rc != RDD_OK) {
00091 return rc;
00092 }
00093 state = (RDD_SHA1_STREAM_FILTER *) f->state;
00094
00095 SHA1_Init(&state->sha1_state);
00096
00097 *self = f;
00098 return RDD_OK;
00099 }
00100
00101 static int
00102 sha1_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00103 {
00104
00105
00106 RDD_SHA1_STREAM_FILTER *state = (RDD_SHA1_STREAM_FILTER *) f->state;
00107
00108 SHA1_Update(&state->sha1_state, (unsigned char *) buf, nbyte);
00109
00110 return RDD_OK;
00111 }
00112
00113 static int
00114 sha1_close(RDD_FILTER *f)
00115 {
00116 if (f == 0) {
00117 return RDD_BADARG;
00118 }
00119
00120 RDD_SHA1_STREAM_FILTER *state = (RDD_SHA1_STREAM_FILTER *) f->state;
00121
00122 SHA1_Final(state->result, &state->sha1_state);
00123
00124 return RDD_OK;
00125 }
00126
00127 static int
00128 sha1_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte)
00129 {
00130 if (f == 0) {
00131 return RDD_BADARG;
00132 }
00133
00134 RDD_SHA1_STREAM_FILTER *state = (RDD_SHA1_STREAM_FILTER *) f->state;
00135
00136 if (nbyte < SHA_DIGEST_LENGTH) {
00137 return RDD_ESPACE;
00138 }
00139
00140 memcpy(buf, state->result, SHA_DIGEST_LENGTH);
00141
00142 return RDD_OK;
00143 }