chiark / gitweb /
server/keymgmt.c: Track and find keys by their 32-bit IDs.
[tripe] / server / chal.c
CommitLineData
37941236 1/* -*-c-*-
37941236 2 *
3 * Cryptographic challenges
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
37941236 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
e04c2d50 16 *
11ad66c2
MW
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
e04c2d50 21 *
37941236 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
37941236 24 */
25
26/*----- Header files ------------------------------------------------------*/
27
28#include "tripe.h"
29
30/*----- Static variables --------------------------------------------------*/
31
21707356 32static bulkchal *bchal;
37941236 33static uint32 oseq;
34static seqwin iseq;
35
21707356 36/*----- Challenges --------------------------------------------------------*/
37941236 37
38/* --- @c_genkey@ --- *
39 *
40 * Arguments: ---
41 *
42 * Returns: ---
43 *
44 * Use: Generates a new challenge key.
45 */
46
47static void c_genkey(void)
48{
b8f727eb
MW
49 bulkalgs *bulk = master->algs.bulk;
50 if (bchal && bchal->ops == bulk->ops && oseq < 0x07ffffff) return;
21707356 51 if (bchal) bchal->ops->freechal(bchal);
b8f727eb
MW
52 bchal = bulk->ops->genchal(bulk);
53 bchal->ops = bulk->ops;
37941236 54 oseq = 0;
55 seq_reset(&iseq);
37941236 56}
57
58/* --- @c_new@ --- *
59 *
3deadf73
MW
60 * Arguments: @const void *m@ = pointer to associated message, or null
61 * @size_t msz@ = length of associated message
62 * @buf *b@ = where to put the challenge
37941236 63 *
64 * Returns: Zero if OK, nonzero on error.
65 *
66 * Use: Issues a new challenge.
67 */
68
3deadf73 69int c_new(const void *m, size_t msz, buf *b)
37941236 70{
3deadf73
MW
71 const octet *p;
72 octet *t;
73 int rc;
37941236 74
75 c_genkey();
76 p = BCUR(b);
3deadf73
MW
77 if (buf_putu32(b, oseq) || (t = buf_get(b, bchal->tagsz)) == 0)
78 { rc = -1; goto done; }
79 if (bchal->ops->chaltag(bchal, m, msz, oseq, t)) { rc = -1; goto done; }
37941236 80 IF_TRACING(T_CHAL, {
3deadf73
MW
81 trace(T_CHAL, "chal: issuing challenge %lu", (unsigned long)oseq);
82 if (msz) trace_block(T_CRYPTO, "chal: message block", m, msz);
37941236 83 trace_block(T_CRYPTO, "chal: challenge block", p, BCUR(b) - p);
84 })
3deadf73
MW
85 rc = 0;
86done:
87 oseq++;
88 return (rc);
37941236 89}
90
91/* --- @c_check@ --- *
92 *
3deadf73
MW
93 * Arguments: @const void *m@ = pointer to associated message, or null
94 * @size_t msz@ = length of associated message
95 * @buf *b@ = where to find the challenge
37941236 96 *
97 * Returns: Zero if OK, nonzero if it didn't work.
98 *
99 * Use: Checks a challenge. On failure, the buffer is broken.
100 */
101
3deadf73 102int c_check(const void *m, size_t msz, buf *b)
37941236 103{
3deadf73 104 const octet *p, *t;
37941236 105 uint32 seq;
37941236 106
21707356 107 if (!bchal) {
c70a7c5c
MW
108 a_warn("CHAL", "impossible-challenge", A_END);
109 goto fail;
110 }
3deadf73
MW
111 p = BCUR(b);
112 if (buf_getu32(b, &seq) || (t = buf_get(b, bchal->tagsz)) == 0) {
f43df819 113 a_warn("CHAL", "invalid-challenge", A_END);
37941236 114 goto fail;
115 }
3deadf73
MW
116 IF_TRACING(T_CHAL, {
117 trace(T_CHAL, "chal: checking challenge, seq = %lu", (unsigned long)seq);
118 if (msz) trace_block(T_CRYPTO, "chal: message block", m, msz);
119 trace_block(T_CRYPTO, "chal: check challenge", p, BCUR(b) - p);
120 })
121 if (bchal->ops->chalvrf(bchal, m, msz, seq, t)) {
f43df819 122 a_warn("CHAL", "incorrect-tag", A_END);
37941236 123 goto fail;
124 }
3deadf73
MW
125 if (seq_check(&iseq, seq, "CHAL")) goto fail;
126 T( trace(T_CHAL, "chal: challenge ok"); )
37941236 127 return (0);
128
129fail:
130 buf_break(b);
131 return (-1);
132}
133
134/*----- That's all, folks -------------------------------------------------*/