|
| 1 | +;;; -*- mode:scheme; coding:utf-8; -*- |
| 2 | +;;; |
| 3 | +;;; sagittarius/crypto/pkix/scrypt.scm - scrypt for PKCS |
| 4 | +;;; |
| 5 | +;;; Copyright (c) 2022 Takashi Kato <ktakashi@ymail.com> |
| 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 | +;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | +;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | +;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | +;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | +;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 24 | +;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | +;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | +;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | +;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +;;; |
| 30 | + |
| 31 | +;; ref |
| 32 | +;; - https://datatracker.ietf.org/doc/html/rfc7914 |
| 33 | +#!nounbound |
| 34 | +(library (sagittarius crypto pkcs scrypt) |
| 35 | + (export *pbes:scrypt* |
| 36 | + pkcs-scrypt-params? <pkcs-scrypt-params> |
| 37 | + make-pkcs-scrypt-params |
| 38 | + pkcs-scrypt-params-salt |
| 39 | + pkcs-scrypt-params-cost-parameter |
| 40 | + pkcs-scrypt-params-block-size |
| 41 | + pkcs-scrypt-params-parallelization-parameter |
| 42 | + pkcs-scrypt-params-key-length |
| 43 | + ) |
| 44 | + (import (rnrs) |
| 45 | + (clos user) |
| 46 | + (sagittarius crypto asn1) |
| 47 | + (sagittarius crypto asn1 modules) |
| 48 | + (sagittarius crypto kdfs scrypt) |
| 49 | + (sagittarius crypto pkcs modules scrypt) |
| 50 | + (sagittarius crypto pkcs algorithms) |
| 51 | + (sagittarius crypto pkcs modules pbes) |
| 52 | + (sagittarius crypto pkcs pbes) ;; for <pkcs-pbes2-params> |
| 53 | + (sagittarius crypto pkix algorithms) |
| 54 | + (sagittarius crypto pkix modules x509) |
| 55 | + (sagittarius combinators)) |
| 56 | +(define sid der-object-identifier->oid-string) |
| 57 | +(define (make-slot-ref getter conv) (lambda (o) (conv (getter o)))) |
| 58 | +(define-class <pkcs-scrypt-params> |
| 59 | + (<asn1-encodable-container> <x509-algorithm-parameters>) |
| 60 | + ((salt :allocation :virtual :cached #t |
| 61 | + :slot-ref (make-slot-ref |
| 62 | + (.$ scrypt-params-salt asn1-encodable-container-c) |
| 63 | + der-octet-string->bytevector) |
| 64 | + :reader pkcs-scrypt-params-salt) |
| 65 | + (cost-parameter :allocation :virtual :cached #t |
| 66 | + :slot-ref (make-slot-ref |
| 67 | + (.$ scrypt-params-cost-parameter asn1-encodable-container-c) |
| 68 | + der-integer->integer) |
| 69 | + :reader pkcs-scrypt-params-cost-parameter) |
| 70 | + (block-size :allocation :virtual :cached #t |
| 71 | + :slot-ref (make-slot-ref |
| 72 | + (.$ scrypt-params-block-size asn1-encodable-container-c) |
| 73 | + der-integer->integer) |
| 74 | + :reader pkcs-scrypt-params-block-size) |
| 75 | + (parallelization-parameter :allocation :virtual :cached #t |
| 76 | + :slot-ref (make-slot-ref |
| 77 | + (.$ scrypt-params-parallelization-parameter |
| 78 | + asn1-encodable-container-c) |
| 79 | + der-integer->integer) |
| 80 | + :reader pkcs-scrypt-params-parallelization-parameter) |
| 81 | + (key-length :allocation :virtual :cached #t |
| 82 | + :slot-ref (make-slot-ref |
| 83 | + (.$ scrypt-params-key-length |
| 84 | + asn1-encodable-container-c) |
| 85 | + (lambda (v) (and v (der-integer->integer v)))) |
| 86 | + :reader pkcs-scrypt-params-key-length))) |
| 87 | +(define (pkcs-scrypt-params? o) (is-a? o <pkcs-scrypt-params>)) |
| 88 | +(define (make-pkcs-scrypt-params (salt bytevector?) |
| 89 | + (cost-parameter integer?) |
| 90 | + (block-size integer?) |
| 91 | + (parallelization-parameter integer?) |
| 92 | + :key (key-length #f)) |
| 93 | + (make <pkcs-scrypt-params> |
| 94 | + :c (make <scrypt-params> |
| 95 | + :salt (bytevector->der-octet-string salt) |
| 96 | + :cost-parameter (integer->der-integer cost-parameter) |
| 97 | + :block-size (integer->der-integer block-size) |
| 98 | + :parallelization-parameter (integer->der-integer parallelization-parameter) |
| 99 | + :key-length (and key-length (integer->der-integer key-length))))) |
| 100 | + |
| 101 | +(define-method oid->x509-algorithm-parameters-types |
| 102 | + ((oid (equal (sid *pbes:scrypt*)))) |
| 103 | + (values <pkcs-scrypt-params> <scrypt-params>)) |
| 104 | +(define-method oid->kdf ((oid (equal (sid *pbes:scrypt*))) kdf-param pbes2-param) |
| 105 | + (define enc (pkcs-pbes2-params-encryption-scheme pbes2-param)) |
| 106 | + (define dk-len (or (pkcs-scrypt-params-key-length kdf-param) |
| 107 | + (encryption-scheme->key-length enc))) |
| 108 | + (lambda (key . ignore) |
| 109 | + (scrypt (string->utf8 key) |
| 110 | + (pkcs-scrypt-params-salt kdf-param) |
| 111 | + (pkcs-scrypt-params-cost-parameter kdf-param) |
| 112 | + (pkcs-scrypt-params-block-size kdf-param) |
| 113 | + (pkcs-scrypt-params-parallelization-parameter kdf-param) |
| 114 | + dk-len))) |
| 115 | +) |
0 commit comments