aboutsummaryrefslogtreecommitdiff
path: root/hmac.rs
blob: 1e71ed1be51110294dcf3804e7c3decdeb9af8da (plain) (blame)
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
/*
 * Copyright 2013 Jack Lloyd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use hash::*;
use std::{libc,ptr,vec};

#[allow(non_camel_case_types)]
pub struct HMAC_CTX {
    md: EVP_MD,
    md_ctx: EVP_MD_CTX,
    i_ctx: EVP_MD_CTX,
    o_ctx: EVP_MD_CTX,
    key_length: libc::c_uint,
    key: [libc::c_uchar, ..128]
}

#[link_args = "-lcrypto"]
#[abi = "cdecl"]
extern {
    fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD);

    fn HMAC_Update(ctx: *mut HMAC_CTX, input: *u8, len: libc::c_uint);

    fn HMAC_Final(ctx: *mut HMAC_CTX, output: *mut u8, len: *mut libc::c_uint);
}

pub struct HMAC {
    priv ctx: HMAC_CTX,
    priv len: uint,
}

pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
    unsafe {

        let (evp, mdlen) = evpmd(ht);

        let mut ctx : HMAC_CTX = HMAC_CTX {
            md: ptr::null(),
            md_ctx: ptr::null(),
            i_ctx: ptr::null(),
            o_ctx: ptr::null(),
            key_length: 0,
            key: [0u8, .. 128]
        };

        HMAC_CTX_init(&mut ctx,
                                 vec::raw::to_ptr(key),
                                 key.len() as libc::c_int,
                                 evp);

        HMAC { ctx: ctx, len: mdlen }
    }
}

impl HMAC {
    pub fn update(&mut self, data: &[u8]) {
        unsafe {
            do data.as_imm_buf |pdata, len| {
                HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
            }
        }
    }

    pub fn final(&mut self) -> ~[u8] {
        unsafe {
            let mut res = vec::from_elem(self.len, 0u8);
            let mut outlen: libc::c_uint = 0;
            do res.as_mut_buf |pres, _len| {
                HMAC_Final(&mut self.ctx, pres, &mut outlen);
                assert!(self.len == outlen as uint)
            }
            res
        }
    }
}

fn main() {
    let mut h = HMAC(SHA512, ~[00u8]);

    h.update([00u8]);

    println(fmt!("%?", h.final()))
}