Debian Codegraph @

lib/rfc/hmac.scm

raw
1 ;;;
2 ;;; hmac - HMAC keyed-hashing
3 ;;;
4 ;;; Copyright (c) 2002-2003 Kimura Fuyuki, All rights reserved.
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;;; RFC 2104 HMAC: Keyed-Hashing for Message Authentication
35
36 (define-module rfc.hmac
37 (use util.digest)
38 (use gauche.sequence)
39 (use gauche.uvector)
40 (use gauche.mop.typed-slot)
41 (export hmac-to hmac-message-to hmac-message hmac-verify
42
43 ;; Low-level API
44 make-hmac <hmac> hmac-update! hmac-final!
45
46 ;; Deprecated
47 hmac-digest hmac-digest-string))
48 (select-module rfc.hmac)
49
50 (autoload gauche.vport open-input-uvector)
51
52 ;; User API
53 (define (hmac-to target algorithm key)
54 (let1 hmac (make-hmac algorithm key)
55 (generator-for-each
56 (cut hmac-update! hmac <>)
57 (cut read-block 4096))
58 (hmac-final! hmac target)))
59
60 ;; User API
61 (define (hmac-message-to target algorithm key message)
62 (define (thunk) (hmac-to target algorithm key))
63 (etypecase message
64 [<string> (with-input-from-string message thunk)]
65 [<u8vector> (with-input-from-port (open-input-uvector message) thunk)]))
66
67 ;; User API
68 (define (hmac-message algorithm key message)
69 (hmac-message-to <u8vector> algorithm key message))
70
71 ;; User API
72 (define (hmac-verify algorithm given-digest key message)
73 (assume-type given-digest <u8vector>)
74 ;; We compare two u8vectors in constant time
75 (let* ([computed-digest (hmac-message algorithm key message)]
76 [given-len (u8vector-length given-digest)]
77 [computed-len (u8vector-length computed-digest)]
78 [len (min given-len computed-len)])
79 (let loop ([i 0] [ok #t])
80 (if (= i len)
81 (and ok (= given-len computed-len))
82 (loop (+ i 1)
83 (and (= (u8vector-ref given-digest i)
84 (u8vector-ref computed-digest i))
85 ok))))))
86
87 ;; Low-level API
88
89 (define-class <hmac> ()
90 ;; All slots are private
91 ((padded-key :immutable #t :init-keyword :padded-key)
92 (hasher :immutable #t :init-keyword :hasher)))
93
94 ;; TRANSIENT:
95 ;; We don't need initialize method for the new API. This is only
96 ;; to catch old code that calls (make <hmac> :key ...).
97 ;; We'd drop this after a few releases.
98 (define-method initialize ((hmac <hmac>) initargs)
99 (next-method)
100 (let-keywords initargs ((padded-key #f)
101 (hasher #f)
102 (key #f))
103 (when key
104 (warn "Instantiating <hmac> directly is obsoleted.\
105 Use make-hmac instead.\n")
106 ;; A very awkward way to keep backward compatibility.
107 ;; NB: The 'hasher' slot of dummy is not the same as the
108 ;; hasher argument passed to make-hmac.
109 (let1 dummy (make-hmac hasher key)
110 (set! (~ hmac'padded-key) (~ dummy'padded-key))
111 (set! (~ hmac'hasher) (~ dummy'hasher))))))
112
113 (define (make-hmac algorithm key :optional (block-size #f))
114 (assume-type key (</> <u8vector> <string>))
115 (assume-type algorithm <message-digest-algorithm-meta>)
116 (let* ([block-size (or block-size (~ algorithm'hmac-block-size))]
117 [key-v (if (string? key) (string->u8vector key) key)]
118 [key-v (if (> (u8vector-length key-v) block-size)
119 (digest-message-to <u8vector> algorithm key-v)
120 key-v)]
121 [key-v ($ u8vector-append key-v
122 $ make-u8vector (- block-size (u8vector-length key-v)) 0)])
123 (rlet1 hmac (make <hmac>
124 :padded-key key-v
125 :hasher (make algorithm))
126 (let1 ipad (u8vector->string (u8vector-xor key-v #x36))
127 (digest-update! (~ hmac'hasher) ipad)))))
128
129 (define-method hmac-update! ((hmac <hmac>) data)
130 (digest-update! (~ hmac'hasher) data))
131
132 (define-method hmac-final! ((hmac <hmac>) :optional (target <string>))
133 (let* ([opad (u8vector->string (u8vector-xor (~ hmac'padded-key) #x5c))]
134 [inner (digest-final! (~ hmac'hasher))]
135 [outer (digest-message-to target
136 (class-of (~ hmac'hasher))
137 (string-append opad inner))])
138 outer))
139
140 ;; Deprecated
141 (define (hmac-digest :key key hasher)
142 (hmac-to <string> hasher key))
143 ;; Deprecated
144 (define (hmac-digest-string string . args)
145 (with-input-from-string string (cut apply hmac-digest args)))