Debian Codegraph @

ext/digest/sha.scm

raw
1 ;;;
2 ;;; sha - SHA-1/SHA-224/SHA-256/SHA-384/SHA-512 message-digest
3 ;;;
4 ;;; Copyright (c) 2002-2003 Kimura Fuyuki, All rights reserved.
5 ;;; Copyright (c) 2008-2024 Shiro Kawai <[email protected]>
6 ;;;
7 ;;; Redistribution and use in source and binary forms, with or without
8 ;;; modification, are permitted provided that the following conditions
9 ;;; are met:
10 ;;;
11 ;;; 1. Redistributions of source code must retain the above copyright
12 ;;; notice, this list of conditions and the following disclaimer.
13 ;;;
14 ;;; 2. Redistributions in binary form must reproduce the above copyright
15 ;;; notice, this list of conditions and the following disclaimer in the
16 ;;; documentation and/or other materials provided with the distribution.
17 ;;;
18 ;;; 3. Neither the name of the authors nor the names of its contributors
19 ;;; may be used to endorse or promote products derived from this
20 ;;; software without specific prior written permission.
21 ;;;
22 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ;;;
34
35
36 ;;; Cf. RFC 3174 US Secure Hash Algorithm 1 (SHA1)
37
38 (define-module rfc.sha
39 (use gauche.uvector)
40 (extend util.digest)
41 (export <sha1> sha1-digest sha1-digest-string
42 <sha224> sha224-digest sha224-digest-string
43 <sha256> sha256-digest sha256-digest-string
44 <sha384> sha384-digest sha384-digest-string
45 <sha512> sha512-digest sha512-digest-string
46 <sha3-224> sha3-224-digest sha3-224-digest-string
47 <sha3-256> sha3-256-digest sha3-256-digest-string
48 <sha3-384> sha3-384-digest sha3-384-digest-string
49 <sha3-512> sha3-512-digest sha3-512-digest-string
50 ))
51 (select-module rfc.sha)
52
53 ;;;
54 ;;; High-level API
55 ;;;
56
57 (define-constant *sha-unit-len* 4096)
58
59 (define (gen-digest init update end)
60 (^[] (let ([ctx (make <sha-context>)]
61 [buf (make-u8vector *sha-unit-len*)])
62 (init ctx)
63 (generator-for-each
64 (^x (update ctx x))
65 (^[] (let1 count (read-block! buf)
66 (cond [(eof-object? count) count]
67 [(< count *sha-unit-len*)
68 (uvector-alias <u8vector> buf 0 count)]
69 [else buf]))))
70 (end ctx))))
71
72 (define sha1-digest (gen-digest %sha1-init %sha1-update %sha1-final))
73 (define sha224-digest (gen-digest %sha224-init %sha224-update %sha224-final))
74 (define sha256-digest (gen-digest %sha256-init %sha256-update %sha256-final))
75 (define sha384-digest (gen-digest %sha384-init %sha384-update %sha384-final))
76 (define sha512-digest (gen-digest %sha512-init %sha512-update %sha512-final))
77 (define sha3-224-digest (gen-digest %sha3-224-init %sha3-224-update %sha3-224-final))
78 (define sha3-256-digest (gen-digest %sha3-256-init %sha3-256-update %sha3-256-final))
79 (define sha3-384-digest (gen-digest %sha3-384-init %sha3-384-update %sha3-384-final))
80 (define sha3-512-digest (gen-digest %sha3-512-init %sha3-512-update %sha3-512-final))
81
82 (define (sha1-digest-string s) (with-input-from-string s sha1-digest))
83 (define (sha224-digest-string s) (with-input-from-string s sha224-digest))
84 (define (sha256-digest-string s) (with-input-from-string s sha256-digest))
85 (define (sha384-digest-string s) (with-input-from-string s sha384-digest))
86 (define (sha512-digest-string s) (with-input-from-string s sha512-digest))
87 (define (sha3-224-digest-string s) (with-input-from-string s sha3-224-digest))
88 (define (sha3-256-digest-string s) (with-input-from-string s sha3-256-digest))
89 (define (sha3-384-digest-string s) (with-input-from-string s sha3-384-digest))
90 (define (sha3-512-digest-string s) (with-input-from-string s sha3-512-digest))
91
92 ;;;
93 ;;; Digest framework
94 ;;;
95
96 (define-macro (define-framework n block-size)
97 (let ([meta (string->symbol #"<sha~|n|-meta>")]
98 [cls (string->symbol #"<sha~|n|>")]
99 [init (string->symbol #"%sha~|n|-init")]
100 [update (string->symbol #"%sha~|n|-update")]
101 [final (string->symbol #"%sha~|n|-final")]
102 [digest (string->symbol #"sha~|n|-digest")])
103 `(begin
104 (define-class ,meta (<message-digest-algorithm-meta>) ())
105 (define-class ,cls (<message-digest-algorithm>)
106 (context)
107 :metaclass ,meta
108 :hmac-block-size ,block-size)
109 (define-method initialize ((self ,cls) initargs)
110 (next-method)
111 (let1 ctx (make <sha-context>)
112 (,init ctx)
113 (slot-set! self 'context ctx)))
114 (define-method digest-update! ((self ,cls) data)
115 (,update (slot-ref self'context) data))
116 (define-method digest-final! ((self ,cls))
117 (,final (slot-ref self'context)))
118 (define-method digest ((class ,meta))
119 (,digest)))))
120
121 (define-framework 1 64)
122 (define-framework 224 64)
123 (define-framework 256 64)
124 (define-framework 384 128)
125 (define-framework 512 128)
126 (define-framework 256 64)
127 (define-framework 384 128)
128 (define-framework 512 128)
129 (define-framework |3-224| 64)
130 (define-framework |3-256| 64)
131 (define-framework |3-384| 128)
132 (define-framework |3-512| 128)
133
134 ;;;
135 ;;; Low-level bindings
136 ;;;
137
138 (inline-stub
139 (declcode
140 (.include <gauche/priv/configP.h>)
141
142 ;; customization for sha2.h
143 (.define SHA2_USE_INTTYPES_H) ; use uintXX_t
144 (.include "sha2.h")
145
146 (.include "sha3.h")
147
148 (.define LIBGAUCHE_EXT_BODY)
149 (.include <gauche/extern.h>) ; fix SCM_EXTERN in SCM_CLASS_DECL
150
151 (define-ctype ScmShaContext
152 :: (.struct
153 (SCM_HEADER ::||
154 version::int ; 0 (uninitialized), 2 or 3
155 ||::(.union
156 (v2::SHA_CTX
157 v3::sha3_context)))))
158 )
159
160 (define-cclass <sha-context> :private
161 ScmShaContext* "Scm_ShaContextClass" ()
162 ()
163 [allocator
164 (let* ([ctx :: ScmShaContext* (SCM_NEW_INSTANCE ScmShaContext klass)])
165 (cast void initargs) ; suppress unused var warning
166 (set! (-> ctx version) 0) ; uninitialized
167 (return (SCM_OBJ ctx)))])
168
169 (define-cfn check-version (ctx::ScmShaContext* vers::int) ::void :static
170 (when (== (-> ctx version) 0)
171 (Scm_Error "%S is uninitialized" ctx))
172 (when (!= (-> ctx version) vers)
173 (Scm_Error "%S is initialized with different version" ctx)))
174
175 (define-cproc %sha1-init (ctx::<sha-context>) ::<void>
176 (set! (-> ctx version) 2)
177 (SHA1_Init (& (-> ctx v2))))
178 (define-cproc %sha224-init (ctx::<sha-context>) ::<void>
179 (set! (-> ctx version) 2)
180 (SHA224_Init (& (-> ctx v2))))
181 (define-cproc %sha256-init (ctx::<sha-context>) ::<void>
182 (set! (-> ctx version) 2)
183 (SHA256_Init (& (-> ctx v2))))
184 (define-cproc %sha384-init (ctx::<sha-context>) ::<void>
185 (set! (-> ctx version) 2)
186 (SHA384_Init (& (-> ctx v2))))
187 (define-cproc %sha512-init (ctx::<sha-context>) ::<void>
188 (set! (-> ctx version) 2)
189 (SHA512_Init (& (-> ctx v2))))
190 (define-cproc %sha3-224-init (ctx::<sha-context>) ::<void>
191 (set! (-> ctx version) 3)
192 (sha3_Init224 (& (-> ctx v3))))
193 (define-cproc %sha3-256-init (ctx::<sha-context>) ::<void>
194 (set! (-> ctx version) 3)
195 (sha3_Init256 (& (-> ctx v3))))
196 (define-cproc %sha3-384-init (ctx::<sha-context>) ::<void>
197 (set! (-> ctx version) 3)
198 (sha3_Init384 (& (-> ctx v3))))
199 (define-cproc %sha3-512-init (ctx::<sha-context>) ::<void>
200 (set! (-> ctx version) 3)
201 (sha3_Init512 (& (-> ctx v3))))
202
203 (define-cise-stmt common-update
204 [(_ update ctx vers data)
205 `(cond
206 [(SCM_U8VECTORP ,data)
207 (,update (& (-> ,ctx ,vers))
208 (cast (const unsigned char*)
209 (SCM_UVECTOR_ELEMENTS (SCM_U8VECTOR ,data)))
210 (SCM_U8VECTOR_SIZE (SCM_U8VECTOR ,data)))]
211 [(SCM_STRINGP ,data)
212 (let* ([b::(const ScmStringBody*) (SCM_STRING_BODY ,data)])
213 (,update (& (-> ,ctx ,vers))
214 (cast (const unsigned char*) (SCM_STRING_BODY_START b))
215 (SCM_STRING_BODY_SIZE b)))]
216 [else (SCM_TYPE_ERROR ,data "u8vector or string")])])
217
218 (define-cproc %sha1-update (ctx::<sha-context> data) ::<void>
219 (check-version ctx 2)
220 (common-update SHA1_Update ctx v2 data))
221 (define-cproc %sha224-update (ctx::<sha-context> data) ::<void>
222 (check-version ctx 2)
223 (common-update SHA224_Update ctx v2 data))
224 (define-cproc %sha256-update (ctx::<sha-context> data) ::<void>
225 (check-version ctx 2)
226 (common-update SHA256_Update ctx v2 data))
227 (define-cproc %sha384-update (ctx::<sha-context> data) ::<void>
228 (check-version ctx 2)
229 (common-update SHA384_Update ctx v2 data))
230 (define-cproc %sha512-update (ctx::<sha-context> data) ::<void>
231 (check-version ctx 2)
232 (common-update SHA512_Update ctx v2 data))
233 (define-cproc %sha3-224-update (ctx::<sha-context> data) ::<void>
234 (check-version ctx 3)
235 (common-update Scm_SHA3_Update ctx v3 data))
236 (define-cproc %sha3-256-update (ctx::<sha-context> data) ::<void>
237 (check-version ctx 3)
238 (common-update Scm_SHA3_Update ctx v3 data))
239 (define-cproc %sha3-384-update (ctx::<sha-context> data) ::<void>
240 (check-version ctx 3)
241 (common-update Scm_SHA3_Update ctx v3 data))
242 (define-cproc %sha3-512-update (ctx::<sha-context> data) ::<void>
243 (check-version ctx 3)
244 (common-update Scm_SHA3_Update ctx v3 data))
245
246 (define-cise-stmt common-final
247 [(_ final ctx vers size)
248 `(let* ([digest::(.array (unsigned char) (,size))])
249 (,final digest (& (-> ,ctx ,vers)))
250 (return (Scm_MakeString (cast (const char*) digest)
251 ,size ,size
252 (logior SCM_STRING_INCOMPLETE
253 SCM_STRING_COPYING))))])
254
255 (define-cproc %sha1-final (ctx::<sha-context>)
256 (check-version ctx 2)
257 (common-final SHA1_Final ctx v2 SHA1_DIGEST_LENGTH))
258 (define-cproc %sha224-final (ctx::<sha-context>)
259 (check-version ctx 2)
260 (common-final SHA224_Final ctx v2 SHA224_DIGEST_LENGTH))
261 (define-cproc %sha256-final (ctx::<sha-context>)
262 (check-version ctx 2)
263 (common-final SHA256_Final ctx v2 SHA256_DIGEST_LENGTH))
264 (define-cproc %sha384-final (ctx::<sha-context>)
265 (check-version ctx 2)
266 (common-final SHA384_Final ctx v2 SHA384_DIGEST_LENGTH))
267 (define-cproc %sha512-final (ctx::<sha-context>)
268 (check-version ctx 2)
269 (common-final SHA512_Final ctx v2 SHA512_DIGEST_LENGTH))
270
271 (define-cfn sha3_224_finalize (buf::uint8_t* ctx::sha3_context*) ::void :static
272 (let* ([r::uint8_t* (cast uint8_t* (sha3_Finalize ctx))])
273 (memcpy buf r SHA224_DIGEST_LENGTH)))
274 (define-cfn sha3_256_finalize (buf::uint8_t* ctx::sha3_context*) ::void :static
275 (let* ([r::uint8_t* (cast uint8_t* (sha3_Finalize ctx))])
276 (memcpy buf r SHA256_DIGEST_LENGTH)))
277 (define-cfn sha3_384_finalize (buf::uint8_t* ctx::sha3_context*) ::void :static
278 (let* ([r::uint8_t* (cast uint8_t* (sha3_Finalize ctx))])
279 (memcpy buf r SHA384_DIGEST_LENGTH)))
280 (define-cfn sha3_512_finalize (buf::uint8_t* ctx::sha3_context*) ::void :static
281 (let* ([r::uint8_t* (cast uint8_t* (sha3_Finalize ctx))])
282 (memcpy buf r SHA512_DIGEST_LENGTH)))
283
284 (define-cproc %sha3-224-final (ctx::<sha-context>)
285 (check-version ctx 3)
286 (common-final sha3_224_finalize ctx v3 SHA224_DIGEST_LENGTH))
287 (define-cproc %sha3-256-final (ctx::<sha-context>)
288 (check-version ctx 3)
289 (common-final sha3_256_finalize ctx v3 SHA256_DIGEST_LENGTH))
290 (define-cproc %sha3-384-final (ctx::<sha-context>)
291 (check-version ctx 3)
292 (common-final sha3_384_finalize ctx v3 SHA384_DIGEST_LENGTH))
293 (define-cproc %sha3-512-final (ctx::<sha-context>)
294 (check-version ctx 3)
295 (common-final sha3_512_finalize ctx v3 SHA512_DIGEST_LENGTH))
296 )