|
@@ -9,38 +9,78 @@
|
9
|
9
|
|
10
|
10
|
#include <stdint.h>
|
11
|
11
|
|
12
|
|
-/**
|
13
|
|
- * A message-digest algorithm
|
14
|
|
- *
|
15
|
|
- */
|
16
|
|
-struct digest_algorithm {
|
|
12
|
+/** A cryptographic algorithm */
|
|
13
|
+struct crypto_algorithm {
|
17
|
14
|
/** Algorithm name */
|
18
|
15
|
const char *name;
|
19
|
|
- /** Size of a context for this algorithm */
|
20
|
|
- size_t context_len;
|
21
|
|
- /** Size of a message digest for this algorithm */
|
22
|
|
- size_t digest_len;
|
23
|
|
- /**
|
24
|
|
- * Initialise digest algorithm
|
25
|
|
- *
|
26
|
|
- * @v context Context for digest operations
|
|
16
|
+ /** Context size */
|
|
17
|
+ size_t ctxsize;
|
|
18
|
+ /** Block size */
|
|
19
|
+ size_t blocksize;
|
|
20
|
+ /** Final output size */
|
|
21
|
+ size_t digestsize;
|
|
22
|
+ /** Initialise algorithm
|
|
23
|
+ *
|
|
24
|
+ * @v ctx Context
|
27
|
25
|
*/
|
28
|
|
- void ( * init ) ( void *context );
|
29
|
|
- /**
|
30
|
|
- * Calculate digest over data buffer
|
|
26
|
+ void ( * init ) ( void *ctx );
|
|
27
|
+ /** Set key
|
31
|
28
|
*
|
32
|
|
- * @v context Context for digest operations
|
33
|
|
- * @v data Data buffer
|
34
|
|
- * @v len Length of data buffer
|
|
29
|
+ * @v ctx Context
|
|
30
|
+ * @v key Key
|
|
31
|
+ * @v keylen Key length
|
|
32
|
+ * @ret rc Return status code
|
35
|
33
|
*/
|
36
|
|
- void ( * update ) ( void *context, const void *data, size_t len );
|
37
|
|
- /**
|
38
|
|
- * Finish calculating digest
|
|
34
|
+ int ( * setkey ) ( void *ctx, void *key, size_t keylen );
|
|
35
|
+ /** Encode data
|
|
36
|
+ *
|
|
37
|
+ * @v ctx Context
|
|
38
|
+ * @v src Data to encode
|
|
39
|
+ * @v dst Encoded data, or NULL
|
|
40
|
+ * @v len Length of data
|
|
41
|
+ * @ret rc Return status code
|
39
|
42
|
*
|
40
|
|
- * @v context Context for digest operations
|
41
|
|
- * @v digest Buffer for message digest
|
|
43
|
+ * For a cipher algorithm, the enciphered data should be
|
|
44
|
+ * placed in @c dst. For a digest algorithm, only the digest
|
|
45
|
+ * state should be updated, and @c dst will be NULL.
|
|
46
|
+ *
|
|
47
|
+ * @v len is guaranteed to be a multiple of @c blocksize.
|
42
|
48
|
*/
|
43
|
|
- void ( * finish ) ( void *context, void *digest );
|
|
49
|
+ void ( * encode ) ( void *ctx, const void *src, void *dst,
|
|
50
|
+ size_t len );
|
|
51
|
+ /** Decode data
|
|
52
|
+ *
|
|
53
|
+ * @v ctx Context
|
|
54
|
+ * @v src Data to decode
|
|
55
|
+ * @v dst Decoded data
|
|
56
|
+ * @v len Length of data
|
|
57
|
+ * @ret rc Return status code
|
|
58
|
+ *
|
|
59
|
+ * @v len is guaranteed to be a multiple of @c blocksize.
|
|
60
|
+ */
|
|
61
|
+ void ( * decode ) ( void *ctx, const void *src, void *dst,
|
|
62
|
+ size_t len );
|
|
63
|
+ /** Finalise algorithm
|
|
64
|
+ *
|
|
65
|
+ * @v ctx Context
|
|
66
|
+ * @v out Algorithm final output
|
|
67
|
+ */
|
|
68
|
+ void ( * final ) ( void *ctx, void *out );
|
44
|
69
|
};
|
45
|
70
|
|
|
71
|
+static inline void digest_init ( struct crypto_algorithm *crypto,
|
|
72
|
+ void *ctx ) {
|
|
73
|
+ crypto->init ( ctx );
|
|
74
|
+}
|
|
75
|
+
|
|
76
|
+static inline void digest_update ( struct crypto_algorithm *crypto,
|
|
77
|
+ void *ctx, const void *data, size_t len ) {
|
|
78
|
+ crypto->encode ( ctx, data, NULL, len );
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+static inline void digest_final ( struct crypto_algorithm *crypto,
|
|
82
|
+ void *ctx, void *out ) {
|
|
83
|
+ crypto->final ( ctx, out );
|
|
84
|
+}
|
|
85
|
+
|
46
|
86
|
#endif /* _GPXE_CRYPTO_H */
|