1 /*
2 * Argon2 reference source code package - reference C implementations
3 *
4 * Copyright 2015
5 * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6 *
7 * You may use this work under the terms of a Creative Commons CC0 1.0
8 * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9 * these licenses can be found at:
10 *
11 * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
12 * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * You should have received a copy of both of these licenses along with this
15 * software. If not, they may be obtained at the above URLs.
16 */
17
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "argon2.h"
23 #include "core.c"
24
cryptonite_argon2_ctx(argon2_context * context,argon2_type type)25 int cryptonite_argon2_ctx(argon2_context *context, argon2_type type) {
26 /* 1. Validate all inputs */
27 int result = validate_inputs(context);
28 uint32_t memory_blocks, segment_length;
29 argon2_instance_t instance;
30
31 if (ARGON2_OK != result) {
32 return result;
33 }
34
35 if (Argon2_d != type && Argon2_i != type && Argon2_id != type) {
36 return ARGON2_INCORRECT_TYPE;
37 }
38
39 /* 2. Align memory size */
40 /* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
41 memory_blocks = context->m_cost;
42
43 if (memory_blocks < 2 * ARGON2_SYNC_POINTS * context->lanes) {
44 memory_blocks = 2 * ARGON2_SYNC_POINTS * context->lanes;
45 }
46
47 segment_length = memory_blocks / (context->lanes * ARGON2_SYNC_POINTS);
48 /* Ensure that all segments have equal length */
49 memory_blocks = segment_length * (context->lanes * ARGON2_SYNC_POINTS);
50
51 instance.version = context->version;
52 instance.memory = NULL;
53 instance.passes = context->t_cost;
54 instance.memory_blocks = memory_blocks;
55 instance.segment_length = segment_length;
56 instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
57 instance.lanes = context->lanes;
58 instance.threads = context->threads;
59 instance.type = type;
60
61 /* 3. Initialization: Hashing inputs, allocating memory, filling first
62 * blocks
63 */
64 result = initialize(&instance, context);
65
66 if (ARGON2_OK != result) {
67 return result;
68 }
69
70 /* 4. Filling memory */
71 result = fill_memory_blocks(&instance);
72
73 if (ARGON2_OK != result) {
74 return result;
75 }
76 /* 5. Finalization */
77 finalize(context, &instance);
78
79 return ARGON2_OK;
80 }
81
cryptonite_argon2_hash(const uint32_t t_cost,const uint32_t m_cost,const uint32_t parallelism,const void * pwd,const size_t pwdlen,const void * salt,const size_t saltlen,void * hash,const size_t hashlen,argon2_type type,const uint32_t version)82 int cryptonite_argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
83 const uint32_t parallelism, const void *pwd,
84 const size_t pwdlen, const void *salt, const size_t saltlen,
85 void *hash, const size_t hashlen, argon2_type type,
86 const uint32_t version){
87
88 argon2_context context;
89 int result;
90 uint8_t *out;
91
92 if (hashlen > ARGON2_MAX_OUTLEN) {
93 return ARGON2_OUTPUT_TOO_LONG;
94 }
95
96 if (hashlen < ARGON2_MIN_OUTLEN) {
97 return ARGON2_OUTPUT_TOO_SHORT;
98 }
99
100 out = malloc(hashlen);
101 if (!out) {
102 return ARGON2_MEMORY_ALLOCATION_ERROR;
103 }
104
105 context.out = (uint8_t *)out;
106 context.outlen = (uint32_t)hashlen;
107 context.pwd = CONST_CAST(uint8_t *)pwd;
108 context.pwdlen = (uint32_t)pwdlen;
109 context.salt = CONST_CAST(uint8_t *)salt;
110 context.saltlen = (uint32_t)saltlen;
111 context.secret = NULL;
112 context.secretlen = 0;
113 context.ad = NULL;
114 context.adlen = 0;
115 context.t_cost = t_cost;
116 context.m_cost = m_cost;
117 context.lanes = parallelism;
118 context.threads = parallelism;
119 context.allocate_cbk = NULL;
120 context.free_cbk = NULL;
121 context.flags = ARGON2_DEFAULT_FLAGS;
122 context.version = version;
123
124 result = cryptonite_argon2_ctx(&context, type);
125
126 if (result != ARGON2_OK) {
127 clear_internal_memory(out, hashlen);
128 free(out);
129 return result;
130 }
131
132 /* if raw hash requested, write it */
133 if (hash) {
134 memcpy(hash, out, hashlen);
135 }
136
137 clear_internal_memory(out, hashlen);
138 free(out);
139
140 return ARGON2_OK;
141 }
142
143