1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::ffi::c_void;

#[repr(C)]
#[derive(Default)]
struct SHA1Context {
    h0: u32,
    h1: u32,
    h2: u32,
    h3: u32,
    h4: u32,
    nl: u32,
    nh: u32,
    data: [u32; 16],
    num: i32,
}

#[repr(C)]
#[derive(Default)]
struct SHA256Context {
    count: [u32; 2],
    hash: [u32; 8],
    wbuf: [u32; 16],
}

#[repr(C)]
#[derive(Default)]
struct SHA512Context {
    count: [u64; 2],
    hash: [u64; 8],
    wbuf: [u64; 16],
}

pub struct Hash;

macro_rules! implement_hash {
    ($func:ident, $struct:ident, $ctx:ident, $one_shot:ident, $init:ident, $update:ident, $final:ident, $len:expr) => {
        extern "C" {
            fn $one_shot(data: *const c_void, len: i32, output: *mut u8) -> *mut u8;
            fn $init(ctx: *mut $ctx) -> i32;
            fn $update(ctx: *mut $ctx, data: *const c_void, len: i32) -> i32;
            fn $final(output: *mut c_void, ctx: *mut $ctx) -> i32;
        }

        impl Hash {
            pub fn $func(data: impl AsRef<[u8]>) -> [u8; $len] {
                let mut output = [0u8; $len];

                unsafe {
                    $one_shot(
                        data.as_ref().as_ptr() as *const c_void,
                        data.as_ref().len() as i32,
                        output.as_mut_ptr(),
                    );
                }

                output
            }
        }

        pub struct $struct {
            ctx: $ctx,
        }

        impl $struct {
            pub fn new() -> Self {
                let mut ctx = $ctx::default();

                unsafe {
                    $init(&mut ctx);
                }

                Self { ctx }
            }

            pub fn update(&mut self, data: impl AsRef<[u8]>) {
                unsafe {
                    $update(
                        &mut self.ctx,
                        data.as_ref().as_ptr() as *const c_void,
                        data.as_ref().len() as i32,
                    );
                }
            }

            pub fn finish(mut self) -> [u8; $len] {
                let mut output = [0u8; $len];
                unsafe { $final(output.as_mut_ptr() as *mut c_void, &mut self.ctx) };
                output
            }
        }
    };
}

implement_hash!(
    sha1,
    SHA1,
    SHA1Context,
    CC_SHA1,
    CC_SHA1_Init,
    CC_SHA1_Update,
    CC_SHA1_Final,
    20
);

implement_hash!(
    sha224,
    SHA224,
    SHA256Context,
    CC_SHA224,
    CC_SHA224_Init,
    CC_SHA224_Update,
    CC_SHA224_Final,
    28
);

implement_hash!(
    sha256,
    SHA256,
    SHA256Context,
    CC_SHA256,
    CC_SHA256_Init,
    CC_SHA256_Update,
    CC_SHA256_Final,
    32
);

implement_hash!(
    sha384,
    SHA384,
    SHA512Context,
    CC_SHA384,
    CC_SHA384_Init,
    CC_SHA384_Update,
    CC_SHA384_Final,
    48
);

implement_hash!(
    sha512,
    SHA512,
    SHA512Context,
    CC_SHA512,
    CC_SHA512_Init,
    CC_SHA512_Update,
    CC_SHA512_Final,
    64
);