- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.3k
gh-136968: fortify macro usage in cryptographic modules #136973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            picnixz
  merged 5 commits into
  python:main
from
picnixz:refactor/cryptography/fortify-macros-136968
  
      
      
   
  Jul 28, 2025 
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -1099,42 +1099,60 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg, | |
| } | ||
|  | ||
| /* | ||
| * One-shot HMAC-HASH using the given HACL_HID. | ||
| * Obtain a view from 'key' and 'msg', storing it in 'keyview' and 'msgview'. | ||
| * | ||
| * Return 0 on success; otherwise set an exception and return -1. | ||
| * | ||
| * The length of the key and message buffers must not exceed UINT32_MAX, | ||
| * lest an OverflowError is raised. The Python implementation takes care | ||
| * of dispatching to the OpenSSL implementation in this case. | ||
| */ | ||
| #define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \ | ||
| do { \ | ||
| Py_buffer keyview, msgview; \ | ||
| GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \ | ||
| if (!has_uint32_t_buffer_length(&keyview)) { \ | ||
| PyBuffer_Release(&keyview); \ | ||
| set_invalid_key_length_error(); \ | ||
| return NULL; \ | ||
| } \ | ||
| GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \ | ||
| PyBuffer_Release(&keyview); \ | ||
| return NULL); \ | ||
| if (!has_uint32_t_buffer_length(&msgview)) { \ | ||
| PyBuffer_Release(&msgview); \ | ||
| PyBuffer_Release(&keyview); \ | ||
| set_invalid_msg_length_error(); \ | ||
| return NULL; \ | ||
| } \ | ||
| uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \ | ||
| Py_hmac_## HACL_HID ##_compute_func( \ | ||
| out, \ | ||
| (uint8_t *)keyview.buf, (uint32_t)keyview.len, \ | ||
| (uint8_t *)msgview.buf, (uint32_t)msgview.len \ | ||
| ); \ | ||
| PyBuffer_Release(&msgview); \ | ||
| PyBuffer_Release(&keyview); \ | ||
| return PyBytes_FromStringAndSize( \ | ||
| (const char *)out, \ | ||
| Py_hmac_## HACL_HID ##_digest_size \ | ||
| ); \ | ||
| static int | ||
| hmac_get_buffer_views(PyObject *key, Py_buffer *keyview, | ||
| PyObject *msg, Py_buffer *msgview) | ||
| { | ||
| if (_Py_hashlib_get_buffer_view(key, keyview) < 0) { | ||
| return -1; | ||
| } | ||
| if (!has_uint32_t_buffer_length(keyview)) { | ||
| PyBuffer_Release(keyview); | ||
| set_invalid_key_length_error(); | ||
| return -1; | ||
| } | ||
| if (_Py_hashlib_get_buffer_view(msg, msgview) < 0) { | ||
| PyBuffer_Release(keyview); | ||
| return -1; | ||
| } | ||
| if (!has_uint32_t_buffer_length(msgview)) { | ||
| PyBuffer_Release(msgview); | ||
| PyBuffer_Release(keyview); | ||
| set_invalid_msg_length_error(); | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|  | ||
| /* | ||
| * One-shot HMAC-HASH using the given HACL_HID. | ||
| */ | ||
| #define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \ | ||
|          | ||
| do { \ | ||
| Py_buffer keyview, msgview; \ | ||
| if (hmac_get_buffer_views(key, &keyview, msg, &msgview) < 0) { \ | ||
| return NULL; \ | ||
| } \ | ||
| uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \ | ||
| Py_hmac_## HACL_HID ##_compute_func( \ | ||
| out, \ | ||
| (uint8_t *)keyview.buf, (uint32_t)keyview.len, \ | ||
| (uint8_t *)msgview.buf, (uint32_t)msgview.len \ | ||
| ); \ | ||
| PyBuffer_Release(&msgview); \ | ||
| PyBuffer_Release(&keyview); \ | ||
| return PyBytes_FromStringAndSize( \ | ||
| (const char *)out, \ | ||
| Py_hmac_## HACL_HID ##_digest_size \ | ||
| ); \ | ||
| } while (0) | ||
|  | ||
| /*[clinic input] | ||
|  | @@ -1329,6 +1347,8 @@ _hmac_compute_blake2b_32_impl(PyObject *module, PyObject *key, PyObject *msg) | |
| Py_HMAC_HACL_ONESHOT(blake2b_32, key, msg); | ||
| } | ||
|  | ||
| #undef Py_HMAC_HACL_ONESHOT | ||
|  | ||
| // --- HMAC module methods ---------------------------------------------------- | ||
|  | ||
| static PyMethodDef hmacmodule_methods[] = { | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.