|
| 1 | +// Copyright 2011 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// This package implements the various OpenPGP string-to-key transforms as |
| 6 | +// specified in RFC 4800 section 3.7.1. |
| 7 | +package s2k |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/md5" |
| 11 | + "crypto/openpgp/error" |
| 12 | + "crypto/ripemd160" |
| 13 | + "crypto/sha1" |
| 14 | + "crypto/sha256" |
| 15 | + "crypto/sha512" |
| 16 | + "hash" |
| 17 | + "io" |
| 18 | + "os" |
| 19 | +) |
| 20 | + |
| 21 | +// Simple writes to out the result of computing the Simple S2K function (RFC |
| 22 | +// 4880, section 3.7.1.1) using the given hash and input passphrase. |
| 23 | +func Simple(out []byte, h hash.Hash, in []byte) { |
| 24 | + Salted(out, h, in, nil) |
| 25 | +} |
| 26 | + |
| 27 | +var zero [1]byte |
| 28 | + |
| 29 | +// Salted writes to out the result of computing the Salted S2K function (RFC |
| 30 | +// 4880, section 3.7.1.2) using the given hash, input passphrase and salt. |
| 31 | +func Salted(out []byte, h hash.Hash, in []byte, salt []byte) { |
| 32 | + done := 0 |
| 33 | + |
| 34 | + for i := 0; done < len(out); i++ { |
| 35 | + h.Reset() |
| 36 | + for j := 0; j < i; j++ { |
| 37 | + h.Write(zero[:]) |
| 38 | + } |
| 39 | + h.Write(salt) |
| 40 | + h.Write(in) |
| 41 | + n := copy(out[done:], h.Sum()) |
| 42 | + done += n |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Iterated writes to out the result of computing the Iterated and Salted S2K |
| 47 | +// function (RFC 4880, section 3.7.1.3) using the given hash, input passphrase, |
| 48 | +// salt and iteration count. |
| 49 | +func Iterated(out []byte, h hash.Hash, in []byte, salt []byte, count int) { |
| 50 | + combined := make([]byte, len(in)+len(salt)) |
| 51 | + copy(combined, salt) |
| 52 | + copy(combined[len(salt):], in) |
| 53 | + |
| 54 | + if count < len(combined) { |
| 55 | + count = len(combined) |
| 56 | + } |
| 57 | + |
| 58 | + done := 0 |
| 59 | + for i := 0; done < len(out); i++ { |
| 60 | + h.Reset() |
| 61 | + for j := 0; j < i; j++ { |
| 62 | + h.Write(zero[:]) |
| 63 | + } |
| 64 | + written := 0 |
| 65 | + for written < count { |
| 66 | + if written+len(combined) > count { |
| 67 | + todo := count - written |
| 68 | + h.Write(combined[:todo]) |
| 69 | + written = count |
| 70 | + } else { |
| 71 | + h.Write(combined) |
| 72 | + written += len(combined) |
| 73 | + } |
| 74 | + } |
| 75 | + n := copy(out[done:], h.Sum()) |
| 76 | + done += n |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Parse reads a binary specification for a string-to-key transformation from r |
| 81 | +// and returns a function which performs that transform. |
| 82 | +func Parse(r io.Reader) (f func(out, in []byte), err os.Error) { |
| 83 | + var buf [9]byte |
| 84 | + |
| 85 | + _, err = io.ReadFull(r, buf[:2]) |
| 86 | + if err != nil { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + h := hashFuncFromType(buf[1]) |
| 91 | + if h == nil { |
| 92 | + return nil, error.UnsupportedError("hash for S2K function") |
| 93 | + } |
| 94 | + |
| 95 | + switch buf[0] { |
| 96 | + case 1: |
| 97 | + f := func(out, in []byte) { |
| 98 | + Simple(out, h, in) |
| 99 | + } |
| 100 | + return f, nil |
| 101 | + case 2: |
| 102 | + _, err := io.ReadFull(r, buf[:8]) |
| 103 | + if err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + f := func(out, in []byte) { |
| 107 | + Salted(out, h, in, buf[:8]) |
| 108 | + } |
| 109 | + return f, nil |
| 110 | + case 3: |
| 111 | + _, err := io.ReadFull(r, buf[:9]) |
| 112 | + if err != nil { |
| 113 | + return |
| 114 | + } |
| 115 | + count := (16 + int(buf[8]&15)) << (uint32(buf[8]>>4) + 6) |
| 116 | + f := func(out, in []byte) { |
| 117 | + Iterated(out, h, in, buf[:8], count) |
| 118 | + } |
| 119 | + return f, nil |
| 120 | + } |
| 121 | + |
| 122 | + return nil, error.UnsupportedError("S2K function") |
| 123 | +} |
| 124 | + |
| 125 | +// hashFuncFromType returns a hash.Hash which corresponds to the given hash |
| 126 | +// type byte. See RFC 4880, section 9.4. |
| 127 | +func hashFuncFromType(hashType byte) hash.Hash { |
| 128 | + switch hashType { |
| 129 | + case 1: |
| 130 | + return md5.New() |
| 131 | + case 2: |
| 132 | + return sha1.New() |
| 133 | + case 3: |
| 134 | + return ripemd160.New() |
| 135 | + case 8: |
| 136 | + return sha256.New() |
| 137 | + case 9: |
| 138 | + return sha512.New384() |
| 139 | + case 10: |
| 140 | + return sha512.New() |
| 141 | + case 11: |
| 142 | + return sha256.New224() |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
0 commit comments