5 Copyright (C) 2001, 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
50 pgp_put_uint32(struct nettle_buffer *buffer, uint32_t i)
52 uint8_t *p = nettle_buffer_space(buffer, 4);
61 pgp_put_uint16(struct nettle_buffer *buffer, unsigned i)
63 uint8_t *p = nettle_buffer_space(buffer, 2);
72 pgp_put_mpi(struct nettle_buffer *buffer, const mpz_t x)
74 unsigned bits = mpz_sizeinbase(x, 2);
75 unsigned octets = (bits + 7) / 8;
79 /* FIXME: What's the correct representation of zero? */
80 if (!pgp_put_uint16(buffer, bits))
83 p = nettle_buffer_space(buffer, octets);
88 nettle_mpz_get_str_256(octets, p, x);
94 pgp_put_string(struct nettle_buffer *buffer,
98 return nettle_buffer_write(buffer, length, s);
103 length_field(unsigned length)
105 if (length < PGP_LENGTH_TWO_OCTET)
107 else if (length < PGP_LENGTH_FOUR_OCTETS)
113 /* bodyLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192
114 * ==> bodyLen - 192 + 192 << 8 = (1st_octet << 8) + (2nd_octet)
117 #define LENGTH_TWO_OFFSET (192 * 255)
120 pgp_put_length(struct nettle_buffer *buffer,
123 if (length < PGP_LENGTH_TWO_OCTETS)
124 return NETTLE_BUFFER_PUTC(buffer, length);
126 else if (length < PGP_LENGTH_FOUR_OCTETS)
127 return pgp_put_uint16(buffer, length + LENGTH_TWO_OFFSET);
129 return NETTLE_BUFFER_PUTC(buffer, 0xff) && pgp_put_uint32(buffer, length);
132 /* Uses the "new" packet format */
134 pgp_put_header(struct nettle_buffer *buffer,
135 unsigned tag, unsigned length)
139 return (NETTLE_BUFFER_PUTC(buffer, 0xC0 | tag)
140 && pgp_put_length(buffer, length));
143 /* FIXME: Should we abort or return error if the length and the field
144 * size don't match? */
146 pgp_put_header_length(struct nettle_buffer *buffer,
147 /* start of the header */
155 length = buffer->size - (start + 2);
156 assert(length < PGP_LENGTH_TWO_OCTETS);
157 buffer->contents[start + 1] = length;
160 length = buffer->size - (start + 3);
161 assert(length < PGP_LENGTH_FOUR_OCTETS
162 && length >= PGP_LENGTH_TWO_OCTETS);
163 WRITE_UINT16(buffer->contents + start + 1, length + LENGTH_TWO_OFFSET);
166 length = buffer->size - (start + 5);
167 WRITE_UINT32(buffer->contents + start + 2, length);
175 pgp_put_userid(struct nettle_buffer *buffer,
179 return (pgp_put_header(buffer, PGP_TAG_USERID, length)
180 && pgp_put_string(buffer, length, name));
184 pgp_sub_packet_start(struct nettle_buffer *buffer)
186 return nettle_buffer_space(buffer, 2) ? buffer->size : 0;
190 pgp_put_sub_packet(struct nettle_buffer *buffer,
195 return (pgp_put_length(buffer, length + 1)
196 && NETTLE_BUFFER_PUTC(buffer, type)
197 && pgp_put_string(buffer, length, data));
201 pgp_sub_packet_end(struct nettle_buffer *buffer, unsigned start)
206 assert(start <= buffer->size);
208 length = buffer->size - start;
209 WRITE_UINT32(buffer->contents + start - 2, length);
213 pgp_put_public_rsa_key(struct nettle_buffer *buffer,
214 const struct rsa_public_key *pub,
217 /* Public key packet, version 4 */
221 /* Size of packet is 16 + the size of e and n */
223 + nettle_mpz_sizeinbase_256_u(pub->n)
224 + nettle_mpz_sizeinbase_256_u(pub->e));
226 if (!pgp_put_header(buffer, PGP_TAG_PUBLIC_KEY, length))
229 start = buffer->size;
231 if (! (pgp_put_header(buffer, PGP_TAG_PUBLIC_KEY,
232 /* Assume that we need two octets */
233 PGP_LENGTH_TWO_OCTETS)
234 && pgp_put_uint32(buffer, 4) /* Version */
235 && pgp_put_uint32(buffer, timestamp)/* Time stamp */
236 && pgp_put_uint32(buffer, PGP_RSA) /* Algorithm */
237 && pgp_put_mpi(buffer, pub->n)
238 && pgp_put_mpi(buffer, pub->e)) )
241 assert(buffer->size == start + length);
247 pgp_put_rsa_sha1_signature(struct nettle_buffer *buffer,
248 const struct rsa_private_key *key,
249 const uint8_t *keyid,
251 struct sha1_ctx *hash)
253 unsigned signature_start = buffer->size;
255 unsigned sub_packet_start;
259 /* Signature packet. The packet could reasonably be both smaller and
260 * larger than 192, so for simplicity we use the 4 octet header
263 if (! (pgp_put_header(buffer, PGP_TAG_SIGNATURE, PGP_LENGTH_FOUR_OCTETS)
264 && NETTLE_BUFFER_PUTC(buffer, 4) /* Version */
265 && NETTLE_BUFFER_PUTC(buffer, type)
266 /* Could also be PGP_RSA_SIGN */
267 && NETTLE_BUFFER_PUTC(buffer, PGP_RSA)
268 && NETTLE_BUFFER_PUTC(buffer, PGP_SHA1)
269 && pgp_put_uint16(buffer, 0))) /* Hashed subpacket length */
272 hash_end = buffer->size;
275 hash_end - signature_start,
276 buffer->contents + signature_start);
278 trailer[0] = 4; trailer[1] = 0xff;
279 WRITE_UINT32(trailer + 2, buffer->size - signature_start);
281 sha1_update(hash, sizeof(trailer), trailer);
284 struct sha1_ctx hcopy = *hash;
285 uint8_t *p = nettle_buffer_space(buffer, 2);
289 sha1_digest(&hcopy, 2, p);
292 /* One "sub-packet" field with the issuer keyid */
293 sub_packet_start = pgp_sub_packet_start(buffer);
294 if (!sub_packet_start)
297 if (pgp_put_sub_packet(buffer, PGP_SUBPACKET_ISSUER_KEY_ID, 8, keyid))
299 pgp_sub_packet_end(buffer, sub_packet_start);
304 if (!(rsa_sha1_sign(key, hash, s)
305 && pgp_put_mpi(buffer, s)))
312 pgp_put_header_length(buffer, signature_start, 4);
317 #define CRC24_INIT 0x0b704ceL
318 #define CRC24_POLY 0x1864cfbL
321 pgp_crc24(unsigned length, const uint8_t *data)
323 uint32_t crc = CRC24_INIT;
326 for (i = 0; i<length; i++)
329 crc ^= ((unsigned) (data[i]) << 16);
330 for (j = 0; j<8; j++)
337 assert(crc < 0x1000000);
342 #define WRITE(buffer, s) (nettle_buffer_write(buffer, strlen((s)), (s)))
344 /* 15 base 64 groups data per line */
345 #define BINARY_PER_LINE 45
346 #define TEXT_PER_LINE BASE64_ENCODE_LENGTH(BINARY_PER_LINE)
349 pgp_armor(struct nettle_buffer *buffer,
354 struct base64_encode_ctx ctx;
356 unsigned crc = pgp_crc24(length, data);
358 base64_encode_init(&ctx);
360 if (! (WRITE(buffer, "BEGIN PGP ")
361 && WRITE(buffer, tag)
362 && WRITE(buffer, "\nComment: Nettle\n\n")))
366 length >= BINARY_PER_LINE;
367 length -= BINARY_PER_LINE, data += BINARY_PER_LINE)
371 = nettle_buffer_space(buffer, TEXT_PER_LINE);
376 done = base64_encode_update(&ctx, p, BINARY_PER_LINE, data);
377 assert(done <= TEXT_PER_LINE);
379 /* FIXME: Create some official way to do this */
380 buffer->size -= (TEXT_PER_LINE - done);
382 if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
388 unsigned text_size = BASE64_ENCODE_LENGTH(length)
389 + BASE64_ENCODE_FINAL_LENGTH;
393 = nettle_buffer_space(buffer, text_size);
397 done = base64_encode_update(&ctx, p, length, data);
398 done += base64_encode_final(&ctx, p + done);
400 /* FIXME: Create some official way to do this */
401 buffer->size -= (text_size - done);
403 if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
407 if (!NETTLE_BUFFER_PUTC(buffer, '='))
411 uint8_t *p = nettle_buffer_space(buffer, 4);
414 base64_encode_group(p, crc);
417 return (WRITE(buffer, "\nBEGIN PGP ")
418 && WRITE(buffer, tag)
419 && NETTLE_BUFFER_PUTC(buffer, '\n'));